You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

268 lines
7.9 KiB

2 weeks ago
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// <20><><EFBFBD><EFBFBD>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD><E1B9B9>
typedef struct Student {
char id[20];
char clas[20];
char name[20];
double score1, score2, score3, totalScore;
} Student;
#define MAX_STUDENTS 100
Student students[MAX_STUDENTS];
int count = 0;
void menu();
void input();
void delete();
void select();
void sort();
void output();
int main() {
char option;
while (1) {
menu();
printf("please input your option: ");
scanf(" %c", &option);
switch (option) {
case '1':
input();
break;
case '2':
delete();
break;
case '3':
select();
break;
case '4':
sort();
break;
case '5':
output();
break;
case '6':
printf("Exiting.\n");
return 0;
default:
printf("Invalid option.\n");
}
}
return 0;
}
void menu() {
printf("\n");
printf("1.input\n");
printf("2.delete\n");
printf("3.select\n");
printf("4.order\n");
printf("5.output\n");
printf("6.quit\n");
}
void input() {
while (1) {
if (count >= MAX_STUDENTS) {
printf("Maximum number of students reached.\n");
return;
}
Student buffer;
printf("Id: ");
scanf("%s", buffer.id);
printf("Class: ");
scanf("%s", buffer.clas);
printf("Name: ");
scanf("%s", buffer.name);
printf("Score1: ");
scanf("%lf", &buffer.score1);
printf("Score2: ");
scanf("%lf", &buffer.score2);
printf("Score3: ");
scanf("%lf", &buffer.score3);
buffer.totalScore = buffer.score1 + buffer.score2 + buffer.score3;
students[count++] = buffer;
char cont[4];
printf("continue? (yes/no): ");
scanf("%s", cont);
if (cont[0] == 'n') break;
}
}
void delete() {
if (count == 0) {
printf("No students.\n");
return;
}
char key[20];
printf("Enter student ID or name to delete: ");
scanf("%s", key);
int i, boo1 = 0;
for (i = 0; i < count; i++) {
if (strcmp(students[i].id, key) == 0 || strcmp(students[i].name, key) == 0) {
boo1 = 1;
break;
}
}
int j;
if (boo1) {
for (j = i; j < count - 1; j++) {
students[j] = students[j + 1];
}
count--;
printf("Student deleted successfully.\n");
output();
} else {
printf("Student not found.\n");
output();
}
}
// void select() {
// char key[20];
// printf("Enter student ID, name, or class to search: ");
// scanf("%s", key);
// int boo1 = 0;
// int i;
// for (i = 0; i < count; i++) {
// if (strcmp(students[i].id, key) == 0 || strcmp(students[i].name, key) == 0 || strcmp(students[i].clas, key) == 0) {
// printf("%s %s %s %.1f %.1f %.1f %.1f\n", students[i].id, students[i].clas, students[i].name,
// students[i].score1, students[i].score2, students[i].score3, students[i].totalScore);
// boo1 = 1;
// }
// }
// if (!boo1) {
// printf("No eligible student found.\n");
// }
// }
void select() {
char input[50];
printf("Enter your query (e.g., 1 classNum-classNum, 2 idNum-idNum, 3 namePrefix*, 4 minScore, 5 classNum.idNum-idNum): ");
scanf(" %[^\n]", input);
int type = input[0] - '0';
char condition[50];
strcpy(condition, input + 2);
int boo1 = 0;
switch (type) {
case 1: {
int startClass, endClass;
sscanf(condition, "%d-%d", &startClass, &endClass);
int i;
for (i = 0; i < count; i++) {
int class = atoi(students[i].clas);
if (class >= startClass && class <= endClass) {
printf("%s %s %s %.1f %.1f %.1f %.1f\n", students[i].id, students[i].clas, students[i].name,
students[i].score1, students[i].score2, students[i].score3, students[i].totalScore);
boo1 = 1;
}
}
break;
}
case 2: {
int startId, endId;
sscanf(condition, "%d-%d", &startId, &endId);
int i;
for (i = 0; i < count; i++) {
int id = atoi(students[i].id);
if (id >= startId && id <= endId) {
printf("%s %s %s %.1f %.1f %.1f %.1f\n", students[i].id, students[i].clas, students[i].name,
students[i].score1, students[i].score2, students[i].score3, students[i].totalScore);
boo1 = 1;
}
}
break;
}
case 3: {
char prefix[20];
sscanf(condition, "%s", prefix);
int prefixLen = strlen(prefix) - 1;
int i;
for (i = 0; i < count; i++) {
if (strncmp(students[i].name, prefix, prefixLen) == 0) {
printf("%s %s %s %.1f %.1f %.1f %.1f\n", students[i].id, students[i].clas, students[i].name,
students[i].score1, students[i].score2, students[i].score3, students[i].totalScore);
boo1 = 1;
}
}
break;
}
case 4: {
double minScore;
sscanf(condition, "%lf", &minScore);
int i;
for (i = 0; i < count; i++) {
if (students[i].totalScore >= minScore) {
printf("%s %s %s %.1f %.1f %.1f %.1f\n", students[i].id, students[i].clas, students[i].name,
students[i].score1, students[i].score2, students[i].score3, students[i].totalScore);
boo1 = 1;
}
}
break;
}
case 5: {
int class, startId, endId;
sscanf(condition, "%d.%d-%d", &class, &startId, &endId);
int i;
for (i = 0; i < count; i++) {
int studentClass = atoi(students[i].clas);
int studentId = atoi(students[i].id);
if (studentClass == class && studentId >= startId && studentId <= endId) {
printf("%s %s %s %.1f %.1f %.1f %.1f\n", students[i].id, students[i].clas, students[i].name,
students[i].score1, students[i].score2, students[i].score3, students[i].totalScore);
boo1 = 1;
}
}
break;
}
default:
printf("Invalid query type.\n");
return;
}
if (!boo1) {
printf("No eligible student found.\n");
}
}
void sort() {
int i, j;
for (i = 0; i < count - 1; i++) {
for (j = i + 1; j < count; j++) {
if (strcmp(students[i].clas, students[j].clas) > 0 ||
(strcmp(students[i].clas, students[j].clas) == 0 && students[i].totalScore < students[j].totalScore)) {
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
printf("Students sorted successfully.\n");
output();
}
void output() {
if (count == 0) {
printf("No students to display.\n");
return;
}
int i;
for (i = 0; i < count; i++) {
printf("%s %s %s %.1f %.1f %.1f %.1f\n", students[i].id, students[i].clas, students[i].name,
students[i].score1, students[i].score2, students[i].score3, students[i].totalScore);
}
}