2014-10-13 17 views
1

首先,我會公開承認這是一項家庭作業。這就是說,我的代碼非常接近,並希望在正確的方向推動。使用Swap函數對類對象數組進行排序後的重複項 - C++

提示: 編寫一個完整的程序,從標準輸入中讀取學生數據,按姓氏/名字排序,然後將結果打印到標準輸出。學生數據由姓氏,名字和成績平均值(浮點值)組成。你應該假設不超過50名學生。

輸入樣例:

潔具亨利87.2 丹特斯愛德蒙91.4 埃爾哈特阿梅利亞92.6

我的代碼接受用戶輸入,交換他們基於姓氏,然後第一個名稱,並輸出學生名單和GPA。測試我的代碼的在線程序輸入六(6)個學生數據。它使用Swap對它們進行正確分類,但第五名學生在第5-49項(50分之外)中重複,然後才輸出學生6.我搜索了論壇,但沒有發現以前的帖子適用。

我試過在輸出循環中使用'while'語句,但是我對布爾值的理解仍然有點弱。嘗試while語句列在我的代碼的頂部。任何援助將不勝感激。

while((list[i].lastName != list[i + 1].lastName) && (list[i].firstName != list[i + 1].firstName)) 

#include <iostream> 
#include <string> 
#include <algorithm> 
using namespace std; 

class student 
{ 
public: 
    string firstName; 
    string lastName; 
    float gpa; 

    student() {}; 
    student(string last, string first, double grade) 
    { 
     firstName = first; 
     lastName = last; 
     gpa = grade; 
    }; 
}; 

int main() 
{ 
    string first; 
    string last; 
    float grade; 

    student list[50]; 


    for(int i = 0; i < 50; i++) 
    { 
     cin >> last; 
     cin >> first; 
     cin >> grade; 
     list[i].lastName = last; 
     list[i].firstName = first; 
     list[i].gpa = grade; 
    } 
    for (int i = 0; i < 50 - 1; i++) 
     { 
      if (list[i].lastName > list[i + 1].lastName) 
      { 
       swap (list[i], list[i+1]); 
      } 
     } 
     for (int i = 0; i < 50 - 1; i++) 
     { 
      if (list[i].firstName > list[i + 1].firstName) 
      { 
      swap (list[i], list[i+1]); 
      } 
     } 


     for(int i = 0; i < 50 - 1; i++) 
     { 
      cout << list[i].lastName << " " << list[i].firstName << " " << list[i].gpa << endl; 
     } 

    return 0; 
} 
+2

您正試圖從輸入中正好讀取50個條目,並且沒有檢查任何I/O錯誤。一旦達到文件結尾,所有這些'>>'操作符都會失敗 - 但是您不會注意到這一點,並插入一個條目,其中包含最後保存的三個變量的值。這就是你最終得到44份同一學生數據的副本。 –

回答

0

輸入到你的程序的數量只保證是<= 50,確實在測試數據中,只有6項,並在您的樣本只輸入有3

你的問題就出在目前您每次運行程序時都需要輸入50個條目,並且當cin >> variable失敗時,不會寫入任何數據。

從您的示例輸入中,所有數據都在一行上輸入。這意味着我們可以使用this answer中描述的方法來處理輸入。

你輸入迴路現在看起來像這樣:

int count = 0; 
std::string line; 
std::getline(cin, line); 
std::istringstream iss(line); 

while ((iss >> last) && (iss >> first) && (iss >> grade)) 
{ 
    list[count].lastName = last; 
    list[count].firstName = first; 
    list[count].gpa = grade; 
    cout <<"student: "<< last << " " << " " << first << " " << grade << endl; 
    count++; 
} 

我們現在讀一氣呵成整行,然後使用流運算符>>讀它。
在功能上,這是非常類似於您的原始代碼,但是,與cin不同,流結束。
iss >> variable當它到達數據的末尾時會返回false,這會使我們擺脫循環。此時,count變量的值將等於學生數據集輸入的數量。

我們現在可以在餘下的循環中使用count(而不是50)。

全碼:

#include <iostream> 
#include <sstream> 
#include <string> 
#include <algorithm> 
using namespace std; 

