2017-05-04 83 views
-4

計算機已被廣泛用於測試學生解決算術問題的能力。我如何使這段代碼更高效和更短?

我想寫一個程序,測試一個學生使用1到100之間的隨機整數加法,減法或乘法。學生首先選擇問題的類型,然後問10個問題有3次機會正確答案。如果三次機會後答案仍不正確,則顯示正確的答案。通過在第一次嘗試中獲得正確答案的10分,第二次嘗試中的5分,第三次嘗試中的2分以及如果所有三次嘗試都錯誤都得0分,計算得分。該程序。

這是我嘗試鞋底這個問題:

#include<iostream> 
#include<stdlib.h> 
#include<time.h> 

using namespace std; 

int num() 
{ 
    return rand()%100+1; 
} 

//Screen ---------------------------------------------------------------------------------------------------------- 
int screen() 
{ 
    int choice; 

    cout<<"Enter the number for the problem type desired.\n"; 
    cout<<" 1. Addition\n"; 
    cout<<" 2. Subtraction\n"; 
    cout<<" 3. Multiplication\n\n"; 
    cout<<"Enter choice: "; 
    cin>>choice; 

    return choice; 
} 

//Multiplications ----------------------------------------------------------------------------------------------------- 
void Multi(int &tries, int&score, int &true_answer) 
{ 
    int num1, num2, answer; 
    num1 = num(); 
    num2 = num(); 
    true_answer = num1+num2; 
    cout<<"\n"<<num1<<" * "<<num2<<" = "; 
    cin>>answer; 

    if(num1 * num2 != answer) 
    { 
    cout<<"Wrong, Try again\n"; 
    tries++; 
    } 
    else 
    { 
    cout<<"CORRECT\n"; 
    } 

    if(tries == 0 && num1*num2 == answer) 
    { 
    score+=10; 
    } 

    if(tries == 1 && num1*num2 == answer) 
    { 
    score += 5; 
    } 

    if(tries == 2 && num1*num2 == answer) 
    { 
    score += 2; 
    } 

    if(tries == 3) 
    { 
    score += 0; 
    } 
} 

// Subtraction ------------------------------------------------------------------------------------------------------- 
void Subtract(int &tries, int &score, int &true_answer) 
{ 
    int num1, num2, answer; 
    num1 = num(); 
    num2 = num(); 
    true_answer = num1 + num2; 
    cout<<"\n"<<num1<<" - "<<num2<<" = "; 
    cin>>answer; 

    if(num1 - num2 != answer) 
    { 
    cout<<"Wrong, Try again\n"; 
    tries++; 
    } 
    else 
    { 
    cout<<"CORRECT\n"; 
    } 

    if(tries == 0 && num1-num2 == answer) 
    { 
    score+=10; 
    } 

    if(tries == 1 && num1-num2 == answer) 
    { 
    score += 5; 
    } 

    if(tries == 2 && num1-num2 == answer) 
    { 
    score += 2; 
    } 

    if(tries == 3) 
    { 
    score += 0; 
    } 
} 

//Addition ------------------------------------------------------------------------------------------------------- 
void Add(int &tries, int &score, int &true_answer) 
{ 
    int num1, num2, answer; 
    num1 = num(); 
    num2 = num(); 
    true_answer = num1+num2; 
    cout<<"\n"<<num1<<" + "<<num2<<" = "; 
    cin>>answer; 

    if(num1+num2 != answer) 
    { 
    cout<<"Wrong, Try again\n"; 
    tries++; 
    } 
    else 
    { 
    cout<<"CORRECT\n"; 
    } 

    if(tries == 0 && num1+num2 == answer) 
    { 
    score+=10; 
    } 

    if(tries == 1 && num1+num2 == answer) 
    { 
    score += 5; 
    } 

    if(tries == 2 && num1+num2 == answer) 
    { 
    score += 2; 
    } 

    if(tries == 3) 
    { 
    score += 0; 
    } 
} 

