08年9月上机最新整理(编辑修改稿)内容摘要:

, %s\n, xx[i])。 } fclose(fp)。 } 答案: int i,j,str1。 for(i=0。 imaxline。 i++) {str1=strlen(xx[i])。 for(j=0。 jstr1。 j++) xx[i][j]+=xx[i][j]4。 } } 9. 函数 ReadDat() 的功能是实现从文件 , 存入到字符串数组 xx中。 请编制函数 encryptChar(),按给定的替代关系对数组 xx中的所有字符进行替代,仍存入数组 xx的对应的位置上,最后调用函数 WriteDat()把结果 xx输出到文件。 替代关系: f(p)=p*11 mod 256( p是数组 xx中某一个字符的 ASCII值, f(p)是计算后新字符的 ASCII值),如果原字符的 ASCII值是偶数或计算后 f(p)的值小于等于 32,则该字符不变,否则将 f(p)所对应的字符进行替代。 注意:部分源程序已给出,原始数据文件存放的格式是:每行的宽度均小于 80个字符。 请勿改动主函数 main()、读函数 ReadDat()和写函数 WriteDat()的内容。 试题程序: include include include include unsigned char xx[50][80]。 int maxline = 0。 /* 文章的总行数 */ int ReadDat(void)。 void WriteDat(void)。 void encryptChar() { } main() { clrscr()。 if(ReadDat()) { printf(数据文件 !\n\007)。 return。 } encryptChar()。 WriteDat()。 } 13 int ReadDat(void) { FILE *fp。 int i= 0。 unsigned char *p。 if ((fp = fopen(,r)) ==NULL) return 1。 while(fgets(xx[i], 80, fp) !=NULL) { p = strchr(xx[i], 39。 \n39。 )。 if(p) *p = 0。 i++。 } maxline = i。 fclose(fp)。 return 0。 } void WriteDat(void) { FILE *fp。 int i。 fp = fopen(, w)。 for(i = 0。 i maxline。 i++) { printf(%s\n, xx[i])。 fprintf(fp, %s\n, xx[i])。 } fclose(fp)。 } 答案: {int i。 char *pf。 for(i=0。 imaxline。 i++) { pf=xx[i]。 while(*pf!=0) {if(*pf%2==0 || *pf*11%256=32)。 else *pf=*pf*11%256。 pf++。 } } } 10. 编写函数 jsValue(), 它的功 能是求 Fibonacci数列中大于 t的最小的一个数 , 结果由函数返回 , 其中 Fibonacci数列 F(n)的定义为 : F(0)=0, F(1)=1 F(n)=F(n1)+F(n2) 最后调用函数 writeDat(), 把结果输出到文件。 例如 : 当 t = 1000时 , 函数值为 1597。 注意:部分源程序已给出。 请勿改动主函数 main()和写函数 WriteDat()的内容。 试题程序: include 14 int jsValue(int t) { } main() { int n。 n=1000。 printf(n=%d, f=%d\n, n, jsValue(n))。 writeDat()。 } writeDat() { FILE *in, *out。 int n,s。 out = fopen(, w)。 s = jsValue(1000)。 printf(%d,s)。 fprintf(out, %d\n, s)。 fclose(out)。 } 答案: { int f1=0,f2=1,fn。 fn=f1+f2。 while(fn=t) {f1=f2。 f2=fn。 fn=f1+f2。 } return fn。 } 11. 请编写函数 countValue(), 它的功能是 : 求 n以内 ( 不包括 n) 同时能被 3与 7整除的所有自然数之和的平方根 s, 并作为函数值返回 , 最后结果 s输出到文件。 例如,若 n为 1000时,函数值应为 s=。 注意:部分源程序已给出。 请勿改动主函数 main()和输入输出函数 progReadWrite()的内容。 试题程序: include include include double countValue(int n) { } main() { clrscr()。 printf(自然数之和的平方根 =%f\n, countValue(1000))。 progReadWrite()。 } progReadWrite() { FILE *wf。 int i, n。 float s。 wf = fopen(, w)。 15 s = countValue(1000)。 fprintf(wf, %f\n, s)。 fclose(wf)。 } 答案: { double xy=。 int i。 for(i=1。 in。 i++) if(i%3==0 amp。 amp。 i%7==0) xy+=i。 xy=sqrt((double)xy)。 return xy。 } 12. 下列程序的功能是:在 3位整数( 100至 999)中寻找符合下面条件的整数,并依次从小到大存入数组 b中;它既是完全平方数,又有两位数字相同,例如144, 676等。 请编制函数 int jsValue(int bb[])实现此功能,满足该条件的整数的个数通过所编制的函数返回。 最后调用函数 writeDat()把结果输出到文件。 注意:部分源程序已给出。 请勿改动主函数 main()和写函数 writeDat()的内容。 试题程序: include int jsValue(int bb[ ]) { } main() { int b[20], num。 num = jsValue(b)。 writeDat(num, b)。 } writeDat(int num, int b[]) { FILE *out。 int i。 out = fopen(, w)。 fprintf(out, %d\n, num)。 for(i = 0。 i num。 i++) fprintf(out, %d\n, b[i])。 fclose(out)。 } 答案: { int i,j,k=0。 int hun,ten,data。 for(i=100。 i=999。 i++) { j=10。 while(j*j=i) {if (i==j*j) {hun=i/100。 data=ihun*100。 ten=data/10。 data=dataten*10。 if(hun==ten || hun==data || ten==data) {bb[k]=i。 16 k++。 } } j++。 } } return k。 } 13. 已知在文件 ( 个数 200) 4位数字的正整数 , 函数 ReadDat() 的功能是读取这若干个正整数并存入数组 xx中。 请编制函数CalValue(),其功能要求:( 1)求出该文件中共有多少个正整数 totNum;( 2)求这些数右移 1位后,产生的新数是偶数的数的个数 totCnt,以及满足此条件的这些数(右移前的值)的算术平均值 totPjz,最后调用函数 WriteDat()把所求的结果输出到文件。 注意:部分源程序已给出。 请勿改动主函数 main()、读函数 ReadDat()和写函数 WriteDat()的内容。 试题程序: include include define MAXNUM 200 int xx[MAXNUM]。 int totNum = 0。 /* 文件 */ int totCnt = 0。 /* 符合条件的正整数的个数 */ double totPjz =。 /* 平均值 */ int ReadDat(void)。 void Writedat(void)。 void CalValue(void) { } main() { int i。 clrscr()。 for(i = 0。 i MAXNUM。 i++) xx[i] = 0。 if(ReadDat()) { printf(数据文件 !\007\n)。 return。 } CalValue()。 printf(文件 = %d 个 \n, totNum)。 printf(符合条件的正整数的个数 = %d 个 \n, totCnt)。 printf(平均值 =%.2lf\n, totPjz)。 Writedat()。 } int ReadDat(void) { FILE *fp。 int i = 0。 if((fp = fopen (, r)) == NULL) return 1。 while(! feof(fp)) { 17 fscanf(fp, %d, amp。 xx[i++])。 } fclose(fp)。 return 0。 } void WriteDat(void) { FILE *fp。 fp = fopen(, w)。 fprintf(fp, %d\n%d\n%.2lf\n, totNum, totCnt, totPjz)。 fclose(fp)。 } 答案: { int i,data。 for(i=0。 iMAXNUM。 i++) { if(!xx[i]) continue。 if(xx[i]0) totNum++。 data=xx[i]1。 if (data%2==0) { totCnt++。 totPjz+=xx[i]。 } } totPjz/=totCnt。 } 14. 已知数据文件 200个 4位数,并已调用读函数 readDat() 把这些数存入数组 a中,请编制一个函数 jsVal(),其功能是:把千位数字和十位数字重新组成一个新的十位数 ab(新十位数的十位数字是原 4位数的千位数字,新十位数的个位数字是原 4位数的十位数字),以及把个位数字和百位数字组成另一个新的十位数 cd(新十位数的十位数字是原 4位数的个位数字,新十位数的个位数字是原 4位数的百位数字),如果新组成两个十位数 abcd=0且 abcd=10且两个数均是奇数,同时两 个新十位数字均不为零,则将满足此条件的 4位数按从大到小的顺序存入数组 b中,并要计算满足上述条件的 4位数的个数 t,最后调用写函数 writeDat()把结果 t及数组 b中符合条件的 4位数输出到。 注意:部分源程序已给出。 程序中已定义数组: a[200], b[200],已定义变量: t。 请勿改动主函数 main()、读函数 readDat()和写函数 writeDat()的内容。 试题程序: include define MAX 200 int a[MAX], b[MAX], t = 0。 void jsVal() { } void readDat() { int i。 FILE *fp。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。