實驗內容:
2-28
實現一個簡單的菜單程序,運行時顯示“Menu:A(dd) D(elete) S(ort) Q(uit),Selete one:”提示用戶輸入。A表示增加,D表示刪除,
S表示排序,Q表示退出。輸入為A、D、S時分別提示“數據已經增加、刪除、排序。”,輸入Q時程序結束。
以下分別是我用兩種語句寫出的簡單菜單程序:
- if...else語句
-
#include<iostream> #include<cstdlib> using namespace std; int main() { char option; while(1) { cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Selet one:"; cin>>option; if(option=='A') { cout<<"data has added"<<endl; continue; } else if(option=='D') { cout<<"data has deleted"<<endl; continue; } else if(option=='S') { cout<<"data has sorted"<<endl; continue; } else if(option=='Q') break; else { cout<<"There is a mistake in the input,please re-enter."<<endl; continue; } } }
- switch語句
-
#include<iostream> #include<cstdlib> using namespace std; int main() { char option; while(1) { cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Selet one:"; cin>>option; switch(option) { case'A':cout<<"data has added "<<endl;break; case'D':cout<<"data has deleted"<<endl;break; case'S':cout<<"data has sorted"<<endl;break; case'Q':exit(0);break; default:cout<<"There is a mistake in the input,please re-enter."<<endl; } } return 0; }
2-29
用窮舉法找出1~100間的質數並顯示出來。
這個程序我用了while和for循環兩種實現方法。算法設計:用一個數除以2到sqrt這個數,其中只要有一個能被整除,那這個數就不是素數,反之是素數。具體代碼如下:
- while循環
-
#include<iostream> #include<math.h> using namespace std; int main() { int i,j,k,sign; i=2; while(i<=100) { sign=1; k=sqrt(i); j=2; while(j<=k) { if(i%j==0) { sign=0; break; } j++; } if(sign) cout<<i<<endl; i++; } }
- for循環
-
#include<iostream> #include<math.h> using namespace std; int main() { int i,j,k,a; for(i=2;i<=100;i++) { a=1; k=sqrt(i); for(j=2;j<=k;j++) { if(i%j==0) { a=0; break; } } if(a) cout<<i<<endl; } }
運行結果如下:
發現這些素數的排列不美觀,於是我用setw()函數美化了一下這些數的排列方式,代碼及運行結果如下:
#include<iostream> #include<math.h> #include<iomanip> using namespace std; int main() { int i,j,k,sign,m=0; for(i=2;i<=100;i++) { sign=1; k=sqrt(i); for(j=2;j<=k;j++) { if(i%j==0) { sign=0; break; } } if(sign) { for(i=2;i<=100;i++) { m++; if(m%6!=0) cout<<setw(5)<<i; if(m%6==0) cout<<setw(5)<<i<<endl; } } } }
、
這個實驗題我只用了兩種循環語句,do...while和while語句相似,就不拿來占篇幅了。關於這些循環語句不做過多陳述。
2-32
在程序中定義一個整型變量,賦以1~100的值,要求用戶猜這個數,比較兩個數的大小,把結果提示給用戶,直到猜對為止。
這個題目我才開始做的時候是自己在程序里定義了用戶要猜的數,這樣程序的重復利用率就比較低,先放源碼:
-
# include<iostream> using namespace std; int main() { int guessnumber=26; int m=0; while(m!=guessnumber) { cout<<"please guess the number(1~100):"; cin>>m; if(m>guessnumber) cout<<"You guessed a big number"<<endl; else if(m<guessnumber) cout<<"You guessed a small number"<<endl; else cout<<"You has guessed the number!"<<endl; } }
考慮到程序的實用價值,進行了一次小升級:
-
# include<iostream> using namespace std; int main() { int guessnumber=26; int m=0; while(m!=guessnumber) { cout<<"please guess the number(1~100):"; cin>>m; if(m>guessnumber) { int i; i=m-guessnumber; if(i>=10) cout<<"Your guessnumber is too big"<<endl; else cout<<"Your guessnumber is very close,but it's still a little big"<<endl; } else if(m<guessnumber) { int j; j=guessnumber-m; if(j>=10) cout<<"Your guessnumber is too small"<<endl; else cout<<"Your guessnumber is very close,but it's still a little small"<<endl; } else cout<<"You has guessed the number!"<<endl; } }
兩個程序運行結果如下:
改變不大,但是從甲方考慮,顯然2更適合。
在第三次升級的時候,考慮到程序的實用性,我加入了隨機數,並允許在用戶猜對數字后重開(清屏),給用戶更多選擇,下面是升級的代碼及運行結果:
#include <cstdlib> #include <iostream> #include <ctime> using namespace std; int main() { srand(time(0)); int m= rand() % 100 + 1;//將隨機數控制在1-100之間 int guessnumber;//用戶輸入的數字 char choice;//作出選擇,是否繼續 cout<< "Hello,master.My name is Xiao U."<<endl <<"I had a problems,can you help me?The method is simple."<<endl <<"Here's a bunch of numbers(0~100),but there's only one right,you should find the right number."<<endl <<"Starting right now"<<endl; do{ cout<< "Please enter a number: "; cin>> guessnumber; if( guessnumber >m) { cout<<"You guessed too high!,it's a pity"; } if( guessnumber <m) { cout<<"You guessed too low!,it's a pity!"; } if( guessnumber ==m) { cout<< "You get it!You have helped me solve the problem. HaHa,The number is: " <<m<<endl; cout<< "Do you want to play again?(Y/N):"; cin>>choice; if(choice== 'Y') { system("cls");//清空屏幕 cout<<"welcome back,master!"<<endl; continue; } else if(choice== 'N') { cout<< "bye,master."; break; } else { cout<< "You entered a wrong character! program will exit!"; break; } } }while(1); system("pause"); return EXIT_SUCCESS;//結束程序 }
2-34
口袋中有紅黃藍白黑5種顏色的球若干個。沒詞蔥口袋中取出3個顏色不同的球,問有多少種取法。
代碼如下:
#include<iostream> using namespace std; int fac(int x) { int i,j; j=1; for(i=1;i<=x;i++) j*=i; return j; } int main() { int al; int a,b,c,d,e; cout<<"Please enter the number of each colour ball" <<endl; cout<<"red,yellow,blue,white,black:"; while(cin>> a>> b>> c>> d>> e) { al=fac(5)/fac(3)/fac(2)*a*b*c*d*e; cout<<"There are"<<al<<"methods to choose three kinds of the balls"; system("pause"); } return 0; }
運行結果:
這一題我還寫了一個拓展版,將5個球拓展到自定義的情況,但是不符合題目要求,就不放代碼了~
實驗反思:
- 2-28這是一個很簡單的c++基礎編程題,但是關於這一題還是有一些話說,作為一個偽小白,平時會閱讀一些別人寫的程序,關於if語句,個人很反對把復雜的難以理解的代碼語句放到if的條件里面,這會嚴重影響到代碼閱讀,盡管很多時候在語法和邏輯上沒有問題。if...else語句還要注意的一個問題就是如何用break和continue控制程序流程,這里不多說。關於switch語句,它的目的,學過c/c++的人應該都知道,就是為了實現深層嵌套的if...else邏輯,switch語句我覺得比較重要的就是定義變量方面的問題,switch結構只能在最后一個標號(這個標號可以是case或default)中定義變量。除此以外,沒有特別需要說明的地方。
- 2-32關於最后升級版的程序,我查了關於srand()函數和清屏函數的相關內容,這個升級給用戶提供了選擇繼續還是再開的選擇,而且在程序實用性上也得到了加強,相比於前兩個,顯然這個價值更高。但是程序現在仍然存在一個bug,就是清屏后產生的隨機數沒有改變,還是和第一次一樣,這樣就達不到對這串代碼的預期。由於這是課堂實驗作業,時間有限,暫時先提交,在日后我會抓緊修改,如果有同學有好的想法,歡迎交流。還有就是,我想在這個程序內加一個計數器,記錄重開的次數,有興趣歡迎交流~