线性加权平均 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 Zhong Yue.\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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值