C 语言 南开一百题
C语言 南开一百题void num(int m,int k,int xx[]) /*标准答案*/ {int data=m+1; int half,n=0,I; while(1) {half=data/2;for(I=2;I<=half;I++) if(data%I==0)break; if(I>half) {xx[n]=data;n++;} if(n>=k)break; data++; } } 或者: void num(int m,int k,int xx[]) {int i,j,s=0; for(i=m+1;k>0;i++) {for(j=2;j<i;j++) if(i%j==0) break; /*注:素数为只能被自己和1整除的数.如果i%j等于0,说明i不是素数,跳出本层循环*/ if(i==j) {xx[s++]=i;k--;} } } void num(int m, int k, int xx[]) { int i=0; for(m=m+1;k>0;m++) if(isP(m)) { xx[i++]=m; k--; } } #include <conio.h> #include <stdio.h> void readwriteDAT() ; int isP(int m) { int i ; for(i = 2 ; i < m ; i++) if(m % i == 0) return 0 ; return 1 ; } void num(int m,int k,int xx[]) { } main() { int m, n, xx[1000] ; clrscr() ; printf("\nPlease enter two integers:") ; scanf("%d,%d", &m, &n ) ; num(m, n, xx) ; for(m = 0 ; m < n ; m++) printf("%d ", xx[m]) ; printf("\n") ; readwriteDAT() ; system("pause"); } void readwriteDAT() { int m, n, xx[1000], i ; FILE *rf, *wf ; rf = fopen("in.dat", "r") ; wf = fopen("out.dat", "w") ; for(i = 0 ; i < 10 ; i++) { fscanf(rf, "%d %d", &m, &n) ; num(m, n, xx) ; for(m = 0 ; m < n ; m++) fprintf(wf, "%d ", xx[m]) ; fprintf(wf, "\n") ; } fclose(rf) ; fclose(wf) ; } IN.DAT 17 5 101 7 321 5 32 4 55 6 76 6 99 4 781 5 22 6 66 3 OUT.DAT 19 23 29 31 37 103 107 109 113 127 131 137 331 337 347 349 353 37 41 43 47 59 61 67 71 73 79 79 83 89 97 101 103 101 103 107 109 787 797 809 811 821 23 29 31 37 41 43 67 71 73 2题目: 题目2:已知数据文件IN.DAT中存有200个四位数, 并已调用读函数readDat()把这些数存入数组a中,请考生编制一函数jsVal(),其功能是: 如果四位数各位上的数字均是0或2或4或6或8, 则统计出满足此条件的个数cnt, 并把这些四位数按从大到小的顺序存入数组b中。最后main()函数调用写函数writeDat( )把结果cnt以及数组b中符合条件的四位数输出到OUT.DAT文件中。 注意: 部分源程序存在文件PROG1.C文件中。 程序中已定义数组: a[200], b[200], 已定义变量: cnt 请勿改动数据文件IN.DAT中的任何数据、主函数main()、读函数readDat()和写函数writeDat()的内容。 ------------------------- void jsVal() /*标准答案*/ {int bb[4]; int I,j,k,flag; for (I=0;I<200;I++) {bb[0]=a[I]/1000; bb[1]=a[I]00/100; bb[2]=a[I]0/10; bb[3]=a[I]; for (j=0;j<4;j++) {if (bb[j]%2==0) flag=1; else {flag=0;break;} } if (flag==1) { b[cnt]=a[I]; cnt++;} } for(I=0;I<cnt-1;I++) for(j=I+1;j<cnt;j++) if (b[I]<b[j]) {k=b[I];b[I]=b[j];b[j]=k;} } PROG1.C #include <stdio.h> #define MAX 200 int a[MAX], b[MAX], cnt = 0; void jsVal() { } void readDat() { int i ; FILE *fp ; fp = fopen("in.dat", "r") ; for(i = 0 ; i < MAX ; i++) fscanf(fp, "%d", &a[i]) ; fclose(fp) ; } void main() { int i ; readDat() ; jsVal() ; printf("满足条件的数=%d\n", cnt) ; for(i = 0 ; i < cnt ; i++) printf("%d ", b[i]) ; printf("\n") ; writeDat() ; system("pause"); } writeDat() { FILE *fp ; int i ; fp = fopen("out.dat", "w") ; fprintf(fp, "%d\n", cnt) ; for(i = 0 ; i < cnt ; i++) fprintf(fp, "%d\n", b[i]) ; fclose(fp) ; } IN.DAT 6012 5099 9012 7025 8088 7055 5334 7607 8145 6012 5099 9012 7025 8088 7055 5334 5795 2896 3019 7747 7607 8145 7090 4140 8626 7905 3735 9620 8714 9496 3640 5249 7671 5925 4687 2982 6227 6478 2638 2793 8298 8246 5892 9861 5795 2896 3019 7747 7607 8145 6012 5099 9012 7025 8088 7055 5334 5795 2896 3019 7747 7607 8145 7090 4140 8626 7905 3735 9620 8714 9496 3640 5249 7671 5925 4687 2982 6227 6478 2638 2793 8298 8246 5892 9861 9110 2269 6951 9800 2439 5339 1064 9994 6762 5752 1001 1030 7989 2845 2958 3820 3010 9486 9798 4014 2783 1604 1628 6466 4101 4128 7127 3262 6332 2076 1860 5834 4580 9057 2614 7852 3789 2897 9194 6317 6276 4285 5610 6945 9137 8348 5434 9162 4303 6779 5025 5137 4630 3535 4048 2697 2438 9791 3903 3650 4899 1557 4745 2573 6288 5421 1563 9385 6545 5061 3905 1074 7840 4596 7537 5961 8327 2104 1055 3317 1282 5368 6571 5440 8274 1919 6789 4542 3570 1500 7044 9288 5302 7577 4018 4619 4922 2076 3297 5898 1699 9276 4439 2729 8725 7507 2729 6736 2566 3227 7901 2973 2353 4805 2546 3406 4824 2060 8645 5886 7549 9279 3310 5429 6344 4100 9604 1146 9234 6202 3477 1492 4800 2194 9937 1304 3454 5477 9230 5382 4064 8472 8262 6724 7219 9968 3398 OUT.DAT 10 8626 8626 8246 8246 8088 8088 8088 6466 6288 4048 3题目: 题目3:函数READDAT()实现从文件IN.DAT中读取一篇英文文章存入到字符串数组XX中;请编制函数STROR(),其函数功能是:以行为单位把字符串中的所有小写字母O左边的字符串内容移到该串的右边存放,然后并把小写字母O删除,余下的字符串内容移到已处理字符串的左边存放.最后把已处理的字符串仍按行重新存入字符串数组XX中,最后调用函数WRITEDAT()把结果XX输出到文件OUT.DAT中. 例如:原文:You can create an index on any field. you have the correct record. 结果: n any field.You can create an index rd.yu have the crrect rec 原始数据文件存放的格式是:每行的宽度均小于80个字符,含标点符号和空格. ------------------- 类型:字符串(单词)的倒置和删除。 答案: void StrOR(void) /*标准答案*/ {int I,j,k,index,strl; char ch; for(I=0;I<maxline;I++) {strl=strlen(xx[I]); index=strl; for(j=0;j<strl;j++) if(xx[I][j]=='o') {for(k=j;k<strl-1;k++) xx[I][k]=xx[I][k+1]; xx[I][strl-1]= ' '; index=j;} for(j=strl-1;j>=index;j--) {ch=xx[I][strl-1]; for(k=strl-1;k>0;k--) xx[I][k]=xx[I][k-1]; xx[I][0]=ch;} } } 或者: void StrOR(void) { int i; char a[80],*p; for(i=0;i<maxline;i++) { p=strchr(xx[i],'o'); while(p) { memset(a,0,80); memcpy(a,xx[i],p-xx[i]); strcpy(xx[i],p+1); strcat(xx[i],a); p=strchr(xx[i],'o'); } } } 或者: void StrOR(void) /*我的非指针解法*/ {int i,righto,j,k; char tem[80]; for(i=0;i<maxline;i++) {k=0;righto=0;memset(tem,0,80); for(j=strlen(xx[i])-1;j>=0;j--) {if(xx[i][j]=='o') {righto=j;break;} } for(j=righto+1;j<strlen(xx[i]);j++) tem[k++]=xx[i][j]; for(j=0;j<righto;j++) {if(xx[i][j]!='o') tem[k++]=xx[i][j];} strcpy(xx[i],tem); }} 或者: 注:题目要求的字符串中所有小写字母o左边的字符串内容移到该串的右边存放,即 将串中“最后”一个字母o左右两侧的内容互换。题中第一个while()特环的作用是让p1 指向最后一个字母'o'。第一个ctrcat()函数的作用是将p1以后的字符都放到新串t中 ,第二个strcat()函数的作用是将p1以前的字符连接到新串t的后面(注意:在些之前要 让p1所指的单元成为p1前面字符串的结束位置*p1='\0')。这时完成左右互换。最后 一个while()循环的作用是删除新串中的所有小写字母'o',采用的删除方法是不是' o'的字母一律留下,否则不留(即相当于删除。) void StrOR(void) {int i; char *p1,*p2,t[80]; for(i=0;i<maxline;i++) {t[0]='\0';p2=xx[i]; while(*p2) {if(*p2=='o') p1=p2; p2++;} strcat(t,p1+1); *p1='\0';strcat(t,xx[i]); p1=xx[i];p2=t; while(*p2) {if(*p2!='o') *p1++=*p2; p2++; } *p1='\0'; }} 或者: 注:该题的主要算法是先让两字符指针都指向串尾,然后使一指针(p1)往前移动,当 出现不是字母时则表示在p1+1与p2之间是一个单词,并将该单词存入一变量(t1),最后 将t1连接到新串中(t);接着再往前找第二个单词,依次类推直到字符串头。由此可知 新串就是原串中各单词的倒排。 void Str0L(void) { int i,k; char *p1,*p2; char t[80],t1[80]; for(i=0;i<maxline;i++) {p2=p1=strchr(xx[i],'\0')-'\0'; t[0]=t1[0]='\0'; k=1; while(k) { while(isalpha(*p1)==0&&p1;!=xx[i]) {p1--;p2=p1;} while(isalpha(*p1)&&p1;>=xx[i]) p1--; memcpy(t1,p1+1,p2-p1); t1[p2-p1]=0; strcat(t,t1); strcat(t," "); if(p1<xx[i]) k=0; } strcpy(xx[i],t); } } PROG1.C # include"stdio.h" # include"string.h" # include"conio.h" char xx[50][80]; int maxline=0; int ReadDat(void); void WriteDat(void); void StrOR(void) { } void main() {clrscr(); if(ReadDat()) {printf("Can't open the file!\n"); return;} StrOR(); WriteDat(); system("pause"); } int ReadDat(void) {FILE *fp;int i=0;char *p; if((fp=fopen("in.dat","r"))==NULL) return 1; while(fgets(xx[i],80,fp)!=NULL) {p=strchr(xx[i],'\n'); if(p) *p=0; i++; } maxline=i; fclose(fp); return 0; } void WriteDat(void) {FILE *fp; int i; fp=fopen("out.dat","w"); for(i=0;i<maxline;i++) {printf("%s\n",xx[i]); fprintf(fp,"%s\n",xx[i]); } fclose(fp); } IN.DAT You can create an index on any field. you have the correct record. You can create an index on any field, on several fields to be used together, or on parts thereof, that you want to use as a key. The keys in indexes allow you quick access to specific records and define orders for sequential processing of a ISAM file. After you no longer need an index, you can delete it. Addition and indexes have no effect on the data records or on other indexes. You may want a field in field in each record to uniquely identify that record from all other records in the file. For example, the Employee Number field is unique if you do not assign the same number to two different employees, and you never reassign these numbers to other employees. If you wish to find or modify the record belonging to a specific employee, this unique field saves the thouble of determining whether you have the correct record. If you do not have a unique field, you must find the first record the matches your key and determine whether the record is the one you want. If it is not the correct one, you must search again to find others. If you know that you have a unique field within your records, you can include this fact in the key description, and ISAM will allow only unique keys. For example, if you specify that the employee numbers are unique, ISAM only lets you add records to the file for, or change numbers to, employee numbers that do not alreadly exist int file. OUT.DAT n any field. Yu can create an index rd. yu have the crrect rec be used Yu can create an index n any field, n several fields t use as a key. The tgether, r n parts theref, that yu want t rds and define keys in indexes allw yu quick access t specific rec nger rders fr sequential prcessing f a ISAM file. After yu n l effect need an index, yu can delete it. Additin and indexes have n ther indexes. n the data recrds r n uniquely identify that Yu may want a field in field in each recrd t yee recrd frm all ther recrds in the file. Fr example, the Empl Number field is unique if yu d nt assign the same number t tw ther different emplyees, and yu never reassign these numbers t a emplyees. If yu wish t find r mdify the recrd belnging t f determining specific emplyee, this unique field saves the thuble rd. whether yu have the crrect rec rd If yu d nt have a unique field, yu must find the first rec u the matches yur key and determine whether the recrd is the ne y thers. want. If it is nt the crrect ne, yu must search again t find u If yu knw that yu have a unique field within yur recrds, y nly can include this fact in the key descriptin, and ISAM will allw yee numbers are unique keys. Fr example, if yu specify that the empl r change unique, ISAM nly lets yu add recrds t the file fr, t alreadly exist int file. numbers t, emplyee numbers that d n 4题目: 题目4:函数ReadDat( )实现从文件IN.DAT中读取一篇英文文章存入到字符串数组xx中; 请编制函数StrOL( ), 其函数的功能是: 以行为单位对行中以空格或标点符号为分隔的所有单词进行倒排,同时去 除标点符号,之后把已处理的字符串(应不含标点符号)仍按行重新存入字符串数组xx中。最后main()函数调用函数WriteDat()把结果xx输出到文件OUT6.DAT中。 例如: 原文: You He Me I am a student. 结果: Me He You student a am I 原始数据文件存放的格式是: 每行的宽度均小于80个字符, 含标点符号和空格。 ----------------- 类型:字符串(单词)的倒置和删除。 答案: void StrOL(void) /*标准答案*/ {int I,j,k,strl,l;char c; for(I=0;I<maxline;I++) for(j=0;j<strlen(xx[I]);j++) {c=xx[I][j]; if ((c>='A'&&c<='Z')||(c>='a'&&c<='z')||c==' ') ; else xx[I][j]=' '; } for(l=0;l<maxline;l++) {char ch[80]={0}; char pp[80]={0}; strl=strlen(xx[l]); I=strl-1; k=1; while(1) {while (((xx[l][I]>='a'&&xx;[l][I]<='z')||(xx[l][I]>='A'&&xx;[l][I]<='z'))&&I>=0) {for(j=k;j>=0;j--) pp[j+1]=pp[j]; pp[0]=xx[l][I]; k++;I--; } strcat(ch,pp);strcpy(pp, """");k=1; if(I==-1)break; while((xx[l][I]<'A'||xx[l][I]>'z')&&I>=0) {for(j=k;j>=0;j--) pp[j+1]=pp[j]; pp[0]=xx[l][I]; k++; I--;} strcat(ch,pp); strcpy(pp,""""); k=0; if(I==-1)break;} strcpy(xx[l],ch); }} 或者: void StrOL(void) { int i,j,k,m,n,ll; char yy[80]; for(i=0; i < maxline; i++) { ll=strlen(xx[i]); k=n=0; for(j=ll-1; j>=0; j--) { if(isalpha(xx[i][j])) k++; else { for(m=1; m<=k; m++) yy[n++]=xx[i][j+m]; k=0; } if(xx[i][j]==' ') yy[n++]=' '; } for(m=1; m<=k; m++) yy[n++]=xx[i][j+m]; yy[n]=0; strcpy(xx[i],yy); } } PROG1.C # include"stdio.h" # include"string.h" # include"conio.h" # include"ctype.h" char xx[50][80]; int maxline=0; int ReadDat(void); void WriteDat(void); void StrOR(void) { } void main() {clrscr(); if(ReadDat()) {printf("Can't open the file!\n"); return;} StrOR(); WriteDat(); system("pause"); } int ReadDat(void) {FILE *fp;int i=0;char *p; if((fp=fopen("in.dat","r"))==NULL) return 1; while(fgets(xx[i],80,fp)!=NULL) {p=strchr(xx[i],'\n'); if(p) *p=0; i++; } maxline=i; fclose(fp); return 0; } void WriteDat(void) {FILE *fp; int i; fp=fopen("out.dat","w"); for(i=0;i<maxline;i++) {printf("%s\n",xx[i]); fprintf(fp,"%s\n",xx[i]); } fclose(fp); } IN.DAT You He Me I am a student You can create an index on any field, on several fields to be used together, or on parts thereof, that you want to use as a key. The keys in indexes allow you quick access to specific records and define orders for sequential processing of a ISAM file. After you no longer need an index, you can delete it. Addition and indexes have no effect on the data records or on other indexes. You may want a field in field in each record to uniquely identify that record from all other records in the file. For example, the Employee Number field is unique if you do not assign the same number to two different employees, and you never reassign these numbers to other employees. If you wish to find or modify the record belonging to a specific employee, this unique field saves the thouble of determining whether you have the correct record. If you do not have a unique field, you must find the first record the matches your key and determine whether the record is the one you want. If it is not the correct one, you must search again to find others. If you know that you have a unique field within your records, you can include this fact in the key description, and ISAM will allow only unique keys. For example, if you specify that the employee numbers are unique, ISAM only lets you add records to the file for, or change numbers to, employee numbers that do not alreadly exist int file. OUT.DAT Me He You student a am I used be to fields several on field any on index an create can You The key a as use to want you that thereof parts on or together define and records specific to access quick you allow indexes in keys longer no you After file ISAM a of processing sequential for orders effect no have indexes and Addition it delete can you index an need indexes other on or records data the on that identify uniquely to record each in field in field a want may You Employee the example For file the in records other all from record two to number same the assign not do you if unique is field Number other to numbers these reassign never you and employees different a to belonging record the modify or find to wish you If employees determining of thouble the saves field unique this employee specific record correct the have you whether record first the find must you field unique a have not do you If you one the is record the whether determine and key your matches the others find to again search must you one correct the not is it If want you records your within field unique a have you that know you If only allow will ISAM and description key the in fact this include can are numbers employee the that specify you if example For keys unique change or for file the to records add you lets only ISAM unique file int exist alreadly not do that numbers employee to numbers 5题目: 题目5:在文件in.dat中有200个正整数,且每个数均在1000至9999之间。函数ReadDat()读取这200个数存放到数组aa中。请编制函数jsSort(),其函数的功能是:要求按每个数的后三位的大小进行升序排列,然后取出满足此条件的前10个数依次存入数组b中,如果后三位的数值相等,则按原先的数值进行降序排列。最后调用函数WriteDat()把结果bb输出到文件out.dat中。 例:处理前 6012 5099 9012 7025 8088 处理后 9012 6012 7025 8088 5099 注意:部分源程序已给出。 请勿改动主函数main()、读数据函数ReadDat()和输出数据函数WriteDat()的内容。 -------------------- void jsSort() /*标准答案*/ {int I,j,data; for(I=0;I<199;I++) for(j=I+1;j<200;j++) {if (aa[I]00>aa[j]00) {data=aa[I];aa[I]=aa[j];aa[j]=data;} else if(aa[I]00==aa[j]00) if(aa[I]<aa[j]) {data=aa[I];aa[I]=aa[j];aa[j]=data;} } for(I=0;I<10;I++) bb[I]=aa[I]; } PROG1.C #include<stdio.h> #include<string.h> #include<conio.h> int aa[200],bb[10]; void jsSort() { } void main() {readDat(); jsSort(); writeDat(); system("pause"); } readDat() {FILE *in; int i; in=fopen("in.dat","r"); for(i=0; i<200; i++) fscanf(in,"%d,",&aa;[i]); fclose(in); } writeDat() {FILE *out; int i; clrscr(); out=fopen("out.dat","w"); for(i=0; i<10; i++){ printf("i=%d,%d\n",i+1,bb[i]); fprintf(out,"%d\n",bb[i]); } fclose(out); } IN.DAT 6012 5099 9012 7025 8088 7055 5334 7607 8145 6012 5099 9012 7025 8088 7055 5334 5795 2896 3019 7747 7607 8145 7090 4140 8626 7905 3735 9620 8714 9496 3640 5249 7671 5925 4687 2982 6227 6478 2638 2793 8298 8246 5892 9861 5795 2896 3019 7747 7607 8145 6012 5099 9012 7025 8088 7055 5334 5795 2896 3019 7747 7607 8145 7090 4140 8626 7905 3735 9620 8714 9496 3640 5249 7671 5925 4687 2982 6227 6478 2638 2793 8298 8246 5892 9861 9110 2269 6951 9800 2439 5339 1064 9994 6762 5752 1001 1030 7989 2845 2958 3820 3010 9486 9798 4014 2783 1604 1628 6466 4101 4128 7127 3262 6332 2076 1860 5834 4580 9057 2614 7852 3789 2897 9194 6317 6276 4285 5610 6945 9137 8348 5434 9162 4303 6779 5025 5137 4630 3535 4048 2697 2438 9791 3903 3650 4899 1557 4745 2573 6288 5421 1563 9385 6545 5061 3905 1074 7840 4596 7537 5961 8327 2104 1055 3317 1282 5368 6571 5440 8274 1919 6789 4542 3570 1500 7044 9288 5302 7577 4018 4619 4922 2076 3297 5898 1699 9276 4439 2729 8725 7507 2729 6736 2566 3227 7901 2973 2353 4805 2546 3406 4824 2060 8645 5886 7549 9279 3310 5429 6344 4100 9604 1146 9234 6202 3477 1492 4800 2194 9937 1304 3454 5477 9230 5382 4064 8472 8262 6724 7219 9968 3398 OUT.DAT 1001 3010 9012 9012 9012 6012 6012 6012 4014 4018
- 粉丝: 0
- 资源: 1
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助