2013-10-21 35 views
2

好吧,所以這段代碼正在殺死我。將數據從文件加載到C++中的數據結構並解釋它

我的目標是從那裏數據被逗號分隔的文件中讀取數據,然後將數據加載到被認爲是「劇院座位」列表結構數組。劇院席位具有某些特徵,如「位置」,「價格」和「地位」。價格是不言自明的。位置涉及「座位」的行號和座位號。狀態與它是否已售出有關。之後,我必須解釋從數據文件中提取的數據,以便進行顯示,如果用戶輸入了某個選項,則用戶可以輕鬆操作這些數據。但這不是我在這個問題上得到的。 我的問題是,從數據文件加載我的數據結構的最佳方法是什麼?

讓我告訴你一點我是從讀取數據文件。

1, 1, 50, 0 
1, 2, 50, 0 
1, 3, 50, 0 
1, 4, 50, 0 

爲了解釋數據文件時,第一個數字是「行」,第二個數字是該行中的座位號,所述第三數目是價格,以及最後的數字(「0」)表示座位未售出。有座被購買,最終數量將是1

現在,這裏是我的代碼。

#include <iostream> 
#include <fstream> 
#include <string> 
#include <iomanip> 
#include <cstdlib> 
#include <cstdio> 
#include <conio.h> 
using namespace std; 

enum seatDimensions{ROWS = 10, SEATS_PER = 16}; 

//Structures 
struct Location 
{ 
    int  row; 
    int  seatNumber; 
}; 
struct Seat 
{ 
    Location seat_location[160]; 
    double  ticketPrice; 
    int   status; 
    int   patronID; 
}; 

//GLOBALS 
const int MAX = 16; 

int main() 
{ 
//arrays for our data 
Seat  seatList[160]; 
//INDEX 
int index = 1; 
//filestream 
fstream dataIn; 

dataIn.open("huntington_data.dat",ios::in); 
if(dataIn.fail()) //same as if(dataIn.fail()) 
{ 
    cout << "Unable to access the data file." << endl; 
    return 999; 
} 

string temp; 
getline(dataIn,temp,','); 
seatList[index].seat_location[index].row = atoi(temp.c_str()); 
getline(dataIn,temp,','); 
seatList[index].seat_location[index].seatNumber = atoi(temp.c_str()); 
getline(dataIn,temp,','); 
seatList[index].ticketPrice = atof(temp.c_str()); 
getline(dataIn,temp,'\n'); 
seatList[index].status = atoi(temp.c_str()); 

    while(!dataIn.eof() && index < MAX) 
    { 
     index++; 
     getline(dataIn,temp,','); 
     seatList[index].seat_location[index].row = atoi(temp.c_str()); 
     getline(dataIn,temp,','); 
     seatList[index].seat_location[index].seatNumber = atoi(temp.c_str()); 
     getline(dataIn,temp,','); 
     seatList[index].ticketPrice = atof(temp.c_str()); 
     getline(dataIn,temp,'\n'); 
     seatList[index].status = atoi(temp.c_str()); 
    } 
getch(); 
return 0; 
} 

現在,從這裏開始,我必須展示座位是否被取走。 顯示屏應該看起來像這樣,因爲還沒有座位。

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 // 16 across 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
// 10 deep 

我知道我不是在輸入正確我的數據,因爲我似乎無法得到這顯示無論我如何努力來清點它。如果你能告訴我哪裏出錯了,請告訴我。任何建議都會很棒。 另外,如果您有任何問題,請詢問。考慮到這個問題,我試圖儘可能具體,但我知道它仍然非常模糊。 請幫忙。

編輯:感謝那些回答我的問題的人。我結束了與我的數據結構走非常不同的路線,但從答案中拉了很多。

+0

+1對問題和你的嘗試的很好的描述。 –

+0

你得到的輸出是什麼?在輸出中除了0或1以外還有其他什麼?你可能想從0開始你的索引,另一個建議是在循環中做所有的閱讀。 – Teeknow

+0

我現在得到的輸出是1.是的,這實際上是我決定去的。謝謝你的建議! –

回答

1

您有每個座椅具有在陣列中的位置,並且然後具有位置陣列的問題:

你需要:

Location seat_location[160]; 

更改爲:

int seat_row; 
int seal_num; 

然後:

seatList[index].seat_location[index].row = atoi(temp.c_str()); 

變爲:

seatList[index].seat_row = index/COLS ; 
seatList[index].seat_num = index % COLS ; 

您可能還喜歡真正考慮您的數據安排到相同的尺寸,你休息的二維數組。

BTW從我的C背景,我建議閱讀整條生產線,並使用sscanf的,例如:

char temp[255]; // Use an appropriate maximum line length +3 for \r\n\0 

fgets(dataIn, &temp); 
sscanf(temp, "%d, %d, %d, %d", &..... 

你也可以考慮用正則表達式來實現它。

+0

嗯...我想我明白你的意思了。我認爲位置結構有點多。但我真誠地感謝你幫助我在這裏。所以,如果我試圖將數據安排到與我計劃安排的座位相對應的二維數組中,我將如何加載這些數據? –

+0

如上所述計算的行和列都將用於將數據索引爲seatList [row] [col]。好消息是,由於結構中的位置會告訴你行列,你不需要存儲它。 –

+0

請問,COLS究竟是什麼?這可能是一個非常業餘的問題,但這解釋了我的處理。 –

0

編寫獲取輸入的字符串,並將其分成項功能:

std::vector<std::string> splitLine(const std::string &str); 

實施幷包含「1,1,50,0」字符串調試它,確保它返回一個字符串矢量每個數字作爲獨立的元素。然後逐行讀取輸入,將每個字符串拆分並分別轉換爲數字。您將簡化代碼,並使其工作更容易。

0

它是一項任務嗎?

我想你正在組織你的數據結構的方式需要改變。

採取的結構也許這樣

struct Seat{ 
    double ticketPrice; 
    int status; 
    int patronId; 
} 

並且有如下的

Seat seatList[10][16]; 第一尺寸的二維陣列是(row number-1),第二尺寸是(seat number-1)

和從文件中讀取數據像這樣

string temp; 
getline(dataIn,temp,','); 
int row = atoi(temp.c_str()); 
getline(dataIn,temp,','); 
int seatNumber = atoi(temp.c_str()); 
getline(dataIn,temp,','); 
//check row and seatnumber > 0 
seatList[row-1][seatNumber-1].ticketPrice = atof(temp.c_str()); 
getline(dataIn,temp,'\n'); 
seatList[row-1][seatNumber-1].status = atoi(temp.c_str()); 

使用兩個簡單的for循環來打印這些結構的輸出。

+0

160 * 160似乎比原來的例子更多的座位。 –

+1

編輯。最大行數= 10,最大座位數= 16 – N3Xg3N

相關問題