【发布时间】: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计算。
每堂课
- 计算增益。
- 增益相互叠加。
- 计算平均值。
另一个例子
如果我目前的平均成绩是 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 学分,
- 课程 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)。你可以显式地写成&n[0],但写成n更符合习惯。写&n是错误的 -
@WilliamPursell 等,
n是int数组,scanf("%s", n)是读取字符串(到intarray 中) -
@RamblinRose :不是
gets()- 已弃用且危险;使用fgets()。