📄 struct.c
字号:
#include<stdio.h>
#include<stdlib.h>
#define N 30
typedef struct student /*定义储存学生成绩信息的数组*/
{
char *name;
int chinese;
int maths;
int phy;
int total;
}ST;
main()
{
ST a[N]; /*存储N个学生信息的数组*/
FILE *fp;
void (*process[3])(ST *)={Output,Bubble,Find}; /*实现相关功能的三个函数*/
int choice,i=0;
Show();
printf("\nChoose:\n?");
scanf("%d",&choice);
while(choice>=0&&choice<=2)
{
fp=fopen("aa.dat","rb");
for(i=0;i<N;i++)
fread(&a[i],sizeof(ST),1,fp); /*把文件中储存的信息逐个读到数组中去*/
fclose(fp);
(*process[choice])(a); /*前面提到的指向函数的指针,选择操作*/
printf("\n");
Show();
printf("\n?");
scanf("%d",&choice);
}
}
void Show()
{
printf("\n****Choices:****\n0.Display the data form\n1.Bubble it according to the total score\n2.Search\n3.Quit!\n");
}
void Output(ST *a) /*将文件中存储的信息逐个输出*/
{
int i,t=0;
printf("Name Chinese Maths Physics Total\n");
for(i=0;i<N;i++)
{
t=a[i].chinese+a[i].maths+a[i].phy;
a[i].total=t;
printf("%4s%8d%8d%8d%8d\n",a[i].name,a[i].chinese,a[i].maths,a[i].phy,a[i].total);
}
}
void Bubble(ST *a) /*对数组进行排序,并输出结果*/
{
int i,pass;
ST m;
for(pass=0;pass<N-1;pass++)
for(i=0;i<N-1;i++)
if(a[i].total<a[i+1].total)
{
m=a[i]; /*结构互换*/
a[i]=a[i+1];
a[i+1]=m;
}
Output(a);
}
void Find(ST *a)
{
int i,t=1;
char m[20];
printf("\nEnter the name you want:");
scanf("%s",m);
for(i=0;i<N;i++)
if(!strcmp(m,a[i].name)) /*根据姓名匹配情况输出查找结果*/
{
printf("\nThe result is:\n%s, Chinese:%d, Maths:%d, Physics:%d,Total:%d\n",m,a[i].chinese,a[i].maths,a[i].phy,a[i].total);
t=0;
}
if(t)
printf("\nThe name is not in the list!\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -