用C++写一个计算大学生绩点的代码
时间: 2023-03-25 13:00:26 浏览: 389
首先,需要输入每门课程的学分和成绩。然后,根据学校的绩点计算规则,将每门课程的成绩转换为对应的绩点。最后,根据加权平均分计算出总绩点。
以下是一个简单的计算大学生绩点的C代码示例:
```
#include <stdio.h>
int main() {
int n; // 课程数
int credit[10]; // 学分
int score[10]; // 成绩
float point[10]; // 绩点
float sum_credit = ; // 总学分
float sum_point = ; // 总绩点
printf("请输入课程数:");
scanf("%d", &n);
// 输入每门课程的学分和成绩
for (int i = ; i < n; i++) {
printf("请输入第%d门课程的学分和成绩:", i+1);
scanf("%d %d", &credit[i], &score[i]);
// 根据成绩计算绩点
if (score[i] >= 90) {
point[i] = 4.;
} else if (score[i] >= 85) {
point[i] = 3.7;
} else if (score[i] >= 82) {
point[i] = 3.3;
} else if (score[i] >= 78) {
point[i] = 3.;
} else if (score[i] >= 75) {
point[i] = 2.7;
} else if (score[i] >= 72) {
point[i] = 2.3;
} else if (score[i] >= 68) {
point[i] = 2.;
} else if (score[i] >= 64) {
point[i] = 1.5;
} else if (score[i] >= 60) {
point[i] = 1.;
} else {
point[i] = ;
}
// 计算总学分和总绩点
sum_credit += credit[i];
sum_point += credit[i] * point[i];
}
// 计算总绩点
float gpa = sum_point / sum_credit;
printf("总绩点为:%.2f\n", gpa);
return ;
}
```