C程序设计课件_第1页
C程序设计课件_第2页
C程序设计课件_第3页
C程序设计课件_第4页
C程序设计课件_第5页
已阅读5页,还剩27页未读 继续免费阅读

下载本文档

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

文档简介

第七章预处理命令

第九章结构体与共用体17.1.宏调用实现变量a、b内容的交换。#include<stdio.h>#defineMYSWAP(z,x,y){z=x;x=y;y=z;}voidmain(){floata=5,b=16,c;MYSWAP(c,a,b);printf("%f%f%f\n",a,b,c);}27.2.程序输出结果(36)。#include<stdio.h>#definef(x)x*xvoidmain(){inta=6,b=2,c;c=f(a)/f(b);//a*a/b*bprintf("%d\n",c);}37.3.程序输出结果(9.840000)。#include<stdio.h>#definePR(a)printf(“%f",a)#defineF(y)3.84+y#definePRINT(a)PR(a);putchar('\n')voidmain(){intx=2;PRINT(F(3)*x);}47.4.swap(a,b)实现两个参数互换。#include<stdio.h>#defineswap(a,b){a=a^b;b=b^a;a=a^b;}//异或,对a和b类型有限制//#defineswap(a,b){a=a+b;b=a-b;a=a-b;}//求和,对a和b上界有限制voidmain(){inta,b;scanf("%d%d",&a,&b);swap(a,b);printf("%d%d\n",a,b);}57.5.编写宏定义MyLpha(c),以判定c是大写字母还是小写字母,当c是小写字母时宏调用取值为1,当c是大写字母时宏调用取值为0。

#include<stdio.h>#defineMyLpha(c)(c>=97)?1:0voidmain(){charc;scanf("%c",&c);if(MyLpha(c))printf("%c\n",c-32);elseprintf("%c\n",c+32);}69.1a.定义一个图书馆相关信息的结构体类型和结构体变量,其中包括成员书号、书名、作者、出版社和价格;从键盘输入10本图书信息,计算并输出这10本图书的平均价格。#include<stdio.h>#include<string.h>#defineN10

typedefstructBookcard{charnum[10];charname[30];charauthor[30];charpublisher[60];floatprice;}Bookcard;voidmain(){ Bookcardbook[N]; inti=0; floatmeanprice=0; for(i=0;i<N;i++){scanf("%s",book[i].num);scanf("%s",book[i].name);scanf("%s",book[i].author);scanf("%s",book[i].publisher);scanf("%f",&book[i].price);meanprice=meanprice+book[i].price; } meanprice=meanprice/N; printf("%.2f",meanprice);}79.1b.#include<stdio.h>#include<string.h>#defineN10typedefstructBookcard{charnum[10];charname[30];charauthor[30];charpublisher[60];floatprice;}Bookcard;voidmain(){//用指针实现赋值Bookcardbook[N],*b;floatmeanprice=0;for(b=book;b<book+N;b++){gets(b->num);gets(b->name);gets(b->author);gets(b->publisher);scanf("%f",&b->price);meanprice=meanprice+b->price;getchar();//fflush(stdin);}meanprice=meanprice/N;printf("%.2f",meanprice);}89.2a.在第1题定义的结构体类型中增加一个成员出版日期,该日期是一个嵌套的结构类型变量,其中包括年、月、日;设计一个输入/输出图书馆信息的函数read和print;并编写主函数定义一个10个元素的结构数组,分别调用输入/输出函数输入和输出图书信息。#include<stdio.h>#include<string.h>#defineN10typedefstructDate{intyear;intmonth;intday;}Date;typedefstructBookcard{charnum[10];charname[30];charauthor[30];charpublisher[60];floatprice;Datedate;}Bookcard;voidread(Bookcard*p){Bookcard*b;for(b=p;b<p+N;b++){gets(b->num);gets(b->name);gets(b->author);gets(b->publisher);scanf("%f",&b->price);scanf("%d%d%d",&b->date.year,&b->date.month,&b->date.day);getchar();//fflush(stdin);}}99.2b.voidprint(Bookcard*p){Bookcard*b;for(b=p;b<p+N;b++){puts(b->num);puts(b->name);puts(b->author);puts(b->publisher);printf("%.2f\n",b->price);printf("%d%d%d\n",b->date.year,b->date.month,b->date.day);}}voidmain(){Bookcardbook[N];read(book);print(book);}109.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;119.3b.voidsort(Bookcard*p){Bookcard*b,*d;for(b=p;b<p+N-1;b++)for(d=b+1;d<p+N;d++)if(strcmp(b->num,d->num)>0exchange(b,d);}voidmain(){Bookcardbook[N];read(book);sort(book);print(book);}B0pbdB1B2B3129.4a.建立一个链表,每个节点包括:书号、书名、作者和出版社,并编写按书号查询和删除节点的函数。#include<stdio.h>#include<string.h>#include<stdlib.h>typedefstructBookcard{charnum[10];charname[30];charauthor[30];charpublisher[60];Bookcard*next;}Bookcard;Bookcard*create(){Bookcard*head;Bookcard*p,*r;charnum[10];head=NULL;gets(num);while(strlen(num)!=0){printf("%d\n",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("end\n");returnhead;}B0headrB1B2B3p139.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;charnum[10];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;}}149.4c.Bookcard*delet(Bookcard*p){Bookcard*b,*pb;charnum[10];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);}B0ppbbB1B2B3159.5a.根据以下学生情况表,编制一个C语言程序,分别应用选择法和冒泡法对该学生情况表按成绩从低到高进行排序处理并输出。

