/*
* main.c
*
* Created on: 2011-6-6
* Author: icelights
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define TotalStu 5 /*学生总数Total student*/
struct Database
{
/*学号Student No.*/
char sn[80];
/*笔试成绩Written test score*/
double wts;
/*理论课综合训练成绩扣分
*Comprehensive training course grades possessed*/
double ctcgp;
/*机考成绩Machine examination results*/
double mer;
/*上机扣分Computer possessed*/
double cp;
/*总成绩Total score*/
double ts;
/*挂科与否Hanged division */
int hd;
};
/*输出文件*/
void output(struct Database Stu[])
{
FILE *fp;
int liv_cnt;
if((fp=fopen("d:\\StuDBA.txt","wt+"))== NULL)
{
puts("Couldn't read the file\n");
}
rewind(fp);
for(liv_cnt = 0; liv_cnt < TotalStu; liv_cnt++)
{
fprintf(fp,"%s %lf %d\n" , Stu[liv_cnt].sn,
Stu[liv_cnt].ts, Stu[liv_cnt].hd);
}
if(fclose(fp))
{
puts("Fail to close the file.\n");
}
}
/*计算成绩&判断挂科*/
void cal(struct Database Stu[])
{
int liv_cnt;
for (liv_cnt = 0; liv_cnt < TotalStu; liv_cnt++)
{
Stu[liv_cnt].ts = (Stu[liv_cnt].wts - Stu[liv_cnt].ctcgp) * 0.6
+ (Stu[liv_cnt].mer - Stu[liv_cnt].cp) * 0.4;
printf("%s%lf%lf%lf%lf",
Stu[liv_cnt].sn, Stu[liv_cnt].wts, Stu[liv_cnt].ctcgp,
Stu[liv_cnt].mer, Stu[liv_cnt].cp);
if (Stu[liv_cnt].ts < 60)
{
Stu[liv_cnt].hd = 0;
}
else
{
Stu[liv_cnt].hd = 1;
}
}
output(Stu);
}
/*接受用户输入*/
void input(void)
{
struct Database Stu[TotalStu];
int liv_cnt;
puts("Please enter the Student No. Written test score");
puts("Comprehensive training course grades possessed");
puts("Machine examination results and Computer possessed");
for (liv_cnt = 0; liv_cnt < TotalStu; liv_cnt++)
{
scanf("%s%lf%lf%lf%lf",
Stu[liv_cnt].sn, &Stu[liv_cnt].wts, &Stu[liv_cnt].ctcgp,
&Stu[liv_cnt].mer, &Stu[liv_cnt].cp);
}/*end of for (liv_cnt = 0; liv_cnt < TotalStu; liv_cnt++)*/
cal(Stu);
}
/*主函数*/
int main(void)
{
input();
return 0;
}
温馨提示:答案为网友推荐,仅供参考