具有功能和结构的类平均值

时间:2014-11-10 13:56:33

标签: c

没关系!我得到了它的工作感谢你们所有人的帮助。我会在我回来后立即发布解决方案。

基本上,我只是想根据结构数据库的用户输入值来查找类平均值。一切正常,除非我似乎无法获得特定任务的类平均值的正确输出。

这是我正在努力的功能:

编辑(我更改了该功能,但旧版本仍在完整的代码段中。)

例如我会输入: 001 100 100 100

002 50 50 50

-1 0 0 0(终止)

输出结果为:

课堂作业平均分为150.00。

(但应该是75.00)

  

void classavg(struct grade record [200])   {

int i;
char  assign[30];
double classavghw = 0, classavge1 = 0, classavge2 = 0, totalhw = 0, totale1 = 0, totale2 = 0;

    i = 0;
    while(record[i].euid != -1)
    {

        totalhw += record[i].hwavg;

        totale1 += record[i].exam1;

        totale2 += record[i].exam2;

        classavghw = totalhw/i;
        classavge1 = totale1/i;
        classavge2 = totale2/i;

        i++;
    }


    printf("Enter the assignment: (homework, exam1, exam2)\n");
    scanf("%s", assign);

    if(strcmp(assign, "homework") == 0)
    {
        printf("The class homework average is: %.2lf\n", classavghw);
    }
    else if(strcmp(assign, "exam1") == 0)
    {
        printf("The class exam1 average is: %.2lf\n", classavge1);
    }
    else if(strcmp(assign, "exam2") == 0)
    {
        printf("The class exam2 average is: %.2lf\n", classavge2);
    }
    else

        printf("Not a valid input!\n");

}

这是完整的代码:

/*
CSCE Homework 05:
HEADER:
Author: Laura Kent (LauraKent@my.unt.edu) lek0073
Date: 11/9/2014
CSCE 1030
Purpose:
*/

#include <stdio.h>

struct Grade
{
   int euid;
   double hwavg;
   double exam1;
   double exam2;

};

void headerinfo(void);
void menu(void);
void database(struct Grade record[200]);
void studentinfo(struct Grade record[200]);
void studentavg(struct Grade record[200]);
void letteravg(int);
void classavg(struct Grade record[200]);

int main()

{

	struct Grade record[200];

	headerinfo();

	char assign[30];

	database(record);

	int input;

	while(1)
	{

		menu();

		scanf("%d",&input);

		switch(input)
		{
			case 1:

				studentinfo(record);

				break;

			case 2:

				studentavg(record);

				break;

			case 3:

				classavg(record);

				break;

			case 4:

				printf("Terminating program: Bye-bye!\n");
				return 0;
				break;

			default:

				printf("Error! Invalid input.\n\n");


		}

	}


}

void headerinfo(void)
{

	printf("Program: Homwork05.c\nAuthor: Laura Kent (LauraKent@my.unt.edu) lek0073\nCSCE 1030\n");
}

void menu(void)
{

	printf("Select one of the following:\n 1. Student grade data \n 2. Student grade average \n 3. Class average for assignment \n 4. Exit\n");

}

void database(struct Grade record[200])
{

	int i;

	printf("Input each student's EUID, homework average, exam 1 grade, and exam 2 grade: \n\n");

	printf("To terminate input '-1' as the student EUID, along with throwaway values for the average and grades.\n");

	for(i=0; i<200; ++i)
	{

		scanf("%d %lf %lf %lf", &record[i].euid, &record[i].hwavg, &record[i].exam1, &record[i].exam2);

		if(record[i].euid == -1)
		{
			break;
		}

	}
}

void studentinfo(struct Grade record[200])

{
	int i, x;

	printf("Enter the student's EUID:\n");

	scanf("%d", &x);

	i = 0;

	while(record[i].euid != -1 && record[i].euid != x)
	{
    		i++;
	}

	printf("EUID:%d  Homework:  %.2f  Exam 1:%.0f  Exam2:  %.0f\n", record[i].euid, record[i].hwavg, record[i].exam1, record[i].exam2);

}

void studentavg(struct Grade record[200])

{
	int i, x;
	float gradeavg;

	printf("Enter the student's EUID:\n");

	scanf("%d", &x);

	i = 0;

	while(record[i].euid != -1 && record[i].euid !=x)
	{
		i++;
	}

	gradeavg = (record[i].hwavg * 0.5) + (record[i].exam1 * 0.25) + (record[i].exam2 * 0.25);

	printf("EUID:%d  Grade average:%.2lf\n", record[i].euid, gradeavg);

	letteravg(gradeavg);


}

void letteravg(average)

{

	char letter;

	if(average > 89.50)
	{
		letter = 'A';
	}
	else if(average <= 89.4999 && average > 79.50)
	{
		letter = 'B';
	}
	else if(average <= 79.4999 && average > 69.50)
	{
		letter = 'C';
	}
	else if(average <= 69.4999 && average > 59.50)
	{
		letter = 'D';
	}
	else
	{
		letter = 'F';
	}

	printf("Letter grade:%c\n", letter);
}

void classavg(struct Grade record[200])
{


	int i;
	char  assign[30];
	double classavghw = 0, classavge1 = 0, classavge2 = 0, totalhw = 0, totale1 = 0, totale2 = 0;

		i = 0;
		while(record[i].euid != -1)
		{

			totalhw += record[i].hwavg;

			totale1 += record[i].exam1;

			totale2 += record[i].exam2;

			classavghw = totalhw/i;
			classavge1 = totale1/i;
			classavge2 = totale2/i;

			i++;
		}


		printf("Enter the assignment: (homework, exam1, exam2)\n");
		scanf("%s", assign);

		if(strcmp(assign, "homework") == 0)
		{
			printf("The class homework average is: %.2lf\n", classavghw);
		}
		else if(strcmp(assign, "exam1") == 0)
		{
			printf("The class exam1 average is: %.2lf\n", classavge1);
		}
		else if(strcmp(assign, "exam2") == 0)
		{
			printf("The class exam2 average is: %.2lf\n", classavge2);
		}
		else

			printf("Not a valid input!\n");


}

1 个答案:

答案 0 :(得分:0)

你没有发布整个代码,但初看起来,似乎你的问题是你从未初始化

double classavghw, classavge1, classavge2, totalhw, totale1, totale2;

这些变量。

此外,在您的代码中,在while()循环中,您过早地递增i [除非这是设计要求,否则无法在没有完整代码的情况下确定]。通常,在循环执行添加后,索引i应该递增。

如果您编辑并添加更多信息,我们可以提供更多线索。