news 2026/5/10 11:52:50

c语言绿皮书第三版链表、文件综合练习

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
c语言绿皮书第三版链表、文件综合练习

1.简单计算器(不支持括号)

// 本计算器只适合,加减乘除的混合运算,包括浮点数的运算,不支持带括号的运算式#include<stdio.h>#defineN5// 声明计算函数doublecalc(double*,char*,int);intmain(){doublea[N];chars1[N];// 计算结果doubleresult;inti=0,temp=0;while(i<N){// 输入计算数字scanf("%lf",a+i);// 输入计算符号scanf("%c",s1+i);temp++;// 输入等于号 结束if(s1[i]=='=')break;i++;}printf("\n\n");printf("要计算的表达式为:");for(i=0;i<temp;i++){if(a[i]==(int)(a[i])){printf("%d",(int)a[i]);}else{printf("%f",a[i]);}printf("%c",s1[i]);}// 计算表达式 函数result=calc(a,s1,temp);if(result==(int)(result)){printf("%d\n",(int)result);}else{printf("%f\n",result);}return0;}doublecalc(double*a,char*s,intt){//a -> 3 9 1//s -> * -// t = 2inti;doublere=0;for(i=0;i<t-1;i++){if((s[i]=='*'||s[i]=='/')){switch(s[i]){case'*':a[i+1]=a[i]*a[i+1];a[i]=0;break;case'/':a[i+1]=a[i]/a[i+1];a[i]=0;break;}}}re=a[0];for(i=0;i<t-1;i++){switch(s[i]){case'*':case'/':case'+':re+=a[i+1];break;case'-':if(a[i+1]!=0){re-=a[i+1];break;}else{s[i+1]='-';}}}returnre;}

2. 动态链表(学生管理系统)

