面试题答案
一键面试#include <stdio.h>
// 宏定义成绩的满分值
#define MAX_SCORE 100
// 定义学生信息结构体
typedef struct {
char name[50];
int age;
int score;
} Student;
// 计算学生成绩得分比例的函数
float calculateScoreRatio(Student student) {
return (float)student.score / MAX_SCORE;
}
你可以使用以下方式调用这个函数:
int main() {
Student student = {"Alice", 20, 85};
float ratio = calculateScoreRatio(student);
printf("得分比例: %.2f%%\n", ratio * 100);
return 0;
}
在上述代码中:
- 首先通过
#define MAX_SCORE 100
定义了成绩的满分值为100。 - 接着定义了
Student
结构体,包含学生的姓名name
、年龄age
和成绩score
。 calculateScoreRatio
函数接受一个Student
结构体变量作为参数,计算并返回学生成绩的得分比例。- 在
main
函数中创建了一个学生实例,并调用calculateScoreRatio
函数得到得分比例并打印。