gpt4 book ai didi

c++ - 从文件中读入哈希表

转载 作者:太空宇宙 更新时间:2023-11-04 13:24:47 24 4
gpt4 key购买 nike

对于我的项目,我需要制作一个哈希表,其中包含股票信息。我想我已经完成了大部分工作,但是我在用文本文件中的信息填充哈希表时遇到了问题。当文件为空时,我的程序启动并正确显示菜单,但是当我将信息放入文本文件时,它会永远卡住,直到 Putty 终止程序。此外,当我尝试添加股票时,它似乎没有正确获取信息,这可能是它永远需要从文件中读取的原因。我试过弄乱文件中的一些东西,但没有任何效果。

编辑:添加一些 cout 语句后,我发现在循环“while(!in.eof())”中是无限的,永远不会到达文件的末尾,我不确定为什么。

哈希表.cpp:

#include "hashtable.h"
#include <iostream>
#include <fstream>
#include<cstring>
using namespace std;

void hashTable::initializeTable()
{
table = new node*[capacity];

int i;
for(i=0; i<capacity; i++)
table[i] = NULL;
}

hashTable::hashTable() : capacity(DEFAULT_CAPACITY), size(0)
{
initializeTable();
}

hashTable::hashTable(char * fileName) : capacity(DEFAULT_CAPACITY), size(0)
{
ifstream in;
data currData;
char tickerSymbol[100];
char name[100];
float netVal;
char date[100];
float yToD;

initializeTable();

in.open(fileName);
if(!in)
{
cerr << "fail to open " << fileName << " for input!" << endl;
return;
}

in.get(tickerSymbol, 100, ';');
while(!in.eof())
{
in.ignore(100, ';');
in.get(name, 100, ';');
in.ignore(100, ';');
in.ignore(100, ';');
in >> netVal;
in.ignore(100, ';');
in.get(date, 100, ';');
in.ignore(100, ';');
in >> yToD;
in.ignore(100, '\n');

currData.setTickerSymbol (tickerSymbol);
currData.setName (name);
currData.setNetValue(netVal);
currData.setDate(date);
currData.setYToD(yToD);

insert(currData);

in.get(tickerSymbol, 10, ';');
}
in.close ();
}

hashTable::hashTable(const hashTable& aTable):capacity(aTable.capacity), size(aTable.size)


{
table = new node*[capacity];

int i;
for(i=0; i<capacity; i++)
{
if (aTable.table[i] == NULL)
table[i] = NULL;
else
{
table[i] = new node(aTable.table[i]->item);

node * srcNode = aTable.table[i]->next;
node * destNode = table[i];
while(srcNode)
{
destNode->next = new node(srcNode->item);
destNode = destNode->next;
srcNode = srcNode->next;
}
destNode->next = NULL;
}
}
}

const hashTable& hashTable::operator= (const hashTable& aTable)
{
if(this == &aTable)
return *this;
else
{
destroyTable();

table = new node*[capacity];
capacity = aTable.capacity;
size = aTable.size;

int i;
for(i=0; i<capacity; i++)
{
if (aTable.table[i] == NULL)
table[i] = NULL;
else
{
table[i] = new node(aTable.table[i]->item);

node * srcNode = aTable.table[i]->next;
node * destNode = table[i];
while(srcNode)
{
destNode->next = new node(srcNode->item);
destNode = destNode->next;
srcNode = srcNode->next;
}
destNode->next = NULL;
}
}
return *this;
}
}

void hashTable::destroyTable ()
{
int i;
for(i=0; i<capacity; i++)
{
node * head = table[i];
node * curr;
while(head)
{
curr = head->next;
head->next = NULL;
delete head;
head = curr;
}
}

delete [] table;
}
hashTable::~hashTable()
{
destroyTable();
}

void hashTable::insert (const data& aData)
{
char key[100];
aData.getTickerSymbol(key);
int index = calculateIndex(key);

node * newNode = new node(aData);

newNode->next = table[index];
table[index] = newNode;
size++;
}

bool hashTable::remove (char * key)
{
int index = calculateIndex(key);

node * curr = table[index];
node * prev = NULL;
char id[100];
while (curr)
{
curr->item.getTickerSymbol (id);
if(strcmp(key, id) == 0)
{
if(!prev)
table[index] = curr->next;
else
prev->next = curr->next;

curr->next = NULL;
delete curr;
size--;
return true;
}
else
{
prev = curr;
curr = curr->next;
}
}
return false;
}

bool hashTable::retrieve (char * key, data & aData)const
{
int index = calculateIndex(key);

node * curr = table[index];
char id[100];
while (curr)
{
curr->item.getTickerSymbol (id);
if(strcmp(key, id) == 0)
{
aData = curr->item;
return true;
}
else
curr = curr->next;
}

return false;
}

