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

下載本文檔

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

文檔簡(jiǎn)介

第七章預(yù)處理命令第九章結(jié)構(gòu)體與共用體,1,7.1.宏調(diào)用實(shí)現(xiàn)變量a、b內(nèi)容的交換。,#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.程序輸出結(jié)果(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.程序輸出結(jié)果(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)實(shí)現(xiàn)兩個(gè)參數(shù)互換。,#include#defineswap(a,b)a=ab;b=ba;a=ab;/異或,對(duì)a和b類(lèi)型有限制/#defineswap(a,b)a=a+b;b=a-b;a=a-b;/求和,對(duì)a和b上界有限制voidmain()inta,b;scanf(%d%d,5,7.5.編寫(xiě)宏定義MyLpha(c),以判定c是大寫(xiě)字母還是小寫(xiě)字母,當(dāng)c是小寫(xiě)字母時(shí)宏調(diào)用取值為1,當(dāng)c是大寫(xiě)字母時(shí)宏調(diào)用取值為0。,#include#defineMyLpha(c)(c=97)?1:0voidmain()charc;scanf(%c,6,9.1a.定義一個(gè)圖書(shū)館相關(guān)信息的結(jié)構(gòu)體類(lèi)型和結(jié)構(gòu)體變量,其中包括成員書(shū)號(hào)、書(shū)名、作者、出版社和價(jià)格;從鍵盤(pán)輸入10本圖書(shū)信息,計(jì)算并輸出這10本圖書(shū)的平均價(jià)格。,#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題定義的結(jié)構(gòu)體類(lèi)型中增加一個(gè)成員出版日期,該日期是一個(gè)嵌套的結(jié)構(gòu)類(lèi)型變量,其中包括年、月、日;設(shè)計(jì)一個(gè)輸入/輸出圖書(shū)館信息的函數(shù)read和print;并編寫(xiě)主函數(shù)定義一個(gè)10個(gè)元素的結(jié)構(gòu)數(shù)組,分別調(diào)用輸入/輸出函數(shù)輸入和輸出圖書(shū)信息。,#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題的基礎(chǔ)上,增加一個(gè)按書(shū)號(hào)遞增排序的排序函數(shù)sort,在主函數(shù)中調(diào)用排序函數(shù)再輸出圖書(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;,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.建立一個(gè)鏈表,每個(gè)節(jié)點(diǎn)包括:書(shū)號(hào)、書(shū)名、作者和出版社,并編寫(xiě)按書(shū)號(hào)查詢(xún)和刪除節(jié)點(diǎn)的函數(shù)。,#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.根據(jù)以下學(xué)生情況表,編制一個(gè)C語(yǔ)言程序,分別應(yīng)用選擇法和冒泡法對(duì)該學(xué)生情況表按成績(jī)從低到高進(jìn)行排序處理并輸出。,#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個(gè)人圍成一圈,從第1個(gè)人開(kāi)始順序報(bào)號(hào)1、2、3。凡報(bào)到“3”者退出圈子。編程找到最后留在圈子的人原來(lái)的序號(hào)。,#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的結(jié)點(diǎn)都插入到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.單鏈表結(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;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.單鏈表結(jié)構(gòu)實(shí)現(xiàn)直接選擇排序。,Student*sort(Student*p)Student*head;Student*pnow,*pnow2,*pmin,*p1,*p2;head=p;pnow=head;/當(dāng)前結(jié)點(diǎn)pnow2=head;/pnow先繼while(pnow-next!=NULL)pmin=pnow;p2=pmin;p1=pmin-next;while(p1!=NULL)if(pmin-gradep1-grade)pmin=p1;/最小結(jié)點(diǎn)p1=p1-next;,if(pmin=pnow)pnow2=pnow;pnow=pnow-next;elsewhile(p2-next!=pmin)p2=p

溫馨提示

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

最新文檔

評(píng)論

0/150

提交評(píng)論