本帖最後由 tonyh 於 2012-2-4 17:30 編輯
將課堂上寫的程式加以改良, 自由發揮.- #include<iostream>
- using namespace std;
- int main()
- {
- float a=0,b=0,c=0,d=0;
- int vote;
- int password,tryagain;
- start:
- cout<<"☆★☆歡迎使用秘密投票機---班長選舉☆★☆"<<endl;
- cout<<"共有三位候選人 1:派大星 2:泡芙阿姨 3:皮老闆"<<endl;
- cout<<"請輸入號碼以投下您神聖的一票: ";
- cin>>vote;
- if(vote==1)
- {
- a++;
- }else if(vote==2)
- {
- b++;
- }else if(vote==3)
- {
- c++;
- }else
- {
- d++;
- }
- system("cls");
- cout<<"已完成投票! 請等候管理人員安排..."<<endl; //許功蓋暫時無法顯示
- password:
- cout<<"管理人員請在此輸入密碼: ";
- cin>>password;
- if(password==123)
- {
- cout<<"1:繼續投票 2.公佈結果"<<endl;
- cout<<"請輸入號碼以決定下一步動作: ";
- cin>>tryagain;
- if(tryagain==1)
- {
- system("cls");
- goto start;
- }else
- {
- system("cls");
- goto result;
- }
-
- }else
- {
- cout<<"輸入錯誤! 請重新輸入!"<<endl;
- goto password;
- }
- result:
- cout<<"☆★☆投票結果☆★☆"<<endl;
- cout<<"1號派大星"<<a<<"票, 2號泡芙阿姨"<<b<<"票, 3號皮老闆"<<c
- <<"票, 廢票"<<d<<"張"<<endl;
- cout<<"三位候選人的得票率分別為..."<<endl;
- cout<<"1號派大星"<<a/(a+b+c+d)*100<<"%, 2號泡芙阿姨"<<b/(a+b+c+d)*100
- <<"%, 3號皮老闆"<<c/(a+b+c+d)*100<<"%"<<endl;
- if(a>b && a>c)
- {
- cout<<"恭喜派大星當選班長!"<<endl;
- }else if(b>a && b>c)
- {
- cout<<"恭喜泡芙阿姨當選班長!"<<endl;
- }else if(c>a && c>b)
- {
- cout<<"恭喜皮老闆當選班長!"<<endl;
- }else
- {
- cout<<"沒有候選人得到最高票數, 選舉無效!"<<endl;
- }
- system("pause");
- return 0;
- }
複製代碼 |