求c语言算加权平均分的代码

求一个算成绩加权平均分的代码。。。。

//经过调试,可以运行
#include"stdio.h"
#include "ctype.h"
#define NAME_LEN 999
#define MAX_PRO 999
int num_parts = 0;
float total_hours = 0,ave_marks = 0;
struct
{
char pro_name[NAME_LEN+1];
float cre_hour;
float marks;
}project[MAX_PRO];
void sum_credit_hour(void);
void GPA(void);
void print(void);
int read_line(char str[], int);
int main()
{
char code;
printf("This program is edited by siyuan lu.\n"
"if you want to make your life more convinent,please ask him to get more.\n\n");
for(;;)
{
printf("操作码:\ni 写入科目及学分\ng 显示平均学分\np 显示已输入的数据\nq 退出程序");
printf("\n请输入操作码: ");
scanf(" %c", &code);

//冗余语句
/*************************
while (getchar() != '\n')
;
*************************/
switch ((int)code)
{
case 105: sum_credit_hour();break;
case 103: GPA();break;
case 112: print();break;
case 113: goto L;break;
default : printf("非法的输入码!");
}
printf("\n");
}
L: ;
return 0;
}
/*这个函数的功能是写入科目名称及学分数并计算已输入的总学分数 这个函数使用了结构体变量*/
void sum_credit_hour(void)
{
int i;
char j;
printf("请输入学科名称:");
read_line(project[num_parts].pro_name, NAME_LEN);
printf("请输入学科学分数:");
scanf("%f", &project[num_parts].cre_hour);
printf("请输入您的成绩:");
scanf("%f", &project[num_parts].marks);
total_hours += project[num_parts].cre_hour;
num_parts++;
}

/*这个函数的功能是计算并显示加权平均分 这个函数使用了结构体变量*/
void GPA(void)
{
int i,total_marks = 0;
for(i = 0;i < num_parts; i++)
{
total_marks += project[i].marks * project[i].cre_hour;
}
ave_marks = total_marks / (int)total_hours;
printf("您的加权平均分为:%.2f
", ave_marks);
}

/*这个函数的功能是显示已输入的数据 这个函数使用了结构体变量*/
void print(void)
{
int i;
printf("学科 学分数 " "成绩\n");
for(i = 0;i < num_parts; i++)
printf("%-10s %-16f%8f\n", project[i].pro_name, project[i].cre_hour,project[i].marks);
}
int read_line(char str[], int n)
{
int ch, i = 0;
while (isspace(ch = getchar()))
;
while (ch != '\n' && ch != EOF)
{
if (i < n)
str[i++] = ch;
ch = getchar();
}
str[i] = '\0';
return i;
}

自己多练练,求这种代码容易被鄙视。。
温馨提示:答案为网友推荐,仅供参考
相似回答