# 羽球計算得失分率 ###### tags: `badminton` 使用軟體 : [Dev-C++ 5.11](https://sourceforge.net/projects/orwelldevcpp/) 也可以使用線上的網站跑 : [Online C++](https://www.onlinegdb.com/online_c++_compiler) google C++ online會有很多網站可以用,不一定要使用這邊提供的網站 於輸入參賽隊伍時輸入exit可離開程式,參賽隊伍若輸入非數字的字串,則重新輸入參賽隊伍 ## 程式碼解釋 該程式為簡單的計算得失分率,即 $$ 總得失分率 : 總得分/總失分 $$ 如果今天比賽是bot 3,那需要考慮直落2和打滿3場的狀況,是否適合使用該程式。 1. 參賽隊伍數 : 該循環有幾個隊伍,假設四循環,則輸入阿拉伯數字4後按下Enter 2. 請輸入每場比賽的局數 : 因為考慮到不是每個比賽都是一局定勝負,可能會有bot 3的狀況,因此這邊要輸入局數。例如4循環一局定勝負,就輸入3。 ## 程式碼 ```cpp= #include <iostream> #include <sstream> #include <string> using namespace std; struct team { string name; double score_get; double score_loss; double score_rate; }; bool is_number(const std::string& s){ std::string::const_iterator it = s.begin(); while (it != s.end() && std::isdigit(*it)) ++it; return !s.empty() && it == s.end(); } int main(){ while(true){ string user_input; cout << "請輸入參賽隊伍數 :\n"; cin >> user_input; if(is_number(user_input)){ int team_counts, game_points; stringstream geek(user_input); geek >> team_counts; cout << "\n請輸入每場比賽的局數\n"; cin >> game_points; team * teams; teams = new team [team_counts]; for(int i =0;i<team_counts;i++){ cout << "請輸入第 " << i+1 << " 隊的比賽結果,得失分請以空白隔開\n輸入格式請依照以下格式 :\n得分 失分。\n"; for(int j=0;j<game_points;j++){ int a,b; cin >> a >> b; teams[i].score_get += a; teams[i].score_loss += b; } teams[i].score_rate = teams[i].score_get / teams[i].score_loss; cout << "第 " << i+1 << " 隊的得失分 : " << teams[i].score_rate << "\n\n"; } cout << "\n\n"; delete [] teams; } else if(user_input == "exit") break; } system("pause"); return(0); } ```