int main() 
{ 
    int answer, choice, score, tries, true_answer; 
    score = 0; 
    tries = 0; 
    srand(time(NULL)); 
    choice = screen(); 

    while(tries!=3) 
    { 
    if(choice == 1) 
    { 
     for(int i = 1; i<=10; i++) 
     { 
     Add(tries, score, true_answer); 
     if(tries==3) 
     { 
      break; 
     } 
     } 
    } 

    if(choice == 2) 
    { 
     for(int i = 1; i<=10; i++) 
     { 
     Subtract(tries, score, true_answer); 
     if(tries==3) 
     { 
      break; 
     } 
     } 
    } 

    if(choice == 3) 
    { 
     for(int i = 1; i<=10; i++) 
     { 
     Multi(tries, score, true_answer); 
     if(tries==3) 
     { 
      break; 
     } 
     } 
    } 

    if(tries == 3) 
    { 
     cout<<"You have missed 3 times. The answer is "<<true_answer<<"."; 
     break; 
    } 
    } 

    cout<<"\n\n"<<"Total Score: "<<score<<"\n\n"; 
    return 0; 
} 
+0

步驟1:刪除無用的評論和過多的空行。另外,使縮進一致。 –

+0

你爲什麼會生成隨機數字?不應該是num1和num2的用戶輸入? – Shadi

回答

0

所有操作有一個區別是可以分開的功能相同的代碼,所以你不必寫爲每個單獨的代碼。

修正了一些不正確的邏輯,如新嘗試的新問題。

您可以使用此代碼作爲起點,把它比作你的,儘量做到更好:

#include<iostream> 
#include<stdlib.h> 
#include<time.h> 
using namespace std; 

int random_num() { return rand() % 100 + 1; } 

void screen() 
{ 
    cout << "\n Enter the number for the problem type desired.\n\n"; 
    cout << " 1. Addition\n"; 
    cout << " 2. Subtraction\n"; 
    cout << " 3. Multiplication\n"; 
    cout << " x. Exit.\n\n"; 

    cout << "\n Enter your choice: "; 
} 

int calc(int n1, int n2, int choice, char &op) 
{ 
    switch (choice) 
    { 
     case 1: op = '+' ; return n1 + n2; 
     case 2: op = '-' ; return n1 - n2; 
     case 3: op = '*' ; return n1 * n2; 
    } 

    cout << "\n Error: Incorrect choice"; 
    return 0; 
} 

int ded(int tries) 
{ 
    switch (tries) 
    { 
     case 0: return 0; 
     case 1: return 5; 
     case 2: return 8; 
    } 
    return 10; 
} 

int main() 
{ 
    int choice, score = 0, tscore = 0, tries = 0; 
    int true_answer, user_answer; 
    char op; 

    srand(time(NULL)); 
    screen(); 
    cin >> choice; 

    while(choice >= 1 && choice <= 3) 
    { 
     bool is_correct = false; 

     int num1 = random_num(); 
     int num2 = random_num(); 

     true_answer = calc(num1, num2, choice, op); 

     do 
     { 
      cout << "\n\n " << num1 << " " << op << " " << num2 << " = "; 
      cin >> user_answer; 

      if (user_answer == true_answer) 
      { 
       is_correct = true; 

       score += 10 - ded(tries); 
       tscore += score; 
       tries = 0; 
      } 
      else 
      { 
       cout << "\n Incorrect answer. \n\n"; 
       ++tries; 
      } 

      if (!(tries > 0 && tries < 3)) 
      { 
       cout << "\n Correct answer.\n\n"; 
       cout << "\n Score = " << score; 
       cout << "\n Total Score = " << tscore << "\n\n\n"; 
      } 
     }while (!is_correct && tries < 3); 

     tries = 0; 
     score = 0; 
     screen(); 
     cin >> choice; 
    } 

    cout << "Good bye! \n\n\n"; 

    return 0; 
}