gpt4 book ai didi

无法理解在 C 中应用结构指针和数组

转载 作者:行者123 更新时间:2023-12-01 01:25:39 25 4
gpt4 key购买 nike

好吧,我的任务是创建成绩簿,该成绩簿将:

  • 添加学生
  • 添加类(class)
  • 将学生添加到类(class)
  • 为每门类(class)的学生打分
  • 按类(class)平均成绩
  • 学生平均成绩

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

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

我可以上传我的结构代码...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.
}
}

最佳答案

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

关于无法理解在 C 中应用结构指针和数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28732951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com