#include<stdio.h>#include<stdlib.h>// #include<malloc.h>#defineNULL0//NULL表示"空地址",即不指向任何类型的数据#defineLENsizeof(structstudent)// 用"LEN"表示要动态开辟存储空间的的大小,sizeof(struct student)求出结构体的长度structstudent// 定义结构体{longnum;floatscore;structstudent*next;//定义了指向本身的指针成员};intn;//定义全局变量n,用来存放动态链表节点的个数structstudent*creat(void)//定义动态链表函数{structstudent*head;//定义头指针structstudent*p1,*p2;//指针p1,p2用在节点的产生n=0;p1=p2=(structstudent*)malloc(LEN);//初始化p1,p2,(struct student *)malloc(LEN)表示将malloc 函数的返回值 强行转化为指向 struct// student 类型的指针,因为 malloc 函数本身返回值为 void 类型的指针,所以这里要进行转化scanf("%ld,%f",&p1->num,&p1->score);//输入 结构体成员 num scorehead=NULL;//初始化 头指针while(p1->num!=0)//用0表示输入结束,所以这里当num不等于零时,继续输入{n+=1;//n在这里记录节点的个数if(n==1){head=p1;//如果n==1,表示第一个节点,应使得 head==p1}else{p2->next=p1;//这里不再是第一个节点,因此让p2的next成员指向下一个节点的首地址}p2=p1;//在将p1的值给p2,使得p1和p2指向同一个节点p1=(structstudent*)malloc(LEN);//开辟下一个节点,使得p1指向它scanf("%ld,%f",&p1->num,&p1->score);//输入下一个节点的数据}p2->next=NULL;//当成员num==0时,表示输入结束因此将p2 的next成员为空,即当链表收尾returnhead;//返回链表的头指针}/*链表的输出*/voidprint(structstudent*head)// 定义链表输出函数{structstudent*p;//定义指向该结构的指针printf("\nNow,These %d record are:\n",n);p=head;//将头指针赋给该指针if(head!=NULL)//如果链表不为空则访问整个链表{do{printf("%ld %5.1f\n",p->num,p->score);p=p->next;}while(p!=NULL);}}/*链表的删除操作*/structstudent*del(structstudent*head,longnum){structstudent*p1,*p2;//定义两个指向结构体的指针if(head==NULL)//判断是否为空链表{printf("\nlist null!\n");gotoend;//如果为空则结束}p1=head;//令p1指向第一个节点while(num!=p1->num&&p1->next!=NULL)// 查找链表的每一个元素{p2=p1;p1=p1->next;}if(num==p1->num)//找到了要删除的元素{if(p1==head)//判断该元素是否为第一个节点元素{head=p1->next;}else{p2->next=p1->next;}printf("delete:%ld\n",num);n=n-1;//节点数目减一}else{printf("%ld not been found! \n",num);}end:returnhead;}/*链表的插入操作*/structstudent*insert(structstudent*head,structstudent*stud){structstudent*p0,*p1,*p2;//定义三个结构体指针变量,p0指向要插入的节点p1=head;p0=stud;//p0指向要插入的节点if(head==NULL)//判断链表是否为空,若是空链表,则将该元素直接插入{head=p0;p0->next=NULL;}else{while((p0->num>p1->num)&&(p1->next!=NULL))//查找该元素要插入的位置{p2=p1;p1=p1->next;}if(p0->num<=p1->num)//找到该元素要插入的位置{if(head==p1)//判断该元素的插入位置是否在第一个元素之前{head=p0;}else//该元素的位置不是在第一个元素之前{p2->next=p0;}p0->next=p1;}else//该元素的位置在最后一个元素位置之后{p1->next=p0;p0->next=NULL;}}n+=1;returnhead;}#if(0)/**************************************************************************************/voidmain(){structstudent*head,stu;longdel_num;printf("input records:\n");head=creat();//建立链表,返回头指针print(head);//打印全部节点printf("\ninput the deleted number:");scanf("%ld",&del_num);//输入要删除的学号head=del(head,del_num);//调用删除函数删该节点print(head);//打印删除后的链表printf("\ninput the inserted record:");scanf("%ld,%f",&stu.num,&stu.score);//输入要插入的节点head=insert(head,&stu);//调用插入函数,将节点插入print(head);}/***************************************************************************************//*该程序只插入了一个节点,若要插入多个节点,如果将程序的最后四行重写一遍,再次输入则会出错*///因为 上述程序主函数定义要插入的节点的地址是固定的,所以要想插入多个节点,则每个节点必须用//malloc 函数从新开辟一个动态存储空间/***************************************************************************************/#else/***************************************************************************************/intmain(){structstudent*head,*stu;longdel_num;printf("input records:\n");head=creat();//建立链表print(head);printf("input delete number: ");scanf("%ld",&del_num);//输入要删除的元素while(del_num!=0){head=del(head,del_num);//调用删除函数print(head);printf("input delete number: ");scanf("%ld",&del_num);}printf("\ninput the insert number: ");stu=(structstudent*)malloc(LEN);//利用 malloc函数开辟动态存储空间scanf("%ld,%f",&stu->num,&stu->score);//输入要插入的元素while(stu->num!=0){head=insert(head,stu);//调用插入函数print(head);printf("input the insert record: ");stu=(structstudent*)malloc(LEN);//将 malloc函数的返回值转化为 指向结构体类型的指针scanf("%ld,%f",&stu->num,&stu->score);}return0;}#endif/**************************************************************************************************/

3.静态链表

#include<stdio.h>#defineNULL_0structstudent{longnum;floatscore;structstudent*next;};intmain(){structstudenta,b,c,*head,*p;a.num=10101;a.score=89.5;b.num=10103;b.score=90;c.num=10107;c.score=85;head=&a;a.next=&b;b.next=&c;c.next=NULL;p=head;do{printf("%ld %5.1f\n",(*p).num,p->score);p=(*p).next;}while(p!=NULL_);return0;}

4.从键盘输入一些字符,逐个把它们送到磁盘上去,直到输入一个"#"为止

