问题描述:大程序构成-多文件模块的学生信息库系统
源代码:
student.h:
#include<stdio.h>
#include<string.h>
#define MaxSize 50
struct student{/*学生信息结构定义*/
int num;
char name[10];
int computer,english,math;
double average;
};
student_system:
#include "student.h"
#include "input_output.c"
#include "aver_sort.c"
#include "modify.c"
int Count=0;/*全局变量,记录当前学生总数*/
int main(void)
{
struct student students[MaxSize];/*定义学生信息结构数组*/
new_student(students);/*输入学生信息结构数组*/
average(students);/*计算每个学生的平均成绩*/
printf("显示输入的学生信息结构数组:\n");
output_student(students);/*显示输入的学生信息结构数组*/
sort(students);/*按学生的平均成绩排序*/
printf("显示排序后的学生信息结构数组:\n");
output_student(students);/*显示排序后的结构数组*/
modify(students);/*修改指定输入的学生信息*/
printf("显示修改后的学生信息结构数组:\n");
output_student(students);/*显示修改后的结构数组*/
return 0;
}
input_output.c:
extern Count; /*其他文件模块声明的全局变量,需要声明为外部变量*/
/*新建学生信息*/
void new_student(struct student students[])
{
int i,n;
if(Count==MaxSize){
printf("The array is full!\n");
return;
}
printf("Input the Number of Students:");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("Input the No.%d student's num:",i+1);
scanf("%d",&students[i].num);
printf("Input the No.%d student's name:",i+1);
scanf("%s",students[i].name);
printf("Input the No.%d student's math score:",i+1);
scanf("%d",&students[i].math);
printf("Input the No.%d student's english score:",i+1);
scanf("%d",&students[i].english);
printf("Input the No.%d student's computer score:",i+1);
scanf("%d",&students[i].computer);
Count++;
}
}
/*输出学生信息*/
void output_student(struct student students[])
{
int i;
if(Count==0){
printf("Count of students is zero!\n");
return;
}
printf("num\tname\tmath\tenglish\tcomputer\taverage\n ");
for(i=0;i<=Count;i++){
printf("%d\t",students[i].num);
printf("%s\t",students[i].name);
printf("%d\t",students[i].math);
printf("%d\t",students[i].english);
printf("%d\t",students[i].computer);
printf("%.2f\t\n",students[i].average);
}
}
aver_sort.c:
extern Count; /*其他文件模块声明的全局变量,需要声明为外部变量*/
/*计算个人平均成绩的函数*/
void average(struct student s[]){
int i;
for(i=0;i<Count;i++)
s[i].average=(s[i].math+s[i].english+s[i].computer)/3.0;
}
/*平均成绩排序的函数*/
void sort(struct student s[])
{
struct student temp;
int i,index,j;
for(i=0;i<Count-1;++i){
index=i;
for(j=i+1;j<Count;j++)
if(s[j].average>s[index].average);
index=j;
temp=s[index];
s[index]=s[i];
s[i]=temp;
}
}
modify.c:
extern Count; /*其他文件模块声明的全局变量,需要声明为外部变量*/
void modify(struct student*p)
{
int num,course,score,i;
printf("Input the number of the students to be update:");
scanf("%d",&num);
printf("Choice the course:1-->math;2-->english;3-->computer:");
scanf("%d",&course);
printf("Input the new score:");
scanf("%d",&score);
for(i=0;i<Count;i++,p++)
if(p->num==num) /*按学号查找*/
break;
if(i<Count) /*找到,修改成绩*/
switch(course){
case 1: p-> math=score;break;
case 2: p-> english=score;break;
case 3: p-> computer=score;break;
}
}
/*查询学生信息的函数*/
void search_student(struct student students[],int num)
{
int i,flag=0;
if(Count==0){
printf("Count of students is zero!\n");
return;
}
for(i=0;i<Count;i++)
if(students[i].num==num){
flag=1;
break;
}
if(flag){
printf("num\tname\tmath\tenglish\tcomputer\taverage\n ");
printf("%d \t %s \t %d \t %d \t %d \t %d \t\n",students[i].num,students[i].name,students[i].math,students[i].english,students[i].computer,students[i].average);
}
else
{
printf("Not found!\n");
}
}
运行结果:
文件组成 运行结果程序参数:
- 输出大小: 154.7060546875 KiB
- 编译时间: 0.31s
网友评论