作者:tANGjIAqIAN
题目1
#include <stdio.h>
// 结构体
struct Student {
char id[12];
char name[11];
int mathScore;
int englishScore;
int programmingScore;
};
// 输入学生信息
void InputStudent(struct Student *student) {
scanf("%s %s %d %d %d", student->id, student->name, &student->mathScore, &student->englishScore, &student->programmingScore);
}
// 输出学生信息
void OutputStudent(struct Student *student) {
printf("%s %10s %d %d %d\n", student->id, student->name, student->mathScore, student->englishScore, student->programmingScore);
}
int main() {
int n;
scanf("%d", &n);
struct Student students[n];
for (int i = 0; i < n; i++) {
InputStudent(&students[i]);
}
for (int i = 0; i < n; i++) {
OutputStudent(&students[i]);
}
return 0;
}

题目二
#include <stdio.h>
#include <string.h>
// 结构体
struct Student {
char id[12];
char name[11];
int mathScore;
int englishScore;
int programmingScore;
};
// 输入学生信息
void InputStudent(struct Student *student) {
scanf("%s %s %d %d %d", student->id, student->name, &student->mathScore, &student->englishScore, &student->programmingScore);
}
// 输出学生信息
void OutputStudent(struct Student *student) {
printf("%s %10s %d %d %d\n", student->id, student->name, student->mathScore, student->englishScore, student->programmingScore);
}
// 根据姓名查找学生信息
void FindStudentByName(struct Student *students, int n, char *name) {
int found = 0;
for (int i = 0; i < n; i++) {
if (strcmp(students[i].name, name) == 0) {
OutputStudent(&students[i]);
found = 1;
break;
}
}
if (!found) {
printf("Not Found!\n");
}
}
int main() {
int n;
scanf("%d", &n);
struct Student students[n];
for (int i = 0; i < n; i++) {
InputStudent(&students[i]);
}
char name[11];
scanf("%s", name);
FindStudentByName(students, n, name);
return 0;
}


题目三
#include <stdio.h>
#include <string.h>
// 结构体
struct Student {
char id[12];
char name[11];
int mathScore;
int englishScore;
int programmingScore;
};
// 输入学生信息
void InputStudent(struct Student *student) {
scanf("%s %s %d %d %d", student->id, student->name, &student->mathScore, &student->englishScore, &student->programmingScore);
}
// 输出学生信息
void OutputStudent(struct Student *student) {
printf("%s %10s %d %d %d\n", student->id, student->name, student->mathScore, student->englishScore, student->programmingScore);
}
// 根据姓名查找学生信息
void FindStudentByName(struct Student *students, int n, char *name) {
int found = 0;
for (int i = 0; i < n; i++) {
if (strcmp(students[i].name, name) == 0) {
OutputStudent(&students[i]);
found = 1;
break;
}
}
if (!found) {
printf("Not Found!\n");
}
}
// 计算平均分
void computeAverage(struct Student *students, int n) {
// 计算学生成绩的平均分
printf("学生成绩的平均分:\n");
for (int i = 0; i < n; i++) {
double average = (students[i].mathScore + students[i].englishScore + students[i].programmingScore) / 3.0;
printf("%s %.4lf\n", students[i].id, average);
}
// 计算每门课的平均分
printf("三门课的平均分:\n");
double mathSum = 0, englishSum = 0, programSum = 0;
for (int i = 0; i < n; i++) {
mathSum += students[i].mathScore;
englishSum += students[i].englishScore;
programSum += students[i].programmingScore;
}
double mathAverage = mathSum / n;
double englishAverage = englishSum / n;
double programAverage = programSum / n;
printf("Math: %6.4lf\n", mathAverage);
printf("English: %6.4lf\n", englishAverage);
printf("CProgram:%6.4lf\n", programAverage);
}
int main() {
int n;
scanf("%d", &n);
struct Student students[n];
for (int i = 0; i < n; i++) {
InputStudent(&students[i]);
}
computeAverage(students, n);
return 0;
}

没脾气了都,这回中文全部乱码,找了半天居然终端编码是GBK,手动改成utf-8没用!怎么之前就不这样呢?噢~,原来是用了最新版的vscode啊,默认编码是utf-8啊。这我找谁说理去啊!
题目四
#include <stdio.h>
#include <string.h>
// 定义学生结构体
struct Student
{
char id[12];
char name[11];
int mathScore;
int englishScore;
int programmingScore;
double averageScore;
};
// 输入学生信息
void InputStudent(struct Student *student)
{
scanf("%s %s %d %d %d", student->id, student->name, &student->mathScore, &student->englishScore, &student->programmingScore);
student->averageScore = (student->mathScore + student->englishScore + student->programmingScore) / 3.0;
}
// 输出学生信息
void OutputStudent(struct Student *student)
{
printf("%s %6.4lf\n", student->id, student->averageScore);
}
// 选择排序
void DescendingSortByAvgscore(struct Student *students, int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
if (students[i].averageScore < students[j].averageScore)
{
struct Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
int main()
{
int n;
scanf("%d", &n);
struct Student students[n];
for (int i = 0; i < n; i++)
{
InputStudent(&students[i]);
}
DescendingSortByAvgscore(students, n);
printf("根据学生成绩的平均分排序结果:\n");
for (int i = 0; i < n; i++)
{
OutputStudent(&students[i]);
}
return 0;
}

题目五
#include <stdio.h>
#include <string.h>
// 定义学生结构体
struct Student
{
char id[12];
char name[11];
int mathScore;
int englishScore;
int programmingScore;
double averageScore;
};
// 输入学生信息
void InputStudent(struct Student *student)
{
scanf("%s %s %d %d %d", student->id, student->name, &student->mathScore, &student->englishScore, &student->programmingScore);
student->averageScore = (student->mathScore + student->englishScore + student->programmingScore) / 3.0;
}
// 输出学生信息
void OutputStudent(struct Student *student)
{
printf("%-10s %s\n", student->name, student->id);
}
// 选择排序
void DescendingSortByName(struct Student *students, int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
if (students[j].name[0] < students[i].name[0])
{
struct Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
int main()
{
int n;
scanf("%d", &n);
struct Student students[n];
for (int i = 0; i < n; i++)
{
InputStudent(&students[i]);
}
DescendingSortByName(students, n);
printf("根据学生姓名排序结果:\n");
for (int i = 0; i < n; i++)
{
OutputStudent(&students[i]);
}
return 0;
}

解释:比较name数组的第一位即可
浙公网安备 33010602011771号