C語言程序設(shè)計實驗報告(一)_第1頁
C語言程序設(shè)計實驗報告(一)_第2頁
C語言程序設(shè)計實驗報告(一)_第3頁
C語言程序設(shè)計實驗報告(一)_第4頁
C語言程序設(shè)計實驗報告(一)_第5頁
已閱讀5頁,還剩13頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、C語言程序設(shè)計實驗報告指導(dǎo)教師: 同組人姓名:專業(yè):班級:日期:成績:實驗組別:第 次實驗:學(xué)生姓名:學(xué)號:實驗名稱:指針實驗一、實驗?zāi)康?1) 熟練掌握指針的說明、賦值、使用。(2) 掌握用指針引用數(shù)組的元素,熟悉指向數(shù)組的指針的使用。(3) 熟練掌握字符數(shù)組與字符串的使用,掌握指針數(shù)組及字符指針數(shù)組的用法。(4) 掌握帶有參數(shù)的 main函數(shù)的用法。二、實驗內(nèi)容及要求1源程序改錯下面的源程序中是否存在錯誤?如果存在,原因是什么?如果存在錯誤要求在計算機上對 這個源程序進行調(diào)試修改,是之能夠正確執(zhí)行。源程序:#i nclude int main( void)float * p;scan f(

2、%f,p);prin tf(%fn,*p);return 0;2源程序修改替換(1) 下面源程序的功能是,通過函數(shù)指針和菜單選擇來調(diào)用字符串拷貝函數(shù)或字符串連接 函數(shù)。請在程序中的下劃線處填寫合適的表達式、語句或代碼片段來完善該程序。源程序:#i nclude #in clude int main( void)char a80,b80,c160,*result=c; int choice,i;doprin tf(tt1 copy stri ng.n ”);prin tf(tt2 connect stri ng.n ”);prin tf(tt3 exit.n);printf(ttinput a

3、number(1-3)please! n”); scan f(%d, &choice);while(choice5);switch (choice)case 1:p=strcpy; break;case 2:p=strcat;break;case 3:goto dow n;getchar();prin tf(i nput the first stri ng please! n);i=0;printf(input the second string please!n); i=0;result=(a,b);prin tf(the result is %sn ”,result);dow n:5ret

4、urn 0;(2) 請上機運行第(1)題程序使之能按下面要求輸出結(jié)果(注:(輸入)表示該行數(shù)據(jù)是鍵盤數(shù)據(jù)):1. copy string。2. Connect string。3. Exit。In put a nu mber ( 1-3) please!2 (輸入)In put the first stri ng please!The more you lear n,(輸入)In put the sec ond stri ng please !The more you get.(輸入)The result is the more you lear n,the more you get.3.跟蹤調(diào)

5、試程序請按下面的要求對所給源程序進行操作,并回答問題和排除錯誤。(1 )單步執(zhí)行源程序。進入strcpy時,watches窗口中s為何值?返回 main時,watches窗口中s為何值?(2)排除源程序中的錯誤,使程序輸出結(jié)果為:there is a boat on the lake.源程序:#i nclude char * strcpy(char*,char*);int main( void)char a20,b60=there is a boat on the lack.;prin tf(%sn,strcpy(a,b);return 0;char *strcpy(char*s,char*t

6、)while(*s+=*t+)5return (s);4程序設(shè)計編寫并上機調(diào)試運行能實現(xiàn)以下功能的程序或函數(shù):(1) 已知一個長整形變量占四個字符,其中每個字節(jié)又分高四位和低四位。試編寫一個程 序,從該長整形變量的高字節(jié)開始,依次取出每個字節(jié)的高四位和低四位并以數(shù)字字符的形 式進行顯示。(2) 利用大小為n的指針數(shù)組指向用 gets函數(shù)輸入的n行,每行不超過 80個字符。試編 寫一個函數(shù),它將每一行中連續(xù)的多個空格字符壓縮為一個空格字符。在調(diào)用函數(shù)中輸出壓 縮空格后的各行,空行不予輸出。(3) 編寫一個程序,輸入n個整數(shù),排序后輸出。排序的原則由命令行可選參數(shù)-d決定,并且有參數(shù)-d時按遞減順

