news 2026/6/25 0:45:34

c语言绿皮书第三版第十三章习题

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
c语言绿皮书第三版第十三章习题

1.习题13.4

#include<stdio.h>#include<stdlib.h>intmain(){FILE*fp;charch;// 打开文件,文件名改为题目要求的 testif((fp=fopen("test","w"))==NULL){printf("can not open file!\n");exit(0);}// 边读边处理,遇到 '!' 停止,不写入 '!'while((ch=getchar())!='!'){// 小写字母转大写,其他字符不变if(ch>='a'&&ch<='z')ch=ch-32;// 或者用 ch = toupper(ch); (需要 #include <ctype.h>)fputc(ch,fp);}fclose(fp);return0;}

2.习题13.5

#include<stdio.h>#include<stdlib.h>intmain(){FILE*fa,*fb,*fp;intchA,chB;// 必须用 int 类型,兼容 EOF// 打开文件(按题目要求的文件名)if((fa=fopen("a.txt","r"))==NULL){printf("can not open file:a!\n");exit(0);}if((fb=fopen("b.txt","r"))==NULL){printf("can not open file:b!\n");exit(0);}if((fp=fopen("c.txt","w"))==NULL){printf("can not open file:c!\n");exit(0);}// 先读两个文件的第一个字符chA=fgetc(fa);chB=fgetc(fb);// 双指针归并排序while(chA!=EOF&&chB!=EOF){if(chA<chB){fputc(chA,fp);chA=fgetc(fa);}else{fputc(chB,fp);chB=fgetc(fb);}}// 写入 A 中剩下的字符while(chA!=EOF){fputc(chA,fp);chA=fgetc(fa);}// 写入 B 中剩下的字符while(chB!=EOF){fputc(chB,fp);chB=fgetc(fb);}// 关闭文件fclose(fa);fclose(fb);fclose(fp);return0;}

3. 习题13.6

#include<stdio.h>#include<stdlib.h>#defineN5// 定义学生结构体类型typedefstructstudent{intnum;charname[10];doublesubject[3];doubleave;}Student;intmain(){FILE*fp;Student st[N];// 定义长度为N的结构体数组inti,j;// 打开文件if((fp=fopen("stud","w"))==NULL){printf("can not open file!\n");exit(0);}// 输入数据并计算平均分for(i=0;i<N;i++){doublesum=0;printf("第%d个学生信息:\n",i+1);printf("学号: ");scanf("%d",&st[i].num);printf("姓名: ");scanf("%s",st[i].name);for(j=0;j<3;j++){printf("科目%d成绩: ",j+1);scanf("%lf",&st[i].subject[j]);sum+=st[i].subject[j];}st[i].ave=sum/3;// 计算平均分printf("\n");}// 写入文件for(i=0;i<N;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

#include<stdio.h>#include<stdlib.h>#defineN5typedefstructstudent{intnum;charname[10];doublesubject[3];doubleave;}stu;intmain(){inti,j,k;stu st[N],ss;FILE*fp1,*fp2;if((fp1=fopen("stud","r"))==NULL){printf("can not open file:stud\n");exit(0);}for(i=0;i<N;i++){if(fread(&st[i],sizeof(st[i]),1,fp1)!=1){printf("file:stud error of reading!\n");exit(0);}}for(i=0;i<N-1;i++){for(j=0;j<N-1-i;j++){if(st[j].ave<st[j+1].ave){ss.num=st[j].num;st[j].num=st[j+1].num;st[j+1].num=ss.num;for(k=0;k<10;k++){ss.name[k]=st[j].name[k];st[j].name[k]=st[j+1].name[k];st[j+1].name[k]=ss.name[k];}for(k=0;k<3;k++){ss.subject[k]=st[j].subject[k];st[j].subject[k]=st[j+1].subject[k];st[j+1].subject[k]=ss.subject[k];}ss.ave=st[j].ave;st[j].ave=st[j+1].ave;st[j+1].ave=ss.ave;}}}if((fp2=fopen("stu_sort","w"))==NULL){printf("can not open file:stu_sort\n");exit(0);}for(i=0;i<N;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