#include<stdio.h>#include<string.h>#defineN5typedefstructStudent{charnum[10];charname[10];charsex;intage;floatgrade;}Student;voidread(Student*p){Student*b;for(b=p;b<p+N;b++){gets(b->num);gets(b->name);scanf("%c%d%f",&b->sex,&b->age,&b->grade);getchar();//fflush(stdin);}}169.5b.voidprint(Student*p[]){inti=0;for(i=0;i<N;i++){puts(p[i]->num);puts(p[i]->name);printf("%c%d%f\n",p[i]->sex,p[i]->age,p[i]->grade);}}179.5c.voidsort(Student*p[]){Student*q;inti=0,j=0;for(i=0;i<N-1;i++)for(j=i+1;j<N;j++)if(p[i]->grade<p[j]->grade){q=p[i];p[i]=p[j];p[j]=q;}}voidmain(){Studentstu[N],*s[N];inti;for(i=0;i<N;i++)s[i]=&stu[i];read(stu);sort(s);print(s);}189.6.已知每个学生情况的结点结构类型。

(1)初始链表为空

(2)新结点插入表头

(3)输出链表

(4)删除结点并输出

#include<stdio.h>#include<string.h>#include<stdlib.h>typedefstructStudent{charnum[10];charname[10];charsex;intage;floatgrade;Student*next;}Student;Student*create(){Student*head;Student*p;charnum[10];head=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("end\n");returnhead;}S1headS0S2p199.6b.voidprint(Student*p){Student*b;b=p;while(b!=NULL){puts(b->num);puts(b->name);printf("%c%d%.2f\n",b->sex,b->age,b->grade);b=b->next;}}B0ppbbB1B2B3Student*delet(Student*p){Student*b,*pb;intage;scanf("%d",&age);pb=p;b=p;while(b!=NULL){if(age==b->age){if(b==p)p=b->next;elsepb->next=b->next;returnp;} pb=b;b=b->next;}}209.6c.voidmain(){Student*head;head=create();head=delet(head);print(head);}219.7.13个人围成一圈,从第1个人开始顺序报号1、2、3。凡报到“3”者退出圈子。编程找到最后留在圈子的人原来的序号。

#include<stdio.h>#defineN13structperson{intnumber;intnextp;}link[N+1];voidmain(){inti,count,h;for(i=1;i<=N;i++){if(i==N)link[i].nextp=1;elselink[i].nextp=i+1;link[i].number=i;}count=0;h=N;while(count<N-1){i=0;while(i!=3){h=link[h].nextp;if(link[h].number)i++;}link[h].number=0;count++;}for(i=1;i<=N;i++)if(link[i].number)printf("%3d",link[i].number);printf("\n");}229.8a.两个链表La和Lb合并成一个递增有序的单链表Lc。#include<stdio.h>#include<string.h>#include<stdlib.h>typedefstructBookcard{charnum[10];charpublisher[60];Bookcard*next;}Bookcard;Bookcard*create(){Bookcard*head;Bookcard*p,*r;charnum[10];head=NULL;gets(num);while(strlen(num)!=0){p=(Bookcard*)malloc(sizeof(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("end\n");returnhead;}239.8b.Bookcard*combin(Bookcard*La,Bookcard*Lb){//把Lb的结点都插入到La中Bookcard*pa1,*pa2,*pb1,*pb2;pa1=pa2=La;pb1=pb2=Lb;do{while((strcmp(pb1->num,pa1->num)>0)&&(pa1->next!=NULL)){ pa2=pa1; pa1=pa1->next;}//在La中找比pb1小的结点,pa2指向if(strcmp(pb1->num,pa1->num)<0){//把pb1插入La中if(pa1==La)La=pb1;elsepa2->next=pb1;pb1=pb1->next;pb2->next=pa1;pa2=pb2;pb2=pb1;}elseif(pa1->next==NULL)break;}while(pb1!=NULL);if((pb1!=NULL)&&(strcmp(pb1->num,pa1->num)>0)&&(pa1->next==NULL)) pa1->next=pb1;returnLa;}24A0LaA1A2A3LbB2pa2pa1pb1B1B0pb2A0LaA1A2A3B0LbB1B2pa2pa1pb1pb2259.8c.voidmain(){Bookcard*La,*Lb,*Lc;La=create();Lb=create();Lc=combin(La,Lb);print(Lc);}269.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;charnum[10];head=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("end\n");returnhead;}279.9b.单链表结构实现直接选择排序。Student*sort(Stu

温馨提示

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

评论

0/150

提交评论