Ejemplo n.º 1
0
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();
		}
	}