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.

132 lines
3.3 KiB

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Student{
char *id;
int cl;
char *name;
float score1;
float score2;
float score3;
float total_score;
}Student;
void mainmenu(){
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 inputStudentinfo(Student *students,int *size){
printf("Id ");
students[*size].id=(char *)malloc(sizeof(char)*10);
scanf("%s",students[*size].id);
printf("class ");
scanf("%d",&students[*size].cl);
printf("name ");
students[*size].name=(char *)malloc(sizeof(char)*10);
scanf("%s",students[*size].name);
printf("score1 ");
scanf("%f",&students[*size].score1);
printf("score2 ");
scanf("%f",&students[*size].score2);
printf("score3 ");
scanf("%f",&students[*size].score3);
(*size)++;
}
void deleteinfo(Student* students, int* size) {
char id[10];
scanf("%s",id);
int i;
for (i=0;i<*size;++i) {
if (strcmp(students[i].id,id) == 0) {
free(students[i].id);
free(students[i].name);
students[i]=students[--(*size)];
return;
}
}
}
void select(Student* students,int size){
char id[10];
scanf("%s",id);
int i;
for (i=0;i<size;++i){
if(strcmp(students[i].id,id)==0){
printf("%s %d %s %.2f %.2f %.2f\n", students[i].id, students[i].cl, students[i].name, students[i].score1, students[i].score2, students[i].score3);
return;
}
}
}
void sort(Student* students,int size) {
int i,j;
for(i=0;i<size - 1;++i) {
for(j=0;j<size-1-i;++j) {
if(students[j].cl>students[j + 1].cl){
Student temp=students[j];
students[j]=students[j + 1];
students[j + 1]=temp;
}
}
}
}
void output(Student* students,int size){
int i;
for(i=0;i<size;++i) {
printf("%s %d %s %.2f %.2f %.2f\n", students[i].id, students[i].cl, students[i].name, students[i].score1, students[i].score2, students[i].score3);
}
}
int main(){
Student students[100];
int size=0;
int option;
int lastop=-1;
char continueFlag[10];
int runflag=1;
while(runflag){
mainmenu();
if(lastop==-1){
scanf("%d",&option);
}else{
option=lastop;
}
do{
switch (option) {
case 1:
inputStudentinfo(students,&size);
break;
case 2:
deleteinfo(students,&size);
break;
case 3:
select(students,size);
break;
case 4:
sort(students,size);
break;
case 5:
output(students,size);
break;
case 6:
runflag=0;
break;
}
if(runflag){
printf("continue?\n");
scanf("%s", continueFlag);
if(strcmp(continueFlag,"yes")==0){
lastop=option;
}else{
lastop=-1;
}
}
}while(strcmp(continueFlag,"yes")==0&&runflag);
}
return 0;
}