Пример #1
0
void Essai3()
{
    cout << "----- 3. Gestion de plusieurs exceptions simultanement ---" << endl;
    // A COMPLETER : Traitez TOUTES les exceptions susceptible d'etre lancee par le bloc de code suivant (try...catch)

    // ...
    {
        Employee e;
        cout << "Encodez un nouvel employe :" << endl;
        cin >> e;
        cout << "Saisissez son mot de passe : ";
        char txt[80];
        cin.getline(txt,80,'\n');
        try {
            e.setPassword(txt);
        } catch(InvalidPasswordException ex) {
            e.display();
        }
        cout << endl << "Voici l'employe encode :" << endl;
        cout << e << endl;
        cout << "et son mot de passe : " << e.getPassword() << endl << endl;
        cout << "Nouvelle fonction : ";
        cin.getline(txt,80,'\n');
        try {
            e.setFunction(txt);
        } catch(InvalidFunctionException ex) {
            e.display();
        }
        cout << "Voici la nouvelle fonction encodee : " << e.getFunction() << endl;
    }
    // ...

    cout << endl;
}
int main()
{
	Employee* alice = new Employee("Alice", "Wonderland", 3211232, ENGINEERING);
	Employee* bob = new Employee("Bob", "Marley", 3211232, MARKETING);
	Employee* charlie = new Employee("Charlie", "Gargia", 3211232, MARKETING);
	Employee* dan = new Employee("Daniel", "Craig", 123123, HR);
	Employee* edward = new Employee("Edward", "Norton", 21211212, ENGINEERING);
	Employee* frank = new Employee("Frank", "Herbert", 233223, SALES);
	Employee* gregory = new Employee("Gregory", "Peck", 344334, MARKETING);

	EmployeeBinarySearchTree* tree = new EmployeeBinarySearchTree();

	tree->insert(dan);
	tree->insert(bob);
	tree->insert(alice);
	tree->insert(charlie);
	tree->insert(frank);
	tree->insert(edward);
	tree->insert(gregory);

	/*
		The tree now looks like this:

					Daniel
			Bob				Frank
		Alice		Charlie		Edward		Gregory
	*/

	cout << endl << "In Order: " << endl;
	tree->traverseInOrder();

	cout << endl << "Pre Order: " << endl;
	tree->traversePreOrder();

	cout << endl << "Post Order: " << endl;
	tree->traversePostOrder();

	cout << endl << "Depth First Search: " << endl;
	Employee* found = tree->depthFirstSearch("Bob");
	if (found != NULL)
	{
		found->display();
	}
	else
	{
		cout << "Not Found" << endl;
	}

	cout << endl << "Breadth First Search: " << endl;
	found = tree->breadthFirstSearch("Frank");
	if (found != NULL)
	{
		found->display();
	}
	else
	{
		cout << "Not Found" << endl;
	}
	getchar();
}
Пример #3
0
	void displayInOrder(Employee* root) {
		if (root == NULL)
			return;
		displayInOrder(root->left);
		root->display();
		displayInOrder(root->right);
	}