void hashTable::display (void)const
{
int i;
node * curr;

cout << "Data in the table: " << endl << endl;
for(i=0; i<capacity; i++)
{
for(curr = table[i]; curr; curr = curr->next)
cout << '\t' << curr->item << endl;
}
}
int hashTable::getSize (void) const
{
return size;
}

void hashTable::writeOut(char * fileName)
{
ofstream out;

out.open(fileName);
if(!out)
{
cerr << "fail to open " << fileName << " for output!" << endl;
return;
}

int i;
char tickerSymbol[100];
char name[100];
node * curr;
for(i=0; i<capacity; i++)
{
for(curr = table[i]; curr; curr = curr->next)
{
curr->item.getTickerSymbol (tickerSymbol);
curr->item.getName (name);
out << tickerSymbol << ';' << name << ';' << curr->item.getNetValue () << '\n';
}
}
out.close ();
}

int hashTable::calculateIndex (char * key)const
{
char * c = key;
int total = 0;
while(*c)
{
total += *c;
c++;
}

return total%capacity;
}

主要.cpp

#include <stdlib.h>
//#include <crtdbg.h>


#include "hashtable.h"
#include <iostream>
using namespace std;

void displayMenu();
char getCommand();
void executeCmd(char command, hashTable& aTable);

void getStock(data & stock);
int getInt(char * prompt);
float getFloat(char * prompt);
void getString(char * prompt, char * input);

void display(const hashTable & aTable);

const int MAX_LEN = 100;

int main()
{
char command = 'a';
char fileName[] = "data.dat";
hashTable stocks(fileName);

displayMenu();
command = getCommand();
while(command != 'q')
{
executeCmd(command, stocks);
displayMenu();
command = getCommand();
}

stocks.writeOut (fileName);
cout << "\nThank you for using CWMS!" << endl << endl;
return 0;
}

void displayMenu()
{
cout << "\nWelcome to CS Stock Management System! "<< endl;
cout << "\ta> add a stock" << endl
<< "\tr> remove a stock" << endl

<< "\ts> search for a stock" << endl
<< "\tl> list all the stocks" << endl
<< "\tq> quit the application" << endl << endl;
}

char getCommand()
{
char cmd;
cout << "Please enter your choice (a, r, s, l or q):";
cin >> cmd;
cin.ignore(100, '\n');
return tolower(cmd);
}

void executeCmd(char command, hashTable& aTable)
{
data stock;
char key[MAX_LEN];

switch(command)
{
case 'a': getStock(stock);
aTable.insert (stock);
cout << endl << "the stock has been saved in the database. " << endl;
break;
case 'r': getString("\nPlease enter the ticker symbol of the stock you want to remove: ", key);
aTable.remove(key);
cout << endl << key << " has been removed from the database. " << endl;
break;
case 's': getString("\nPlease enter the ticker symbol of the stock you want to search: ", key);
aTable.retrieve (key, stock);
cout << endl << "Information about " << key << ": " << endl << '\t' << stock << endl;
break;
case 'l': display(aTable);
break;
default: cout << "illegal command!" << endl;
break;
}
}
void display(const hashTable & aTable)
{
aTable.display();
}

void getStock(data & stock)
{
char tickerSymbol[MAX_LEN];
char name[MAX_LEN];
float netVal;
char date[MAX_LEN];
float yToD;

cout << "\nPlease enter information about the stock: " << endl;
getString("\tticker symbol: ", tickerSymbol);
getString("\tname: ", name);
netVal = getFloat("\tnet asset value: ");
getString("\tdate of that value: ", date);
yToD = getFloat("\tyear to date return: ");

stock.setTickerSymbol (tickerSymbol);
stock.setName (name);
stock.setNetValue (netVal);
stock.setDate (date);
stock.setYToD (yToD);
}
int getInt(char * prompt)
{
int temp;
cout << prompt;
cin >> temp;
while(!cin)
{
cin.clear ();
cin.ignore(100, '\n');
cout << "Illegal input -- try again: ";
cin >> temp;
}
cin.ignore(100, '\n');
return temp;
}
float getFloat(char * prompt)
{
float temp;
cout << prompt;
cin >> temp;
while(!cin)
{
cin.clear ();
cin.ignore(100, '\n');
cout << "Illegal input -- try again: ";
cin >> temp;
}
cin.ignore(100, '\n');
return temp;
}
void getString(char * prompt, char * input)
{
cout << prompt;
cin.get(input, MAX_LEN, '\n');
cin.ignore (100, '\n');
}

最佳答案

没关系,我的 data.dat 文件中的行末尾没有分号,所以它永远不会到达末尾。

关于c++ - 从文件中读入哈希表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33750521/

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