需要帮助计算a,b,c,s,d和f的实例

时间:2014-03-14 20:39:26

标签: c++

我必须在得分阵列中计算A,B,C,D和F的实例,但我对如何做到这一点感到很遗憾。 score1,score2和score3数组包含整数,因此我认为我需要使用if语句来比较分数,但我不确定如何计算并显示它。我已经阅读了我的整本书并搜索了这个网站和其他人,但找不到任何可以帮助我的东西。这是我的代码:

#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;

int getAverage(int,int,int);
string letterGrade(int);
void getInfo();

int main(int argc, char *argv[]) {
   getInfo();
   system("PAUSE");
    return EXIT_SUCCESS;
}

void getInfo() {
   const int info = 5;
   string last[info];
   string first[info];
   int ID[info];
   int score1[info];
   int score2[info];
   int score3[info];
   for (int count = 0; count < info; count++) {
      cout << "last name\n";
      cin >> last[count];
      cout << "first name\n";
      cin >> first[count];
      cout << "enter ID\n";
      cin >> ID[count];
      cout << "enter test 1\n";
      cin >> score1[count];
      cout << "enter test 2\n";
      cin >> score2[count];
      cout << "enter test 3\n";
      cin >> score3[count];   
   }
   cout << endl;
   for (int count = 0; count < info; count++) {
      cout << last[count] 
        << setw(10) << first[count] 
        << setw(10) << ID[count] 
        << setw(10) <<score1[count]
        << setw(10) << score2[count] 
        << setw(10) << score3[count] 
        << setw(10) << getAverage(score1[count],score2[count],score3[count])
        << setw(10) << letterGrade(getAverage(score1[count],score2[count],score3[count]));
      cout << endl;
   }   
}
string letterGrade(int average) {
   string lGrade;
   if (average > 89)
      lGrade = "A";
   else if (average > 79 && average < 90)
      lGrade = "B";
    else if (average > 69 && average < 80)
      lGrade = "C";
    else if (average > 59 && average < 70)
      lGrade = "D";
    else if (average >= 0 && average < 60)
      lGrade = "F";
    return lGrade;
}

int getAverage(int score1,int score2,int score3) {
   int average;
   average = ((score1 + score2 + score3)/ 3);
   return average;
}

3 个答案:

答案 0 :(得分:1)

int gradeCounts[5]; 

// ..

    if (average > 89) {
        lGrade = "A";
        gradeCounts[0] = gradeCounts[0] + 1;
    } else if (average > 79 && average < 90){
        lGrade = "B";
        gradeCounts[1] = gradeCounts[1] + 1;
    } else if (average > 69 && average < 80){
        lGrade = "C";
        gradeCounts[2] = gradeCounts[2] + 1;
    } else if (average > 59 && average < 70){
        lGrade = "D";
        gradeCounts[3] = gradeCounts[3] + 1;
    } else if (average >= 0 && average < 60){
        lGrade = "F";
        gradeCounts[4] = gradeCounts[4] + 1;
    }
    return lGrade;

答案 1 :(得分:1)

我并不是百分之百肯定你正在尝试做什么,但我会说只是为numAs,numBs,numCs,numDs和numFs创建变量,并将它们全部设置为等于0第二个循环。然后在调用letterGrade时适当增加数字,如下所示:

int numAs = 0, numBs = 0, numCs = 0, numDs = 0, numFs = 0;

// ..

if (average > 89){
    lGrade = "A";
    numAs++;
}
else if (average > 79 && average < 90){
    lGrade = "B";
    numBs++;
}
else if (average > 69 && average < 80){
    lGrade = "C";
    numCs++;
}
else if (average > 59 && average < 70){
    lGrade = "D";
    numDs++;
}
else if (average >= 0 && average < 60){
    lGrade = "F";
    numFs++;
}
return lGrade;

// ..    

然后在那个for循环之后,只需告诉成绩:

cout << "Number of As: " << numAs << endl;
cout << "Number of Bs: " << numBs << endl;
cout << "Number of Cs: " << numCs << endl;
cout << "Number of Ds: " << numDs << endl;
cout << "Number of Fs: " << numFs << endl;

答案 2 :(得分:0)

我知道你的作业是 C ++ 而我的(部分)答案是 C ,但我认为无论如何都可能值得分享,告诉你有不同的方法可以达到同样的效果。您可能有兴趣调整我的letter_grade()函数,例如,它使用常见的“过早返回”习惯而不是if-else if-else的序列。

#include <stdio.h>
#include <stdlib.h>

#define N_SCORES (100)

char letter_grade(int score) {
    if(score > 100 || score < 0) {
        return (char)0;
    }

    if(score > 85) return 'A';
    if(score > 70) return 'B';
    if(score > 55) return 'C';
    if(score > 40) return 'D';
    return 'F';
}

int main(void) {
    int histogram['F'-'A'+1] = {0};
    char letter;
    int score;

    int i;
    for(i=0; i<N_SCORES; i++) {
        score  = rand() % 101;
        letter = letter_grade( score );
        if( letter == 0 ) {
            fprintf(stderr, "Ignored invalid score (%d)\n", score);
            continue;
        }
        histogram[ letter - 'A' ]++;
    }

    for(letter='A'; letter<='F'; letter++) {
        if( letter=='E' )
            continue;
        printf("Number of %c grades: %2d\n", letter, 
               histogram[letter-'A']);
    }
    return 0;
}