C语言之学生成绩表

练习:班主任需要在计算机中录入n个同学的成绩信息,信息包含(学号、姓名、性别、总成绩、语文成绩、英语成绩、数学成绩)。

程序在编写时有如下要求:

1.  n由班主任确定,且使用malloc申请内存;

2. 学号以201805xx形式,姓名为英文名,语文成绩、英语成绩、数学成绩由班主任输入,总成绩需要由程序计算;

程序在运行时打印提示功能信息,程序需要有如下功能:

1. 按学号升序;

2. 按总成绩降序;

3. 输入学生姓名,查询学生信息,且程序具有检查不合法姓名功能


环境:GCC,代码如下:

#include <stdio.h>
#include <stdlib.h>


typedef struct Stu
{
int num;
char name[64];
char sex;
float total_score;
float chinsese_score;
float english_score;
float math_score;
}student;


int input_student_info(student *pstu, int n);
int print_student_info(student *pstu, int n);
int student_num_ascend(student *pstu, int n);
int score_total_descend(student *pstu, int n);
int query_studnet_info(char *stu_name, student *pstu, int n);


int main(int argc, const char *argv[])
{
int num, fun, quit;
student *pstu = NULL;
char query_name[64] = {0};


printf("Input the number of student!\n");
scanf("%d",&num);
pstu = (student *)malloc(sizeof(student) * num);
if(pstu == NULL)
{
printf("Malloc %d students mem fail!\n",num);
return 1;
}
input_student_info(pstu, num);
do
{
printf("\n");
printf("Which func do you want?\n");
printf("1:student num ascend print\n");
printf("2:stduent total score descend print\n");
printf("3:query single stduent infomation by name!\n");
printf("4:quit\n");
scanf("%d",&fun);


switch (fun)
{
case (1):
student_num_ascend(pstu, num);
print_student_info(pstu, num);
break;
case (2):
score_total_descend(pstu,num);
print_student_info(pstu, num);
break;
case (3):
printf("Input student name!\n");
scanf("%s",query_name);
query_studnet_info(query_name, pstu, num);
break;
case (4):
quit = 1;
break;
default:
printf("Input error!\n");
}


}while(quit == 0);


free(pstu);
pstu = NULL;


return 0;
}




int input_student_info(student *pstu, int n)
{
int i;


for(i = 0; i< n; i++)
{
printf("\n");
printf("Input %d student infomation as follow! \n",i+1);
printf("schoool_number name chinsese_score english_score math_score!\n");
scanf("%d %s %f %f %f",&(pstu+i)->num, (pstu+i)->name, &(pstu+i)->chinsese_score, &(pstu+i)->english_score, &(pstu+i)->math_score);
(pstu+i)->total_score = (pstu+i)->chinsese_score + (pstu+i)->english_score + (pstu+i)->math_score;
}


return 0;
}


int print_student_info(student *pstu, int n)
{
int i;


printf("\n");
printf("schoool_number name total_score chinsese_score english_score math_score!\n");
for(i = 0; i< n; i++)
{
printf("%d %s %f %f %f %f\n",(pstu+i)->num, (pstu+i)->name, (pstu+i)->total_score, (pstu+i)->chinsese_score, (pstu+i)->english_score, (pstu+i)->math_score);
}


return 0;
}
int student_num_ascend(student *pstu, int n)
{
int i, j;
student temp;
printf("\nstudent num ascend!\n");
for(i = n-1; i > 0; i--)
{
for(j = 0; j < i; j++)
{
if((pstu+j)->num > (pstu+j+1)->num)
{
temp = *(pstu+j);
*(pstu+j) = *(pstu+j+1);
*(pstu+j+1) = temp;
}
}
}


return 0;
}
int score_total_descend(student *pstu, int n)
{
int i, j;
student temp;


printf("\ntotal score descend!\n");
for(i = n-1; i > 0; i--)
{
for(j = 0; j < i; j++)
{
if((pstu+j)->total_score < (pstu+j+1)->total_score)
{
temp = *(pstu+j);
*(pstu+j) = *(pstu+j+1);
*(pstu+j+1) = temp;
}
}
}


return 0;
}
int query_studnet_info(char *stu_name, student *pstu, int n)
{
int i;


printf("\nquery single stduent infomation!\n");
for(i = 0; i< n; i++)
{
if(strcmp(stu_name,(pstu+i)->name) == 0)
{
printf("schoool_number name total_score chinsese_score english_score math_score!\n");
printf("%d %s %f %f %f %f\n",(pstu+i)->num, (pstu+i)->name, (pstu+i)->total_score, (pstu+i)->chinsese_score, (pstu+i)->english_score, (pstu+i)->math_score);
}
}


return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42048417/article/details/80204691