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;
}
總結
- 上一篇: 学通信工程兼修计算机,创名堂 | 第二期
- 下一篇: 可汗学院为什么选择Go