//从键盘输入一些字符,逐个把它们送到磁盘上去,直到输入一个"#"为止#include<stdio.h>#include<stdlib.h>intmain(){FILE*fp;charch,filename[10];scanf("%s",filename);if((fp=fopen(filename,"w"))==NULL){printf("can not open file \n");exit(0);}ch=getchar();ch=getchar();while(ch!='#'){fputc(ch,fp);putchar(ch);ch=getchar();}putchar(10);fclose(fp);return0;}

5.文件复制

//将一个磁盘文件中的信息复制到另一个磁盘文件中#include<stdio.h>#include<stdlib.h>intmain(){FILE*in,*out;charch,infile[10],outfile[10];printf("Enter the infile name:\n");scanf("%s",infile);printf("Enter the outfile name:\n");scanf("%s",outfile);if((in=fopen(infile,"r"))==NULL){printf("can not open infile \n");exit(0);}if((out=fopen(outfile,"w"))==NULL){printf("can not open outfile \n");exit(0);}while((ch=fgetc(in))!=EOF){fputc(ch,out);}fclose(in);fclose(out);return0;}
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/10 11:51:51

终极2048 AI助手指南:如何用智能算法轻松突破4096分

终极2048 AI助手指南&#xff1a;如何用智能算法轻松突破4096分 【免费下载链接】2048-ai AI for the 2048 game 项目地址: https://gitcode.com/gh_mirrors/20/2048-ai 你是否曾经在2048游戏中陷入困境&#xff0c;眼看就要合成2048却因一步失误功亏一篑&#xff1f;20…

作者头像 李华
网站建设 2026/5/10 11:50:16

解密ImageGlass:超越传统图片查看器的专业解决方案

解密ImageGlass&#xff1a;超越传统图片查看器的专业解决方案 【免费下载链接】ImageGlass &#x1f3de; A lightweight, versatile image viewer 项目地址: https://gitcode.com/gh_mirrors/im/ImageGlass 在数字图像处理日益普及的今天&#xff0c;一个高效、轻量且…

作者头像 李华
网站建设 2026/5/10 11:45:50

C-Lodop云打印控件在HTTPS环境下的部署与安全配置指南

1. HTTPS环境下C-Lodop打印控件的部署挑战 最近帮客户做Web系统升级时遇到个典型问题&#xff1a;原本在HTTP环境下运行良好的C-Lodop打印功能&#xff0c;迁移到HTTPS后突然罢工了。这其实是很多企业在安全升级过程中都会遇到的场景——当网站协议从HTTP切换到HTTPS&#xff0…

作者头像 李华
网站建设 2026/5/10 11:45:48

Windows下CLion配置NDK开发环境避坑指南:从CMake工具链到ABI选择

Windows下CLion配置NDK开发环境避坑指南&#xff1a;从CMake工具链到ABI选择 当Android开发进入性能敏感领域时&#xff0c;NDK&#xff08;Native Development Kit&#xff09;便成为突破Java层性能瓶颈的利器。而CLion作为JetBrains家族中专业的C/C IDE&#xff0c;其智能代码…

作者头像 李华
网站建设 2026/5/10 11:44:02

暗黑3按键宏终极方案:D3KeyHelper图形化配置工具深度解析

暗黑3按键宏终极方案&#xff1a;D3KeyHelper图形化配置工具深度解析 【免费下载链接】D3keyHelper D3KeyHelper是一个有图形界面&#xff0c;可自定义配置的暗黑3鼠标宏工具。 项目地址: https://gitcode.com/gh_mirrors/d3/D3keyHelper 你是否在暗黑3中厌倦了重复的按…

作者头像 李华
网站建设 2026/5/10 11:41:50

终极QMC音频解密工具:免费高效转换QQ音乐加密文件完整指南

终极QMC音频解密工具&#xff1a;免费高效转换QQ音乐加密文件完整指南 【免费下载链接】qmc-decoder Fastest & best convert qmc 2 mp3 | flac tools 项目地址: https://gitcode.com/gh_mirrors/qm/qmc-decoder 你是否曾经从QQ音乐下载了喜爱的歌曲&#xff0c;却发…

作者头像 李华