//a.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CSIZE 4
struct name
{
char fname[30];
char lname[30];
};
struct student
{
struct name sname;
float grade[3];
float average;
};
void getGrade(struct student a[])
{
int n = 0, flag = 1;
char f[30], l[30];
int i;
while(flag)
{
printf("Input the name:");
scanf("%s %s", f, l);
for (i = 0; i < CSIZE; ++i)
{
if(!strcmp(a[i].sname.fname, f) &&
!strcmp(a[i].sname.lname, l))
{
if(a[i].grade[0] != 0)
{
printf("This student has been registered\n");
break;
}
printf("Input 3 grade for %s%s:", f, l);
scanf("%f %f %f",
&a[i].grade[0],
&a[i].grade[1],
&a[i].grade[2]);
n++;
if(n == CSIZE)
{
flag = 0;
}
break;
}
}
if(i == CSIZE)
{
printf("Student %s%s is not found\n", f, l);
}
}
}
void getAverage(struct student a[])
{
for (int i = 0; i < CSIZE; ++i) {
a[i].average = (a[i].grade[0] + a[i].grade[1] + a[i].grade[2]) / 3;
}
}
void Print(struct student a[])
{
for (int i = 0; i < CSIZE; ++i) {
printf("%s%s : %5.2f %5.2f %5.2f, average = %5.2f\n", a[i].sname.fname,
a[i].sname.lname, a[i].grade[0], a[i].grade[1], a[i].grade[2], a[i].average);
}
}
void APrint(struct student a[])
{
float sum = 0.0;
for (int i = 0; i < CSIZE; ++i) {
sum += a[i].average;
}
printf("Class average is %.2f\n", sum / 4);
}
int main(void)
{
struct student stu[CSIZE] = {
{.sname = {"1", "2"}, .grade = {0.0, 0.0, 0.0}},
{.sname = {"3", "4"}, .grade = {0.0, 0.0, 0.0}},
{.sname = {"5", "6"}, .grade = {0.0, 0.0, 0.0}},
{.sname = {"7", "8"}, .grade = {0.0, 0.0, 0.0}}
};
getGrade(stu);
getAverage(stu);
Print(stu);
APrint(stu);
return 0;
}
这条代码可以运行 , 也可以正常输入输出。
//b.c
#include <stdio.h>
#include "stdlib.h"
#include <string.h>
#include <ctype.h>
#include "practiceit.h"
#include <math.h>
int main(void)
{
//全部初始化,不然不能把整个Student传进去。
Student student[CSIZE] ={
{{"a", "A"} ,
{0.0,0.0 , 0.0},
0.0},
{{"b", "B"},
{0.0,0.0 , 0.0},
0.0},
{{"c", "C"},
{0.0,0.0 , 0.0},
0.0},
{{"d", "D"},
{0.0,0.0 , 0.0},
0.0}
};
Getgread(student);
doave(student);
showit(student);
showave(student);
return 0;
}
//practice.c
#include <stdio.h>
#include "stdlib.h"
#include <string.h>
#include <ctype.h>
#include "practiceit.h"
#include <math.h>
void Getgread(Student arr[])//输入数据
{
int i;
int num = 0 ;
int flag = 1;
char last[10];
char first[10];
while(flag)
{
printf("Enter lname and fname!\n");
scanf_s("%s %s" , last , first);
for(i = 0 ; i < CSIZE ; i++)
{
if(!strcmp(arr[i].idcard.lname , last) &&
!strcmp(arr[i].idcard.fname , first))//定位
{
if(arr[i].grade.Chinese != 0)
{
printf("again!\n");
break;
}
puts("Enter your grade!\n");
scanf_s("%f %f %f" ,
&arr[i].grade.Chinese,
&arr[i].grade.Math,
&arr[i].grade.English);
num++;
if(num == 4)
{
flag = 0;
}
break;
}
}
if(i == CSIZE)//四次没找到
printf("NO him!\n");
}
}
void doave(Student arr[])//算平均分
{
//float total = 0.0 ;
for (int i = 0; i < CSIZE; ++i)
{
arr[i].avergae =
( arr[i].grade.Chinese +
arr[i].grade.Math +
arr[i].grade.English) / 3;
}
}
void showit(Student arr[])//展示所有数据
{
for (int i = 0; i < CSIZE; ++i)
{
printf("%s%s : %5.2f %5.2f %5.2f, average = %5.2f\n",
arr[i].idcard.lname , arr[i].idcard.fname ,
arr[i].grade.Chinese ,
arr[i].grade.Math ,
arr[i].grade.English,
arr[i].avergae);
}
}
void showave(Student arr[])//展示总平均分
{
float total = 0.0;
for (int i = 0; i < CSIZE; ++i)
{
total += arr[i].avergae;
}
printf("%5.2f" , total / 4);
}
//practice.h
#define CSIZE 4
typedef struct {
char lname[10];
char fname[10];
}Idcard;
typedef struct {
float Chinese;
float Math;
float English;
}Grade;
typedef struct {
Idcard idcard ;
Grade grade;
float avergae;
}Student;
void Getgread(Student arr[]);//输入数据
void doave(Student arr[]);//算平均分
void showit(Student arr[]);//展示所有数据
void showave(Student arr[]);//展示总平均分
#endif //PRACTICE_PRACTICEIT_H
这三条代码是同一个程序的,只不过想把a.c这个程序换成多文件形式。但是多文件那个程序就可以正常运行,但是不能正常输入输出,看了好久都没看出哪里有问题T T。想知道多文件那条程序到底哪里有bug?