7、序,否則按遞增順序排序。要求將排序算法定義成函數(shù),利用指向函數(shù)的指針是該函數(shù)實現(xiàn)遞增或遞減排序。(4) 設(shè)每個班有n個學(xué)生,每個學(xué)生所修的 M門課程的成績,并且都存放到相應(yīng)的數(shù)組中, 試編寫以下函數(shù): 計算每個學(xué)生各門課程平均成績 計算全班每門課程的平均成績 分別統(tǒng)計低于全班各門課程平均成績的人數(shù) 分別統(tǒng)計全班各門課程不及格的人數(shù)和90分以上(包含90)的人數(shù)。在調(diào)用函數(shù)中輸出結(jié)果(要求都用指針操作,不得用下標(biāo))5選做題(1) 編寫并上機調(diào)試運行能實現(xiàn)以下功能的程序:設(shè)有N位整數(shù)和M位小數(shù)(N=20, M=10 )的兩個數(shù)據(jù)a, b。編程計算a+b并輸出結(jié)果。 如:12345678912345

8、678912.1234567891+98765432109876543210.0123456789(2) 編寫一個使用復(fù)雜聲明的char * ( *p2)(const char*,const char *); ”的程序。 提示:p中元素可為 strcmp,strstr等函數(shù)名。三、實驗步驟及結(jié)果1源程序改錯錯誤:懸掛指針,指針未初始化 改后程序:#in cludeint main( void)float *p,a;p=&a;scan f(%f,p);prin tf(%fn,*p);return 0;2源程序完善、修改、替換#in clude #in clude int main( void)c

9、har *(*p)(char *a,char *b);char a80,b80,c160,*result=c;int choice,i;doprin tf(tt1 copy stri ng.n ”);prin tf(tt2 connect stri ng.n ”);prin tf(tt3 exit.n);prin tf(tt in put a number(1-3)please!n); scan f(%d, &choice); while(choice5);switch(choice)case 1:p=strcpy;break;case 2:p=strcat;break;case 3:goto

10、 dow n;getchar();prin tf(i nput the first stri ng please!n);i=0;while(ai=getchar()!=n)i+;ai=0:printf(input the second string please!n); i=0;while(bi=getchar()!=n)i+- bi=0: result=(*p)(a,b); prin tf(the result is %sn,result);dow n:return 0;g *C: Docuents and Sett ingsibMIy DociULent e學(xué)樓.c作業(yè)Ic實鯊 1 指針實

11、鯊 12. 1 copy string.2 connect string.3 exit.input a numberplease*input the first string please*the more you learn,input the second string please *the more you get-the result is the more you learn,the more you get.Process returned 0 execution time : 37.250 s Press any key to continue.3.跟蹤調(diào)試程序VetchesE

12、 Local vari aElfesNo locals.B Fiinct i on Arfument ss = 0x2ff2c峨Ei峨” =0x22fefO thare is 直 bot on th lack. ,V&tcKeslxE Local var i aElesNb locals.B Fuiict i on Arfumentss = 0x22 f49 “t = 0x22ffDd g#i nclude char * strcpy(char*,char*);int main( void)char a20,b60=there is a boat on the lack.; prin tf(%

13、sn,strcpy(a,b);return 0;char *strcpy(char*s,char*t)char *m;m=s;while(*s+=*t+)5return (m);4程序設(shè)計(1)#in clude#in clude#defi ne MAX 32int mai n()long int n;int i;char sMAX;prin tf(please in put a nu mber:n);scan f(%ld, &n);if(n =-2147483647&n=0)for(i=0; n!=0;i+)si=n%2+0;n/=2;for(;i=MAX-1;i+)si=O:elsen=_

14、(n+1);for(i=0; n!=0;i+)si=n%2+0;n/=2;for(;i=MAX-2;i+)si=O;for(i=0;i=28;i-)putchar(si);putchar(t);for(i=27;i=24;i-)putchar(si);putchar(t);for(i=23;i=20;i-)putchar(si);putchar(t);for(i=19;i=16;i-)putchar(si); putchar(t);for(i=15;i=12;i-) putchar(si); putchar(t);for(i=11;i=8;i-) putchar(si); putchar(t)

