用C语言写这个程序:【问题描述】输入某个学生的信息(含学号,姓名,年龄,六门课程分数-高数/英语/c语言/工程制图/大学物理/信息论,它们的课程积分分别为4/3/4/3/3/3),计算课程总积分。 【输入形式】 学号 姓名 年龄 课程分数 【输出形式】 学号 姓名 年龄 课程总积分 【样例输入】 18201819 zhangone 20 90 57 81 85 79 91 【样例输出】 18201819 zhangone 20 17
时间: 2023-06-12 15:07:28 浏览: 171
以下是用C语言实现的代码:
```c
#include <stdio.h>
int main()
{
int id, age;
char name[20];
int math, english, c, engineering, physics, info;
int total_score;
// 输入学生信息
printf("请输入学生信息:\n");
scanf("%d %s %d %d %d %d %d %d", &id, name, &age, &math, &english, &c, &engineering, &physics, &info);
// 计算课程总积分
total_score = math * 4 + english * 3 + c * 4 + engineering * 3 + physics * 3 + info * 3;
// 输出学生信息和课程总积分
printf("%d %s %d %d\n", id, name, age, total_score);
return 0;
}
```
注意,这里使用了一个字符串数组来存储学生的姓名,这是因为直接使用字符变量只能存储一个字符,无法存储多个字符构成的字符串。同时,scanf函数中的%s表示读取一个字符串,它会自动将输入的字符序列存储到一个字符数组中。
相关问题
用c语言结构体实现:输入某个学生的信息(含学号,姓名,年龄,六门课程分数-高数/英语/c语言/工程制图/大学物理/信息论,它们的课程积分分别为4/3/4/3/3/3),计算课程总积分。 【输入形式】 学号 姓名 年龄 (换行)课程分数 【输出形式】 学号 姓名 年龄 (换行)课程总积分
以下是一种可能的实现方式:
```c
#include <stdio.h>
#define NUM_COURSES 6
#define SCORE_WEIGHTS {4, 3, 4, 3, 3, 3}
struct student {
int id;
char name[20];
int age;
int scores[NUM_COURSES];
};
int calculate_total_score(struct student s) {
int total_score = 0;
const int score_weights[NUM_COURSES] = SCORE_WEIGHTS;
for (int i = 0; i < NUM_COURSES; i++) {
total_score += s.scores[i] * score_weights[i];
}
return total_score;
}
int main() {
struct student s;
printf("请输入学生信息(学号 姓名 年龄):");
scanf("%d %s %d", &s.id, s.name, &s.age);
printf("请输入六门课程的分数:");
for (int i = 0; i < NUM_COURSES; i++) {
scanf("%d", &s.scores[i]);
}
int total_score = calculate_total_score(s);
printf("学号:%d\n姓名:%s\n年龄:%d\n课程总积分:%d\n",
s.id, s.name, s.age, total_score);
return 0;
}
```
这个程序定义了一个 `student` 结构体,包含学生的各种信息。其中,`scores` 数组存储了学生的六门课程分数,`SCORE_WEIGHTS` 宏定义了各门课程的积分系数。`calculate_total_score` 函数根据课程分数和积分系数计算课程总积分。程序从用户输入中获取学生信息和课程分数,并输出学生的各种信息和课程总积分。
输入某个学生的信息(含学号,姓名,年龄,六门课程分数-高数/英语/c语言/工程制图/大学物理/信息论,它们的课程学分分别为4/3/4/3/3/3,计算课程总学分。c语言结构体代码
下面是一个简单的 C 语言结构体代码,用于输入学生信息并计算总学分:
```c
#include <stdio.h>
struct student {
int id;
char name[20];
int age;
float math_score;
float english_score;
float c_score;
float engineering_score;
float physics_score;
float information_score;
};
int main() {
struct student s;
float total_credits;
printf("Enter student ID: ");
scanf("%d", &s.id);
printf("Enter student name: ");
scanf("%s", s.name);
printf("Enter student age: ");
scanf("%d", &s.age);
printf("Enter math score: ");
scanf("%f", &s.math_score);
printf("Enter English score: ");
scanf("%f", &s.english_score);
printf("Enter C score: ");
scanf("%f", &s.c_score);
printf("Enter engineering score: ");
scanf("%f", &s.engineering_score);
printf("Enter physics score: ");
scanf("%f", &s.physics_score);
printf("Enter information score: ");
scanf("%f", &s.information_score);
// 计算总学分
total_credits = 4 * s.math_score + 3 * s.english_score + 4 * s.c_score +
3 * s.engineering_score + 3 * s.physics_score +
3 * s.information_score;
printf("Total credits: %f\n", total_credits);
return 0;
}
```
阅读全文