第一篇:實驗1-C語言實驗報告
C語言程序設計(A)
(2011-2012-1)
實驗報告1
教學班級: 機械094 學號: 01 姓名:譚亮恩 課程教師:
曹瑛
實驗輔導教師:
曹瑛
江西理工大學
P6-4:/*P6-4
表示第6頁 第四題*/
int main(){ float r,s;r=15.5;s=2*3.14*r;
printf(“r=%4.2f,s=%f”, r, s);return 0;}
結果是:r=15.5
s=97.34
P32-3-1: /*P32-3-1
表示第6頁 第四題 第一小題*/ #include
表示第6頁 第四題 第二小題*/ #include
結果是: *ABCDEF*
江西理工大學
第二篇:c語言實驗報告實驗4.doc
實驗4 邏輯結構程序設計
1、實驗目的、要求
(1)C語言表示邏輯量的方法。
(2)學會正確使用邏輯運算符和邏輯表達式。
(3)熟悉掌握if語句和switch語句。
(4)掌握簡單的算法及程序調試。
2、實驗內容:
(1)輸入程序并運行,觀察運行結果并分析。
#include
int main()
{int a=1,b=3,c=5,d=4,x;
if(a
If(c Else ????????????????????????2 If(a If(b Else x=3;????????????????????????4 Else x=6;????????????????????????2 Else x=7;????????????????????????1 printf(“x=%dn”,x); Return(0);} 實驗前分析:因已知:A,B,C,D分別為1,3,5,4,故可知只有A (2)、編寫程序: Y=X(X<1);Y=2X-1(1= 程序編寫如下: #include Int main() {int x,y; Scanf(“%d”,x); If(x<1)y=x; If(x>=1&&x,10)y=2*x-1; If(x>=10)y=3*x-11; Printf(“%d”,y); Return(0);} 進行檢驗,發現程序有錯誤,無法運行。 將程序改為: #include Int main() {int x,y; Scanf(“%d”,x); If(x<1)y=x; Else if(x>=1&&x<10)y=2*x-1; Else if(x>=10)y=3*x-11; Printf(“%d”,y); Return(0);} 實驗分析: 1、在if語句中,要注意IF和ELSE的配合使用,不能單獨使用IF。 2、此程序中還應注意if語句的嵌套和各層之間的邏輯關系。 3、再輸入該程序時,涉及到乘法運算的表示,應注意計算機語言與書面語言的區別,在C語言中,*表示乘法運算。 在程序中加入clrscr()運算符,清空遺留數據: #include Int main() {int x,y; Scanf(“%d”,x); Clrscr(); If(x<1)y=x; Else if(x>=1&&x<10)y=2*x-1; Else if(x>=10)y=3*x-11; Printf(“%d”,y); Return(0);} 運行該程序,得到預期結果,但輸入數據被清除;將clrscr()移動到scanf之前。再次運行,得到了預期的輸入和輸出數據,故可知,clrscr()函數的作用是清楚該語句之前的所有的輸出的數據和輸入的數據。以后用此語句時要注意輸入位置。 (3)補足程序并運行。 輸入兩個數,將較小者輸出,應用條件運算符。 #include Int main() {int a, b,min; Printf(“n please input two numbersn”); Scanf(“%d%d”,&a,&b); Min=min(a,b); Printf(“min=%d”,min); Return(0;)} 運行該程序,顯示程序錯誤,無法輸出結果。 將程序改為: #include Int main() {int a, b,min; Printf(“n please input two numbersn”); Scanf(“%d%d”,&a,&b); Min=(a>b?b:a); Printf(“min=%d”,min); 再次運行程序,屏幕上顯示“please input two numbers”,輸入兩個數字6和9,輸出數字為6,再換多組數字,結果與與其相同,可知實驗成功。 實驗分析:輸入比較兩數大小的函數時,不能想當然,如認為“min=min(a,b)”就是輸出較小數的函數,而應使用三目運算符(A?B:C)來表示。 (5)給出一個百分制成績,要求輸出成績等級A,B,C,D,E:90分以上為A,80——89分為 B,70——79分為C,60——69分為D,60分以下為E。 1、分別用IF和SWITCH語句來實現。 2、輸入成績為大于100分或小于0分時,顯示出錯成績,程序結束。 用IF函數表示: #include Int main() {int a; Printf(“n please input your scoren ”); Scanf(“%d”,&a); If(a>=90&&a<=100)printf(“A”); Else if(a>=80&&a<=89)printf(“B”); Else if(a>=70&&a<=79)printf(“C”); Else if(a>=60&&a<=69)printf(“D”); Else if(a>=0&&a<=59)printf(“E”); Else printf(“data error”); Return(0);} 檢查無錯誤,運行程序,無論輸入數值為多少,輸出結果均為“data error”。 將程序改為: #include Int main() {int a; Printf(“n please input your scoren ”); Scanf(“%d”,&a); {If(a>=90&&a<=100)printf(“A”); Else if(a>=80&&a<=89)printf(“B”); Else if(a>=70&&a<=79)printf(“C”); Else if(a>=60&&a<=69)printf(“D”); Else if(a>=0&&a<=59)printf(“E”); Else printf(“data error”);} Return(0);} 即在if函數外套一組大括號,使之成為一個語句。再次輸入數據,得到正確結果。實驗分析: 1、注意if和else的配套使用,不能遺漏。 2、邏輯運算符&&表示“并且”,除此之外,“!”表示“非”,“||”表示“或”,以后使用注 意區分。 3、使用switch語句: #include {int a,b; Printf(“n please input your scoren”); Scanf(“%d”,&a); If(a<=100&&a>=0) {b=a/10; Switch(b) {case 10 printf(“A”);break; Case 9 printf(“A”);break; Case 8 printf(“B”);break; Case 7 printf(“C”);break; Case 6 printf(“E”);break; Case 5 printf(“E”);break; Case 4 printf(“E”);break; Case 3 printf(“E”);break; Case 2 printf(“E”);break; Case 1 printf(“E”);break; Case 0 printf(“E”);break; Default printf(“data error”);}} Else printf(“data error); Return(0);} 運行程序,顯示程序有誤,經檢驗,錯誤為case語句后沒有加冒號,加上后,限制結果與預期相同。 實驗改進:該程序中,反復輸入case語句,效率低下,可將條件相同的情況進行合并,進行簡化,變成如下形式: #include Int main() {int a,b; Printf(“n please input your scoren”); Scanf(“%d”,&a); If(a<=100&&a>=0) {b=a/10; Switch(b) {case 10:Case 9 :printf(“A”);break; Case 8 :printf(“B”);break; Case 7 :printf(“C”);break; Case 6 :Case 5 :Case 4 :Case 3 :Case 2 :Case 1 :Case 0:printf(“E”);break;Default printf(“data error”);}} Else printf(“data error); Return(0);} 運行后,得到相同的結果,故簡化成功。 實驗分析:此實驗中,應注意break語句的使用,不能遺漏;case語句應用大括號括住,表示整體的使用;條件允許可以對程序進行適當的簡化,提高運算效率;區分IF和SWITCH函數的共性與區別,提高對兩者的認識。 實驗心得: 1、注意IF語句的使用規則,要與ELSE搭配使用,掌握了多層IF函數的使用方法。 2、掌握了關系運算符與邏輯運算符“與”“或”“非”的使用法方法,“&&”“||”“!”。 3、注意區分數學表達語句與計算機語句的區別,了解計算機語句的特點。 4、必要時可以利用輔助設計語句對程序進行改進,或對程序進行簡化,便于加深理解,方 便操作。 5、掌握了IF語句與SWITCH語句用法的特點,及其中的易錯點(ELSE的使用和BREAK的使用)。 一、實驗目的1、2、3、4、掌握關系表達式和邏輯表達式的使用。掌握選擇結構程序設計的一般方法。熟練使用if語句進行程序設計。 掌握使用switch語句實現多分支選擇結構。 二、實驗內容 有一分段函數如下: 編寫程序,輸入(x實數)的值,輸出以如下格式:x=??.??,y=??.??(即小數部分保留2位)程序代碼: #include “stdio.h” int main(){ float x,y;scanf(“%f”,&x);if(x<1) y=x*x;if(x>=1&&x<10) y=5*x-1; if(x>=10) y=2*x+4;printf(“x=%.2f,y=%.2fn”,x,y);return 0;} 2、從鍵盤輸入三個實數,輸出最大數和最小數。樣例輸入:1.23 3.45 5.67 樣例輸出:5.67 1.23 程序代碼: #include “stdio.h” int main(){ float a,b,c,max,min;scanf(“%f%f%f”,&a,&b,&c);if(a>b){max=a;min=b;} else {max=b;min=a;} if(a>c) if(b>c){min=c;} else {min=b;} else {max=c;} printf(“%.2f %.2fn”,max,min);return 0;} 3、讀入3個非零的double數后,判別這三個值是否可以表示一個三角形的三條邊。樣例輸入1:1.23 1.23 1.23 樣例輸出1:yes.樣例輸入2:5.23 3.45-12.34 樣例輸出2:no.程序代碼: #include “stdio.h” int main(){ double a,b,c;scanf(“%lf%lf%lf”,&a,&b,&c);if(a>0&&b>0&&c>0) if(a+b>c&&b+c>a&&a+c>b) printf(“yes.n”); else printf(“no.n”); else printf(“no.n”); return 0;} 4、讀入3個非零整數后,判別這三個值是否可以表示一個直角三角形的三條邊。樣例輸入1:3 4 5 樣例輸出1:yes.樣例輸入2:5 6 1 樣例輸出2:no.程序代碼: #include “stdio.h” int main(){ int a,b,c;scanf(“%d%d%d”,&a,&b,&c);if(a>0&&b>0&&c>0) if(a+b>c&&b+c>a&&a+c>b) if(a*a+b*b==c*c||b*b+c*c==a*a||a*a+c*c==b*b) printf(“yes.n”); else printf(“no.n”); else printf(“no.n”); else printf(“no.n”); return 0;} 5、編程設計一個簡單的計算器程序,要求根據用戶從鍵盤輸入的表達式: 操作數1 運算符op操作數2 計算表達式的值,指定的運算符為加(+)、減(-)、乘(*)、除(/)。 樣例輸入1:21.23+12.56 樣例輸出1:21.23+12.56=33.79 樣例輸入2:1*2 樣例輸出2:1.00*2.00=2.00 在做除法運算時,若操作數2為0,則輸出:除數為0 程序代碼: #include “stdio.h” int main(){ float x,y;char ch;scanf(“%f%c%f”,&x,&ch,&y);switch(ch){ case '+':printf(“%.2f+%.2f=%.2fn”,x,y,x+y);break;case '-':printf(“%.2f-%.2f=%.2fn”,x,y,x-y);break;case '*':printf(“%.2f*%.2f=%.2fn”,x,y,x*y);break; case '/':if(y==0) {printf(“除數為0n”);} else printf(“%.2f/%.2f=%.2fn”,x,y,x/y);break;} return 0;} 6、描述 某產品生產成本c=c1+m*c2,其中c1為固定成本,c2為單位產品可變成本,m為生產數量。當m<10000時,c1=20000元,c2=10元;當m≥10000時,c1=40000元,c2=5元; 編寫一個程序,其功能為:根據輸入的生產數量,輸出總生產成本及單位生產成本。輸入 生產數量 輸出 生產數量 總生產成本 單位可變成本 樣例輸入 6000 樣例輸出 6000 80000 10 程序代碼: #include “stdio.h” int main(){ int m,c1,c2,c;scanf(“%d”,&m);if(m<10000){c1=20000;c2=10;} else {c1=40000;c2=5;} c=c1+c2*m;printf(“%d %d %dn”,m,c,c2);return 0;} 7、描述 根據鍵盤輸入的一個字符所屬類別,判別它屬于:大寫字母(輸出:1)、小寫字母(輸出:2)、數字字符(輸出:3)、其它字符(輸出:4)。 輸入 一個字符 輸出 字符類別號 樣例輸入 A 樣例輸出 程序代碼 #include “stdio.h” int main(){ char ch;scanf(“%c”,&ch);if(65<=ch&&ch<=90) printf(“1n”); else if(97<=ch&&ch<=122) printf(“2n”);else if(47<=ch&&ch<=58) printf(“3n”);else printf(“4n”);return 0;} 8、描述 實現如下分段函數: 輸入 是一個實型數據。輸出 以如下格式輸出:x=0.250,y=5.250(即輸出一律保留3位小數)樣例輸入 0.250 樣例輸出 x=0.250,y=5.250 程序代碼 #include “stdio.h” int main(){ float x,y;scanf(“%f”,&x);if(x<3){y=x+5;} else if(x==3){y=2*x;} else if(x<10){y=6*x-4;} else {y=3*x-11;} printf(“x=%.3f,y=%.3fn”,x,y);return 0;} 三、實驗體會 通過選擇結構這一章的學習,我了解了關系表的事和邏輯表達式的使用以及選擇結構程序設計的一般方法。知道了if語句的使用和switch語句多分支選擇結構。經過這一章的學習,我對c語言程序設計有了更大的興趣,希望能帶給我以后更大的學習樂趣。 學號:__________ 姓名:__________ 班級:__________ 日期:__________ 指導教師:__________ 成績:__________ 實驗一 上機操作初步和簡單的C程序設計 一、實驗目的1、熟悉C語言運行環境Turbo C++3.02、會簡單的程序調試 3、熟悉C語言各種類型數據的輸入輸出函數的使用方法 4、掌握順序結構程序設計 二、實驗內容 1、上機運行本章3個例題,熟悉所用系統的上機方法與步驟。(習題1.7) 2、編寫一個C程序,輸入a、b、c 3個值,輸出其中最大者。(習題1.6) 3、設圓半徑r=1.5,圓柱高h=3,求圓周長、圓面積、圓球表面積、圓球體積、圓柱體積。用scanf輸入數據,輸出計算結果,輸出時要求有文字說明,取小數點后2位數字。注意:在Trubo C++ 3.0中不能輸入漢字,只能輸入英文或拼音。(習題4.8) 4、運行如下程序,寫出運行結果。第一┆范文網www.tmdps.cn整理該文章,版權歸原作者、原出處所有...#include void main() { int a=1,b=2; a=a+b;b=a-b;a=a-b; printf(“%d,%dn”,a,b); } 三、實驗步驟與過程 四、程序調試記錄 C語言程序設計(B) (2010-2011-2) 實驗報告 教學班級: 學號: 姓名: 課程教師: 實驗輔導教師: 江西理工大學 自由編輯的程序 一、實驗前的源程序: //任意整數的疊加 #include 實驗錯誤報告: [Error] D:Program FilesC-Free 4temp未命名10.cpp:7: parse error before `for' [Error] D:Program FilesC-Free 4temp未命名10.cpp:7: parse error before `)' 構建中止 未命名10: 2 個錯誤, 0 個警告 實驗后的源程序: //任意整數的疊加 #include int i,j,sum=0;printf(“please input a int numbern”);scanf(“%d”,&j);for(i=0;i<=j;i++)sum=sum+i;printf(“此數的疊加=%dn”,sum);} 二、實驗前的源程序: /*小寫字母轉大寫字母*/ #include 江西理工大學 } c2='s';c1=c1-32;c2=c2-32;printf(“%c,%cn”,c1,c); 實驗錯誤報告: [Error] D:Program FilesC-Free 4temp未命名11.cpp:9: `c' undeclared(first use this function)[Error] D:Program FilesC-Free 4temp未命名11.cpp:9:(Each undeclared identifier is reported only once [Error] D:Program FilesC-Free 4temp未命名11.cpp:9: for each function it appears in.)構建中止 未命名11: 3 個錯誤, 0 個警告 實驗后的源程序: /*小寫字母轉大寫字母*/ #include 三、實驗前的源程序: /*查看某一年是否為閏年*/ #include { if(year%100==0) { if(year%400==0) i=1; else 江西理工大學 i=0; } else i=1; } else i=0;if(i) printf(“%d 是閏年n”,year);else printf(“%d 不是閏年n”,year);} 實驗錯誤報告: [Error] D:Program FilesC-Free 4temp未命名14.cpp:15: parse error before `else' [Error] D:Program FilesC-Free 4temp未命名14.cpp:25: parse error at end of input 構建中止 未命名14: 2 個錯誤, 0 個警告 實驗后的源程序: /*查看某一年是否為閏年*/ #include { if(year%100==0) { if(year%400==0) i=1; else i=0; } else i=1; } else i=0;if(i) 江西理工大學 printf(“%d 是閏年n”,year);else printf(“%d 不是閏年n”,year);} 數據的輸入和輸出 四、程序改錯題 改錯前的源程序;#include #include 改錯前的源程序;#include 江西理工大學 #include long x=7654123;x*=10;printf(“x=%7d”,x);} 改錯前的源程序: #include #include 五、程序編寫題:已知char ch’b’;int i=3 ,j=5;float x=22.354,y=435.6789;根據下面的輸出結果編寫程序。ch =’b’,ASCII=98 i=3□□□□□□j=5 x=22.35□□□y=435.68 實驗前的源程序: #include 江西理工大學{ char ch='b';int i=3,j=5;float x=22.354,y=435.6789;printf(“ch='%c',ASCII=%dn”,ch,ch);printf(“i=%d j=%dn”,i,j);printf(“x=%.2f y=%.2fn”,x,y);} 實驗錯誤報告:無 實驗后的源程序: #include j=%dn”,i,j);printf(“x=%.2f y=%.2fn”,x,y);} 六、從鍵盤輸入一行字符,統計其中小寫字母、大寫字母和其它字符的個數: 實驗前的源程序: #include “stdio.h” void main(){ printf(“請任意輸入一串字符:n”); char ch,sum1=0,sum2=0,other=0; ch=getchar(); while(c!='n') { if(c>='A'&&c<='Z')sum1++; else if(c>='a'&&c<='z')sum2++; else other++; c=getchar(); } printf(“大寫字母的個數:%dn”,sum1);printf(“小寫字母的個數:%dn”,sum2); 江西理工大學printf(“其他字符母個數:%dn”,other);} 實驗錯誤報告: [Error] D:Program FilesC-Free 4temp未命名7.cpp:7: `c' undeclared(first use this function)[Error] D:Program FilesC-Free 4temp未命名7.cpp:7:(Each undeclared identifier is reported only once [Error] D:Program FilesC-Free 4temp未命名7.cpp:7: for each function it appears in.)構建中止 未命名7: 3 個錯誤, 0 個警告 實驗后的源程序: #include “stdio.h” void main(){ printf(“請任意輸入一串字符:n”); char ch,sum1=0,sum2=0,other=0; ch=getchar(); while(ch!='n') { if(ch>='A'&&ch<='Z')sum1++; else if(ch>='a'&&ch<='z')sum2++; else other++; ch=getchar(); } printf(“大寫字母的個數:%dn”,sum1);printf(“小寫字母的個數:%dn”,sum2);printf(“其他字符母個數:%dn”,other);} 七、使用以下公式求∏的近似值,要求精確到最后一項的絕對值小于10e-4 ∏/4=1-1/3+1/5-1/7+…… 實驗前的源程序: #include “stdio.h” #include “math.h” main(){ 江西理工大學 } float sum=0;int i,j;for(i=1;;i++){ j=2*i-1;if(1.0/j>0.0001){ sum+=pow(-1,i+1)*(1.o/j);continue;break;} printf(“∏=%fn”,sum*4.0); 實驗錯誤報告: [Error] D:Program FilesC-Free 4temp未命名9.cpp:13: nondigits in number and not hexadecimal [Error] D:Program FilesC-Free 4temp未命名9.cpp:19: parse error at end of input 構建中止 未命名9: 2 個錯誤, 0 個警告 實驗后的源程序: #include “stdio.h” #include “math.h” main(){ float sum=0;int i,j;for(i=1;;i++){ j=2*i-1;if(1.0/j>0.0001){ sum+=pow(-1,i+1)*(1.0/j);continue;} break;} printf(“∏=%fn”,sum*4.0);} 八、用選擇法對10個整數排序: 實驗前的源程序: 江西理工大學#include scanf(“%d”,a[i]);} printf(“n”);for(i=0;i<10;i++)for(j=0;j<10-j;j++){ if(a[j]>a[j+1]) { k=a[j]; a[j]=a[j+1]; k=a[j+1];} printf(“這10個整數從小到大排列為:”);for(j=0;j<10;j++){ printf(“%d ”,a[j]);} printf(“n”);printf(“這10個整數從大到小排列為:”);for(j=9;j>=0;j--){ printf(“%d ”,a[j]);} 實驗錯誤報告: [Error] D:Program FilesC-Free 4temp未命名1.cpp:33: parse error at end of input 構建中止 未命名1: 1 個錯誤, 0 個警告 實驗后的源程序: //用選擇法對10個整數排序 #include 江西理工大學 int i,j,k;for(i=0;i<10;i++){ scanf(“%d”,a[i]);} printf(“n”);for(i=0;i<10;i++)for(j=0;j<10-j;j++){ if(a[j]>a[j+1]){ k=a[j]; a[j]=a[j+1]; k=a[j+1];} } printf(“這10個整數從小到大排列為:”);for(j=0;j<10;j++){ printf(“%d ”,a[j]);} printf(“n”);printf(“這10個整數從大到小排列為:”);for(j=9;j>=0;j--){ printf(“%d ”,a[j]);} } 九、求一個3*3的整數矩陣對角線元素之積: 實驗前的源程序: #include scanf(“%d”,&a[i][j])} for(i=0;i<3;i++) 江西理工大學 { for(j=0;j<3;j++) { printf(“%d ”,a[i][j]); } printf(“n”); } printf(“n”); for(i=0;i<3;i++) { for(j=0;j<3;j++) if(i=j) ji*=a[i][j]; printf(“主對角線的積為:%dn”,ji); } } 實驗錯誤報告: [Error] D:Program FilesC-Free 4temp未命名4.cpp:11: parse error before `}' 構建中止 未命名4: 1 個錯誤, 0 個警告 實驗后的源程序: #include scanf(“%d”,&a[i][j]);} for(i=0;i<3;i++){ for(j=0;j<3;j++) { printf(“%d ”,a[i][j]); } printf(“n”); } 江西理工大學 printf(“n”); for(i=0;i<3;i++) { for(j=0;j<3;j++) if(i=j) ji*=a[i][j]; printf(“主對角線的積為:%dn”,ji); } } 十、將一個數組中的值按你需從新存放。例如,原來順序為8,6,5,4,1。要求改為1,4,5,6,8。 實驗前的源程序: #include scanf(“%d”,&a[i]);} for(i=0;i t=a[i]; a[i]=a[j-i-1]; t=a[j-i-1];} printf(“該數組逆序排列為:”); for(i=0;i printf(“%d ”,a[i]);printf(“n”);} 實驗錯誤報告: [Error] D:Program FilesC-Free 4temp未命名3.cpp:25: parse error at end of input 構建中止 未命名3: 1 個錯誤, 0 個警告 江西理工大學實驗后的源程序: #include scanf(“%d”,&a[i]);} for(i=0;i t=a[i]; a[i]=a[j-i-1]; a[j-i-1]=t;} printf(“該數組逆序排列為:”); for(i=0;i printf(“%d ”,a[i]);} printf(“n”);} 江西理工大學第三篇:c語言實驗二實驗報告
第四篇:c語言實驗報告
第五篇:C語言 實驗報告