1.Init
#include <stdio.h>
#include <string.h>
// 定义学生结构体
typedef struct {
char first_name[50];
char last_name[50];
char stu_id[13]; // 12位数字,加1位用于字符串终止符
int score;
int retake;
char gpa_rank[3];
} Student;
// 根据成绩计算GPA等级
void calculate_gpa_rank(int score, char* gpa_rank) {
if (score >= 93) {
strcpy(gpa_rank, "A+");
} else if (score >= 85) {
strcpy(gpa_rank, "A");
} else if (score >= 80) {
strcpy(gpa_rank, "B+");
} else if (score >= 75) {
strcpy(gpa_rank, "B");
} else if (score >= 70) {
strcpy(gpa_rank, "C+");
} else if (score >= 65) {
strcpy(gpa_rank, "C");
} else if (score >= 60) {
strcpy(gpa_rank, "D");
} else {
strcpy(gpa_rank, "F");
}
}
// 初始化学生信息
void initialize_student(Student* student, const char* first_name, const char* last_name, const char* stu_id, int score) {
strcpy(student->first_name, first_name);
strcpy(student->last_name, last_name);
strcpy(student->stu_id, stu_id);
student->score = score;
// 计算重修信息
if (strncmp(stu_id, "2022", 4) == 0) {
student->retake = 0;
} else {
student->retake = 1;
}
// 计算GPA等级
calculate_gpa_rank(score, student->gpa_rank);
}
// 检查学号是否合法
int is_valid_stu_id(const char* stu_id) {
if (strlen(stu_id) != 12) {
return 0;
}
if (strncmp(stu_id, "2022", 4) == 0 || strncmp(stu_id, "2021", 4) == 0 || strncmp(stu_id, "2020", 4) == 0) {
return 1;
}
return 0;
}
int main() {
Student students[100];
int student_count = 0;
char buffer[100];
// 读取输入数据直到EOF
while (fgets(buffer, sizeof(buffer), stdin)) {
char first_name[50], last_name[50], stu_id[13];
int score;
// 解析输入数据
if (sscanf(buffer, "%s %s %s %d", first_name, last_name, stu_id, &score) == 4) {
// 检查学号是否合法并初始化学生信息
if (is_valid_stu_id(stu_id)) {
initialize_student(&students[student_count], first_name, last_name, stu_id, score);
student_count++;
}
}
}
// 输出合法的学生信息
printf("Name_f Name_l stu_id score retake GPA rank\n");
for (int i = 0; i < student_count; i++) {
printf("%s %s %s %d %d %s\n", students[i].first_name, students[i].last_name, students[i].stu_id, students[i].score, students[i].retake, students[i].gpa_rank);
}
return 0;
}
2.Add
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
// 定义学生结构体
typedef struct {
char first_name[50];
char last_name[50];
char stu_id[13]; // 12位数字,加1位用于字符串终止符
int score;
int retake;
char gpa_rank[3];
} Student;
// 初始化数据库中的学生信息
Student students[100] = {
{"Jingyu", "LI", "202200000000", 85, 0, "A"},
{"Jy", "LEE", "202200100000", 89, 0, "A"},
{"Jxxxyx", "Leeeee", "202100100000", 100, 1, "A+"},
{"Jingyu11", "LI", "202200000001", 85, 0, "A"}
};
int student_count = 4; // 初始数据库中的学生人数
// 根据成绩计算GPA等级
void calculate_gpa_rank(int score, char* gpa_rank) {
if (score >= 93) {
strcpy(gpa_rank, "A+");
} else if (score >= 85) {
strcpy(gpa_rank, "A");
} else if (score >= 80) {
strcpy(gpa_rank, "B+");
} else if (score >= 75) {
strcpy(gpa_rank, "B");
} else if (score >= 70) {
strcpy(gpa_rank, "C+");
} else if (score >= 65) {
strcpy(gpa_rank, "C");
} else if (score >= 60) {
strcpy(gpa_rank, "D");
} else {
strcpy(gpa_rank, "F");
}
}
// 检查学号是否合法
bool is_valid_stu_id(const char* stu_id) {
if (strlen(stu_id) != 12) {
return false;
}
if (strncmp(stu_id, "2022", 4) == 0 || strncmp(stu_id, "2021", 4) == 0 || strncmp(stu_id, "2020", 4) == 0) {
return true;
}
return false;
}
// 添加新学生信息的函数
void Add(const char* first_name, const char* last_name, const char* stu_id, int score) {
if (is_valid_stu_id(stu_id) && score >= 0 && score <= 100) {
Student student;
strcpy(student.first_name, first_name);
strcpy(student.last_name, last_name);
strcpy(student.stu_id, stu_id);
student.score = score;
// 计算重修信息
if (strncmp(stu_id, "2022", 4) == 0) {
student.retake = 0;
} else {
student.retake = 1;
}
// 计算GPA等级
calculate_gpa_rank(score, student.gpa_rank);
students[student_count] = student;
student_count++;
}
}
// 输出所有学生信息
void print_students() {
printf("Name_f Name_l stu_id score retake GPA\n");
for (int i = 0; i < student_count; i++) {
printf("%s %s %s %d %d %s\n", students[i].first_name, students[i].last_name, students[i].stu_id, students[i].score, students[i].retake, students[i].gpa_rank);
}
printf("Total: %d\n", student_count);
}
int main() {
char first_name[50], last_name[50], stu_id[13];
int score;
// 提示用户输入追加信息,直到用户结束输入
while (scanf("%s %s %s %d", first_name, last_name, stu_id, &score) == 4) {
Add(first_name, last_name, stu_id, score);
}
// 输出更新后的学生信息
print_students();
return 0;
}
3.Analysis
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
// 定义函数计算GPA等级
const char* calculate_gpa_rank(int score) {
if (score >= 93 && score <= 100) {
return "A+";
} else if (score >= 85) {
return "A";
} else if (score >= 80) {
return "B+";
} else if (score >= 75) {
return "B";
} else if (score >= 70) {
return "C+";
} else if (score >= 65) {
return "C";
} else if (score >= 60) {
return "D";
} else {
return "F";
}
}
// 检查成绩是否合法
bool is_valid_score(int score) {
return score >= 0 && score <= 100;
}
// GPA计数器结构体
typedef struct {
int a_plus;
int a;
int b_plus;
int b;
int c_plus;
int c;
int d;
int f;
} GPACounter;
// 初始化GPA计数器
void init_gpa_counter(GPACounter* counter) {
counter->a_plus = 0;
counter->a = 0;
counter->b_plus = 0;
counter->b = 0;
counter->c_plus = 0;
counter->c = 0;
counter->d = 0;
counter->f = 0;
}
// 更新GPA计数器
void update_gpa_counter(GPACounter* counter, const char* gpa_rank) {
if (strcmp(gpa_rank, "A+") == 0) {
counter->a_plus++;
} else if (strcmp(gpa_rank, "A") == 0) {
counter->a++;
} else if (strcmp(gpa_rank, "B+") == 0) {
counter->b_plus++;
} else if (strcmp(gpa_rank, "B") == 0) {
counter->b++;
} else if (strcmp(gpa_rank, "C+") == 0) {
counter->c_plus++;
} else if (strcmp(gpa_rank, "C") == 0) {
counter->c++;
} else if (strcmp(gpa_rank, "D") == 0) {
counter->d++;
} else if (strcmp(gpa_rank, "F") == 0) {
counter->f++;
}
}
// 输出GPA统计结果
void print_gpa_statistics(GPACounter* counter) {
if (counter->a_plus > 0) printf("A+: %d\n", counter->a_plus);
if (counter->a > 0) printf("A: %d\n", counter->a);
if (counter->b_plus > 0) printf("B+: %d\n", counter->b_plus);
if (counter->b > 0) printf("B: %d\n", counter->b);
if (counter->c_plus > 0) printf("C+: %d\n", counter->c_plus);
if (counter->c > 0) printf("C: %d\n", counter->c);
if (counter->d > 0) printf("D: %d\n", counter->d);
if (counter->f > 0) printf("F: %d\n", counter->f);
}
int main() {
GPACounter counter;
init_gpa_counter(&counter);
int score;
// printf("Enter scores (enter a non-numeric value to stop):\n");
while (scanf("%d", &score) == 1) {
if (is_valid_score(score)) {
const char* gpa_rank = calculate_gpa_rank(score);
update_gpa_counter(&counter, gpa_rank);
}
}
print_gpa_statistics(&counter);
return 0;
}
4.Delete
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
// 定义学生结构体
typedef struct {
char first_name[50];
char last_name[50];
char stu_id[13]; // 12位数字,加1位用于字符串终止符
int score;
int retake;
char gpa_rank[3];
} Student;
// 初始化数据库中的学生信息
Student students[100] = {
{"Jingyu", "LI", "202200000000", 85, 0, "A"},
{"Jy", "LEE", "202200100000", 89, 0, "A"},
{"Jxxxyx", "Leeeee", "202100100000", 100, 1, "A+"},
{"Jingyu11", "LI", "202200000001", 85, 0, "A"}
};
int student_count = 4; // 初始数据库中的学生人数
// 删除指定ID的学生信息的函数
void Delete(const char* stu_id) {
bool found = false;
for (int i = 0; i < student_count; i++) {
if (strcmp(students[i].stu_id, stu_id) == 0) {
found = true;
// 移动后续学生信息覆盖要删除的学生信息
for (int j = i; j < student_count - 1; j++) {
students[j] = students[j + 1];
}
student_count--;
break;
}
}
if (!found) {
printf("Student with ID %s not found.\n", stu_id);
}
}
// 输出所有学生信息
void print_students() {
printf("Name_f Name_l stu_id score retake GPA\n");
for (int i = 0; i < student_count; i++) {
printf("%s %s %s %d %d %s\n", students[i].first_name, students[i].last_name, students[i].stu_id, students[i].score, students[i].retake, students[i].gpa_rank);
}
printf("Total: %d\n", student_count);
}
int main() {
char stu_id[13];
// 提示用户输入要删除的学生ID,直到用户结束输入
//printf("Enter student ID to delete (enter a non-numeric value to stop):\n");
while (scanf("%s", stu_id) == 1) {
Delete(stu_id);
}
// 输出更新后的学生信息
print_students();
return 0;
}
5.Search
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
// 定义学生结构体
typedef struct {
char first_name[50];
char last_name[50];
char stu_id[13]; // 12位数字,加1位用于字符串终止符
int score;
int retake;
char gpa_rank[3];
} Student;
// 初始化数据库中的学生信息
Student students[100] = {
{"Jingyu", "LI", "202200000000", 85, 0, "A"},
{"Jy", "LEE", "202200100000", 89, 0, "A"},
{"Jxxxyx", "Leeeee", "202100100000", 100, 1, "A+"},
{"Jingyu11", "LI", "202200000001", 85, 0, "A"},
{"Jingyuuuu", "LI", "202200000900", 59, 0, "F"},
{"Jingyuuuu", "Lous", "202100000900", 60, 1, "D"},
{"Asss", "Dccc", "202212344321", 84, 0, "B+"},
{"Asssd", "Dccec", "202122244321", 83, 1, "B+"}
};
int student_count = 8; // 初始数据库中的学生人数
// 根据学号查找学生信息的函数
void Search(const char* id) {
for (int i = 0; i < student_count; i++) {
if (strcmp(students[i].stu_id, id) == 0) {
printf("%s %s %s %d %d %s\n", students[i].first_name, students[i].last_name, students[i].stu_id, students[i].score, students[i].retake, students[i].gpa_rank);
return;
}
}
printf("Not Found.\n");
}
int main() {
char id[13];
// 提示用户输入要查找的学生ID,直到用户输入0结束
// printf("Enter student ID to search (enter 0 to stop):\n");
while (true) {
scanf("%s", id);
if (strcmp(id, "0") == 0) {
break;
}
Search(id);
}
return 0;
}
6.Sort by score
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h> // 包含 qsort 函数所需的头文件
#define MAX_STUDENTS 100
// 定义学生结构体
typedef struct {
char first_name[50];
char last_name[50];
char stu_id[13]; // 12位数字,加1位用于字符串终止符
int score;
int rank;
} Student;
Student students[MAX_STUDENTS];
int student_count = 0;
// 添加学生信息的函数
void Add_student(const char* first_name, const char* last_name, const char* stu_id, int score) {
if (student_count < MAX_STUDENTS) {
strcpy(students[student_count].first_name, first_name);
strcpy(students[student_count].last_name, last_name);
strcpy(students[student_count].stu_id, stu_id);
students[student_count].score = score;
student_count++;
}
}
// 比较函数用于排序
int compare_students(const void* a, const void* b) {
Student* studentA = (Student*)a;
Student* studentB = (Student*)b;
if (studentA->score != studentB->score) {
return studentB->score - studentA->score;
} else {
return strcmp(studentA->stu_id, studentB->stu_id);
}
}
// 按成绩排序并生成排名
void Sort_by_score() {
qsort(students, student_count, sizeof(Student), compare_students);
for (int i = 0; i < student_count; i++) {
if (i == 0) {
students[i].rank = 1;
} else {
if (students[i].score == students[i-1].score) {
students[i].rank = students[i-1].rank;
} else {
students[i].rank = i + 1;
}
}
}
}
// 输出学生信息
void print_students() {
printf("Name_f Name_l stu_id score Rank\n");
for (int i = 0; i < student_count; i++) {
printf("%s %s %s %d %d\n", students[i].first_name, students[i].last_name, students[i].stu_id, students[i].score, students[i].rank);
}
}
int main() {
char first_name[50], last_name[50], stu_id[13];
int score;
// 提示用户输入学生信息,直到用户结束输入
while (scanf("%s %s %s %d", first_name, last_name, stu_id, &score) == 4) {
Add_student(first_name, last_name, stu_id, score);
}
// 按成绩排序并生成排名
Sort_by_score();
// 输出排序后的学生信息
print_students();
return 0;
}
7.Encrypt & Decrypt
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
// 加密函数
void encrypt(char* input, int key) {
for (int i = 0; input[i] != '\0'; i++) {
if (input[i] >= '0' && input[i] <= '9') {
input[i] = ((input[i] - '0' + key) % 10) + '0';
}
}
}
// 解密函数
void decrypt(char* input, int key) {
for (int i = 0; input[i] != '\0'; i++) {
if (input[i] >= '0' && input[i] <= '9') {
input[i] = ((input[i] - '0' - key + 10) % 10) + '0';
}
}
}
int main() {
char command;
int key;
char grades[1000]; // 假设输入的成绩列表不超过1000个字符
// 读取指令
scanf(" %c", &command);
// 读取密钥
scanf("%d", &key);
// 读取成绩列表
getchar(); // 清除前一个输入留下的换行符
fgets(grades, sizeof(grades), stdin);
// 去掉换行符
grades[strcspn(grades, "\n")] = '\0';
// 处理指令
if (command == 'e') {
encrypt(grades, key);
} else if (command == 'd') {
decrypt(grades, key);
} else {
printf("Invalid command.\n");
return 1;
}
// 输出结果
printf("%s\n", grades);
return 0;
}