结构体与共用体(1)_第1页
结构体与共用体(1)_第2页
结构体与共用体(1)_第3页
结构体与共用体(1)_第4页
结构体与共用体(1)_第5页
已阅读5页,还剩29页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、精选ppt第七章第七章 预处理命令预处理命令第九章第九章 结构体与共用体结构体与共用体精选ppt#include #define MYS) z=x;x=y;y=z;void main()float a=5,b=16,c;MYS);printf(%f %f %fn,a,b,c);7.1.宏调用实现变量a、b内容的交换。精选ppt7.2.程序输出结果(36)。#include #define f(x) x*xvoid main()int a=6,b=2,c;c=f(a)/f(b); /a*a/b*bprintf(%dn,c);精选ppt7.3.程序输出结果(9.840000)。#include #

2、define PR(a) printf(“%f,a)#define F(y) 3.84+y#define PRINT(a) PR(a); putchar(n)void main()int x=2;PRINT(F(3)*x);精选ppt7.4.s)实现两个参数互换。#include #define s) a=ab; b=ba;a=ab;/异或,对a和b类型有限制/#define s) a=a+b;b=a-b;a=a-b;/求和,对a和b上界有限制void main()int a, b;scanf(%d%d,&a,&b);s);printf(%d %dn,a,b);精选ppt#in

3、clude #define MyLpha(c) (c=97)?1:0void main()char c;scanf(%c,&c);if (MyLpha(c)printf(%cn,c-32);elseprintf(%cn,c+32);7.5.编写宏定义MyLpha(c),以判定c是大写字母还是小写字母,当c是小写字母时宏调用取值为1,当c是大写字母时宏调用取值为0。 精选ppt9.1a. 定义一个图书馆相关信息的结构体类型和结构体变量,其中包括成员书号、书名、作者、出版社和价格;从键盘输入10本图书信息,计算并输出这10本图书的平均价格。#include #include #define

4、 N 10 typedef struct Bookcardchar num10;char name30;char author30;char publisher60;float price; Bookcard;void main()Bookcard bookN;int i=0;float meanprice=0;for (i=0;iN;i+)scanf(%s,booki.num);scanf(%s,);scanf(%s,booki.author);scanf(%s,booki.publisher);scanf(%f,&booki.price);meanprice=m

5、eanprice+booki.price;meanprice=meanprice/N;printf(%.2f,meanprice);精选ppt9.1b.#include #include #define N 10typedef struct Bookcardchar num10;char name30;char author30;char publisher60;float price; Bookcard;void main() /用指针实现赋值Bookcard bookN, *b;float meanprice=0;for (b=book;bnum);gets(b-name);gets(b-

6、author);gets(b-publisher);scanf(%f,&b-price);meanprice=meanprice+b-price;getchar();/fflush(stdin);meanprice=meanprice/N;printf(%.2f,meanprice);精选ppt9.2a. 在第1题定义的结构体类型中增加一个成员出版日期,该日期是一个嵌套的结构类型变量,其中包括年、月、日;设计一个输入/输出图书馆信息的函数read和print;并编写主函数定义一个10个元素的结构数组,分别调用输入/输出函数输入和输出图书信息。#include #include #def

7、ine N 10typedef struct Dateint year;int month;int day;Date;typedef struct Bookcardchar num10;char name30;char author30;char publisher60;float price;Date date; Bookcard;void read(Bookcard *p)Bookcard *b;for (b=p;bnum);gets(b-name);gets(b-author);gets(b-publisher);scanf(%f,&b-price);scanf(%d%d%d,&

8、amp;b-date.year,&b-date.month,&b-date.day); getchar();/fflush(stdin);精选ppt9.2b.void print(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);void main()Bookcard bookN;read(book);prin

9、t(book);精选ppt9.3a. 在第2题的基础上,增加一个按书号递增排序的排序函数sort,在主函数中调用排序函数再输出图书信息。void exchange(Bookcard *b, Bookcard *d)Bookcard book1;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.mon

10、th=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,book

11、1.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;精选ppt9.3b.void sort(Bookcard *p)Bookcard *b, *d;for (b=p;bp+N-1;b+)for(d=b+1;dnum,d-num)0exchange(b,d);void main()Bookcard boo

12、kN;read(book);sort(book);print(book);B0pbdB1B2B3精选ppt9.4a. 建立一个链表,每个节点包括:书号、书名、作者和出版社,并编写按书号查询和删除节点的函数。#include #include #include typedef struct Bookcardchar num10;char name30;char author30;char publisher60;Bookcard *next; Bookcard;Bookcard *create()Bookcard *head;Bookcard *p, *r;char num10;head=NUL

