c语言绿皮书第三版第十三章习题 1.习题13.4#includestdio.h#includestdlib.hintmain(){FILE*fp;charch;// 打开文件文件名改为题目要求的 testif((fpfopen(test,w))NULL){printf(can not open file!\n);exit(0);}// 边读边处理遇到 ! 停止不写入 !while((chgetchar())!!){// 小写字母转大写其他字符不变if(chachz)chch-32;// 或者用 ch toupper(ch); 需要 #include ctype.hfputc(ch,fp);}fclose(fp);return0;}2.习题13.5#includestdio.h#includestdlib.hintmain(){FILE*fa,*fb,*fp;intchA,chB;// 必须用 int 类型兼容 EOF// 打开文件按题目要求的文件名if((fafopen(a.txt,r))NULL){printf(can not open file:a!\n);exit(0);}if((fbfopen(b.txt,r))NULL){printf(can not open file:b!\n);exit(0);}if((fpfopen(c.txt,w))NULL){printf(can not open file:c!\n);exit(0);}// 先读两个文件的第一个字符chAfgetc(fa);chBfgetc(fb);// 双指针归并排序while(chA!EOFchB!EOF){if(chAchB){fputc(chA,fp);chAfgetc(fa);}else{fputc(chB,fp);chBfgetc(fb);}}// 写入 A 中剩下的字符while(chA!EOF){fputc(chA,fp);chAfgetc(fa);}// 写入 B 中剩下的字符while(chB!EOF){fputc(chB,fp);chBfgetc(fb);}// 关闭文件fclose(fa);fclose(fb);fclose(fp);return0;}3. 习题13.6#includestdio.h#includestdlib.h#defineN5// 定义学生结构体类型typedefstructstudent{intnum;charname[10];doublesubject[3];doubleave;}Student;intmain(){FILE*fp;Student st[N];// 定义长度为N的结构体数组inti,j;// 打开文件if((fpfopen(stud,w))NULL){printf(can not open file!\n);exit(0);}// 输入数据并计算平均分for(i0;iN;i){doublesum0;printf(第%d个学生信息:\n,i1);printf(学号: );scanf(%d,st[i].num);printf(姓名: );scanf(%s,st[i].name);for(j0;j3;j){printf(科目%d成绩: ,j1);scanf(%lf,st[i].subject[j]);sumst[i].subject[j];}st[i].avesum/3;// 计算平均分printf(\n);}// 写入文件for(i0;iN;i){if(fwrite(st[i],sizeof(Student),1,fp)!1){printf(file write error\n);exit(0);}}fclose(fp);printf(数据已成功写入文件stud\n);return0;}4.习题13.7#includestdio.h#includestdlib.h#defineN5typedefstructstudent{intnum;charname[10];doublesubject[3];doubleave;}stu;intmain(){inti,j,k;stu st[N],ss;FILE*fp1,*fp2;if((fp1fopen(stud,r))NULL){printf(can not open file:stud\n);exit(0);}for(i0;iN;i){if(fread(st[i],sizeof(st[i]),1,fp1)!1){printf(file:stud error of reading!\n);exit(0);}}for(i0;iN-1;i){for(j0;jN-1-i;j){if(st[j].avest[j1].ave){ss.numst[j].num;st[j].numst[j1].num;st[j1].numss.num;for(k0;k10;k){ss.name[k]st[j].name[k];st[j].name[k]st[j1].name[k];st[j1].name[k]ss.name[k];}for(k0;k3;k){ss.subject[k]st[j].subject[k];st[j].subject[k]st[j1].subject[k];st[j1].subject[k]ss.subject[k];}ss.avest[j].ave;st[j].avest[j1].ave;st[j1].avess.ave;}}}if((fp2fopen(stu_sort,w))NULL){printf(can not open file:stu_sort\n);exit(0);}for(i0;iN;i){if((fwrite(st[i],sizeof(st[i]),1,fp2))!1){printf(file:stu_sort error of writing!\n);exit(0);}}fclose(fp1);fclose(fp2);return0;}5.习题13.8#includestdio.h#includestdlib.h#defineN5typedefstructstudent{intnum;charname[10];doublesub[3];doubleave;}stu;intmain(){FILE*fp1,*fp2;inti,j,k;intleap1;// 标记是否需要插入到末尾stu temp,st[N1];// 数组大小6下标0~5// 输入新学生信息printf(Please input:\n);printf(num: );scanf(%d,temp.num);printf(name: );scanf(%s,temp.name);printf(sub[0]: );scanf(%lf,temp.sub[0]);printf(sub[1]: );scanf(%lf,temp.sub[1]);printf(sub[2]: );scanf( %lf,temp.sub[2]);// 加空格跳过空白符// 计算平均分temp.ave(temp.sub[0]temp.sub[1]temp.sub[2])/3;// 打开原文件if((fp1fopen(stu_sort,r))NULL){printf(can not open file:stu_sort\n);exit(0);}// 读取所有学生数据for(i0;iN;i){if((fread(st[i],sizeof(stu),1,fp1))!1){printf(file:stu_sort error of reading\n);exit(0);}}// 查找插入位置并插入for(i0;iN;i){if(temp.avest[i].ave){leap0;// 把从i开始的元素后移一位for(jN-1;ji;j--){st[j1]st[j];// 直接结构体赋值不用手动复制字段}st[i]temp;// 插入新学生break;}}// 如果所有学生平均分都更高插入到末尾下标Nif(leap){st[N]temp;}// 写入新文件if((fp2fopen(charu,w))NULL){printf(can not open file:charu!\n);exit(0);}for(i0;iN1;i){if((fwrite(st[i],sizeof(stu),1,fp2))!1){printf(file:charu error of writing!\n);exit(0);}}fclose(fp1);fclose(fp2);printf(插入完成已写入文件charu\n);return0;}6.习题13.9#includestdio.h#includestdlib.h#defineN6typedefstructstudent{intnum;charname[10];doublesub[3];doubleave;}stu;voidmain(){stu st[N];inti;FILE*fp1,*fp2;if((fp1fopen(charu,r))NULL){printf(can not open file:chatu!\n);exit(0);}if((fp2fopen(stu_sort,w))NULL){printf(can not open file:chatu!\n);exit(0);}for(i0;iN;i){if((fread(st[i],sizeof(st[i]),1,fp1))!1){printf(file:charu error of reading!\n);exit(0);}if((fwrite(st[i],sizeof(st[i]),1,fp2))!1){printf(file:stu_sort error of writing!\n);exit(0);}}fclose(fp1);fclose(fp2);}7.习题13.12#includestdio.h#includestdlib.h#includestring.h#defineN100// 每行最大长度intmain(){chars[N];FILE*fp;if((fpfopen(str,w))NULL){printf(can not open file:str!\n);exit(0);}// 1. 输入若干行写入文件支持空格空行结束printf(请输入若干行字符输入空行结束\n);while(1){fgets(s,N,stdin);intlenstrlen(s);if(len0s[len-1]\n){s[len-1]\0;}if(strlen(s)0)break;// 空行结束输入fprintf(fp,%s\n,s);}// 2. 回到文件开头读取并转大写输出rewind(fp);printf(\n转换为大写后\n);while(fgets(s,N,fp)!NULL){intlenstrlen(s);if(len0s[len-1]\n){s[len-1]\0;}// 小写转大写for(intj0;s[j]!\0;j){if(s[j]as[j]z){s[j]0xdf;}}printf(%s\n,s);}fclose(fp);return0;}