Пример #4
0
int main(int argc, char const *argv[])
{
	Employee first;
	Employee second("second");
	Employee third(second);
	first.display();
	second.display();
	third.display();
	return 0;
}
Пример #5
0
int main() {
	Employee alice = {"Alice", "Wonderland", -123.32f};
	alice.display();

	alice.setSalary(-125.32);
	alice.display();

	Employee bob("Bob", "Marley", 21.2);
	bob.display();

	Employee charlie("Chalie", "Garcia");
	charlie.display();

	cout << bob.getSalary() << endl;

	Employee* ptr = &alice;
	ptr = &bob;
	ptr = &charlie;

	ptr->setSalary(123.32);
	ptr->display();

	ptr = &bob;
	ptr->display();

	ptr = new Employee("Dan", "Akroid", 234.43);
	ptr->display();
	
	delete ptr;

	ptr = &alice;
	ptr->display();
}
int main()
{
    const int MAX = 100;

    string C1[] = {"TND012", "TNG033", "TNK031"};
    string C2[] = {"TNCG018", "TND004"};

    Teacher T("Anna", C2, 2);
    Programmer P1("Aida", C1, 3, "Smalltalk");
    Researcher R("Monika", "Swearing theory");

    Employee E;
    E = T;  //slicing
    E.display();
    cout << endl;

    //Teacher T1(T);
    Teacher T1;
    T1 = T;

    T1.display();
    cout << endl;

    T.display();
    cout << endl;
    T.set_name("Olivia Bla"); //inherited member function
    T.display();
    cout << endl;

    P1.display();
    cout << endl;

    R.display();
    cout << endl;

    Programmer P2 = P1; //copy constructor of class Programmer is called
    //Programmer P2(P1); // same as Programmer P2 = P1;

    P2.display();
    cout << endl;

    P2 = P2; //test self-assignment

    P2.display();
    cout << endl;

   return 0;
}
Пример #7
0
int main()
{
	/*
	HourlyEmployee bob("Bob", 40, 100);
	bob.setHours((float)60);

	setName("ewq");

	Employee alice("Alice");
	alice.setName("Wonderland");
	alice.setName("Dr.", "Alice");

	cout << alice.getName() << endl;

	cout << "Inherited display()" << endl;
	alice.display();
	bob.display("Bob's Info:");
	bob.display();

	Employee e[] = {bob, alice};

	double total = 0.0;
	for (int i = 0; i < 2; i++)
	{
		total += e[i].getPaycheck();
	}

	cout << "Total: " << endl;
	cout << total << endl;
*/
	HourlyEmployee* he = new HourlyEmployee("Alice", 12.23, 34.45);
	FulltimeEmployee* fe = new FulltimeEmployee("Bob", 123321);
	he->display();
	fe->display();

	Employee* e = he;
	e->display();

	Employee* es[] = { he, fe };
	es[0]->display();
	es[1]->display();

	getchar();
}
Пример #8
0
int main()
{
	Employee* rootB = NULL;

	Employee* A = new Employee("A");
	Employee* B = new Employee("B");
	Employee* C = new Employee("C");
	Employee* D = new Employee("D");
	Employee* E = new Employee("E");
	Employee* F = new Employee("F");
	Employee* G = new Employee("G");

	insert(D, &rootB);
	insert(B, &rootB);
	insert(C, &rootB);
	insert(A, &rootB);
	insert(F, &rootB);
	insert(E, &rootB);
	insert(G, &rootB);

	cout << endl << "Display In Order Root B" << endl;
	displayInorder(rootB);
	cout << "Done" << endl;

	Employee* alice = new Employee("Alice");
	Employee* bob = new Employee("Bob");
	Employee* charlie = new Employee("Charlie");

	Employee* root = NULL;

	root = bob;
	bob->left = alice;
	bob->right = charlie;

	// in order display
	root->left->display();
	root->display();
	root->right->display();

	Employee* dan = new Employee("Dan");
	Employee* ed = new Employee("Edward");
	Employee* frank = new Employee("Frank");

	ed->right = frank;
	ed->left = dan;

	charlie->right = ed;

	cout << endl << "Display Preorder Tree" << endl;
	displayPreorder(root);
	cout << endl << "Display Postorder Tree" << endl;
	displayPostorder(root);
	cout << endl << "Display In Order Tree" << endl;
	displayInorder(root);

	cout << endl << "Search for Edward" << endl;
	Employee* found = search("Edward", root);
	if (found != NULL)
	{
		found->display();
	}
	else
	{
		cout << "Not found" << endl;
	}

	getchar();
}
Пример #9
0
int main()
{
	cout << "\nPolynomials" << endl;

	float c1[] = { 12, 23, 34, 45 };
	float c2[] = { 21, 32, 43, 54, 67,78 };

	Polynomial p1(3, c1);
	Polynomial p2(3, c2);

	p1.display();
	p2.display();

	Polynomial p3 = p1.plus(p2);

	p3.display();

	cout << "\nStructures" << endl;

	Employee * charlie = new Employee(); // default constructor
	charlie->name = "Charlie";
	charlie->salary = 22222;
	charlie->display();

	Employee * dan = new Employee("Dan", 321123); // all argument constructor
	dan->display();

	Employee bob("Bob", 321123);

	Employee alice;
	alice.name = "Alice";
	alice.salary = 100000;

	alice.display();
	alice.giveRaise(0.05);
	alice.display();


	MyInteger int1;
	MyInteger int2;
	MyInteger int3 = addMyIntegers(int1, int2);

	int3 = int1.add(int2);

	MyFloat float1;
	MyFloat float2;
	MyFloat float3 = addMyFloats(float1, float2);

	float3 = float1.add(float2);

	if (1 == 1) exit(0);

	cout << "\nDynamic Arrays" << endl;

	int arr1[] = { 1, 2, 3, 4 };
	for (int i = 0; i < 4; i++){
		cout << arr1[i] << endl;
	}

	int size;
	cout << "Size: ";
	cin >> size;

	int * arr2 = new int[size];

	for (int i = 0; i < size; i++) {
		cin >> arr2[i];
	}

	for (int i = 0; i < size; i++) {
		cout << arr2[i] << endl;
	}

	cout << "\nPointers" << endl;

	int a = 123;
	int aa = 234;
	char b = 'Q';

	cout << a << endl;
	cout << aa << endl;
	cout << b << endl;

	int * p = &a;

	cout << p << endl;
	cout << *p << endl;

	*p = 321;

	cout << *p << endl;
	cout << a << endl;

	p = &aa;

	cout << p << endl;
	cout << *p << endl;
	cout << aa << endl;
	changeInteger(&aa, 432);
	cout << aa << endl;

}
Пример #10
0
/* Main Function, follows these steps:
1. get filename
2. read in xml records and create an employee by calling Employee::fromXML(steam);
3. cout each employee object using display()
4. write employee objects to a new file 'employee.bin'
5. clear employees vector
6. iterate through new file and reload employees vector with employee records
7. print out each employee record as xml. toXML()
8. search for employee 12345. retrieve()
9. change salary of retrieved employee to 150,000.0
10. write employee back to the file
11. retrieve the same employee again and print its salary
12. create a new employee with my choice of data!! such freedom
13. store my new employee. store()
14. retrieve and display my new employee
*/
int main(int argc, char* argv[])
{
    // Check for arg
    if (argv[1] == nullptr) {
        //throw a fit here
        cout << "no arguments passed... noob" << endl;
        return 0;
    }

    // Step 1: get file name and open file
    fstream file;
    file.open(argv[1]);
    if (file.fail()) {
        cout << "File could not open... exiting" << endl;
        return 0;
    }

    vector<unique_ptr<Employee>> employees; // init employees vector

    // Step 2: read in employee objects from xml file
    while (!file.eof()) {
        try {
            Employee * emp = Employee::fromXML(file);
            if (emp != nullptr) {
                employees.push_back(unique_ptr<Employee>(emp));
            }
        }
        catch (runtime_error e) {
            cout << e.what() << endl;
        }
    }
    // Step 3: display each employee record
    for (int i = 0; i < employees.size(); i++) {
        employees[i]->display(cout);
    }

    // Step 4: write employee objects to a new file 'employee.bin'
    fstream outFile("employee.bin", ios::out | ios::binary | ios::trunc);
    if (!outFile.fail() && !outFile.bad()) {
        for (int i = 0; i < employees.size(); i++) {
            employees[i]->write(outFile);
        }
    }
    outFile.close();

    // Step 5: clear employees vector
    employees.clear();

    // Step 6: iterate through new file and reload employees vector with employee records
    fstream inFile("employee.bin", ios::in | ios::out | ios::binary);
    if (!inFile.fail() && !inFile.bad()) {
        Employee* emp = Employee::read(inFile);
        while (emp->validateEmployeeData()) {
            employees.push_back(unique_ptr<Employee>(emp));
            emp = Employee::read(inFile);
        }
    }

    // Step 7: print out each employee record as xml.toXML()
    for (int i = 0; i < employees.size(); i++) {
        employees[i]->toXML(cout);
    }

    // Step 8: search for employee 12345. retrieve()
    int idToSearch = 12345;
    Employee* emp = Employee::retrieve(inFile, idToSearch);

    // Step 9: change salary of retrieved employee to 150, 000.0
    double newSalary = 150000;
    if (emp != nullptr) {
        emp->setSalary(newSalary);
    }

    // Step 10: write employee back to the file
    emp->store(inFile);

    // Step 11: retrieve the same employee again and print its salary
    emp = Employee::retrieve(inFile, idToSearch);
    cout << "Employees new salary: " << emp->getSalary() << endl;

    // Step 12: create a new employee with my choice of data!!such freedom
    Employee* goku = new Employee(9001, "Goku", "out in the woods", "Somewhere", "NA", "NA", "1-800-OVER-9000", 9001);

    // Step 13: store my new employee.store()
    goku->store(inFile);

    // Step 14: retrieve and display my new employee
    goku = Employee::retrieve(inFile, 9001);
    goku->display(cout);

    // We're Done!!! huzzah!
    inFile.close();
    delete goku;
    employees.clear();


    cin.get();
}
Пример #11
0
int main(int argc, char** argv) {

	//Check for arg
	if(argc < 2)
		error("no file name provided. Exiting.");

	//Verify file exists
	std::filebuf buffer;
	if(!buffer.open(argv[1],std::ios::in))
		error("Error opening file. Verify it exists.");

	std::istream in(&buffer);	

	//Verify tag structure
	if(!pass1(&in))
		error("mismatched tag brackets. \nPlease verify correct structure.");
	
	std::filebuf buffer2;
	buffer2.open(argv[1],std::ios::in);
	std::istream in2(&buffer2);
	if(!pass2(&in2))
		error("Mismatched tags. Please verify all tags have matching close tag");
	
	std::filebuf buffer3;
	buffer3.open(argv[1],std::ios::in);
	std::istream in3(&buffer3);

	std::vector<Employee*> employees;	

	//Read in Employees to XML 
	Employee* e;
	while(!in3.eof())
	{
		char c = in3.peek();
		if(isspace(c) || c == EOF)
		{
			in3.get();
			continue;
		}

//std::cout << in3.peek() << std::endl;
//		std::cout << in3.eof() << std::endl;
		e = Employee::fromXML(in3);

		if(e == nullptr)
			error("Missing or incorrect employee attribute.");
		employees.push_back(e);
	}
	//delete e;
	
	buffer3.close();

	//Print out employees to std::cout
	for(int i=0; i < employees.size(); i++)
	{
		employees[i]->display(std::cout);
		std::cout << std::endl;
		std::cout.flush();
	}
	std::cout << std::endl;

	//Print out employees to file
	//
	//this is stupid and kind of hack-y but I did it for a good reason. 
	//When opening my fstream below, opening with the 'in' and 'out' params
	//consistently failed. I presume because when doing so fstream tries to 
	//open an istream from a non-existent file.
	//
	//So instead I open, create file, close, and reopen with in,out, and binary.
	std::fstream fs(OUTPUT_FILE,std::ios::out);
	fs.close();
	fs.open(OUTPUT_FILE,std::ios::in | std::ios::out | std::ios::binary);
	if(fs.fail())
		error("Failed to open fstream.");
	for(int i = 0; i < employees.size(); i++)
	{
		employees[i]->write(fs);
		fs << "\n";
//		delete employees[i];
	}
	fs.flush();

	//clear vector
	employees.clear();

	//reset fstream to beginning of file - for whatever reason seekg only wored once.
	//had to close and reopen for each ensuing operation./
	refreshFstream(fs);

	//repopulate vector with calls to read (from employee.bin...correct???)
	while(!fs.eof())
	{
		if(isspace(fs.peek()))
		{
			fs.get();
			continue;
		}

		Employee* e = Employee::read(fs);
		if(e != nullptr)
			employees.push_back(e);
	}

	//print to cout XML representation of employees
	for(int i = 0; i < employees.size(); i++)
	{
		employees[i]->toXML(std::cout);
		std::cout << "\n";
//		delete employees[i];
	}
	std::cout << std::endl;

	//reset fstream to beginning of file
	fs.clear();
	fs.seekg(0,fs.beg);

	//find employee 12345 and print out
	e = Employee::retrieve(fs,12345);

	std::cout << "Found:" << std::endl;
	e->display(std::cout);
	std::cout << std::endl;

	//update salary
	e->setSalary(150000.0);

	//store in XML file
	refreshFstream(fs);
	e->store(fs);

//	delete e;

	//Retrieve and print salary
	refreshFstream(fs);
	e = Employee::retrieve(fs,12345);
	std::cout << e->getSalary() << "\n" << std::endl;

//	delete e;

	//is it awesome or pathetic that I knew these lyrics? I'll let you be the judge.
	Employee* my_e = new Employee(1987,std::string("Rick Astley"),std::string("Never Gonna Give You Up street"),std::string("Never Gonna Let You Down city"),std::string("Never Gonna Run Around and Desert You state"),std::string("England"),std::string("1-888-RICKROLLED"),1000.0);

	refreshFstream(fs);
	my_e->store(fs);
	fs.flush();
	
	//reset fs for retrieval and printing of Mr. Astley
	refreshFstream(fs);
	e = Employee::retrieve(fs,1987);
	e->display(std::cout);
	std::cout << std::endl;

//	delete e;

	fs.close();
//	delete my_e;
//	delete e;
}