2016-02-25 50 views
2

所以我有一個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(); 
} 
+0

所以你的數據保存到O1 ... O4,然後製作一套全新的結構體ptro1 ... ptro4並從中讀取數據,並且你爲什麼會空虛而感到困惑? – Donnie

回答

2

在下面的函數,你需要使用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; 
} 
-1

您應該創建一個將成員變量初始化爲0的構造函數。大多數編譯器默認情況下不會這樣做。

struct Inner{ 

Inner() : id(0),midterm1(0),midtermTotal(0), lab_H(0), finalExam(0), total(0) 
{ 
} 

int id; 
string name; 
    double midterm1; 
    double midterm2; 
    double midtermTotal; 
    double lab_H; 
    double finalExam; 
    double total; 

};

+0

這不回答這個問題,應該是一個評論。 – Donnie

+0

我相信他預計未設置的值爲0,而不是垃圾。此代碼在Visual C++輸出中輸出 ....學生實驗室和作業:-6.27744e + 66 學生期末考試:-6.27744e + 66 學生ID:-842150451 ... –