如何在C ++中对多个变量使用测试相同的条件

时间:2018-05-03 02:48:47

标签: c++11

#include <iostream>
int main()
{
    using namespace std;
    int P, C, M, marks;
    cout << "Grading System\n\n";
    cout << "Enter Marks in Phy\n";
    cin >> P;
    cout << "Enter Marks in Chem\n";
    cin >> C;
    cout << "Enter Marks in Maths\n";
    cin >> M;
    cout << "Grades\n\nPhysics Chemistry Maths\n";
    if (marks < 50)
        cout << "Fail";
    else if (marks <= 60)
        cout << "C";
    else if (marks <= 70)
        cout << "B";
    else if (marks <= 80)
        cout << "B+";
    else if (marks <= 90)
        cout << "A";
    else if (marks <= 95)
        cout << "A+";
    else
        cout << "Error";
    return 0;
}

有没有办法在上面运行if语句的多个标记值,如 marks = P. 然后测试mark = C,marks = M并分别给出输出,

2 个答案:

答案 0 :(得分:1)

制作一系列标记和功能以检查各自的等级。

#include <iostream>

using namespace std;

void checkGrade(int marks) //function to check grade
{
    if (marks < 50)
        cout << "Fail";
    else if (marks <= 60)
        cout << "C";
    else if (marks <= 70)
        cout << "B";
    else if (marks <= 80){
        cout << "B+";
    }
    else if (marks <= 90){
        cout << "A";
    }
    else if (marks <= 95){
        cout << "A+";
    }
    else{
        cout << "Error";
    }
}

int main()
{
    cout << "Enter no. of students\n\n";

    int students;
    cin >> students;// Take input of no. of students

    int P[students], C[students], M[students]; //Declare array of marks, with each index depicting each student. Size of the array would be the number of students you entered

    for( int i = 0; i < students; i++ )
    {
        cout << "Grading System\n\n";
        cout << "Enter Marks in Phy\n";
        cin >> P[i];
        cout << "Enter Marks in Chem\n";
        cin >> C[i];
        cout << "Enter Marks in Maths\n";
        cin >> M[i];
        cout << "Grades\n\nPhysics Chemistry Maths\n";

        int count = 3;
        while(count) //To check grades in different subjects, the loop runs 'count' number of times ie. for each subject.
        {
            if(count == 3){
                cout<<"\n Grade in Physics ";
                checkGrade(P[i]); //Function called to check grade
            }
            else if(count == 2){
                cout<<"\n Grade in Chemistry ";
                checkGrade(C[i]); //Function called to check grade
            }
            else if(count == 1){
                cout<<"\n Grade in Maths ";
                checkGrade(M[i]); //Function called to check grade
            }
            count--;
        }

    }
    return 0;
}

上述程序存储各个标记数组中所有学生的数据。 checkGrade函数将标记作为输入并逐个显示等级。我建议你谷歌学习概念并解决一些问题。

答案 1 :(得分:1)

您可以通过在单独的函数中实现测试标记逻辑来使程序更加模块化,并将其命名为checkGrade,并将您作为输入的值作为参数传递。 例如:

#include <iostream>
using namespace std; //I moved this command outside the main function so it applies globally
void checkGrade(int);
int main()
{
    int P, C, M;
    cout << "Grading System\n\n";
    cout << "Enter Marks in Phy\n";
    cin >> P;
    cout << "Enter Marks in Chem\n";
    cin >> C;
    cout << "Enter Marks in Maths\n";
    cin >> M;
    cout << "Grades\n\nPhysics Chemistry Maths\n";
    checkGrade(P);
    checkGrade(C);
    checkGrade(M);

    return 0;
}

void checkGrade(int marks){
    if (marks < 50)
    cout << "Fail";
    else if (marks <= 60)
    cout << "C";
    else if (marks <= 70)
    cout << "B";
    else if (marks <= 80)
    cout << "B+";
    else if (marks <= 90)
    cout << "A";
    else if (marks <= 95)
    cout << "A+";
    else{cout << "Error";}
}

或者你可以使用像MadaZZ这样的数组来消除为每个用户输入单独调用checkGrade()的需要。