#include<stdio.h>#include<stdlib.h>#defineN5typedefstructstudent{intnum;charname[10];doublesub[3];doubleave;}stu;intmain(){FILE*fp1,*fp2;inti,j,k;intleap=1;// 标记是否需要插入到末尾stu temp,st[N+1];// 数组大小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((fp1=fopen("stu_sort","r"))==NULL){printf("can not open file:stu_sort\n");exit(0);}// 读取所有学生数据for(i=0;i<N;i++){if((fread(&st[i],sizeof(stu),1,fp1))!=1){printf("file:stu_sort error of reading\n");exit(0);}}// 查找插入位置并插入for(i=0;i<N;i++){if(temp.ave>st[i].ave){leap=0;// 把从i开始的元素后移一位for(j=N-1;j>=i;j--){st[j+1]=st[j];// 直接结构体赋值,不用手动复制字段}st[i]=temp;// 插入新学生break;}}// 如果所有学生平均分都更高,插入到末尾(下标N)if(leap){st[N]=temp;}// 写入新文件if((fp2=fopen("charu","w+"))==NULL){printf("can not open file:charu!\n");exit(0);}for(i=0;i<N+1;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

#include<stdio.h>#include<stdlib.h>#defineN6typedefstructstudent{intnum;charname[10];doublesub[3];doubleave;}stu;voidmain(){stu st[N];inti;FILE*fp1,*fp2;if((fp1=fopen("charu","r"))==NULL){printf("can not open file:chatu!\n");exit(0);}if((fp2=fopen("stu_sort","w+"))==NULL){printf("can not open file:chatu!\n");exit(0);}for(i=0;i<N;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

#include<stdio.h>#include<stdlib.h>#include<string.h>#defineN100// 每行最大长度intmain(){chars[N];FILE*fp;if((fp=fopen("str","w+"))==NULL){printf("can not open file:str!\n");exit(0);}// 1. 输入若干行,写入文件(支持空格,空行结束)printf("请输入若干行字符(输入空行结束):\n");while(1){fgets(s,N,stdin);intlen=strlen(s);if(len>0&&s[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){intlen=strlen(s);if(len>0&&s[len-1]=='\n'){s[len-1]='\0';}// 小写转大写for(intj=0;s[j]!='\0';j++){if(s[j]>='a'&&s[j]<='z'){s[j]&=0xdf;}}printf("%s\n",s);}fclose(fp);return0;}
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/25 0:43:33

ChatGPT资源大全:从开源仓库到AI应用开发实战指南

1. 项目概述&#xff1a;一个汇聚ChatGPT相关资源的开源仓库最近在折腾AI应用开发&#xff0c;特别是围绕ChatGPT的各类工具和项目时&#xff0c;发现了一个宝藏级的开源仓库&#xff1a;jqueryscript/ChatGPT-Resources。这可不是一个简单的代码库&#xff0c;而是一个由社区驱…

作者头像 李华
网站建设 2026/5/9 23:05:54

【审计专栏】【财务领域】第八十八篇 货币效应和货币沉淀和货币的呼吸吐纳 01

编号 类型 货币沉淀/流动/扩张/循环模式 货币杠杆类型 货币算法逐步推理思考的数学方程式 时序方程式 需求的所有参数和特征列表【数学模型、拓扑、特征向量、集合、概率模式和概率时序和概率因子、统计和统计分布和统计特征、逻辑、概率与统计特征、随机性、不确定性、数…

作者头像 李华
网站建设 2026/5/9 23:03:44

基于Matrix与OpenAI API构建智能聊天机器人:从原理到部署实践

1. 项目概述&#xff1a;一个能让你在Matrix上“召唤”ChatGPT的智能机器人 如果你和我一样&#xff0c;既是开源即时通讯协议Matrix的忠实用户&#xff0c;又对ChatGPT这类大语言模型&#xff08;LLM&#xff09;的强大能力爱不释手&#xff0c;那你一定有过这样的想法&#x…

作者头像 李华
网站建设 2026/5/9 23:03:18

CANN/cannbot-skills torch_npu接口列表

torch_npu接口列表 【免费下载链接】cannbot-skills CANNBot 是面向 CANN 开发的用于提升开发效率的系列智能体&#xff0c;本仓库为其提供可复用的 Skills 模块。 项目地址: https://gitcode.com/cann/cannbot-skills 本章节包含常用自定义接口&#xff0c;包括创建ten…

作者头像 李华
网站建设 2026/5/9 23:01:33

海豚1船舶自主靠离泊MPC控制算法与试验【附程序】

✨ 本团队擅长数据搜集与处理、建模仿真、程序设计、仿真代码、EI、SCI写作与指导&#xff0c;毕业论文、期刊论文经验交流。 ✅ 专业定制毕设、代码 ✅ 如需沟通交流&#xff0c;可以私信&#xff0c;或者点击《获取方式》 &#xff08;1&#xff09;基于二阶贝塞尔曲线的平滑…

作者头像 李华