【问题标题】:How can I calculate University grade average in case of literal grades?如果是文字成绩,我如何计算大学平均成绩?
【发布时间】:2019-12-31 10:27:13
【问题描述】:

我正在尝试编写一个从用户那里获取数据并计算他/她的平均值的 C 程序。

它只是获得讲座的学分和讲座的字母等级。文字等级系统如下:

  • AA=4
  • BA=3.5
  • BB=3
  • CB=2.5
  • CC=2
  • DC=1.5
  • DD=1
  • FF=0

可以通过将字母等级的对应值(例如AA = 4)和课程学分相乘来找到增益。例如,如果成绩为“AA”,则该讲座对平均值的增益贡献将为4*credit_of_lecture

重量等级平均值由total_gain/total_credit_number计算。

每堂课

  1. 计算增益。
  2. 增益相互叠加。
  3. 计算平均值。

另一个例子

如果我目前的平均成绩是 3.00 和 47 个学分,那么这意味着我到目前为止有 141 个增益点。这个学期我修了两门课,其中一门是AA,3学分,另一门是BB,3学分。对于这个学期,我的收益将是 21 (4*3 + 3*3)。 21 被添加到先前的增益 (141)。总收益现在是 162。总学分是 47+3+3=53。新的权重等级平均值是 162/53=3.05

我的代码

我不能做的是当用户输入AABA时如何使用条件状态来设置增益?如果用户输入aa 并获得 3 个学分,则增益为 12,如果用户输入 ba 并获得 2 个学分,则增益为 7

为了简化,(例如)我到目前为止获得了 47 个学分,我的平均成绩是 2.66。

这学期我修了 6 门课

  • 课程 a 是 4 学分 aa,
  • 课程 b 是 3 学分,
  • 课程 c 是 3 学分 dd 等。

我的最终平均成绩是多少?

int main(){
    int sayi,i,j /*credit number of the lectur*/;
    int kredi /*obtained credit*/, abd, gain;
    float gurtalama; //current grade average

    int x[2],n[5];// x will be letter grade n will be name of the lecture.

    printf("number of obtained credits: "); //gets the number of obtained credits
    scanf("%d",&kredi);

    printf("current grade average : "); // current weight grade average
    scanf("%f",&gurtalama);

    printf("how many lectures you take this semestr? : "); // number of lectures are get to decide how many times the for loop will work
    scanf("%d",&sayi);

    for(i=0;i<sayi;i++) {
        printf("Name of the lecture: "); // this is for not to confuse it may not be necessary
        scanf("%s",&n);

        printf("Credit number of lecture: "); //for each lecture, lecture credit
        scanf("%d",&j);

        printf("Letter grade of lecture: "); // letter grade to convert it into 4 based system
        scanf("%s",&x);

        printf("%s ",n);            //Name of the lecture
        printf("%d Kredi. ",j);     //Credit of lecture
        printf("%s \n",x);          //letter grade of lecture
    }
}

【问题讨论】:

  • 请正确格式化您的代码,并请给我们minimal reproducible example
  • AA, BA... 是什么意思? BA 能拿到多少分?如何计算?
  • 如果n 是一个字符数组,那么你需要scanf("%s", n)。你可以显式地写成&amp;n[0],但写成n 更符合习惯。写&amp;n是错误的
  • @WilliamPursell 等,nint 数组,scanf("%s", n) 是读取字符串(到 intarray 中)
  • @RamblinRose :不是gets() - 已弃用且危险;使用fgets()

标签: c average


【解决方案1】:

您的问题是将文字等级转换为数值-

有多种方法可以实现您的目标:我选择了一种解决方案,该解决方案可以将输入等级与允许的等级进行直接比较。这可以通过定义查找表以优雅的方式完成。

建议的解决方案将包含:

  • 来自用户的输入检索。这取决于您,尤其是因为如果您正确定义了引擎,用户界面是一个可以以不同方式实现的细节(不仅使用scanfs 的循环,而且,对于例如,处理输入文本文件)。
  • 不区分大小写的管理。只录取大写字母等级(例如“BA”)。只需稍作改动,您就可以轻松地接受较低的字符等级。
  • 讲座名称等额外讲座信息的管理。

示例代码

#include <stdio.h>
#include <string.h>

typedef struct
{
  char  literal[3];
  float numeric;
} GradeInfo_t; 

typedef struct
{
  /* Add all the extra info relevant to you: lecture name etcetera */
  unsigned int credits;
  char         grade[3];
} LectureInfo_t;

如您所见,如果您想编写高效的代码,可以定义一些结构以便将相关数据放在一起。通过这种方式,您将能够(稍后)定义结构数组,使您可以循环遍历它们。

第一个结构将使单个等级与其数值之间的关联成为可能。第二个包含输入讲座信息。

/* Look-Up table to translate litaral grades into numeric values */
static const GradeInfo_t gradeLUT[] =
{
  { "AA", 4.0 },
  { "BA", 3.5 },
  { "BB", 3.0 },
  { "CB", 2.5 },
  { "CC", 2.0 },
  { "DC", 1.5 },
  { "DD", 1.0 },
  { "FF", 0.0 }
};

static const LectureInfo_t lecturesTestArray[] =
{
  { 4, "BB" }, 
  { 3, "AA" }, 
  { 4, "CC" }, 
  { 5, "BA" }, 
  { 3, "BB" }, 
  { 4, "DD" },
  { 4, "QQ" }, /* Unexpected literal. Will the program skip it? */
  { 3, "DC" }, 
  { 6, "AA" }, 
  { 5, "BA" }, 
  { 4, "CB" }, 
  { 2, "FF" }, 
};

这里我使用之前定义的结构来定义两个数组:

  1. 一个查找表,其中包含所有等级字符串文字及其对应的数值
  2. 一组与他们的学分成绩配对的讲座。在插入也错误的等级。我希望程序丢弃它。预期平均成绩为2,75581395348837


int main(void)
{
  /* Here you get grades and lecture info from the user. I define them        *
   * globally using a test array.                                             */
  int totalCredits = 0, i, j;
  int testElems=(sizeof(lecturesTestArray)/sizeof(LectureInfo_t));
  int totalGradeValues=(sizeof(gradeLUT)/sizeof(GradeInfo_t));;
  float gainsSum = 0, average;

  /* For every element of the test lecture array */
  for( i=0; i<testElems; i++)
  {
    /* Until a matching grade is found */
    for( j=0; j<totalGradeValues; j++)
    {
      /* Compare current lecture grade with current gradeInfo */
      if(strcmp(lecturesTestArray[i].grade, gradeLUT[j].literal) == 0 )
      {
        /* that's it! Process its data */
        totalCredits += lecturesTestArray[i].credits;
        gainsSum += lecturesTestArray[i].credits * gradeLUT[j].numeric;
        break;
      }
    }

    /* Just for fun: let's check that the "wrong" grade has been skipped */
    if( j == totalGradeValues )
    {
      printf("Grade %s (index %d) is wrong and has not been processed!\n", lecturesTestArray[i].grade, i);
    }
  }

  /* Test array consumed! Lets calculate average and print it */
  average = gainsSum / totalCredits;

  printf("Average grade is %0.2f", average);

  return 0;
}

主要。我只是遍历测试值,找到与它们相关的等级并更新计算。我跳过了 wrong 输入值。我最后计算了打印它的平均值(只有两位小数,就像你的例子一样)。

这是输出:

Grade QQ (index 6) is wrong and has not been processed!
Average grade is 2.76

【讨论】:

    猜你喜欢
    • 2019-08-25
    • 1970-01-01
    • 1970-01-01
    • 2015-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多