2015-05-29 60 views
-8

對於我的C++任務,我必須從一組三個測試分數中找到最低,最高和平均測試分數。如果在C++中找到最高和最低的測試分數,兩個或全部三個測試分數是否相同?這裏是我迄今爲止運行的代碼,除非兩個分數相同。如何從3個考試分數中找出最低分和最高分時考慮考試分數中的關係?

#include <iostream> 
using namespace std; 
int main() 
{ 
int _midtermgrade, _projectaverage, _homeworkaverage, average; 
cout << " Please enter your midterm grade:" << endl; 
cin >> _midtermgrade; 
cout << "Please enter your project average:" << endl; 
cin >> _projectaverage; 
cout << " Please enter your homework average:" << endl; 
cin >> _homeworkaverage; 
if((_midtermgrade >_projectaverage)&&(_midtermgrade >_homeworkaverage)){ 
    cout << "Your highest score is your midterm " << _midtermgrade endl; 
} 
if ((_projectaverage >_midtermgrade)&(_projectaverage>_homeworkaverage)) 
cout << "Your highest score is your project " << _projectaverage << endl; 
} 
if((_homeworkaverage >_projectaverage&(_homeworkaverage>_midtermgrade)){ 
cout << "Your highest score is your homework "<< _homeworkaverage <<endl; 
} 
if((_midtermgrade < _projectaverage)&&(_midtermgrade<_homeworkaverage)){ 
    cout << "Your lowest score is your midterm " << _midtermgrade<< endl; 
} 
if((_projectaverage<_midtermgrade)&&(_projectaverage<_homeworkaverage)){ 
    cout << "Your lowest score is your project "<< _projectaverage<<endl; 
} 
if ((_homeworkaverage<_projectaverage)&&_midtermgrade)){ 
    cout 

<< "Your lowest score is your homework " << _homeworkaverage << endl; 
} 

average =((_midtermgrade * 20 +_projectaverage* 40 + _homeworkaverage*15) 
    /(20 + 40 + 15)); 
cout << "Your currentaverage is " << average <<endl; 

if (_midtermgrade < 70){ 
    cout << "You have a failing grade on the midterm." << endl; 
} 
else 
     cout << "You have a passing grade on the midterm." << endl; 

if (_projectaverage < 70){ 
    cout << "You have a failing grade on the project." << endl;} 
    else 
     cout << "You have a passing grade on the project." << endl; 

if (_homeworkaverage < 70){ 
    cout << "You have a failing grade on the homework." << endl; 
} else 
     cout << "You have a passing grade on the homework." << endl; 

return 0; 
} 

回答

0

乍一看你的代碼,我可以立刻告訴你你做了太多的工作。你應該使用函數和函數調用來使你的代碼更加模塊化。這將減少代碼膨脹,使您的代碼更易於閱讀/遵循,並使您的代碼更容易維護,如果您希望在將來添加/更改某些內容。

例如,您可以創建最小和最大函數以查找最高和最低測試分數。

例如...

int MAX(int test_score1, int test_score2, int test_score3) 
{ 
    if(conditional statement) 
     return something; 
} 

後您實現您的最小值和最大值的功能,創建另一個功能找到同樣的方式平均測試成績。

至於你的原始問題,如果你有兩個或三個相同的測試分數,那麼最簡單的方法是使用邏輯運算符==來測試所有三個測試分數之間的相等性。所有你必須弄清楚的是你的控制流程(程序)中你想測試所有三個測試分數是否相等。

要了解有關功能的更多信息,請參閱此C++ online tutorial page。如果此答案解決了您的問題,請不要忘記單擊左側的複選標記。否則,如果您有更多問題,請隨時留下評論,我會盡力幫助您。

+0

你可以看看我剛剛發佈的問題謝謝。 – user4954919