c - 具有结构的用户输入数据库需要找到类(class)平均值

标签 c arrays database structure

好吧,我的 c 程序的下一步遇到了麻烦。基本上,它是一个接受用户输入,然后使用 switch 语句来检索或计算个人或组统计数据的数据库。注意: EUID(卷号)存储在 EUID 数组的位置 i 中的学生将其作业平均分、考试 1 成绩和考试 2 成绩存储在相应数组的位置 i 中。

到目前为止,代码可以运行,但当涉及到 switch 语句时,我被难住了。

我只需要有关 switch 语句的第一种情况的建议,但为了以防万一,我也包括了其余的情况。

简而言之,案例 1 是如何让程序使用特定的 EUID 打印出学生的成绩和平均成绩。输出应如下所示:

1

Please enter student EUID: 1893839

EUID: 1893839 Homework: 84.34 Exam 1: 84 Exam 2: 76

这是我到目前为止所拥有的:

#include <stdio.h>
#include <math.h>


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

};


int main()

{
    struct Grade record[200];

    int input, 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.");

    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;
        }

    }

    while(1)
    {

        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");

        scanf("%d\n",&input);

        switch(input)
        {
            case 1:

                printf("Enter the student's EUID:\n");
                   /*The output here should be the EUID, homework avgerage, exam 1 grade and exam 2 grade. */ 


                break;

            case 2:

                printf("Enter the student's EUID:\n");   
                   /*Finds and prints the the students average and letter grade. */

                   /* (HW * 0.5) + (EXAM1 * 0.25) + (EXAM2 * 0.25) */  


                break;

            case 3:
                   /* Finds total class average. */ 
                break:

            case 4:

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

            default:

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

    }

}

最佳答案

基本上,您正在寻找的似乎是沿线的东西......

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

注意:%.0f 输出 0 位小数。

为了计算平均值,可以在第一个 for 循环中完成此操作,该循环读取并填充数组。

您可能想在网上搜索“printf double”; here's an example reference 。 -我几乎从未见过提到的是,您可以使用 %2$f 选择参数编号(其中 2 表示参数编号 2)。

要查找 EUID 的索引,您可以使用如下内容:

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

-您当然需要 scanf 来获取 euid。

关于c - 具有结构的用户输入数据库需要找到类(class)平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26839180/

相关文章:

c - C 中的结合性和优先级

c++ - 需要一些帮助将我的结果写入文件

c++ - 使用 void 指针的动态数组实现

php - MySQL 额外列信息

mysql - 在 SQL 中存储数据的正确方法

c - "The C Programming Book"中的函数 getop() 是如何工作的?

c - 为什么我得到 NULL is undefined 错误?

php - 更快地解析 php 中的数组

java - 如何将重复元组列表转换为唯一元组列表?

sql - 用于填充记录集和返回 ID 的 Postgres 函数