当您从3个考试成绩中找出最低和最高分时,如何计算考试成绩的关系?

时间:2015-05-29 23:00:28

标签: c++

对于我的C ++作业,我必须从一组三个测试分数中找到最低,最高和平均的考试成绩。如果在C ++中找到最高和最低测试分数时,两个或所有三个具有相同的测试分数,我需要多少额外的代码?这是我到目前为止的代码,它运行正常,除非两个分数是相同的。

#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;
}

1 个答案:

答案 0 :(得分:0)

从第一眼看你的代码,我就可以马上告诉你,你做的工作太多了。您应该使用函数和函数调用来使代码更加模块化。这将减少代码膨胀,使您的代码更易于阅读/遵循,并使您的代码更容易维护,如果您希望将来添加/更改某些内容。

例如,您可以创建最小和最大功能来查找最高和最低测试分数。

例如......

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

实施最小和最大功能后,创建另一个功能,以相同的方式查找平均测试分数。

至于您的原始问题,如果您有两个或三个相同的测试分数,那么最简单的方法是使用逻辑运算符==来测试所有三个测试分数之间的相等性。您需要弄清楚的是,您希望在控制流(程序)中测试所有三个测试分数的相等性。

要了解有关函数的更多信息,请参阅此C++ online tutorial page。如果此答案解决了您的问题,请不要忘记单击左侧的复选标记。否则,如果您有更多问题,请随时给我发表评论,我会尽力帮助您。