Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cmath>
- #include <cstdlib>
- #include <string>
- using namespace std;
- class Dog
- {
- private:
- string name;
- int age;
- string breed;
- public:
- Dog();
- Dog(string n, string br, int a);
- void Display();
- string GetName();
- };
- void Dog::Display()
- {
- cout << "Name: " << name << endl;
- cout << "Breed: " << breed << endl;
- cout << "Age: " << age << endl;
- }
- string Dog::GetName()
- {
- return name;
- }
- class List
- {
- private:
- vector<Dog> l;
- int FindNext(string name);
- public:
- List();
- void Display();
- bool Add(Dog d);
- bool Delete(string name);
- bool Search(string name, Dog& d);
- int Count();
- int menu();
- };
- int List::Count()
- {
- return l.size();
- }
- void List::Display()
- {
- if(Count()==0)
- {
- cout << "List is empty." << endl;
- }
- else
- {
- for(int i =0; i<< Count(); i++)
- {
- l[i].Display();
- }
- }
- }
- int List::FindNext(string name)
- {
- int rv=0;
- if(l.empty()==0)
- int left =0;
- {
- int mid = l.size()/2;
- bool done = false;
- while(done==false)
- {
- if(l[mid].GetName()>=name)
- {
- right = mid;
- mid=(mid+left)/2;
- }
- else
- {
- left = mid+1;
- mid = (left+right)/2;
- }
- if(left==right)
- done = true;
- }
- rv = mid;
- }
- return rv;
- }
- bool List::Add(Dog d)
- {
- int pos = FindNext(d.GetName());
- l.push_back(d);
- for(int i = l.size()-1; i>pos; i--)
- {
- l[i] = l[i-1];
- }
- l[pos] = d;
- return true;
- }
- bool List::Search(string name, Dog& d)
- {
- bool rv =false;
- bool done = false;
- int mid = l.size()/2;
- int left = 0;
- int right = l.size()-1;
- if(l.empty()==0)
- {
- while(done==false)
- {
- if(l[mid].GetName()==name)
- {
- done = true;
- rv = true;
- }
- else
- {
- if(l[mid].GetName()>name)
- {
- right = mid;
- mid = (left+right)/2;
- }
- else
- {
- left = mid+1;
- mid = (left+right)/2;
- }
- if(left==right)
- done = true;
- if(l[mid].GetName()==name)
- rv = true;
- }
- }
- }
- if(rv=true)
- d = l[mid];
- return rv;
- }
- int List::menu()
- {
- int choice;
- system("CLS"); //Flush the stream
- cout << "--Menu--" << endl << endl;
- cout << "1. Add Dog" << endl;
- cout << "2. Delete Dog" << endl;
- cout << "3. Animal Display" << endl;
- cout << "4. Quit" << endl;
- cin >> choice;
- while((choice < 0 || choice > 4) || (cin.fail() !=0))
- {
- if(cin.fail() !=0)
- {
- cout << "Error! Can't read it! \n" << endl << "Please type it again in number(1~4).";
- cin.clear();
- cin.ignore(256,'\n');
- }
- else if(choice < 0 || choice > 4)
- {
- cout << "Between 1~4 please." << endl; // between 1~4
- }
- cout << "--Menu--" << endl << endl;
- cout << "1. Add Dog" << endl;
- cout << "2. Delete Dog" << endl;
- cout << "3. Animal Display" << endl;
- cout << "4. Quit" << endl;
- cin >> choice;
- }
- return choice;
- }
- int main()
- {
- List Animal;
- int m;
- bool goOn = true;
- while(goOn==true)
- {
- m=Animal.menu();
- switch(m)
- {
- case 1:// Sphere
- {
- break;
- }
- case 2:// Sort sphere
- {
- string name;
- cout << "Name to delete? ";
- cin >> name;
- if(Animal.Delete(name)==false)
- cout << "Not found" <<endl;
- break;
- }
- case 3:// Displays all
- {
- Animal.Display();
- break;
- }
- case 4: //Quit, couldn't find any method, other than exit(0);
- {
- cout << "Thank you for using the program and See you later!";
- goOn = false;
- break;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment