




已阅读5页,还剩28页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第七章预处理命令第九章结构体与共用体,1,7.1.宏调用实现变量a、b内容的交换。,#include#defineMYSWAP(z,x,y)z=x;x=y;y=z;voidmain()floata=5,b=16,c;MYSWAP(c,a,b);printf(%f%f%fn,a,b,c);,2,7.2.程序输出结果(36)。,#include#definef(x)x*xvoidmain()inta=6,b=2,c;c=f(a)/f(b);/a*a/b*bprintf(%dn,c);,3,7.3.程序输出结果(9.840000)。,#include#definePR(a)printf(“%f,a)#defineF(y)3.84+y#definePRINT(a)PR(a);putchar(n)voidmain()intx=2;PRINT(F(3)*x);,4,7.4.swap(a,b)实现两个参数互换。,#include#defineswap(a,b)a=ab;b=ba;a=ab;/异或,对a和b类型有限制/#defineswap(a,b)a=a+b;b=a-b;a=a-b;/求和,对a和b上界有限制voidmain()inta,b;scanf(%d%d,5,7.5.编写宏定义MyLpha(c),以判定c是大写字母还是小写字母,当c是小写字母时宏调用取值为1,当c是大写字母时宏调用取值为0。,#include#defineMyLpha(c)(c=97)?1:0voidmain()charc;scanf(%c,6,9.1a.定义一个图书馆相关信息的结构体类型和结构体变量,其中包括成员书号、书名、作者、出版社和价格;从键盘输入10本图书信息,计算并输出这10本图书的平均价格。,#include#include#defineN10typedefstructBookcardcharnum10;charname30;charauthor30;charpublisher60;floatprice;Bookcard;,voidmain()BookcardbookN;inti=0;floatmeanprice=0;for(i=0;iname);gets(b-author);gets(b-publisher);scanf(%f,8,9.2a.在第1题定义的结构体类型中增加一个成员出版日期,该日期是一个嵌套的结构类型变量,其中包括年、月、日;设计一个输入/输出图书馆信息的函数read和print;并编写主函数定义一个10个元素的结构数组,分别调用输入/输出函数输入和输出图书信息。,#include#include#defineN10typedefstructDateintyear;intmonth;intday;Date;typedefstructBookcardcharnum10;charname30;charauthor30;charpublisher60;floatprice;Datedate;Bookcard;,voidread(Bookcard*p)Bookcard*b;for(b=p;bnum);gets(b-name);gets(b-author);gets(b-publisher);scanf(%f,9,9.2b.,voidprint(Bookcard*p)Bookcard*b;for(b=p;bnum);puts(b-name);puts(b-author);puts(b-publisher);printf(%.2fn,b-price);printf(%d%d%dn,b-date.year,b-date.month,b-date.day);,voidmain()BookcardbookN;read(book);print(book);,10,9.3a.在第2题的基础上,增加一个按书号递增排序的排序函数sort,在主函数中调用排序函数再输出图书信息。,voidexchange(Bookcard*b,Bookcard*d)Bookcardbook1;strcpy(book1.num,b-num);strcpy(,b-name);strcpy(book1.author,b-author);strcpy(book1.publisher,b-publisher);book1.price=b-price;book1.date.year=b-date.year;book1.date.month=b-date.month;book1.date.day=b-date.day;strcpy(b-num,d-num);strcpy(b-name,d-name);strcpy(b-author,d-author);strcpy(b-publisher,d-publisher);b-price=d-price;,b-date.year=d-date.year;b-date.month=d-date.month;b-date.day=d-date.day;strcpy(d-num,book1.num);strcpy(d-name,);strcpy(d-author,book1.author);strcpy(d-publisher,book1.publisher);d-price=book1.price;d-date.year=book1.date.year;d-date.month=book1.date.month;d-date.day=book1.date.day;,book1=*b;*b=*d;*d=book1;,11,9.3b.,voidsort(Bookcard*p)Bookcard*b,*d;for(b=p;bnum,d-num)0exchange(b,d);,voidmain()BookcardbookN;read(book);sort(book);print(book);,B0,p,b,d,B1,B2,B3,12,9.4a.建立一个链表,每个节点包括:书号、书名、作者和出版社,并编写按书号查询和删除节点的函数。,#include#include#includetypedefstructBookcardcharnum10;charname30;charauthor30;charpublisher60;Bookcard*next;Bookcard;,Bookcard*create()Bookcard*head;Bookcard*p,*r;charnum10;head=NULL;gets(num);while(strlen(num)!=0)printf(%dn,strlen(num);p=(Bookcard*)malloc(sizeof(Bookcard);strcpy(p-num,num);gets(p-name);gets(p-author);gets(p-publisher);if(head=NULL)head=p;elser-next=p;,r=p;gets(num);if(r!=NULL)r-next=NULL;printf(endn);returnhead;,B0,head,r,B1,B2,B3,p,13,9.4b.,voidprint(Bookcard*p)Bookcard*b;b=p;while(b!=NULL)puts(b-num);puts(b-name);puts(b-author);puts(b-publisher);b=b-next;,voidsearch(Bookcard*p)Bookcard*b;charnum10;gets(num);b=p;while(b!=NULL)if(strcmp(num,b-num)=0)puts(b-num);puts(b-name);puts(b-author);puts(b-publisher);b=b-next;,14,9.4c.,Bookcard*delet(Bookcard*p)Bookcard*b,*pb;charnum10;gets(num);pb=p;b=p;while(b!=NULL)if(strcmp(num,b-num)=0)if(b=p)p=b-next;elsepb-next=b-next;returnp;pb=b;b=b-next;,voidmain()Bookcard*head;head=create();print(head);search(head);head=delet(head);print(head);,B0,p,pb,b,B1,B2,B3,15,9.5a.根据以下学生情况表,编制一个C语言程序,分别应用选择法和冒泡法对该学生情况表按成绩从低到高进行排序处理并输出。,#include#include#defineN5typedefstructStudentcharnum10;charname10;charsex;intage;floatgrade;Student;,voidread(Student*p)Student*b;for(b=p;bnum);gets(b-name);scanf(%c%d%f,16,9.5b.,voidprint(Student*p)inti=0;for(i=0;inum);puts(pi-name);printf(%c%d%fn,pi-sex,pi-age,pi-grade);,17,9.5c.,voidsort(Student*p)Student*q;inti=0,j=0;for(i=0;igradegrade)q=pi;pi=pj;pj=q;,voidmain()StudentstuN,*sN;inti;for(i=0;inum,num);gets(p-name);scanf(%c%d%f,S1,head,S0,S2,p,19,9.6b.,voidprint(Student*p)Student*b;b=p;while(b!=NULL)puts(b-num);puts(b-name);printf(%c%d%.2fn,b-sex,b-age,b-grade);b=b-next;,B0,p,pb,b,B1,B2,B3,Student*delet(Student*p)Student*b,*pb;intage;scanf(%d,20,9.6c.,voidmain()Student*head;head=create();head=delet(head);print(head);,21,9.7.13个人围成一圈,从第1个人开始顺序报号1、2、3。凡报到“3”者退出圈子。编程找到最后留在圈子的人原来的序号。,#include#defineN13structpersonintnumber;intnextp;linkN+1;voidmain()inti,count,h;for(i=1;i=N;i+)if(i=N)linki.nextp=1;elselinki.nextp=i+1;linki.number=i;,count=0;h=N;while(countpublisher);if(head=NULL)head=p;elser-next=p;r=p;gets(num);if(r!=NULL)r-next=NULL;printf(endn);returnhead;,23,9.8b.,Bookcard*combin(Bookcard*La,Bookcard*Lb)/把Lb的结点都插入到La中Bookcard*pa1,*pa2,*pb1,*pb2;pa1=pa2=La;pb1=pb2=Lb;dowhile(strcmp(pb1-num,pa1-num)0),24,A0,La,A1,A2,A3,Lb,B2,pa2,pa1,pb1,B1,B0,pb2,A0,La,A1,A2,A3,B0,Lb,B1,B2,pa2,pa1,pb1,pb2,25,9.8c.,voidmain()Bookcard*La,*Lb,*Lc;La=create();Lb=create();Lc=combin(La,Lb);print(Lc);,26,9.9a.单链表结构实现直接选择排序。,voidprint(Bookcard*p)Bookcard*b;b=p;while(b!=NULL)puts(b-num);puts(b-publisher);b=b-next;,Student*create()Student*head,*p;charnum10;head=NULL;gets(num);while(strlen(num)!=0)p=(Student*)malloc(sizeof(Student);strcpy(p-num,num);gets(p-name);scanf(%c%d%f,27,9.9b.单链表结构实现直接选择排序。,Student*sort(Student*p)Student*head;Student*pnow,*pnow2,*pmin,*p1,*p2;head=p;pnow=head;/当前结点pnow2=head;/pnow先继while(pnow-next!=NULL)pmin=pnow;p2=pmin;p1=pmin-next;while(p1!=NULL)if(pmin-gradep1-grade)pmin=p1;/最小结点p1=p1-next;,if(pmin=pnow)pnow2=pnow;pnow=pnow-next;elsewhile(p2-next!=pmin)p2=p
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 网红电影观影感受创新创业项目商业计划书
- 橡胶制胶工晋升考核试卷及答案
- 智能摄像头宠物看护系统创新创业项目商业计划书
- 保健食品质量控制体系创新创业项目商业计划书
- 轻冶料浆配料工标准化作业考核试卷及答案
- 井矿盐采卤工标准化作业考核试卷及答案
- 考点解析华东师大版8年级下册期末测试卷含答案详解(新)
- 量子通信新质生产力
- 解析卷-冀教版8年级下册期末试卷附参考答案详解(典型题)
- 解析卷-湖北省洪湖市中考数学真题分类(位置与坐标)汇编难点解析试题(详解版)
- 石英长石无氟浮选分离工艺研究现状
- 对铁路机车乘务员规章培训的探讨与实践
- GB/T 18947-2003矿用钢丝增强液压软管及软管组合件
- 法律合规网络知识竞赛试题汇总
- 2016风行菱智m5原厂维修手册及电路图-14
- 车辆维修项目投标方案
- 女生青春期生理健康教育
- 《绿色建筑》绿色建筑与建筑节能课件
- 商场撤场申请书
- 第二章汤小丹计算机操作系统 官方课件 第四版
- 炭疽病诊断治疗与
评论
0/150
提交评论