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.

165 lines
3.4 KiB

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Student
{
char id[20];
char clas[20];
char name[20];
double score1;
double score2;
double score3;
double score;
struct Student* next;
}student;//
student* head, * tail;
int total;
void ins(student t)
{
tail = tail->next=(student*)malloc(sizeof(student));
(*tail) = t;
tail->next = NULL;
total++;
}
// 删除学生信息
void del(student* u)
{
student* v = u->next;
u->next = v->next;
if (v == tail)
{
tail = u;
}
free(v);
total--;
}
// 输入学生信息
void input()
{
student t;
printf("Id ");
scanf("%s", t.id);
printf("class ");
scanf("%s", t.clas);
printf("name ");
scanf("%s", t.name);
printf("score1 ");
scanf("%lf", &t.score1);
printf("score2 ");
scanf("%lf", &t.score2);
printf("score3 ");
scanf("%lf", &t.score3);
t.score=t.score1+t.score2+t.score3;
ins(t);
}
// 输出学生信息
void output()
{
for (student* u = head;u->next != NULL;u = u->next) {
student* v = u->next;
printf("%s,%s,%s,%.1lf,%.1lf,%.1lf,%.1lf\n", v->id, v->clas, v->name,
v->score1, v->score2, v->score3, v->score);
}
}
// 删除指定学生信息
void cut()
{
char s[10];
scanf("%s",s);
for (student* u = head;u->next != NULL;) {
student* v = u->next;
if (strcmp(s, v->id) == 0 || strcmp(s, v->name) == 0)del(u);
else u = u->next;
}
output();
}
// 查询指定学生信息
int selec(int flag)
{
char s[10];
scanf("%s", s);
for (student* u=head;u->next != NULL;u = u->next) {
student* v = u->next;
if (strcmp(s, v->clas) == 0 || strcmp(s, v->id) == 0)
{
flag = 1;
printf("%s,%s,%s,%.1lf,%.1lf,%.1lf,%.1lf\n", v->id, v->clas, v->name,
v->score1, v->score2, v->score3, v->score);
}
}
if (flag == 0)
printf("there is no eligible student\n");
return flag;
}
void sort(student* st, student* ed)
{
student t;
for (student* i=st;i !=ed;i++)
for (student* j = ed-1;j !=i;j--)
{
int x=strcmp(j->clas, (j - 1)->clas);
if (x<0||x == 0 && j->score > (j - 1)->score) {
t=*j;
*j = *(j - 1);
*(j - 1)=t;
}
}
}
void order()
{
student* students = (student*)malloc(sizeof(student) * total);
int n = 0;
while (head->next != NULL) {
students[n++] = *(head->next);
del(head);
}
sort(students, students + n);
for (int i = 0;i < n;i++)
ins(students[i]);
free(students);
output();
}
int main()
{
int flag = 0;
tail = head = (student*)malloc(sizeof(student));
head->next = NULL;
total = 0;
while (1) {
printf("1.input\n"
"2.delete\n"
"3.select\n"
"4.order\n"
"5.output\n"
"6.quit\n"
"please input your option\n");
char option[10];
while (1) {
scanf("%s", option);
if (strlen(option) == 1 && option[0] >= '1' && option[0] <= '6')break;
printf("wrong input\n");
}
if (option[0] == '6')break;
while (1) {
if (option[0] == '1')input();
else if (option[0] == '2')cut();
else if (option[0] == '3')selec(flag);
else if (option[0] == '4')order();
else output();
if (option[0] == '4' || option[0] == '5')break;
if (option[0] == '3' &&flag==0)break;
printf("continue?\n");
char buf[10];
while (1) {
scanf("%s", buf);
if (strcmp(buf, "yes") == 0 || strcmp(buf, "no") == 0)break;
printf("wrong input\n");
}
if (strcmp(buf, "no") == 0)break;
}
}
return 0;
}