void read_stu(HashTable<Student> &h, HashTable2<Student> & h2) { std::ifstream infile; infile.open("E://info.txt", ios::in); string line; while(getline(infile, line)) { string id = ""; string name = ""; int age; int sex; string temp = ""; int len = line.length(); int i; id = line.substr(0, 18); name = line.substr(19, 6); temp = line.substr(26, 2); age = str_to_i(temp); temp = line[29]; if (temp[0] == '0') sex = 0; else sex = 1; Student stu(id, name, age, sex); Student info(id, name, age, sex); h.insert(stu); h2.insert(info); totalNumber++; if (infile.eof()) { break; } } infile.close(); }
int main() { //create_stu(); welcome(); command_tips(); cout << endl; srand(time(0)); HashTable<Student> h; HashTable2<Student> table; //read_stu(h, table); string command; while(cin >> command) { if (command[0] == '$') { if (command[1] == 'Q') break; switch(command[1]) { case 'I': { cout << "Please input a student's information (ID, name, age, gender):" << endl; string id, name; int age; bool sex; cin >> id >> name >> age >> sex; Student student(id, name, age, sex); if (h.insert(student)) { cout << "\nInserted successfully: " << endl; //print the information that is removed cout << "Name: " << student.getName() << endl; cout << "ID: " << student.getID() << endl; cout << "Age: " << student.getAge() << endl; cout << "Gender: " << (student.getSex() == 0? "Male":"Female") << endl << endl; } else { cout << "\nInserted failed: The system already contains the student" << endl; } } break; case 'R': { cout << "Please input the student's ID:" << endl; string s2; cin >> s2; h.remove(s2); cout << endl; } break; case 'F': { cout << "Please input the student's ID:" << endl; string s2; cin >> s2; h.findKey(s2); cout << endl; } break; case 'T': { cout << "Please wait for inputing the information from the file." << endl; read_stu(h, table); cout << "Input finished" << endl; cout << "Tatal numbers of student: " << totalNumber << endl; vector<list<Student> > ve = h.getList(); double ave_CHI = 0, ave_OPEN = 0; int num = 30; for (int i = 0; i < num; i++) { list<Student>::iterator p = ve[rand() % ve.size()].end(); string s1 = (*(--p)).getID(); double CHAIN, OPEN; ave_CHI += (double)h.cal_time(s1) ; ave_OPEN += (double)table.cal_time(s1); cout << "chaning: " << setw(8) << (double)h.cal_time(s1) << " s" << " open addressing: " << setw(8) << (double)table.cal_time(s1) << " s" << endl; Sleep(100); } cout << "Average time with chaining: " << (double)(ave_CHI / num) << " s"<< endl; cout << "Average time with open addressing: " << (double)(ave_OPEN / num) << " s" << endl; cout << endl; } break; default: { cout << "Command error! Please enter a right command!" << endl; command_tips(); } break; } } else { cout << "Command error! Please enter a right command!" << endl; command_tips(); } }
int main( int argc, char *argv[] ) { string dicType = argv[1]; string hashType = argv[2]; string filename = argv[3]; HashTable<string> smallChain; HashTable<string> largeChain; HashTable2<string> smallProbe; HashTable2<string> largeProbe; int collission = 0; list<int> l; list<int> l2; ifstream myfile("small.txt"); string line; while(getline(myfile, line)) { smallChain.insert(line); } myfile.close(); ifstream myfile2("small.txt"); while(getline(myfile2, line)) { smallProbe.insert(line); collission = smallProbe.countC(line); l.push_back(collission); } myfile2.close(); ifstream myfile3("large.txt"); while(getline(myfile3, line)) { largeChain.insert(line); } myfile3.close(); ifstream myfile4("large.txt"); while(getline(myfile4, line)) { largeProbe.insert(line); collission = largeProbe.countC(line); l2.push_back(collission); } myfile4.close(); string outputFilename = filename; string outputFilename2 = filename; string outputFilename3 = filename; if(filename.find('.') != string::npos) { int index = filename.find('.'); outputFilename.erase(index, outputFilename.size() - 1); outputFilename2.erase(index, outputFilename2.size() - 1); outputFilename3.erase(index, outputFilename3.size() - 1); } outputFilename += ".out"; outputFilename2 += ".stats"; outputFilename3 += "2.txt"; ifstream myfile5(filename.c_str()); ofstream outfile(outputFilename.c_str()); ofstream outfile2(outputFilename2.c_str()); ofstream outfile3(outputFilename3.c_str()); while(getline(myfile5, line)) { istringstream ss(line); char value; while (ss >> value) { if ((value >= 97 && value <= 122) || (value >= 65 && value <= 90)) { value = tolower(value); } else { value = ' '; } outfile3 << value; if (ss.peek() == ' ') { outfile3 << ' '; } } outfile3 << endl; } myfile5.close(); ifstream myfile6(outputFilename3.c_str()); //outfile3 is the processed text file if (dicType == "-1") { if (hashType == "-chain") { while(getline(myfile6, line)) { istringstream ss(line); string value; while (ss >> value) { if (smallChain.contains(value) == false) { outfile << value << endl; } } } vector<int> a(50, 0); a = smallChain.countLists(a); for (int i = 0; i < a.size(); i++) { if (a[i] != 0) { outfile2 << i << " " << a[i] << endl; } } } else if (hashType == "-probe") { while(getline(myfile6, line)) { istringstream ss(line); string value; while (ss >> value) { if (smallProbe.contains(value) == false) { outfile << value << endl; } } } l.sort(); list<int>::iterator itr = l.begin(); int biggest = l.back(); vector<int> v; v.resize(biggest + 1); for (int i = 0; i < l.size(); i++) { v[*itr]++; itr++; } for (int i = 0; i < v.size(); i++) { outfile2 << i << " " << v[i] << endl; } } else {