gpt4 book ai didi

c++ - 遍历内部结构的链表

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:14:31 28 4
gpt4 key购买 nike

所以我有一个由 4 个学生组成的链表,链表的每个节点内都有一个结构,其中包含一些关于学生的数据。我想遍历这个链表并打印每个结构中的数据。我可以遍历链接列表期望所有数据打印为 0。任何帮助将不胜感激。

#include<stdlib.h>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdlib>
using namespace std;

void displayGrades( struct Outer *O);
void calculateGrades(struct Outer *O);
void readGrades( struct Outer *O);

struct Inner{
int id;
string name;
double midterm1;
double midterm2;
double midtermTotal;
double lab_H;
double finalExam;
double total;
};
Inner i1;

struct Outer{
Inner data;
Outer *next;
};
struct Outer o1, o2, o3, o4;

int main()
{

readGrades(&o1);
calculateGrades(&o1);
displayGrades(&o1);
//o1.next = &o2;
/*
readGrades(&o2);
calculateGrades(&o2);
displayGrades(&o2);
//o2.next =&o3;

readGrades(&o3);
calculateGrades(&o3);
displayGrades(&o3);
//o3.next =&o4;

readGrades(&o4);
calculateGrades(&o4);
displayGrades(&o4);
//o4.next =NULL;
*/


Outer *ptro1;
ptro1 = new Outer;
Outer *ptro2;
ptro2 = new Outer;
Outer *ptro3;
ptro3 = new Outer;
Outer *ptro4;
ptro4 = new Outer;
Outer *head=ptro1;

ptro1->next = ptro2;
ptro2->next = ptro3;
ptro3->next = ptro4;
ptro4->next = NULL;

while(head!=NULL) // && i<=2)
{
cout<<"Student ID: "<<head->data.id<<endl;
cout<<"Student Midterm1: "<<head->data.midterm1<<endl;
cout<<"Student Midterm2: "<<head->data.midterm2<<endl;
cout<<"Student Labs and Homework: "<<head->data.lab_H<<endl;
cout<<"Student Final Exam: "<<head->data.finalExam<<endl;
head = head->next;
}
return 0;


}

void readGrades(struct Outer *O){

cout<<"Enter the student's id: "<<endl;
cin>>o1.data.id;
cout<<"Enter the student's midterm #1 grade: ";
cin>>o1.data.midterm1;
cout<<"Enter the student's midterm #2 grade: ";
cin>>o1.data.midterm2;
cout<<"Enter the student's lab and homework grade: ";
cin>>o1.data.lab_H;
cout<<"Enter the student's final exam grade: ";
cin>>o1.data.finalExam;
}

void displayGrades(struct Outer *O){

cout<<"The students final grade is: ";

if(O->data.total>=90)
{
cout<<"A"<<endl;
}
else if(O->data.total<=89 && O->data.total>=80)
{
cout<<"B"<<endl;
}
else if(O->data.total<=79 && O->data.total>=70)
{
cout<<"C"<<endl;
}
else if(O->data.total<=69 && O->data.total<60)
{
cout<<"F"<<endl;
}
}

void calculateGrades(struct Outer *O){
O->data.midtermTotal=(((O->data.midterm1/50)+(O- >data.midterm2/50))/2)*35;
O->data.lab_H=(O->data.lab_H/20)*25;
O->data.finalExam=(O->data.finalExam/100)*40;
O->data.total=O->data.midtermTotal+O->data.lab_H+O->data.finalExam;
//displayGrades();
}

最佳答案

在下面的函数中,你需要在传递它时使用 O 但你正在使用o1 每次都会覆盖

void readGrades(struct Outer *O)
{
cout<<"Enter the student's id: "<<endl;
cin>>O->data.id;
cout<<"Enter the student's midterm #1 grade: ";
cin>>O->data.midterm1;
cout<<"Enter the student's midterm #2 grade: ";
cin>>O->data.midterm2;
cout<<"Enter the student's lab and homework grade: ";
cin>>O->data.lab_H;
cout<<"Enter the student's final exam grade: ";
cin>>O->data.finalExam;
}

关于c++ - 遍历内部结构的链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35639402/

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