#include
#include
#include
#define MAX_STUDENTS 100
#define MAX_NAME_LEN 20
#define MAX_ID_LEN 13
#define MAX_STUDENTS 100
typedef struct {
char first_name[MAX_NAME_LEN];
char last_name[MAX_NAME_LEN];
char id[MAX_ID_LEN];
float score;
float gpa;
char rebuild;
const char* gpa_grade;
} Student;
void add_student(Student students[], int n);
void modify_student(Student students[], int n);
void calculate_gpa(Student students[], int n);
void show_all_info(Student students[],int n);
void sort_by_score(Student students[],int n);
void search(Student students[],int n);
void encrypt(Student students[],int n);
void encryptString(char *str, int key);
void decrypt(char *str, int key);
char encryptedString[MAX_STUDENTS * 3 + 1]; // 每个成绩最多占3个字符,再加上结尾的空字符('\0')
int main() {
Student students[MAX_STUDENTS];
int i, choice, n ,N;
// encryptedString[0] = '\0'; // 初始化为空字符串
// 得到用户想先输入的学生数
printf("请输入初始数据个数:\n");
scanf("%d",&N);
n=N;
// 输入初始数据
printf("请输入初始数据:\n");
for(int i=0;i= MAX_STUDENTS) {
printf("Cannot add more students. Maximum limit reached.\n");
break;
}
add_student(students, n);
n++;
break;
case 2:
if (n == 0) {
printf("No students added yet.\n");
break;
}
modify_student(students, n);
break;
case 3:
if (n == 0) {
printf("No students added yet.\n");
break;
}
calculate_gpa(students, n);
break;
case 4:
show_all_info(students,n);
break;
case 5:
sort_by_score(students,n);
show_all_info(students,n);
break;
case 6:
search(students,n);
break;
case 7:
encrypt(students,n);
break;
case 8:
int key;
printf("请输入密钥");
scanf("%d",&key);
decrypt(encryptedString, key);
break;
case 9:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
void add_student(Student students[], int n) {
printf("\nEnter details of student %d:\n", n + 1);
printf("First name: ");
scanf("%s", students[n].first_name);
printf("Last name: ");
scanf("%s", students[n].last_name);
printf("ID: ");
scanf("%s", students[n].id);
printf("Score: ");
scanf("%f", &students[n].score);
}
void modify_student(Student students[], int n) {
char id[MAX_ID_LEN];
int i;
printf("\nEnter ID of student to modify: ");
scanf("%s", id);
for (i = 0; i < n; i++) {
if (strcmp(students[i].id, id) == 0) {
printf("Enter new details for student %d:\n", i + 1);
printf("First name: ");
scanf("%s", students[i].first_name);
printf("Last name: ");
scanf("%s", students[i].last_name);
printf("ID: ");
scanf("%s", students[i].id);
printf("Score: ");
scanf("%f", &students[i].score);
return;
}
}
printf("Student with ID %s not found.\n", id);
}
void calculate_gpa(Student students[], int n) {
int i;
float total_score = 0, total_gpa = 0;
for (i = 0; i < n; i++) {
if (students[i].score >= 92) {
students[i].gpa = 4.5;
} else if (students[i].score >= 88) {
students[i].gpa = 4.0;
} else if (students[i].score >=80 ) {
students[i].gpa = 3.5;
} else if (students[i].score >= 75) {
students[i].gpa = 3.0;
} else if (students[i].score >= 70) {
students[i].gpa = 2.5;
} else if (students[i].score >= 60) {
students[i].gpa = 2.0;
} else {
students[i].gpa = 0.0;
}
total_score += students[i].score;
total_gpa += students[i].gpa;
}
printf("\nGPA of each student:\n");
for (i = 0; i < n; i++) {
printf("%s %s: %.2f\n", students[i].first_name, students[i].last_name, students[i].gpa);
}
printf("Average score: %.2f\n", total_score / n);
printf("Average GPA: %.2f\n", total_gpa / n);
}
void show_all_info(Student students[], int n){
printf("-----------------------------------\n");
printf("姓名 绩点 等级 分数 是否重修\n");
for (int i = 0; i < n; i++) {
if (students[i].score >= 92) {
students[i].gpa = 4.5;
} else if (students[i].score >= 88) {
students[i].gpa = 4.0;
} else if (students[i].score >= 80) {
students[i].gpa = 3.5;
} else if (students[i].score >= 75) {
students[i].gpa = 3.0;
} else if (students[i].score >= 70) {
students[i].gpa = 2.5;
} else if (students[i].score >= 60) {
students[i].gpa = 2.0;
} else {
students[i].gpa = 0.0;
}
// 评判A+等级
if (students[i].gpa >= 4.5) {
students[i].gpa_grade = "A+";
} else if (students[i].gpa >= 4.0) {
students[i].gpa_grade = "A";
} else if (students[i].gpa >= 3.5) {
students[i].gpa_grade = "B+";
} else if (students[i].gpa >= 3.0) {
students[i].gpa_grade = "B";
} else if (students[i].gpa >= 2.5) {
students[i].gpa_grade = "C+";
} else if (students[i].gpa >= 2.0) {
students[i].gpa_grade = "C";
} else if (students[i].gpa >= 1.5) {
students[i].gpa_grade = "D";
} else {
students[i].gpa_grade = "F";
}
if (strncmp(students[i].id, "2022", 4) == 0){
students[i].rebuild= 0;
}
else{
students[i].rebuild= 1;
}
printf("%s %s %.1f %s %.1f %s",students[i].first_name, students[i].last_name, students[i].gpa, students[i].gpa_grade, students[i].score,students[i].rebuild?"是":"否");
printf("\n");
}
printf("-----------------------------------");
}
void sort_by_score(Student students[],int n){
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j = 92) {
students[i].gpa = 4.5;
} else if (students[i].score >= 88) {
students[i].gpa = 4.0;
} else if (students[i].score >= 80) {
students[i].gpa = 3.5;
} else if (students[i].score >= 75) {
students[i].gpa = 3.0;
} else if (students[i].score >= 70) {
students[i].gpa = 2.5;
} else if (students[i].score >= 60) {
students[i].gpa = 2.0;
} else {
students[i].gpa = 0.0;
}
printf("%s %s %.1f %.1f",students[i].first_name, students[i].last_name, students[i].gpa, students[i].score);
printf("\n");
printf("-----------------------------------");
}
}
if(flag !=1){
printf("The student's infomation not exit");
}
}
}
void encryptString(char *str, int key) {
int length = strlen(str);
for (int i = 0; i < length; i++) {
int digit = str[i] - '0'; // 将字符转换为数字
int encryptedDigit = (digit + key) % 10; // 加密运算
str[i] = encryptedDigit + '0'; // 将加密后的数字转换回字符
}
}
void encrypt(Student students[],int n){
int key;
// char encryptedString[MAX_STUDENTS * 3 + 1]; // 每个成绩最多占3个字符,再加上结尾的空字符('\0')
// encryptedString[0] = '\0'; // 初始化为空字符串
printf("请输入密钥:");
scanf("%d",&key);
// 将成绩拼接为字符串
for (int i = 0; i < n; i++) {
char scoreString[4]; // 3个字符用于存储成绩,再加上结尾的空字符('\0')
snprintf(scoreString, sizeof(scoreString), "%.0f",students[i].score);
strcat(encryptedString, scoreString);
}
printf("加密前的字符串:%s\n", encryptedString);
// 加密字符串
encryptString(encryptedString, key);
printf("加密后的字符串:%s\n", encryptedString);
}
void decrypt(char *str, int key) {
int length = strlen(str);
for (int i = 0; i < length; i++) {
int digit = str[i] - '0'; // 将字符转换为数字
int decryptedDigit = (digit - key+10) % 10; // 解密运算
str[i] = decryptedDigit + '0'; // 将解密后的数字转换回字符
}
printf("解密后的字符串:%s\n", str);
}