#include?<stdlib.h>
#define?ARRAY_LEN?100?/*數組長度*/
typedef?struct?{
int?no;?/*學號*/
char?name[ARRAY_LEN];?/*姓名*/
float?score1;?/*平時成績*/
float?score2;?/*期中成績*/
float?score3;?/*期末成績*/
float?totalScore;?/*學期成績*/
}?student;
/*初始化*/
void?initInfo?(student?stu[],?int?*stuNum)?{
*stuNum?=?2;?/*學生人數設定*/
}
/*輸入學生信息*/
void?inputInfo?(student?stu[],?int?stuIndex)?{
int?i;
printf?("第%d名學生↓\n",?stuIndex+1);
printf?("學號:");
scanf?("%d",&stu[stuIndex].no);
printf?("姓名:");
scanf?("%s",&stu[stuIndex].name);
printf?("平時成績:");
scanf?("%f",&stu[stuIndex].score1);
printf?("期中成績:");
scanf?("%f",&stu[stuIndex].score2);
printf?("期末成績:");
scanf?("%f",&stu[stuIndex].score3);
putchar?('\n');
}
/*計算學期成績*/
void?calculationScore?(student?stu[],?int?stuIndex)?{
float?sco1Ratio?=?0.1;?/*平時成績比例*/
float?sco2Ratio?=?0.2;?/*期中成績比例*/
float?sco3Ratio?=?0.7;?/*期末成績比例*/
stu[stuIndex].totalScore?=?stu[stuIndex].score1?*?sco1Ratio?+
stu[stuIndex].score2?*?sco2Ratio?+
stu[stuIndex].score3?*?sco3Ratio;
}
/*輸出學生成績*/
void?printInfo?(student?stu[],?int?stuIndex)?{
int?i;
printf?("%d\t",stu[stuIndex].no);
printf?("%s\t",stu[stuIndex].name);
printf?("%.2f\t\t",stu[stuIndex].score1);
printf?("%.2f\t\t",stu[stuIndex].score2);
printf?("%.2f\t\t",stu[stuIndex].score3);
printf?("%.2f",stu[stuIndex].totalScore);
putchar?('\n');
}
int?main?(void)?{
int?stuNum,i;
student?stu[ARRAY_LEN];
initInfo?(stu,&stuNum);
/*輸入、計算*/
puts?("請輸入學生信息:");
putchar?('\n');
for?(i=0;?i<stuNum;?i++)?{
inputInfo?(stu,i);
calculationScore?(stu,i);
}
putchar?('\n');
printf?("%d名學生成績輸入完畢!",?stuNum);
putchar?('\n');
puts?("================================================================\n");
/*輸出*/
puts?("學號\t姓名\t平時成績\t期中成績\t期末成績\t學期成績");
for?(i=0;?i<stuNum;?i++)
printInfo?(stu,i);
getch?();?/*屏幕暫留*/
return?0;
}
運行結果
以下圖示改為2名學生,上方源代碼為題主要求的5名學生