C程序設(shè)計(jì)課件_第1頁
C程序設(shè)計(jì)課件_第2頁
C程序設(shè)計(jì)課件_第3頁
C程序設(shè)計(jì)課件_第4頁
C程序設(shè)計(jì)課件_第5頁
已閱讀5頁,還剩27頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第七章預(yù)處理命令

第九章結(jié)構(gòu)體與共用體17.1.宏調(diào)用實(shí)現(xiàn)變量a、b內(nèi)容的交換。#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.程序輸出結(jié)果(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.程序輸出結(jié)果(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)實(shí)現(xiàn)兩個(gè)參數(shù)互換。#include<stdio.h>#defineswap(a,b){a=a^b;b=b^a;a=a^b;}//異或,對(duì)a和b類型有限制//#defineswap(a,b){a=a+b;b=a-b;a=a-b;}//求和,對(duì)a和b上界有限制voidmain(){inta,b;scanf("%d%d",&a,&b);swap(a,b);printf("%d%d\n",a,b);}57.5.編寫宏定義MyLpha(c),以判定c是大寫字母還是小寫字母,當(dāng)c是小寫字母時(shí)宏調(diào)用取值為1,當(dāng)c是大寫字母時(shí)宏調(diào)用取值為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.定義一個(gè)圖書館相關(guān)信息的結(jié)構(gòu)體類型和結(jié)構(gòu)體變量,其中包括成員書號(hào)、書名、作者、出版社和價(jià)格;從鍵盤輸入10本圖書信息,計(jì)算并輸出這10本圖書的平均價(jià)格。#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(){//用指針實(shí)現(xiàn)賦值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題定義的結(jié)構(gòu)體類型中增加一個(gè)成員出版日期,該日期是一個(gè)嵌套的結(jié)構(gòu)類型變量,其中包括年、月、日;設(shè)計(jì)一個(gè)輸入/輸出圖書館信息的函數(shù)read和print;并編寫主函數(shù)定義一個(gè)10個(gè)元素的結(jié)構(gòu)數(shù)組,分別調(diào)用輸入/輸出函數(shù)輸入和輸出圖書信息。#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題的基礎(chǔ)上,增加一個(gè)按書號(hào)遞增排序的排序函數(shù)sort,在主函數(shù)中調(diào)用排序函數(shù)再輸出圖書信息。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.建立一個(gè)鏈表,每個(gè)節(jié)點(diǎn)包括:書號(hào)、書名、作者和出版社,并編寫按書號(hào)查詢和刪除節(jié)點(diǎn)的函數(shù)。#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.根據(jù)以下學(xué)生情況表,編制一個(gè)C語言程序,分別應(yīng)用選擇法和冒泡法對(duì)該學(xué)生情況表按成績(jī)從低到高進(jìn)行排序處理并輸出。

#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.已知每個(gè)學(xué)生情況的結(jié)點(diǎn)結(jié)構(gòu)類型。

(1)初始鏈表為空

(2)新結(jié)點(diǎn)插入表頭

(3)輸出鏈表

(4)刪除結(jié)點(diǎn)并輸出

#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個(gè)人圍成一圈,從第1個(gè)人開始順序報(bào)號(hào)1、2、3。凡報(bào)到“3”者退出圈子。編程找到最后留在圈子的人原來的序號(hào)。

#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.兩個(gè)鏈表La和Lb合并成一個(gè)遞增有序的單鏈表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的結(jié)點(diǎn)都插入到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小的結(jié)點(diǎn),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.單鏈表結(jié)構(gòu)實(shí)現(xiàn)直接選擇排序。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.單鏈表結(jié)構(gòu)實(shí)現(xiàn)直接選擇排序。Student*sort(Stu

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論