c语言_输入、输出、排序、打印学生成绩单

c语言_输入、输出、排序、打印学生成绩单
c语言_输入、输出、排序、打印学生成绩单

#include

#include

#include

//确定最大值

#define N 30

typedef struct student

{

long id;

char name[10];

float score[4];

}STUDENT;

STUDENT stu[N];

void addc(struct student *pt, int n);

void adds(struct student *pt, int n);

void pxs(struct student *pt, int (*comp)(float a, float b), int n);

int ds(float a, float b);

int as(float a, float b);

void pxi(struct student *pt, int n);

void pxn(struct student *pt, int n);

void seai(struct student *pt, int n);

void sean(struct student *pt, int n);

void ana(struct student *pt, int n);

void wri(STUDENT *pt, int n);

void rea(STUDENT *pt, int n);

//主函数

int main()

{

int n, con = 14, i, j;

printf("Input student number(n<30):");

scanf("%d",&n);

if(n>N || n < 1)

return 0;

else

{

//主程序

while(con != 0)

{

//输出

printf("\nManagement for Students' scores\n");

printf("1.Append record\n");

printf("2.Calculate total and average score of every course\n");

printf("3.Calculate total and average score of every student\n");

printf("4.Sort in descending order by total score of every student\n");

printf("5.Sort in ascending order by total score of every student\n");

printf("6.Sort in ascending order by number\n");

printf("7.Sort in dictionary order by name\n");

printf("8.Search by number\n");

printf("9.Search by name\n");

printf("10.Statistic analysis\n");

printf("11.List record\n");

printf("12.Write to a file\n");

printf("13.Read from a file\n");

printf("0.Exit\n");

// 录入学生的人数:

printf("Please Input your choice:");

scanf("%d",&con);

// 录入每个学生的学号、姓名和各科成绩:

if(con == 1)

{

printf("Input student's ID,name and score:\n");

for(i = 0; i < n; i++)

{

scanf("%ld", &stu[i].id);

getchar();

fgets(stu[i].name, sizeof(stu[i].name), stdin);///有问题!!!

scanf("%f %f %f",&stu[i].score[0], &stu[i].score[1], &stu[i].score[2]);

}

}

// 计算每门课程的总分和平均分:

else if(con == 2)

{

addc(stu, n);

}

// 计算每个学生各门课程的总分和平均分:

else if(con == 3)

{

adds(stu, n);

}

// 按总成绩由高到低排出名次表:

else if(con == 4)

{

printf("Sort in descending order by total score of every student:\n");

printf("NO\tName\tMT\tEN\tPH\tSUM\tA VER\n");

pxs(stu, ds, n);

}

// 按总成绩由低到高排出名次表:

else if(con == 5)

{

printf("Sort in ascending order by total score of every student:\n");

printf("NO\tName\tMT\tEN\tPH\tSUM\tA VER\n");

pxs(stu, as, n);

}

// 按学号由小到大排出成绩表:

else if(con == 6)

{

printf("Sort in ascending order by number:\n");

printf("NO\tName\tMT\tEN\tPH\tSUM\tA VER\n");

pxi(stu, n);

}

// 按姓名的字典顺序排出成绩表:

else if(con == 7)

{

printf("Sort in dictionary order by name:\n");

printf("NO\tName\tMT\tEN\tPH\tSUM\tA VER\n");

pxn(stu, n);

}

// 按学号查询学生排名、学号、姓名及其考试成绩:

else if(con == 8)

{

seai(stu, n);

}

// 按姓名查询学生排名、学号、姓名及其考试成绩:

else if(con == 9)

{

sean(stu, n);

}

// 按100,(90~100)(80~89)(70~79)(60~69)(0~59)统计各科每个类别的人数以及所占的百分比:

else if(con == 10)

{

ana(stu, n);

}

//输出信息

else if(con == 11)

{

printf("NO\tName\tMT\tEN\tPH\tSUM\tA VER\n");

for(i=0;i

{

printf("%ld\t", stu[i].id);

for(j=0;stu[i].name[j]!='\n';j++)

{

putchar(stu[i].name[j]);

}

printf("\t%.0f\t%.0f\t%.0f\t%.0f\t%.0f\n", stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].score[3], (stu[i].score[3]/3));

}

}

//写入文件

else if(con == 12)

{

wri(stu, n);

}

//读取文件

else if(con == 13)

{

rea(stu, n);

}

else if(con == 0)

{

printf("End of program!\n");

}

else

{

printf("Input error!\n");

}

}

}

return 0;

}

