ADD file via upload

main
pfwail38f 1 year ago
parent d24cf6a1f5
commit 3de285a9c6

201
8.c

@ -0,0 +1,201 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT_NUM 100
struct Student {
char* id;
char* clas;
char* name;
double score1;
double score2;
double score3;
double score;
};
struct Student stu[MAX_STUDENT_NUM];
int stu_num = 0;
void print_menu() {
printf("1.input\n");
printf("2.delete\n");
printf("3.select\n");
printf("4.order\n");
printf("5.output\n");
printf("6.quit\n");
printf("please input your option\n");
}
void add_student() {
while (1) {
printf("Id ");
stu[stu_num].id = (char*)malloc(sizeof(char) * 20);
scanf("%s", stu[stu_num].id);
printf("class ");
stu[stu_num].clas = (char*)malloc(sizeof(char) * 20);
scanf("%s", stu[stu_num].clas);
printf("name ");
stu[stu_num].name = (char*)malloc(sizeof(char) * 20);
scanf("%s", stu[stu_num].name);
printf("score1 ");
scanf("%lf", &stu[stu_num].score1);
printf("score2 ");
scanf("%lf", &stu[stu_num].score2);
printf("score3 ");
scanf("%lf", &stu[stu_num].score3);
stu[stu_num].score = stu[stu_num].score1 + stu[stu_num].score2 + stu[stu_num].score3;
stu_num++;
printf("continue? ");
char answer[10];
scanf("%s", answer);
if (strcmp(answer, "no") == 0) {
break;
}
}
}
void delete_student() {
int i, j;
while (1) {
printf("input student id or name to delete\n");
char query[20];
scanf("%s", query);
int deleted = 0;
for (i = 0; i < stu_num; i++) {
if (strcmp(query, stu[i].id) == 0 || strcmp(query, stu[i].name) == 0) {
free(stu[i].id);
free(stu[i].clas);
free(stu[i].name);
for (j = i; j < stu_num - 1; j++) {
stu[j] = stu[j + 1];
}
stu_num--;
deleted = 1;
i--;
}
}
if (!deleted) {
printf("no such student\n");
}
else {
for (i = 0; i < stu_num; i++) {
printf("%s,%s,%s,%.1lf,%.1lf,%.1lf,%.1lf\n", stu[i].id, stu[i].clas, stu[i].name, stu[i].score1, stu[i].score2, stu[i].score3, stu[i].score);
}
}
printf("continue? ");
char answer[10];
scanf("%s", answer);
if (strcmp(answer, "no") == 0) {
break;
}
}
}
void select_student() {
int i;
while (1) {
printf("input student id or class to select\n");
char query[20];
scanf("%s", query);
double sum_score1 = 0.0, sum_score2 = 0.0, sum_score3 = 0.0, sum_score = 0.0;
int count = 0;
for (i = 0; i < stu_num; i++) {
if (strcmp(query, stu[i].id) == 0 || strcmp(query, stu[i].clas) == 0) {
sum_score1 += stu[i].score1;
sum_score2 += stu[i].score2;
sum_score3 += stu[i].score3;
sum_score += stu[i].score;
count++;
}
}
if (count == 0) {
printf("no such student\n");
}
else {
printf("average score1: %.1lf\n", sum_score1 / count);
printf("average score2: %.1lf\n", sum_score2 / count);
printf("average score3: %.1lf\n", sum_score3 / count);
printf("average score: %.1lf\n", sum_score / count);
}
printf("continue? ");
char answer[10];
scanf("%s", answer);
if (strcmp(answer, "no") == 0) {
break;
}
}
}
void order_student() {
int i, j;
while (1) {
printf("input 1 to order by id or 2 to order by score\n");
int order_type;
scanf("%d", &order_type);
if (order_type != 1 && order_type != 2) {
printf("invalid order type\n");
continue;
}
for (i = 0; i < stu_num; i++) {
for (j = i + 1; j < stu_num; j++) {
if ((order_type == 1 && strcmp(stu[i].id, stu[j].id) > 0) ||
(order_type == 2 && stu[i].score < stu[j].score)) {
struct Student temp = stu[i];
stu[i] = stu[j];
stu[j] = temp;
}
}
}
for (i = 0; i < stu_num; i++) {
printf("%s,%s,%s,%.1lf,%.1lf,%.1lf,%.1lf\n", stu[i].id, stu[i].clas, stu[i].name, stu[i].score1, stu[i].score2, stu[i].score3, stu[i].score);
}
printf("continue? ");
char answer[10];
scanf("%s", answer);
if (strcmp(answer, "no") == 0) {
break;
}
}
}
void output_student() {
int i;
for (i = 0; i < stu_num; i++) {
printf("%s,%s,%s,%.1lf,%.1lf,%.1lf,%.1lf\n", stu[i].id, stu[i].clas, stu[i].name, stu[i].score1, stu[i].score2, stu[i].score3, stu[i].score);
}
}
int main() {
while (1) {
print_menu();
int option;
scanf("%d", &option);
switch (option) {
case 1:
add_student();
break;
case 2:
delete_student();
break;
case 3:
select_student();
break;
case 4:
order_student();
break;
case 5:
output_student();
break;
case 6:
printf("goodbye!\n");
exit(0);
default:
printf("invalid option\n");
break;
}
}
return 0;
}
Loading…
Cancel
Save