
#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);
}

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335