實驗結論:
1.驗證性實驗部分
①函數聲明和函數定義各自的作用及二者的區別:
如果編譯器在調用一函數時,發現在之前未定義便會報錯,而函數聲明就是告訴編譯器“在后面有這個函數”,編譯器便會向下繼續尋找。當然在調用之前就將函數定義好也是可以的,所以我認為函數定義兼有函數聲明的功能。
舉一個知乎上看到的比喻:
小明,你去把窗子開一下。(誰是小明?報錯了。)
小明是身份證號為XXXXX的同學。//定義
小明,你去把窗子開一下。 //調用
有一個小明 //聲明
小明,你去把窗子開一下。 //調用
小明是身份證號為XXXXX的同學。 //定義
鏈接:https://www.zhihu.com/question/54655761/answer/141337226
來源:知乎
②什么是形參?什么是實參?函數參數和返回值在函數中起到什么作用?

差不多就這樣吧……
③函數參數傳遞過程中,值傳遞和引用傳遞區別
值傳遞僅僅傳遞的是值,就如我上圖所示。引用傳遞,傳遞的是內存地址,修改后會改變內存地址對應儲存的值。
2.編程實驗部分
(1)2-28
#include <iostream> using namespace std; //if……else break contiue版本 int main() { char select = 0; while (true) //構建循環 { cout << "Menu: Add Delete Sort Quit,select one:"; cin >> select; if (select == 'A') { cout << "數據已增加" << endl; continue; } else if (select == 'D') { cout << "數據已刪除" << endl; continue; } else if (select == 'S') { cout << "數據已排序" << endl; continue; } else if (select == 'Q') break; else cout << "ERROR" << endl; //輸入其他數據返回error } return 0; }
#include <iostream> using namespace std; //switch版本 int main() { char select=0; while(true) //構建循環 { cout<<"Menu: Add Delete Sort Quit,select one:"; cin>>select; switch (select) { case 'A':cout<<"數據已增加"<<endl;continue; case 'D':cout<<"數據已刪除"<<endl;continue; case 'S':cout<<"數據已排序"<<endl;continue; case 'Q':break; } break; } return 0; }
(2)2-29
判斷質數算法:
從2到要判斷的數字的一半,進行取余數的操作,如果結果有為0,則不是質數。
#include <iostream> using namespace std; //for循環 int main() { int num,i,status; for (num = 2; num<=100; num++) { status = 1; //條件判斷 for (i = 2; i <= (num/2); i++) //從2到數字的一半進行窮舉 { if (num%i == 0) { status = 0; //如果不是素數,條件判斷為假,跳出循環 break; } } if (status) //當判斷為真時,即為素數時,輸出這個數 cout << num << endl; } return 0; }
#include <iostream> using namespace std; //while循環 int main() { int num, i, status; num = 2; while(num<=100) { status = 1; //條件判斷 i = 2; while ( i <= (num / 2)) //從2到數字的一半進行窮舉 { if (num%i == 0) { status = 0; //如果不是素數,條件判斷為假,跳出循環 break; } i++; } if (status) //當判斷為真時,即為素數時,輸出這個數 cout << num << endl; num++; } return 0; }
#include <iostream> using namespace std; //do……while循環 int main() { int num, i, status; num = 2; do { status = 1; //條件判斷 i = 2; do //從2到數字的一半進行窮舉 { if (num%i == 0&&num!=2) //因為do……while先執行語句,想出的笨方法 { status = 0; //如果不是素數,條件判斷為假,跳出循環 break; } i++; } while (i <= (num / 2)); if (status) //當判斷為真時,即為素數時,輸出這個數 cout << num << endl; num++; } while (num <= 100); return 0; }
(3)2-32
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; //while循環 int main() { int num, guess; srand((unsigned int)time(NULL)); //調用時間函數傳送不同的seed值 num = 1 + rand() % 100; //生成每次不同的隨機數 while (1) { cout << "Enter the number you guess:"; cin >> guess; if(num==guess) //對兩個數字進行判斷 { cout << "Congratulations!!" << endl; break; } if (num < guess) { cout << "Too large……" << endl; continue; } if (num > guess) { cout << "Too small……" << endl; continue; } } return 0; }
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; //do while循環 int main() { int num, guess; srand((unsigned int)time(NULL)); //調用時間函數傳送不同的seed值 num = 1 + rand() % 100; //生成每次不同的隨機數 do { cout << "Enter the number you guess:"; cin >> guess; if(num==guess) //對兩個數字進行判斷 { cout << "Congratulations!!" << endl; break; } if (num < guess) { cout << "Too large……" << endl; continue; } if (num > guess) { cout << "Too small……" << endl; continue; } } while (1); return 0; }
(4)2-34
思路:兩種
1.參照書本例3-9,利用遞歸的方法實現不同組合
2.利用for循環,檢驗每種情況,是否滿足顏色不同后輸出
#include <iostream> using namespace std; //思路1 int comm(int, int); int main() { int n, k; n=5;k=3 //5種顏色,摸3個 cout << comm(n, k) << endl; return 0; } int comm(int n, int k) { if (k > n) return 0; else if (k == 0 || n == k) //遞歸結束條件是n=k或k=0 return 1; //此時值為1 else return comm(n - 1, k) + comm(n - 1, k - 1); }
#include <iostream> using namespace std; //思路2 enum color{Red,Yellow,Blue,White,Black}; //定義枚舉 int main() { int i, j, k, n = 0; //n代表種數 for (i = Red; i<=Black; i++) for (j = i + 1; j<=Black; j++) for (k = j + 1; k <=Black; k++) { if (i != j && j != k && k != i) //3球顏色不相同 cout << i << " " << j << " " << k<<endl; n++; } cout << n << endl; return 0; }
實驗總結與體會:
在本次的實驗中,題目2-32中使用了時間函數作為rand()的seed值,省去了手動輸入,並提高了猜數游戲的可玩性。在實驗2-34中,在使用遞歸后,在網上又看見了使用for循環進行枚舉的方法,並自己嘗試了一下。
收獲不少,因為在自己寫完程序后,程序肯定不是最優的,需要與別人的代碼進行比較和參考,了解了別人的思路,才能寫出最好的。自己也任然有很多地方需要加強,比如在2-29中的do while語句中采用了比較笨的方法解決do while先執行后判斷的特點。