如果是文字成绩,我如何计算大学平均成绩?
How can I calculate University grade average in case of literal grades?
我正在尝试编写一个从用户那里获取数据并计算 his/her 平均值的 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
计算。
每节课
- 计算增益。
- 收益相加
- 计算平均值。
另一个例子
如果我目前的体重平均分是 3.00 和 47 个学分,那么这意味着我到目前为止有 141 个增益点。这学期我修了两门课,其中一门是AA,3个学分,另一门是BB,3个学分。这个学期我的收获是 21 (4*3 + 3*3)。 21 被添加到先前的增益 (141)。总增益现在是 162。总学分是 47+3+3= 53。新权重平均分是 162/53= 3.05
我的代码
我不能做的是当用户输入AA
或BA
时如何使用条件状态来设置增益?如果用户输入 aa
并获得 3 个积分,则增益将为 12,如果用户输入 ba
并获得 2 个积分,增益将为 7
为了简单起见,(例如)到目前为止我获得了 47 个学分,我的平均成绩是 2.66。
这学期我选了6门课
- 课程a是4学分aa,
- 课程 b 是 3 个学分 ba,
- 课程 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
}
}
您的问题是将文字成绩转换为数值-
有多种方法可以实现您的目标:我选择了一种解决方案,该解决方案将输入等级与一组允许的等级进行直接比较。这可以通过定义 查找 table.
以优雅的方式完成
提议的解决方案将不包含:
- 来自用户的输入检索。这取决于您,尤其是因为如果您正确定义 引擎 ,用户界面是一个可以用不同方式实现的细节(不仅是
scanf
的循环s 而且,例如,处理输入文本文件)。
- 不区分大小写的管理。仅接受大写字母成绩(例如 "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" },
};
这里我使用之前定义的结构来定义两个数组:
- a 查找table所有等级字符串文字及其对应的数值
- 他们的学分成绩夫妇的一组讲座测试。也插入了一个错误的等级。我希望程序丢弃它。预期平均成绩为 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;
}
主要。我只是循环遍历测试值,找到与它们相关的等级并更新计算。我跳过 错误的 输入值。我最终计算出打印它的平均值(只有两位小数,就像你的例子一样)。
这是输出:
Grade QQ (index 6) is wrong and has not been processed!
Average grade is 2.76
我正在尝试编写一个从用户那里获取数据并计算 his/her 平均值的 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
计算。
每节课
- 计算增益。
- 收益相加
- 计算平均值。
另一个例子
如果我目前的体重平均分是 3.00 和 47 个学分,那么这意味着我到目前为止有 141 个增益点。这学期我修了两门课,其中一门是AA,3个学分,另一门是BB,3个学分。这个学期我的收获是 21 (4*3 + 3*3)。 21 被添加到先前的增益 (141)。总增益现在是 162。总学分是 47+3+3= 53。新权重平均分是 162/53= 3.05
我的代码
我不能做的是当用户输入AA
或BA
时如何使用条件状态来设置增益?如果用户输入 aa
并获得 3 个积分,则增益将为 12,如果用户输入 ba
并获得 2 个积分,增益将为 7
为了简单起见,(例如)到目前为止我获得了 47 个学分,我的平均成绩是 2.66。
这学期我选了6门课
- 课程a是4学分aa,
- 课程 b 是 3 个学分 ba,
- 课程 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
}
}
您的问题是将文字成绩转换为数值-
有多种方法可以实现您的目标:我选择了一种解决方案,该解决方案将输入等级与一组允许的等级进行直接比较。这可以通过定义 查找 table.
以优雅的方式完成提议的解决方案将不包含:
- 来自用户的输入检索。这取决于您,尤其是因为如果您正确定义 引擎 ,用户界面是一个可以用不同方式实现的细节(不仅是
scanf
的循环s 而且,例如,处理输入文本文件)。 - 不区分大小写的管理。仅接受大写字母成绩(例如 "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" },
};
这里我使用之前定义的结构来定义两个数组:
- a 查找table所有等级字符串文字及其对应的数值
- 他们的学分成绩夫妇的一组讲座测试。也插入了一个错误的等级。我希望程序丢弃它。预期平均成绩为 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;
}
主要。我只是循环遍历测试值,找到与它们相关的等级并更新计算。我跳过 错误的 输入值。我最终计算出打印它的平均值(只有两位小数,就像你的例子一样)。
这是输出:
Grade QQ (index 6) is wrong and has not been processed!
Average grade is 2.76