## 将主函数内容分到子函数中

krole 发布于 2015/03/31 08:42

#include<stdio.h>
typedef struct student
{
char name[10];
char id[10];
int scord1;
int scord2;
double scord3;
}student;
int main()
{
FILE *fp=NULL, *ftp=NULL;
int i = 0, a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0;
double average = 0;
student s[28];
if (fp == 0)
{
printf("error");
exit(1);
}
for (i = 0; i < 28; i++)
{
fscanf(fp, "%s%s%d%d", s[i].name, s[i].id, &s[i].scord1, &s[i].scord2);
s[i].scord3 = 0.3*s[i].scord1 + 0.7*s[i].scord2;
average += s[i].scord3;
}
fclose(fp);
ftp = fopen("e:\\write.txt", "w");
if (ftp == 0)
{
printf("error");
exit(1);
}
for (i = 0; i < 28; i++)
{
fprintf(ftp, "%-10s%-10s%-4d%-4d%-4.2f\n", s[i].name, s[i].id, s[i].scord1, s[i].scord2,     s[i].scord3);
}
for (i = 0; i < 28; i++)
{
if (s[i].scord3 >= 90)
a++;
if (s[i].scord3 >= 80 && s[i].scord3 < 90)
b++;
if (s[i].scord3 >= 70 && s[i].scord3 < 80)
c++;
if (s[i].scord3 >= 60 && s[i].scord3 < 70)
d++;
if (s[i].scord3 >= 50 && s[i].scord3 < 60)
e++;
if (s[i].scord3 >= 40 && s[i].scord3 < 50)
f++;
if (s[i].scord3 < 40)
g++;
}
fprintf(ftp, "总 90分以上 %d人 %.2lf%% 应考 28\n成 80--89分 %d人 %.2lf%% 实考 28\n绩 70--79分    %d人 %.2lf%% 缓考 0分\n 60--69分    %d人 %.2lf%% 作弊    0\n析 50--59分    %d人 %.2lf%% 旷考 0\n40--49分 %d人 %.2lf%% 免考 0\n40分以下 %d人 %.2lf%% 平均分 %.2lf", a, (double)100 * a / 28, b, (double)100 * b / 28, c, (double)100 * c / 28, d, (double)100 * d / 28, e, (double)100 * e / 28, f, (double)100 * f / 28, g, (double)100 * g / 28, average / 28);
fclose(ftp);
return 0;
}

#include<stdio.h>
typedef struct student
{
char name[10];
char id[10];
int scord1;
int scord2;
double scord3;
}student;
FILE *fp=NULL, *ftp=NULL;
int i, a=0, b=0, c=0, d=0, e=0, f=0,g=0;
double average=0;
student s[28];
void aa()
{
for (i = 0; i < 28; i++)
{
fscanf(fp, "%s%s%d%d", s[i].name, s[i].id, &s[i].scord1, &s[i].scord2);
s[i].scord3 = 0.3*s[i].scord1 + 0.7*s[i].scord2;
average += s[i].scord3;
}
fclose(fp);
}
void bb()
{
ftp = fopen("e://write.txt", "w");
for (i = 0; i < 28; i++)
{
fprintf(ftp, "%-10s%-10s%-4d%-4d%-4.2f\n", s[i].name, s[i].id, s[i].scord1, s[i].scord2,         s[i].scord3);

}
fclose(ftp);
}
void cc()
{
for (i = 0; i < 28; i++)
{
if (s[i].scord3 >= 90)
a++;
if (s[i].scord3 >= 80 && s[i].scord3 < 90)
b++;
if (s[i].scord3 >= 70 && s[i].scord3 < 80)
c++;
if (s[i].scord3 >= 60 && s[i].scord3 < 70)
d++;
if (s[i].scord3 >= 50 && s[i].scord3 < 60)
e++;
if (s[i].scord3 >= 40 && s[i].scord3 < 50)
f++;
if (s[i].scord3 < 40)
g++;
}
}
void dd()
{
ftp = fopen("e://write.txt", "w");
fprintf(ftp, "总 90分以上 %d人 %.2lf%% 应考 28\n成 80--89分 %d人 %.2lf%% 实考 28\n绩 70--79分% d人 %.2lf%% 缓考 0分\n 60--69分    %d人 %.2lf%% 作弊    0\n析 50--59分    %d人 %.2lf%% 旷考 0\n40--49分 %d人 %.2lf%% 免考 0\n40分以下 %d人 %.2lf%% 平均分 %.2lf", a, (double)100 * a / 28, b, (double)100 * b / 28, c, (double)100 * c / 28, d, (double)100 * d / 28, e, (double)100 * e / 28, f, (double)100 * f / 28, g, (double)100 * g / 28, average / 28);
fclose(ftp);
}
int main()
{
void  aa();
void bb();
void cc();
void dd();
}