15、;for(i=7;i=4;i-) putchar(si); putchar(t);for(i=3;i=0;i-) putchar(si); putchar(t);return 0;(2)#in clude#in clude #in clude #defi ne N 3 void output(char*s); int main( void)int i;char *sN;prin tf(please in put your massage: n); for(i=0;iN;i+)si=malloc(80); gets(si);prin tf(your massage has bee n cha n

16、ged in to;n); for(i=0;iN;i+)output(si); prin tf(%sn,si); return 0;void output(char*s)int i,j;for(i=0,j=0;si!=0;i+,j+)if(isspace(si)for(;isspace(si);i+) sj+=; sj=si; else sj=si; sj=si;口Ld(3)oinowyo LIP what#in clude#defi ne MAX 5void swap(i nt v,i nt i,i nt j);void qsort(i nt v,i nt left,i nt right);

17、int main (i nt argc,char *argv)int i=0,sMAX;if(-argc0&(*+argv)0=_)if(*argv)1=d)while(i=0;i-)prin tf(%dt,si);elseprin tf(wr on g:n);elsewhile(ient s and Sett ings.ibaly DociULent e學(xué)樓作業(yè)Ic實鯊l指針實鯊4 please input youi* massage: whatQ execution time : 46.234 s cont inue.for(i=0;iright)return;swap(v,left,(l

18、eft+right)/2); last=left;for(i=left+1;i=right;i+) if(vivleft) swap(v,+last,i);swap(v,left,last); qsort(v,left,last-1); qsort(v,last+1,right);void swap(i nt v,i nt i,i nt j)int temp;temp=vi; vi=vj;vj=temp;(4)#i nclude #defi ne N 5#defi ne M 3struct coursechar *s;int dN; smM;void saverage(struct cours

19、e *sm);void caverage(struct course *sm);void summary_below(struct course *sm);void summary_both(struct course *sm);int main( void)int i;prin tf(Please in put the n ames of the %d courses.n,M);for(i=0;is=malloc(10)=NULL)prin tf(ERROR);for(i=0;is);for(k=0;kd+k);saverage(sm);caverage(sm);summary_below(

20、sm);summary_both(sm);return 0;void saverage(struct course *sm)double sum=0,av;int i,k;for(i=0;iN;i+)printf(This is the %d students average gradet,i+1); for(k=0;kd+i);av=sum/M;prin tf(%lfn,av);sum=0;void caverage(struct course *sm)double sum=0,av;int i,k;for(i=0;is);for(k=0;kd+k);av=sum/N;prin tf(%lf

21、n,av);sum=0;void summary_below(struct course *sm)double sum=0,av;int i,k;for(i=0;iM;i+)for(k=0;kd+k);av=sum/N;prin tf(I n %s the nu mber of below-average stude nt ist,(sm+i)-s); for(sum=0,k=0;kd+k)av)sum+;prin tf(%dn,(i nt)sum);sum=0;void summary_both(struct course *sm)int i,k;int fsum=0,esum=0;for(

22、i=0;iM;i+)prin tf(The course is %st,(*(sm+i).s);for(k=0;kd+k)d+k)90)esum+;prin tf(the failed is %d,while the above 90 is %dn,fsum,esum); fsum=0,esum=0;names of the 3 courses.Please input thegradesof Ej studentsofthe87 78 92 58 68Please input thegradesof Ej studentsofthe56 62 58 73 94Please input the

23、gradesof Ej studentsofthemath subjectphysics subjectc_languagePlease input the oath phys ic s c_languagesubject77 68 92 57 90ThisThisThisThisThisThisThisThisis is is is is is is isthe the the the the the the the1 studentJ s2 studentf s3 student1s4 studentf s5 student1saverage average average average

24、 averagegrade grade grade grade grade73.33333369.33333380.66666762.66666784.00000076.60000068.60000076.8000002average grade of mathaverage grade of physics average grade of c_language number of below-auerage student isIn math theIn physics the number of below-auerage student is In c_languageThe co l

25、ipse isThe course is The co Ltrse isthe number of math the phys ics the c_languageis above above3290 is 190 is 1below-auepage student failed is 1,while the failed is 2,while thethe failed is 1,while the above 90 is 1Process returned 0 execution time : 86.563 s Press any key to continue.s *C: Docuent s an

溫馨提示

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

評論

0/150

提交評論