C语言程序设计教材九斗验证,C语言实验报告参考答案(原)
《C語言實驗報告參考答案(原)》由會員分享,可在線閱讀,更多相關《C語言實驗報告參考答案(原)(29頁珍藏版)》請在人人文庫網上搜索。
1、C 語言實驗報告參考答案實驗一 熟悉 C 語言程序開發環境及數據描述四、程序清單1編寫程序實現在屏幕上顯示以下結果:The dress is longThe shoes are bigThe trousers are black答案:#includemain()printf(The dress is longn);printf(The shoes are bign);printf(The trousers are blackn); 2編寫程序: a=150,b=20,c=45, 編寫求 a/b、a/c(商)和 a%b a%c(余數)的程序。 (2)a=160,b=46,c=18,d=170, 。
2、編寫求 (a+b)/(b-c)*(c-d) 的程序。答案:(1)#includemain()int a,b,c,x,y;a=150;b=20;c=45;x=a/b;y=a/c;printf(a/b的商 =%dn,x);printf(a/c的商 =%dn,y);x=a%b;y=a%c;printf(a/b的余數 =%dn,x);printf(a/c的余數 =%dn,y);(2)#include main()int a,b,c,d;float x;a=160;b=46;c=18;d=170;x=(a+b)/(b-c)*(c-d);printf(a+b)/(b-c)*(c-d)=%fn,x);當 a。
3、b 時,將 b 賦給 c; 賦給 c。 (提示:用條件運算符 )答案:#include main()int a,b,c;a=0;b=-10;c= (ab) ? b:a;printf(c = %dn,c);五、調試和測試結果1. 編譯、連接無錯,運行后屏幕上顯示以下結果:The dress is longThe shoes are bigThe trousers are black2、(1) 編譯、連接無錯,運行后屏幕上顯示以下結果: a/b 的商 =7 a/c 的商 =3a/b的余數=10a/c的余數=15(2)編譯、連接無錯,運行后屏幕上顯示以下結果:(a+b)/(b-c)*(c-d)=-1。
4、064.00003. 編譯、連接無錯,運行后屏幕上顯示以下結果:c =-10實驗二順序結構程序設計四、程序清單1 鍵盤輸入與屏幕輸出練習問題1 D。問題 2 改 printf(%c,%c,%dn,a,b,c); 這條語句 改成:printf(%c %c %dn,a,b,c);問題 3 改 scanf(%c%c%d,&a,&b,&c);這條語句改為:scanf(%c , %c, %d,&a,&b,&c);問題 4 改 printf(%c,%c,%dn,a,b,c);這條語句改成:%c %dn,a,b,c);問題 5 把 scanf(%c%c%d,&a,&b,&c);和 printf(%c,%c,。
5、%dn,a,b,c);改成 scan f(%c%*c%c%*c%d,&a,& b, &c);prin tf(%c,%c,%dn,a,b,c);2(1)從鍵盤輸入兩個八進制數,計算兩數之和并分別用十進制和十六進制數形式輸出。#i nclude int mai n()int a,b,c;scan f(%d%d,&a,&b);c = a + b;prin tf(%dn,c);prin tf(%x n,c);return 0;2(2)編寫程序:從鍵盤輸入兩個實數a和x,按公式計算并輸出y的值:y a5 sin (ax)ln(a x) eax#in clude#in clude int mai n()t。
6、 *77is negative ”。c 并顯示。float a,x,y;scanf(%f%f,&a,&x);y = pow(a,5) + sin(a*x) + exp(a*x) + log(a+x);printf(y=%fn,y);return 0; 五、調試和測試結果2(1) 輸入: 12 14輸出: 261a2(2) 輸入: 1 0輸出: 2.000000實驗三 選擇結構程序設計 四、設計流程(算法描述)(請寫出上機內容 2(3) 題的算法描述 ) 主要是兩兩比較,然后得出最大的數 五、程序清單(1) 輸入一個整數, 若大于等于 0,輸出提示信息 “ is positive ”,否則輸出 。
7、#include #includemain()int a;scanf(%d,&a);if(a=0) printf(the number is positven);else printf(the number is negetiven);return 0;(2) 輸入兩個整數 a 和 b, 若 a=b 時,求其積 c 并顯示;若 amain()int a,b,c;scanf(%d%d,&a,&b);if(a=b)printf(c=%dn,a*b);elseprintf(c=%dn,a/b);return 0;(3) 輸入 a、 b、 c 三個整數,輸出最大數。#includemain()int 。
8、a,b,c,x;scanf(%d%d%d,&a,&b,&c);if(a=b)x=a;elsex=b;if (x#includeint main()int i,j,sum;sum = 0;for (i=1;i#includemain()int i,j,sum=0;for(i=2;i#in clude mai n()float x,s in x,i,t;printf(請輸入一個x值(弧度值):);sca nf(%f, &x);sin x=0; t=x;i=1;while(fabs(t)=1e-6) sin x=s in x+t;t=t*(-x*x/(2*i*(2*i+1);i+;prin tf(s。
9、 in (%.2f)=%.6fn,x,si nx);六、調試和測試結果1:結果:the sum is : 5050the square sum is: 3383502:結果:6 28 4963、輸入 0,輸出 sin(0.00)=0.000000 輸入 1.57,輸出 sin(1.57)=1.000000輸入 0.5,輸出 sin(0.50)=0.479426實驗五函數和編譯預處理四、設計流程(算法描述)( 請寫出上機內容 2 的算法描述 )求素數的方法就是:給定一個大于3的數x,從2到X的平方根遍歷,只要有數可以被 x 整除,就不是素數五、程序清單1編寫自定義函數 long power(in。
10、t m,int n) ,計算 mn 的值。利用此函數編程序實現:從鍵盤輸入兩個整數 m和n,計算出mn的值。#includelong power(int m,int n)/ 要返回的是 long 型int i;long s;/ 因為是要返回的數,所以這里也定義為 long 型s=1;for(i=1;i#includeint prime(int m)int i,k;k=sqrt(m);for(i=2;ik)return 1;return 0;main()int i,k;k=0;for(i=3;i#include int count(int x)int sum,i;sum =0;/ 記住因子的個數。
11、 for(i=1;is wav :14ai 于 J1.U9T9i31- MT石亍 :Jta* K91! J*V3i #?麻鼻 JU 4VJ k ,i 哼J.石9 乜亦為 4133 斗斗丄4mjiH3 *SKWif ) n工 Ul. 閒令耳 于39 3SS N;轡 -4019 4沖4 40143GT14461433 4(3V 4?3 E4i9 HE 曲11 聞卡ST* -tttktl3*11 L 務料1* -?a* 國iif丄上N 專 dL A 量町署1L aeffl? 茁&H3 3?31 SfiH J 2ft 9 7卅H首毎吝33 3? :TMPWTI號辛 苫応 937 av V119S3bM。
12、V 話7峠號ZN吁S咅 J IMdx -liMi3J 1H/ifa?afbV313313 3-I31*11334331asa v3B71-!.14 ft住H三咅3saiJ-9WV用 專0石丄TEN* * 專甘號P 十*14 ! i4B17*49 3T#34 33*7峠(弓且立或二”49 立mt 斗Kk4Mbl2.3、輸出結果為:實驗六數組四、設計流程(算法描述)(請寫出上機內容1的算法描述)設置兩個變量分別指示頭和尾。第一個和最后一個元素值互換,然后頭和尾變量 向里移動,最終到兩變量相遇為止。五、程序清單1.編寫程序:從鍵盤輸入一串整數保存到數組中,調用函數an tit on e()將數組反序。
13、輸出。自定義函數void antitone(int a,int n)實現將數組中的 n個數據按逆序存放。void an tit on e(i nt a,i nt n)int i,j;int k;i=0;j=n-1;while(i#includevoid Mad(int a,int n) int i;a0=2;a1=3; for(i=2;iai;i+);k1 = abs(x-ai-1);k2 = abs(x-ai); if(k1k2)printf(the most similar x number is:%dn,ai); elseprintf(the most similar x number 。
14、is:%dn,ai-1);return 0;3. 編程實現:輸入 10 個學生 5 門課的成績并完成如下功能(1) 求每個學生的平均分;(2) 求每門課程的平均分。#include #include #define num 10typedef struct studentchar name20;float math;float englis;float computer;float Chinese;float history;STUDENT;int main(void)STUDENT stunum;int i;float score,sum,average;char s10;float sco。
15、reMath,scoreEng,scoreCom,scoreChi,scoreHis;for(i=0;imax)max=ttij; ppj=max;五、調試和測試結果(寫出上機內容1中填空的內容)(1)( 1)sum=0 ( 2) ( 3) _J(2)( 1)J ( 2)_i ( 3) ap+i實驗八指針(1)四、程序清單(請寫出上機內容2中的函數)求出每個位上的數字,然后放在千位上的數字乘以1000,放在百位上的數字乘以100,放在10位上的數字乘以10,然后相加。void fun (i nt a,i nt b,lo ng *c) int a10,a1,b10,b1;a10=a/10;a1=。
16、a%10;b10=b/10;b1=b%10;*c = a10 * 1000 + b1 * 100 + a1 *10 + b10;五、調試和測試結果(請寫出上機內容1的輸出結果)1(1)輸出結果為:8,7,7,86(3)(1)x=10 y=20x=20 y=10【1】int *p【2】&ai【3】pi實驗九指針(2)設計流程(算法描述)(請寫出上機內容2中的算法描述)i=0當 *(x+i)!= 0return 1i=i+1return 0五、程序清單1 已知一個整型數組a5,其各元素值為4,6,8,10,12。使用指針編程求數組元素之積。#in elude int mai n(void)int 。
17、a=4,6,8,10,12,sum;int *p;sum=1;for(p=a;pave=0; for(i=0;iave+=a-si; a-ave/=N;五、調試和測試結果 (請寫出上機內容 1的填空結果 ) 上機內容 1 的填空結果(1) -sno (2) -name (3) &t實驗十一 共用體與枚舉 文件四、程序清單( 請寫出上機內容 2 中的程序源代碼 )#include #include #include int main(void)int i,sum;FILE *fd;char s10,*p,ch;if( (fd=fopen(D:shi.txt,wt)=NULL)printf(cre。
18、at the file failedn);exit(0);elsefor(i=1;i14);return c;STUDENT *init()return NULL;STUDENT *create()int i; int s;STUDENT *h=NULL,*info;for(;) info=(STUDENT *)malloc(sizeof(STUDENT);if(!info)printf(nout of memory);return NULL;inputs(enter no:(10 digitals .enter 0 to exit),info-no,11); if(info-no0=0) b。
19、reak; /*when the first number is 0,break*/ inputs(enter name:(name,15);printf(please input scores n);s=0; /*s is sum,begins with 0*/ for(i=0;iscorei); /* socre0 stores maths scores,socore1 program scores*/if(info-scorei100|info-scoreiscorei100|info-scoreiscorei;info-sum=s; info-order=0;info-next=h;h。
20、=info;return(h);inputs(char *prompt, char *s, int count)char p255;doprintf(prompt);scanf(%s,p); if(strlen(p)count)printf(n too long! n);while(strlen(p)count); strcpy(s,p); /*Print infor*/ void print(STUDENT *h)int i=0;STUDENT *p;clrscr();p=h;printf(nnn*STUDENT*n);printf(|rec| NO. | name | maths | pr。
21、ogram | sum |order|n); printf(|-| |n);while(p!=NULL)i+;%3dprintf(|%3d|%-10s|%-15s|%7d|%9d|%4.2f|n,i,p-no,p-name,p-score0,p-score1,p-sum,p-order);p=p-next;printf(H*end*n);STUDENT *delete(STUDENT *h)STUDENT *p,*q;char s11;clrscr();printf(please enter the number you want to delete n); scanf(%s,s);q=p=h。
22、;while(strcmp(p-no,s)&p!=NULL)q=p;p=p-next;if(p=NULL)printf(nlist no %s studentn,s);elseprintf(nnn*STUDENT*n);printf(| NO. | name | maths | program | sum |order|n);printf(|n);%3dprintf(|%-10s|%-15s|%7d|%9d|%4.2f|n,p-no,p-name,p-score0,p-score1,p-sum,p-order);printf(H*end*n);getch();if(p=h) h=p-next;。
23、else q-next=p-next;free(p);printf(n have deleted No %s studentn,s); return(h);STUDENT *searchno(STUDENT *h)STUDENT *p,*q; char s11;clrscr();printf(please enter the number you want to search n); scanf(%s,s);q=p=h; while(strcmp(p-no,s)&p!=NULL)q=p; p=p-next; if(p=NULL) printf(n %s No Found!n,s);elsepr。
24、intf(n %s Found!n,s);printf(nnn*STUDENT*n);printf(| NO. | name | maths | program | sum |order|n); printf(|n);%3dprintf(|%-10s|%-15s|%7d|%9d|%4.2f|n,p-no,p-name,p-score0,p-score1,p-sum,p-order);printf(H*end*n);getch(); return(h);void search(STUDENT *h) STUDENT *p; char s15; clrscr();printf(please ent。
25、er name for searchn); scanf(%s,s);p=h;while(strcmp(p-name,s)&p!=NULL) p=p-next;if(p=NULL)printf(n %s No Found!n,s); elseprintf(n %s Found!n,s);printf(nnn*STUDENT*n);printf(| NO. | name | maths | program | sum |order|n);%3dprintf(|n);printf(|%-10s|%-15s|%7d|%9d|%4.2f|n,p-no,p-name,p-score0,p-score1,p。
26、-sum,p-order);printf(H*end*n);STUDENT *insert(STUDENT *h) STUDENT *p,*q,*info;char s11;int s1,i;printf(please enter the No.which this record will be located before n); scanf(%s,s);printf(nplease new recordn);info=(STUDENT *)malloc(sizeof(STUDENT); if(!info)printf(nout of memory); return NULL;inputs(。
27、enter no:(10 digitals),info-no,11); inputs(enter name:(name,15); printf(please input scores n);s1=0;for(i=0;iscorei);if(info-scorei100|info-scoreiscorei100|info-scoreiscorei;info-sum=s1; info-order=0;info-next=NULL;p=h;q=h;while(strcmp(p-no,s)&p!=NULL)q=p;p=p-next;if(p=NULL) if(p=h) h=info;elseq-next=info;elseif(p=h)info-next=p; h=info;elseinfo-next=p;q-next=info;printf(n have inserted %s studentn,info-name);return(h);/* SAVE*/void save(STUDENT *h。
總結
以上是生活随笔為你收集整理的C语言程序设计教材九斗验证,C语言实验报告参考答案(原)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于django的视频点播网站开发-st
- 下一篇: 苹果笔记本电脑好用吗_苹果新品发布会消息