第二章C++實驗
阿新 • • 發佈:2019-03-16
實驗 not using ret bre true 輸入 total 時間
2-28
(1)用if else 語句
#include<iostream> using namespace std; int main(){ char alphabet; while (true) { cout << "Menu : A(dd) D(elete) S(ort) Q(uit) , Select one:" << endl; cin >> alphabet ; if(alphabet==‘A‘) { cout << "Data has added" << endl;} else if(alphabet==‘D‘) { cout << "Data has deleted" << endl; } else if(alphabet==‘S‘) { cout << "Data has sorted" << endl; } else if(alphabet==‘Q‘) break; else cout << "import error,please import another one" << endl; } return 0; }
這裏用while(true)多組輸入,在題目的基礎上還多加了一個輸入錯誤的提示。代碼運行結果如下:
(2)用switch
switch語句相對於if語句簡便了很多
#include<iostream> using namespace std; int main(){ char alphabet; while(true) { cout << "Menu: A(dd) D(elete) S(ort) Q(uit),Select one :"<<endl; cin>> alphabet; if(alphabet==‘Q‘) break; switch(alphabet){ case ‘A‘: cout << "Data has been added" << endl; break; case ‘D‘: cout << "Data has been deleted" << endl; break; case ‘S‘: cout << "Data has been sorted" << endl; break; default : cout << "import error,please import another one" <<endl; } } return 0; }
和第一問沒有多大區別,運行時間比上一個要快,運行結果如下:
2-29 用窮舉法找出1-100的質數
(1)用while
思路是i%j(j<i),如果i=j,就是質數
代碼如下:
#include<iostream> using namespace std; int main(){ int i=2; while(i<=100){ int j=2; while(j<i){ if(i%j==0) break; j++; } if(j==i) cout << i << endl; i++; } return 0; }
運行結果如下:
(2)用do while
由於排列的不夠美觀,所以這次把結果的排列優化了一遍,代碼如下:
#include<iostream> #include<iomanip> using namespace std; int main(){ int i=2; int a=0; while(i<=100){ int j=2; while(j<i){ if(i%j==0) break; j++; } if(j==i) { a++; cout << setw(3) << i; if(a%5==0) { cout << endl; } } i++; } return 0; }
運行結果如下:
(3)用for循環:
代碼如下:
#include<iostream> #include<iomanip> using namespace std; int main(){ int i; int a=0; for( i=2; i<=100; i++ ) { int j=2; for( ; j<i; j++) { if(i%j==0) break; } if(i==j) { a++; cout << setw(3) << i; if(a%5==0) cout << endl; } } return 0; }
運行結果如下:
從結果來看,3個結果for循環用的時間最少。
2-32 猜數字
(1)用while
#include<iostream> using namespace std; const int GUESSNUMBER=28; int main(){ cout << " Please guess a number from 1 to 100 :" << endl; int num; while(true){ cin >> num; if(num==GUESSNUMBER) { cout << "Congratulations!You are right!" <<endl; break; } if(num>GUESSNUMBER) { cout << "Bigger than num" << endl; } if(num<GUESSNUMBER) { cout << "smaller than num" << endl; } } return 0; }
第一次是定義了一個數字
運行結果如下:
(2)第二次我運用了rand函數,在上一題的基礎上優化了一下:
#include<iostream> #include<cstdlib> using namespace std; int main(){ cout << " Please guess a number from 1 to 100 :" << endl; int num; int GUESSNUMBER; GUESSNUMBER = 1+(rand()%100); while(true){ cin >> num; if(num==GUESSNUMBER) { cout << "Congratulations!You are right!" <<endl; break; } if(num>GUESSNUMBER) { cout << "Bigger than num" << endl; } if(num<GUESSNUMBER) { cout << "smaller than num" << endl; } } return 0; }
運行結果如下:
2-34
(1)不考慮順序。我是運用了for循環。如果前面比後面的大,就break,代碼如下:
#include<iostream> using namespace std; int main(){ int total=0; enum Colour { red=1 , yellow , blue , white , black }; int i,j,k; for( i=1; i<=3; i++) { for(j=2;j<=4;j++) { if(i>=j) continue; for(k=3;k<=5;k++) { if(j>=k) continue; total++; } } } cout << "total = " <<total << endl; return 0; }
運行結果如下:
(2)我考慮了順序,也是用for循環
代碼如下:
#include<iostream> #include<cstring> using namespace std; int main(){ enum Colour { red=0 , yellow , blue , white , black }; char *s[]={ "red", "yellow" , "blue" , "white" , "black"}; int i,j,k; int total=0; for( i=0; i<=4; i++) { for(j=0;j<=4;j++) { if(i==j) continue; for( k=0; k<=4; k++) { if(j==k||i==k) continue; total++; cout <<s[i]<<‘ ‘<<s[j]<<‘ ‘<<s[k]<<endl; } } } cout << total << endl; return 0; }
運行結果如下:
第二章C++實驗