【问题标题】:Cannot understand applying structure pointers and arrays in C无法理解在 C 中应用结构指针和数组
【发布时间】:2015-04-28 06:27:38
【问题描述】:

好的,所以我的任务是创建一个成绩册,它将:

  • 添加学生
  • 添加课程
  • 将学生添加到课程中
  • 为每门课程的学生添加成绩
  • 课程平均成绩
  • 学生的平均成绩

最后一个给我带来了麻烦。我不得不在上周的某个时候完成这个项目,所以我使用了一种廉价的解决方法,基本上让用户为他们所做的每笔成绩交易写下一个“成绩 ID”,如果他们想要任何东西的平均值,他们只需要输入任何等级 ID 的数量和它的平均值。现在我们回到这段代码并实现它的其他方面,但我真的需要弄清楚如何让它更像一个现实的程序。它将允许用户只输入学生 ID 并获得他们特定的平均成绩,而不必知道每个学生的成绩 ID。但是……我就是不知道该怎么做。

所以我放弃了我的旧想法,重新开始,已经到了我上次碰壁并强制解决方法的地步。我把我坚持的部分注释掉了。这是我的 addenroll 函数,它将学生添加到课程中,并且与我为特定学生添加成绩的代码非常相似。

我可以上传我的结构代码.. .c 程序的其余部分等。我只是不希望我的帖子跨越这么多页面!

void addenroll(enrollment * stuff, courses * cstuff, students *sstuff)                              
{                                                                                                                               
    int i,j;
    int temp;

    printf("Please enter the course ID you would like to add students to.\n");  
    scanf("%d",&temp);

    for(i = 0; i<cstuff->course_cnt;i++)
    {
        if(temp == cstuff->course_list[i].course_id)
        {
            stuff->enroll_list[stuff->enroll_cnt].e_course_id    = temp;
        }
    }

    printf("How many students would you like to add?\n");
    int choice;
    scanf("%d", &choice);

    for(i=0; i<choice; i++)
    {
        printf("Please enter the Student ID you would like to add to Course: %d\n", stuff->enroll_list[stuff->enroll_cnt].e_course_id);
        scanf("%d", &temp);

        for(j=0;j<sstuff->stu_cnt;j++)
            if(temp == sstuff->stu_list[j].stu_id)                  //Storing the student ID here 
            {
                printf("id match success\n\n");
                //Here is where I got stuck last time...
                //I know this has matched correctly, and here is where I would add a student to a course
                //Also, later when adding grades I'll need to use this same sort of thing to find the corresponding student
                //Just no idea how...
                //I will have this enroll_list[this #].*e_stu_id point to my stu_list[j].stu_id  
                //But I'm not sure of the format to put this in, and also once I have this done
                //I need to add grades here that I can find and average easily on a per course basis since each
                //enrollment only holds one course so I need only to average each enrollment. The problem comes when
                //I need only to find the average of a particular student, as the grades will be stored in an array held
                //ONLY inside this specific enrollment. And since it is in this enrollment, it will not be
                //matched with any particular student anymore, just an array full of grades.
            }
    }

【问题讨论】:

    标签: c arrays structure


    【解决方案1】:
    a couple of ideas come immediately to mind.
    
    1) for the student, traverse their sub records, 
       accumulating the count of grades and the sum of grades.
       then when at the end of the traversal, calculate the average
    
    2) add fields one for the number of grades, one for the sum of the grades
       update those two fields when a grade is added
       and when the average is needed, use those two fields
    

    【讨论】:

    • 我的问题是,例如:用户决定将学生添加到班级。所以我的程序去enroll_list[0].e_course_id;假设这里添加了 5 个学生。然后在enroll_list[0].grades 中是该注册中所有成绩的数组。但是当他们添加另一个班级时,比如说只有 3 名学生被放入enroll_list [1] ......那里的成绩由每个enroll_list 持有,而不是由学生ID 链接。它只是enroll_list[#] 中的一个数组,所以我可以得到每个班级的平均值,我只是不知道如何跟踪每个ID 的学生。
    猜你喜欢
    • 2019-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 1970-01-01
    • 2019-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多