-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;
}
步驟1:刪除無用的評論和過多的空行。另外,使縮進一致。 –
你爲什麼會生成隨機數字?不應該是num1和num2的用戶輸入? – Shadi