class student 
{ 
public: 
    string firstName; 
    string lastName; 
    float gpa; 

    student() {}; 
    student(string last, string first, double grade) 
    { 
     firstName = first; 
     lastName = last; 
     gpa = grade; 
    }; 
}; 

int main() 
{ 
    string first; 
    string last; 
    float grade; 

    student list[50]; 

    int count = 0; 
    std::string line; 
    std::getline(cin, line); 
    std::istringstream iss(line); 

    cout<<"input data: \n"; 
    while ((iss >> last) && (iss >> first) && (iss >> grade)) 
    { 
     list[count].lastName = last; 
     list[count].firstName = first; 
     list[count].gpa = grade; 
     cout <<"student: "<< last << " " << " " << first << " " << grade << endl; 
     count++; 
    } 

    for (int i = 0; i < count-1; i++) 
     { 
      if (list[i].lastName > list[i + 1].lastName) 
      { 
       swap (list[i], list[i+1]); 
      } 
     } 
     for (int i = 0; i < count-1; i++) 
     { 
      if (list[i].firstName > list[i + 1].firstName) 
      { 
      swap (list[i], list[i+1]); 
      } 
     } 


     for(int i = 0; i < count; i++) 
     { 
      cout << list[i].lastName << " " << list[i].firstName << " " << list[i].gpa << endl; 
     } 

    return 0; 
} 

我有在ideone的這一個正在運行的版本供您:http://ideone.com/Y9jOkt 我也不過做了一些進一步的增強是在這個版本: 刪除using namespace stdsee here for why 使用一個std::vector而不是一個裸陣列,將適應輸入的數量,而不是一個固定的大小。 創建了一個位置current_student對象來讀取數據,保持意圖清晰,並與std::vector一起使用。也可以使用裸陣列來完成。

我還想指出,你的排序是不對的。
此輸入:

f f 9 e e 8 d d 7 c c 6 b b 5 a a 4 

應該提供這樣的輸出:

a a 4 
b b 5 
c c 6 
d d 7 
e e 8 
f f 9 

而是出來是這樣的:

d d 7 
c c 6 
b b 5 
a a 4 
e e 8 
f f 9 

你正在做一個冒泡排序(我想這是你的意圖),但只做一個傳球。 我可以在這裏回答,但作爲一個單獨的問題,你應該在另一個問題中提出這個問題。

+0

關於排序,這是一個非常簡單的修復,分兩步:第一步:在每一種排序中添加一個嵌套循環。第2步:先按名字排序,以便按姓氏內的名字排序條目。 http://ideone.com/4liN2T – Baldrickk

2
#include <iostream> 
#include <string> 
#include <algorithm> 
using namespace std; 

class student 
{ 
public: 
    string firstName; 
    string lastName; 
    float gpa; 

    student() {}; 
    student(string last, string first, double grade) 
    { 
     firstName = first; 
     lastName = last; 
     gpa = grade; 
    }; 
}; 

int main() 
{ 
    string first; 
    string last; 
    float grade; 

    student list[5]; 


    for(int i = 0; i < 5; i++) //use 5 instead of 50 because myprogramminglab only input 5 entries, not 50, it lies to you 
    { 
     cin >> last; 
     cin >> first; 
     cin >> grade; 
     list[i].lastName = last; 
     list[i].firstName = first; 
     list[i].gpa = grade; 
    } 
    for (int i = 0; i < 4; i++) 
     { 
      if (list[i].lastName > list[i + 1].lastName) 
      { 
       swap (list[i], list[i+1]); 
      } 
     } 
     for (int i = 0; i < 4; i++) 
     { 
      if (list[i].firstName > list[i + 1].firstName) 
      { 
      swap (list[i], list[i+1]); 
      } 
     } 


     for(int i = 0; i < 5; i++) 
     { 
      cout << list[i].lastName << " " << list[i].firstName << " " << list[i].gpa << endl; 
     } 

    return 0; 
} 

/評論:我相信這個任務是來自myprogramminglab。唯一需要解決的問題是從50更改爲5,myprogramminglab只輸入5個條目而不是50個,因爲它說/