void addc(struct student *pt, int n)

{

int i, j;

float sum;

for(j=0;j<3;j++)

{

sum = 0;

for(i=0;i<3;i++)

{

sum += stu[i].score[j];

}

printf("course %d:sum=%.0f,aver=%.0f\n", (j+1), sum, (sum/n));

}

void adds(struct student *pt, int n)

{

int i;

for(i=0;i

{

stu[i].score[3] = stu[i].score[0] + stu[i].score[1] + stu[i].score[2];

printf("student %d:sum=%.0f,aver=%.0f\n", (i+1), stu[i].score[3], (stu[i].score[3]/3));

}

}

void pxs(struct student *pt, int (*comp)(float a, float b), int n)

{

int i, j;

struct student temp;

for(i=0;i

{

for(j=0;j<(n-i-1);j++)

{

if((*comp)(stu[j].score[3], stu[j+1].score[3]))

{

temp = stu[j];

stu[j] = stu[j+1];

stu[j+1] = temp;

}

}

}

for(i=0;i

{

printf("%ld\t", stu[i].id);

for(j=0;stu[i].name[j]!='\n';j++)

{

putchar(stu[i].name[j]);

}

printf("\t%.0f\t%.0f\t%.0f\t%.0f\t%.0f\n", stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].score[3], (stu[i].score[3]/3));

}

}

int ds(float a, float b)

{

return a

}

int as(float a, float b)

{

return a>b;

}

void pxi(struct student *pt, int n)

{

int i, j;

struct student temp;

for(i=0;i

{

for(j=0;j<(n-i-1);j++)

{

if(stu[j].id > stu[j+1].id)

{

temp = stu[j];

stu[j] = stu[j+1];

stu[j+1] = temp;

}

}

}

for(i=0;i

{

printf("%ld\t", stu[i].id);

for(j=0;stu[i].name[j]!='\n';j++)

{

putchar(stu[i].name[j]);

}

printf("\t%.0f\t%.0f\t%.0f\t%.0f\t%.0f\n", stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].score[3], (stu[i].score[3]/3));

}

}

void pxn(struct student *pt, int n)

{

int i, j;

struct student temp;

for(i=0;i

{

for(j=0;j<(n-i-1);j++)

{

if(strcmp(stu[j].name,stu[j+1].name)>0)

{

temp = stu[j];

stu[j] = stu[j+1];

stu[j+1] = temp;

}

}

}

for(i=0;i

{

printf("%ld\t", stu[i].id);

for(j=0;stu[i].name[j]!='\n';j++)

{

putchar(stu[i].name[j]);

}

printf("\t%.0f\t%.0f\t%.0f\t%.0f\t%.0f\n", stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].score[3], (stu[i].score[3]/3));

}

}

void seai(struct student *pt, int n)

{

int i, j, con=0;

long id;

struct student temp;

printf("Input the number you want to search:");

scanf("%ld", &id);

for(i=0;i

{

for(j=0;j<(n-i-1);j++)

{

if(stu[j].score[3]

{

temp = stu[j];

stu[j] = stu[j+1];

stu[j+1] = temp;

}

}

}

for(i=0;i

{

if(stu[i].id==id)

{

printf("%d\t%ld\t", (i+1), stu[i].id);

for(j=0;stu[i].name[j]!='\n';j++)

{

putchar(stu[i].name[j]);

}

printf("\t%.0f\t%.0f\t%.0f\t%.0f\t%.0f\n", stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].score[3], (stu[i].score[3]/3));

con=1;

break;

}

}

if(con==0)

printf("Not found!\n");

}

void sean(struct student *pt, int n)

{

int i, j, con=0;

char name[10];

struct student temp;

printf("Input the name you want to search:");

getchar();

fgets(name, sizeof(name), stdin);///有问题!!!!

for(i=0;i

{

for(j=0;j<(n-i-1);j++)

{

if(stu[j].score[3]

{

temp = stu[j];

stu[j] = stu[j+1];

stu[j+1] = temp;

}

}

}

for(i=0;i

{

if(strcmp(stu[i].name, name)==0)

{

printf("%d\t%ld\t", (i+1), stu[i].id);

for(j=0;stu[i].name[j]!='\n';j++)

{

putchar(stu[i].name[j]);

}

printf("\t%.0f\t%.0f\t%.0f\t%.0f\t%.0f\n", stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].score[3], (stu[i].score[3]/3));

con=1;

break;

}

}

if(con==0)

printf("Not found!\n");

}

void ana(struct student *pt, int n)

{

int i, j, n1, n2, n3, n4, n5, n6;

for(j=0;j<3;j++)

{

n1=0;n2=0;n3=0;n4=0;n5=0;n6=0;

for(i=0;i

{

if(stu[i].score[j]<60)

n1+=1;

else if(stu[i].score[j]<70)

n2+=1;

else if(stu[i].score[j]<80)

n3+=1;

else if(stu[i].score[j]<90)

n4+=1;

else if(stu[i].score[j]<=100)

{

n5+=1;

if(stu[i].score[j]==100)

n6+=1;

}

}

printf("For course %d:\n", (j+1));

printf("<60\t%d\t%.2f%%\n", n1, 100*(float)n1/n);

printf("%d-%d\t%d\t%.2f%%\n", 60, 69, n2, 100*(float)n2/n);

printf("%d-%d\t%d\t%.2f%%\n", 70, 79, n3, 100*(float)n3/n);

printf("%d-%d\t%d\t%.2f%%\n", 80, 89, n4, 100*(float)n4/n);

printf("%d-%d\t%d\t%.2f%%\n", 90, 100, n5, 100*(float)n5/n);

printf("%d\t%d\t%.2f%%\n", 100, n6, 100*(float)n6/n);

}

}

void wri(STUDENT *pt, int n)

{

FILE *fp;

int i, j;

struct student temp;

for(i=0;i

{

for(j=0;j<(n-i-1);j++)

{

if(stu[j].id > stu[j+1].id)

{

temp = stu[j];

stu[j] = stu[j+1];

stu[j+1] = temp;

}

}

}

if((fp = fopen("student.txt", "w")) == NULL)

exit(0);

//fprintf(fp, "NO\tName\tMT\tEN\tPH\tSUM\tA VER\n");

for(i=0;i

{

fprintf(fp, "%ld\t", stu[i].id);

for(j=0;stu[i].name[j]!='\n';j++)

{

fprintf(fp, "%c", stu[i].name[j]);

}

fprintf(fp, "\t%.0f\t%.0f\t%.0f\t%.0f\t%.0f\n", stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].score[3], (stu[i].score[3]/3));

}

fclose(fp);

printf("save successfully!");

}

void rea(STUDENT *pt, int n)

{

FILE *fp;

int m;

long a;

float c,d,e,f,g;

char b[10];

//char head[50];

if((fp = fopen("student.txt", "r")) == NULL)

exit(0);

//fgets(head, 50, fp);

//puts(head);

printf("NO\tName\tMT\tEN\tPH\tSUM\tA VER\n");

for(m = 0; m

{

//printf("scjdbfdsdj");

fscanf(fp, "%ld", &a);

printf("%ld\t", a);

fscanf(fp, "%10s", b);

printf("%s\t", b);

fscanf(fp, " %f", &c);

printf("%.0f\t", c);

fscanf(fp, "%f", &d);

printf("%.0f\t", d);

fscanf(fp, "%f", &e);

printf("%.0f\t", e);

fscanf(fp, "%f", &f);

printf("%.0f\t", f);

fscanf(fp, "%f", &g);

printf("%.0f\n", g);

//printf("jdhergjdkfnjherdgfdhb\n");

}

fclose(fp);

}

相关主题
相关文档
最新文档