当前位置:首页 > 计算机三级考试上机最新南开100精简版
}
writeDat() {
FILE *fp;
fp=fopen(\T\ fprintf(fp,\ fclose(fp); }
************************************************************************** ★题目43(字符排序题)
无忧id 82题(只是将结果按“从大到小”排序)
函数ReadDat()实现从文件in.dat中读取20行数据存放到字符串数组xx中(每行字符串长度均小于80)。请编制函数jsSort(),其函数的功能是:以行为单位对字符串变量的下标为奇数的字符按其ASCII值从小到大的顺序进行排序,排序后的结果仍按行重新存入字符串数组xx中,最后调用函数WriteDat()把结果xx输出到文件out.dat中。 例如:位置 0 1 2 3 4 5 6 7 源字符串 a b c d e f g h 则处理后字符串 a h c f e d g b 部分源程序存在文件prog1.c中。
请勿改动主函数main()、读数据函数ReadDat()和输出数据函数WriteDat()的内容。 #include
char xx[20][80];
void jsSort() {int i,j,k,strl; char ch;
for(i=0;i<20;i++) { strl=strlen(xx[i]);
for(j=1;j
void main() {
readDat(); jsSort();
writeDat(); }
readDat() {
FILE *in; int i=0; char *p;
in=fopen(\
while(i<20&&fgets(xx[i],80,in)!=NULL){ p=strchr(xx[i],'\\n'); if(p)*p=0; i++; }
fclose(in); }
writeDat() {
FILE *out; int i;
out=fopen(\ clrscr();
for(i=0;i<20;i++){ printf(\ fprintf(out,\ }
fclose(out); }
************************************************************************ ★☆题目44(字符串左右排序交换题 )
函数ReadDat()实现从文件in.dat中读取20行数据存放到字符串数组xx中(每行字符串长度均小于80)。请编制函数jsSort(),其函数的功能是:以行为单位对字符串按给定的条件进行排序,排序后的结果仍按行重新存入字符串数组xx中,最后调用函数WriteDat()把结果xx输出到文件out.dat中。
条件:从字符串中间一分为二,左边部分按字符的ASCII值降序排序,排序后左边部分与右边部分进行交换。如果原字符串长度为奇数,则最中间的字符不参加处理,字符仍放在原位置上。
例如:位置 0 1 2 3 4 5 6 7 8 源字符串 a b c d h g f e 1 2 3 4 9 8 7 6 5 则处理后字符串 h g f e d c b a
8 7 6 5 9 4 3 2 1 部分源程序存在文件prog1.c中。
请勿改动主函数main()、读数据函数ReadDat()和输出数据函数WriteDat()的内容。 #include
char xx[20][80];
void jsSort()
{ int i,j,k,strl,half; char temp;
for(i=0;i<20;i++) { strl=strlen(xx[i]); half=strl/2;
for(j=0;j { temp=xx[i][j]; xx[i][j]=xx[i][k]; xx[i][k]=temp;} for(j=half-1,k=strl-1;j>=0;j--,k--) { temp=xx[i][j]; xx[i][j]=xx[i][k]; xx[i][k]=temp;} } } void main() { readDat(); jsSort(); writeDat(); } readDat() { FILE *in; int i=0; char *p; in=fopen(\ while(i<20&&fgets(xx[i],80,in)!=NULL){ p=strchr(xx[i],'\\n'); if(p)*p=0; i++; } fclose(in); } writeDat() { FILE *out; int i; clrscr(); out=fopen(\ for(i=0;i<20;i++){ printf(\ fprintf(out,\ } fclose(out); } ************************************************************************** 题目55(素数统计排序题) 已知数据文件in.dat中存有300个四位数,并已调用读函数ReadDat()把这些数存入数组a中,请编制一函数jsValue(),其功能是:求出所有这些四位数是素数的个数cnt,再把所有满足此条件的四位数依次存入数组b中,然后对数组b的四位数按从小到大的顺序进行排序,最后调用写函数writeDat()把结果输出到out.dat文件。 例如:5591是素数,则该数满足条件存入数组b中,且个数cnt=cnt+1。9812是非素数,则该数不满足条件忽略。 部分源程序存在文件prog1.c中。 程序中已定义数组:a[300],b[300],已定义变量:cnt 请勿改动主函数main()、读函数ReadDat()和写函数writeDat()的内容。 #include int a[300],b[300],cnt=0; int isP(int m){ int i; for(i=2;i if(m%i==0)return 0; return 1; } jsValue() {int i,j,value; for(i=0;i<300;i++) if(isP(a[i])) b[cnt++]=a[i]; for(i=0;i if(b[i]>b[j]) {value=b[i]; b[i]=b[j]; b[j]=value;} }
共分享92篇相关文档