13、L;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);return head;B0headrB1B2B3p精选ppt9.4b.void print(Bookcard *

14、p)Bookcard *b;b=p;while(b!=NULL)puts(b-num);puts(b-name);puts(b-author);puts(b-publisher);b=b-next;void search(Bookcard *p)Bookcard *b;char num10;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;精选ppt9.4c. Bookcard *delet(Bookcar

15、d *p)Bookcard *b ,*pb;char num10;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;return p;pb=b;b=b-next;void main()Bookcard *head;head=create();print(head);search(head);head=delet(head);print(head);B0ppbbB1B2B3精选ppt9.5a. 根据以下学生情况表,编制一个C语言程序,分别应用选择法和冒泡法对该

16、学生情况表按成绩从低到高进行排序处理并输出。 #include #include #define N 5typedef struct Studentchar num10;char name10;char sex;int age;float grade; Student;void read(Student *p)Student *b;for (b=p;bnum);gets(b-name);scanf(%c%d%f,&b-sex,&b-age,&b-grade);getchar();/fflush(stdin);精选ppt9.5b. void print(Student *

17、p)int i=0;for (i=0;inum);puts(pi-name);printf(%c %d %fn,pi-sex,pi-age,pi-grade);精选ppt9.5c. void sort(Student *p)Student *q;int i=0,j=0;for(i=0;iN-1;i+)for(j=i+1;jgradegrade)q=pi;pi=pj;pj=q;void main()Student stuN, *sN;int i;for (i=0; iN; i+)si=&stui;read(stu);sort(s);print(s);精选ppt9.6. 已知每个学生情况的

18、结点结构类型。(1)初始链表为空(2)新结点插入表头(3)输出链表(4)删除结点并输出 #include #include #include typedef struct Studentchar num10;char name10;char sex;int age;float grade;Student *next; Student;Student *create()Student *head;Student *p;char num10;head=NULL;gets(num);while(strlen(num)!=0)p=(Student*)malloc(sizeof(Student);str

19、cpy(p-num,num);gets(p-name);scanf(%c%d%f,&p-sex,&p-age,&p-grade);getchar();p-next=head;head=p;gets(num);printf(endn);return head;S1headS0S2p精选ppt9.6b.void print(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;B0ppbbB1B2B3S

20、tudent *delet(Student *p)Student *b ,*pb;int age;scanf(%d,&age);pb =p; b=p;while(b!=NULL)if (age=b-age)if (b=p)p=b-next;elsepb -next=b-next;return p;pb=b;b=b-next;精选ppt9.6c.void main()Student *head;head=create();head=delet(head);print(head);精选ppt9.7. 13个人围成一圈,从第1个人开始顺序报号1、2、3。凡报到“3”者退出圈子。编程找到最后留

21、在圈子的人原来的序号。 #include #define N 13struct personint number;int nextp;linkN+1;void main()int i,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(countN-1)i=0;while(i!=3)h=linkh.nextp;if(linkh.number)i+;linkh.number=0;count+;for (i=1;i=N;i+)if (linki.numb

22、er)printf(%3d,linki.number);printf(n);精选ppt9.8a. 两个链表La和Lb合并成一个递增有序的单链表Lc。#include #include #include typedef struct Bookcardchar num10;char publisher60;Bookcard *next; Bookcard;Bookcard *create()Bookcard *head;Bookcard *p, *r;char num10;head=NULL;gets(num);while(strlen(num)!=0)p=(Bookcard*)malloc(si

23、zeof(Bookcard);strcpy(p-num,num);gets(p-publisher);if (head=NULL)head=p;elser-next=p;r=p;gets(num);if (r!=NULL)r-next=NULL;printf(endn);return head;精选ppt9.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

24、)0)&(pa1-next!=NULL)pa2=pa1;pa1=pa1-next;/在La中找比pb1小的结点,pa2指向if(strcmp(pb1-num,pa1-num)next=pb1;pb1=pb1-next;pb2-next=pa1;pa2=pb2;pb2=pb1;else if (pa1-next=NULL)break;while(pb1!=NULL);if(pb1!=NULL)&(strcmp(pb1-num,pa1-num)0)&(pa1-next=NULL)pa1-next=pb1;return La;精选pptA0LaA1A2A3LbB2pa2pa1

25、pb1B1B0pb2A0LaA1A2A3B0LbB1B2pa2pa1pb1pb2精选ppt9.8c.void main()Bookcard *La, *Lb, *Lc;La=create();Lb=create();Lc=combin(La,Lb);print(Lc);精选ppt9.9a. 单链表结构实现直接选择排序。void print(Bookcard *p)Bookcard *b;b=p;while(b!=NULL)puts(b-num);puts(b-publisher);b=b-next;Student *create()Student *head, *p;char num10;he

26、ad=NULL;gets(num);while(strlen(num)!=0)p=(Student*)malloc(sizeof(Student);strcpy(p-num,num);gets(p-name);scanf(%c%d%f,&p-sex,&p-age,&p-grade);getchar();p-next=head;head=p;gets(num);printf(endn);return head;精选ppt9.9b. 单链表结构实现直接选择排序。Student *sort(Student *p)Student *head;Student *pnow, *pnow2, *pmin, *p1, *p2;head=p;pnow=head;/当前结点pnow2=head;/pnow先继w

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论