Add task8.cpp

main
pv3e4i5aj 4 days ago
parent 8898d11188
commit b47f76708e

@ -0,0 +1,341 @@
#include <stdio.h>
#include <string.h>
#include <math.h>
#define MARK 200
#define MAX_NUMBER 100
int l=0;//记录结构体现有同学的个数
//定义结构体
struct Student
{
int ID;//学号
int classes;
char name[8];
float math_grade;
float physics_grade;
float English_grade;
float sum;
};
struct Student information[MAX_NUMBER];
void Interation(void);
int pow10(int M)
{
int num=1;
for(int i=1;i<=M;i++)
{
num*=10;
}
return num;
}
//显示主界面
void Show_The_Main_interface(void)
{
for(int i=1;i<=30;i++)
{
printf(" ");
}
printf("1.input\n");
for(int i=1;i<=30;i++)
{
printf(" ");
}
printf("2.delete\n");
for(int i=1;i<=30;i++)
{
printf(" ");
}
printf("3.select\n");
for(int i=1;i<=30;i++)
{
printf(" ");
}
printf("4.order\n");
for(int i=1;i<=30;i++)
{
printf(" ");
}
printf("5.output\n");
for(int i=1;i<=30;i++)
{
printf(" ");
}
printf("6.quit\n");
printf("please input your option\n");
}
//输入第i个同学的信息
void Input_information(struct Student information[MAX_NUMBER],int i)
{
l++;
char remind_information[8];
scanf("%s%d",remind_information,&information[i].ID);
scanf("%s%d",remind_information,&information[i].classes);
scanf("%s%s",remind_information,&information[i].name);
scanf("%s%f",remind_information,&information[i].math_grade);
scanf("%s%f",remind_information,&information[i].physics_grade);
scanf("%s%f",remind_information,&information[i].English_grade);
//计算总分,便于之后的对总成绩的排序
information[i].sum=information[i].math_grade+\
information[i].physics_grade+information[i].English_grade;
printf("continue? \n");
char ch[5];scanf("%s",ch);
if(ch[0]=='y')
{
Input_information(information,i+1);
}
else
{
Interation();
}
}
//对结构体中的从start同学到end同学排序
void Sort(struct Student information[MAX_NUMBER],int start,int end)
{
//先班级
for(int i=start;i<=end;i++)
{
for(int j=start;j<=end-1;j++)
{
if(information[j].classes>information[j+1].classes)
{
struct Student tmp=information[j];
information[j]=information[j+1];
information[j+1]=tmp;
}
}
}
//同班同学再成绩
for(int i=start;i<=end;i++)
{
int con=0;//记录连续了几人
for(int j=1;j<=end-i;j++)
{
if(information[i].classes==information[i+j].classes)
{con=j;
}
else
{
break;
}
}
if(con)//有人是同班同学,在此内排序
{
for(int m=i;m<=i+con;m++)
{
for(int n=i;n<=i+con-1;n++)
{
if(information[n].sum<information[n+1].sum)
{
struct Student tmp=information[n];
information[n]=information[n+1];
information[n+1]=tmp;
}
}
}
}
i=i+con;
}
}
//按格式输出
void output(struct Student information[MAX_NUMBER],int start,int end)
{
//排完序后输出
for(int i=start;i<=end;i++)
{
printf("%d,%d,",information[i].ID,information[i].classes);
for(int j=0;information[i].name[j]!='\0';j++)
{
printf("%c",information[i].name[j]);
}
printf(",%.1f,%.1f,%.1f,%.1f\n",information[i].math_grade,\
information[i].physics_grade,information[i].English_grade,information[i].sum);
}
}
//根据学号或者姓名对结构体中的从start同学到end同学,在这里面查找同学
int original_location(char delete_student[MAX_NUMBER],struct Student information[MAX_NUMBER],int start,int end)
{
int mark=MARK;//标记学生的初始位置 ,初始化为MARK方便区分
if(delete_student[0]<='9'&&delete_student[0]>='0')
{
int num=0;
for(int i=0;i<strlen(delete_student);i++)
{
num+=(delete_student[i]-'0')*pow10(strlen(delete_student)-i-1);
}
for(int i=0;i<=2;i++)
{
if(num==information[i].ID)
{
mark=i;
break;
}
}
}
else//输入了字符串
{
for(int i=0;i<=2;i++)
{
if(strcmp(delete_student,information[i].name)==0)
{
mark=i;
break;
}
}
}
return mark;
}
void Delete_information(struct Student information[MAX_NUMBER])
{
//输入被删除同学的信息
char delete_student[8];
scanf("%s", delete_student);
//判断同学是否在系统里面
if(original_location(delete_student,information,0,l-1)==MARK)
{//说明同学不在系统里面
//Sort(information,0,l-1);
output(information,0,l-1);
}
else
{
struct Student left[MAX_NUMBER];//left作为一个结构体的中间存储点
int j=0;
for(int i=0;i<=l-1;i++)
{
if(i!=original_location(delete_student,information,0,l-1))
{
left[j]=information[i];//left比 information少一个人
j++;
}
}
l=l-1;
for(int i=0;i<=l-1;i++)
{
information[i]=left[i];//对information更新人数比原来少一一个
}
//Sort(information,0,l-1);
output(information,0,l-1);
}
printf("continue? \n");
char ch[5];scanf("%s",ch);
if(ch[0]=='y')
{
if(l>1)
{
Delete_information(information);
}
else
{
printf("请至少保留一个学生信息\n");
Interation();
}
}
else
{
Interation();
}
}
void Select_information(struct Student information[MAX_NUMBER])
{
//输入待查找同学的信息
int select_student;
scanf("%d", &select_student);
struct Student select_information[MAX_NUMBER];//存放所有找到的同学信息
int j=0;
int sign=0;//标记同学是否在系统里面
for(int i=0;i<l;i++)
{
if(information[i].ID==select_student||information[i].classes==select_student)
{
sign=1;
select_information[j]=information[i];
j++;
}
}
if(sign)
{
//Sort(select_information,0,j-1);
output(select_information,0,j-1);
printf("continue? \n");
char ch[5];scanf("%s",ch);
if(ch[0]=='y')
{
Select_information(information);
}
else
{
Interation();
}
}
else
{
printf("there is no eligible student\n");
Interation();
}
}
//用户与系统交互
void Interation(void)
{
Show_The_Main_interface();
int choice=0;scanf("%d",&choice);
if(choice==1)//进行输入操作
{
Input_information(information,l);
}
else if(choice==2)
{
Delete_information(information);
}
else if(choice==3)
{
Select_information(information);
}
else if(choice==4)
{
if(l>=3)
{
Sort(information,0,l-1);
output(information,0,l-1);
}
else
{
printf("学生人数小于3请输入学生信息后再来排序\n");
}
Interation();
}
else if(choice==5)
{
output(information,0,l-1);
Interation();
}
else if(choice==6)
{
}
}
int main()
{
Interation();
}
Loading…
Cancel
Save