⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xsort3.c

📁 c数值算法的源码
💻 C
字号:
/* Driver for routine sort3 */

#include <stdio.h>
#include <string.h>
#include <math.h>
#include "nr.h"
#include "nrutil.h"

#define NLEN 64

main()
{
	int i,j;
	char dummy[NLEN],amsg[NLEN+1],bmsg[NLEN+1],cmsg[NLEN+1];
	float *a,*b,*c;
	FILE *fp;

	a=vector(1,NLEN);
	b=vector(1,NLEN);
	c=vector(1,NLEN);
	(void) strcpy(amsg,"I'd rather have a bottle in front of");
	(void) strcat(amsg," me than a frontal lobotomy.");
	printf("\noriginal message:\n%s\n",amsg);
	/* read array of random numbers */
	if ((fp = fopen("tarray.dat","r")) == NULL)
		nrerror("Data file tarray.dat not found\n");
	fgets(dummy,NLEN,fp);
	for (i=1;i<=NLEN;i++) fscanf(fp,"%f",&a[i]);
	fclose(fp);
	/* create array b and array c */
	for (i=1;i<=NLEN;i++) {
		b[i]=i;
		c[i]=NLEN+1-i;
	}
	/* sort array a while mixing b and c */
	sort3(NLEN,a,b,c);
	/* scramble message according to array b */
	bmsg[NLEN]=amsg[NLEN];  /* null terminating character */
	for (i=1;i<=NLEN;i++) {
		j=b[i];
		bmsg[i-1]=amsg[j-1];
	}
	printf("\nscrambled message:\n%s\n",bmsg);
	/* unscramble according to array c */
	cmsg[NLEN]=amsg[NLEN];
	for (i=1;i<=NLEN;i++) {
		j=c[i];
		cmsg[j-1]=bmsg[i-1];
	}
	printf("\nmirrored message:\n%s\n",cmsg);
	free_vector(c,1,NLEN);
	free_vector(b,1,NLEN);
	free_vector(a,1,NLEN);
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -