一、實驗內容
1、ex 2_28
(1) 用if...else判斷
1 #include2執行結果如下:using
namespace
std;
3int
main()420
return0;
21 }
(2)用switch語句
1 #include2結果同上。using
namespace
std;
3int
main()
417 cout<
menu: a(dd) d(elect) s(ort) q(uit), select one:";
18}19return0;
20 }
2、ex 2_29
(1)while迴圈
1 #include2 #include3結果如下:using
namespace
std;
4int
main()515
if(i==j)
1620 i++;
21if(k%5==0) cout<
23return0;
24 }
(2)do...while迴圈
1 #include2 #include3(3)for迴圈using
namespace
std;
4int
main()
5while(j<=i);
15if(i==j)
1620 i++;
21if(k%5==0) cout<
22 }while(i<=100
);23
return0;
24 }
1 #include2 #include33、ex 2_32(1)while迴圈using
namespace
std;
4int
main()516
if(k%5==0) cout<
18return0;
19 }
1 #include2 #include 3 #include 4(2)do...while迴圈using
namespace
std;
5int
main()
619 cout<
please write down the number you guess:";
20}21return0;
22 }
1 #include2 #include 3 #include 4效果如下:using
namespace
std;
5int
main()
620 cout<
please write down the number you guess:";
21 }while(m!=n);
22return0;
23 }
效果如下:
二、實驗反思
1、注意"a"與'a'的區別,"a"表示字串,'a'表示字元,比較的是ascii碼。
2、三種迴圈各具特點,用前應思考其作用,明顯看出29題用for迴圈簡潔。
3、生成隨機數時,注意標頭檔案cstdlib,ctime;設定域寬時,注意標頭檔案iomanip。
4、使用列舉型別,返回球顏色時,我使用乙個了函式將數字與顏色對應。
三、實驗小評
C 實驗一 簡單程式設計
內容提要 設計程式找出1 100間的質數,顯示出來。把有10個整數元素的陣列用氣泡排序法按由小到大公升序排列。二維陣列 m個同學n門課成績處理,字元陣列存放姓名。基本要求 用for或while或do while迴圈語句實現,能對資料進行排序的程式,能運用二維陣列能熟練對學生成績進行輸入,查詢,排序,...
實驗一 C 簡單程式設計
這次的實驗主要是四個問題,在解決問題的基礎上要求使用不同的迴圈結構和分支結構。在複習c語言的同時了解c 不同於c語言的地方。2 28 問題描述 實現乙個簡單的選單程式,執行時現實 menu a dd d elete s ort q uit select one 提示使用者輸入。a表示增加,d表示刪除...
實驗一 C 簡單程式設計
實驗一 2 28 實現乙個簡單的選單程式,執行時顯示 menu a dd d elete s ort q uit selete one 提示使用者輸入。a表示增加,d表示刪除,s表示排序,q表示退出。輸入為a d s時分別提示 資料已經增加 刪除 排序。輸入q時程式結束。1 if.else.語句,b...