pclovechristian

Dog

May 2nd, 2012
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <cstdlib>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. class Dog
  10. {
  11.     private:
  12.     string name;
  13.     int age;
  14.     string breed;
  15.     public:
  16.     Dog();
  17.     Dog(string n, string br, int a);
  18.     void Display();
  19.     string GetName();
  20.  
  21. };
  22.  
  23. void Dog::Display()
  24. {
  25.     cout << "Name: " << name << endl;
  26.     cout << "Breed: " << breed << endl;
  27.     cout << "Age: " << age << endl;
  28.  
  29. }
  30.  
  31. string Dog::GetName()
  32. {
  33.     return name;
  34. }
  35.  
  36. class List
  37. {
  38.     private:
  39.     vector<Dog> l;
  40.     int FindNext(string name);
  41.  
  42.     public:
  43.     List();
  44.     void Display();
  45.     bool Add(Dog d);
  46.     bool Delete(string name);
  47.     bool Search(string name, Dog& d);
  48.     int Count();
  49.     int menu();
  50. };
  51.  
  52.  
  53. int List::Count()
  54. {
  55.     return l.size();
  56. }
  57.  
  58. void List::Display()
  59. {
  60.     if(Count()==0)
  61.     {
  62.         cout << "List is empty." << endl;
  63.     }
  64.         else
  65.         {
  66.             for(int i =0; i<< Count(); i++)
  67.             {
  68.                 l[i].Display();
  69.             }
  70.         }
  71. }
  72.  
  73. int List::FindNext(string name)
  74. {
  75.     int rv=0;
  76.     if(l.empty()==0)
  77.     int left =0;
  78.     {
  79.         int mid = l.size()/2;
  80.         bool done = false;
  81.         while(done==false)
  82.         {
  83.             if(l[mid].GetName()>=name)
  84.             {
  85.                 right = mid;
  86.                 mid=(mid+left)/2;
  87.             }
  88.             else
  89.             {
  90.                 left = mid+1;
  91.                 mid = (left+right)/2;
  92.             }
  93.             if(left==right)
  94.             done = true;
  95.         }
  96.         rv = mid;
  97.     }
  98.     return rv;
  99. }
  100.  
  101. bool List::Add(Dog d)
  102. {
  103.     int pos = FindNext(d.GetName());
  104.     l.push_back(d);
  105.  
  106.     for(int i = l.size()-1; i>pos; i--)
  107.     {
  108.         l[i] = l[i-1];
  109.     }
  110.     l[pos] = d;
  111.     return true;
  112. }
  113.  
  114. bool List::Search(string name, Dog& d)
  115. {
  116.     bool rv =false;
  117.     bool done = false;
  118.     int mid = l.size()/2;
  119.     int left = 0;
  120.     int right = l.size()-1;
  121.     if(l.empty()==0)
  122.     {
  123.         while(done==false)
  124.         {
  125.             if(l[mid].GetName()==name)
  126.             {
  127.                 done = true;
  128.                 rv = true;
  129.             }
  130.             else
  131.             {
  132.                 if(l[mid].GetName()>name)
  133.                 {
  134.                     right = mid;
  135.                     mid = (left+right)/2;
  136.                 }
  137.                 else
  138.                     {
  139.                         left = mid+1;
  140.                         mid = (left+right)/2;
  141.                     }
  142.                     if(left==right)
  143.                         done = true;
  144.  
  145.                     if(l[mid].GetName()==name)
  146.                         rv = true;
  147.             }
  148.         }
  149.     }
  150.     if(rv=true)
  151.     d = l[mid];
  152.     return rv;
  153. }
  154.  
  155. int List::menu()
  156. {
  157.     int choice;
  158.         system("CLS"); //Flush the stream
  159.         cout << "--Menu--" << endl << endl;
  160.         cout << "1. Add Dog" << endl;
  161.         cout << "2. Delete Dog" << endl;
  162.         cout << "3. Animal Display" << endl;
  163.         cout << "4. Quit" << endl;
  164.         cin >> choice;
  165.  
  166.     while((choice < 0 || choice > 4) || (cin.fail() !=0))
  167.     {
  168.         if(cin.fail() !=0)
  169.         {
  170.             cout << "Error! Can't read it! \n" << endl << "Please type it again in number(1~4).";
  171.             cin.clear();
  172.             cin.ignore(256,'\n');
  173.         }
  174.         else if(choice < 0 || choice > 4)
  175.         {
  176.             cout << "Between 1~4 please." << endl; // between 1~4
  177.         }
  178.         cout << "--Menu--" << endl << endl;
  179.         cout << "1. Add Dog" << endl;
  180.         cout << "2. Delete Dog" << endl;
  181.         cout << "3. Animal Display" << endl;
  182.         cout << "4. Quit" << endl;
  183.         cin >> choice;
  184.     }
  185.     return choice;
  186. }
  187.  
  188.  
  189. int main()
  190. {
  191.     List Animal;
  192.     int m;
  193.     bool goOn = true;
  194.  
  195.     while(goOn==true)
  196.     {
  197.         m=Animal.menu();
  198.         switch(m)
  199.         {
  200.             case 1:// Sphere
  201.             {
  202.  
  203.                 break;
  204.             }
  205.             case 2:// Sort sphere
  206.             {
  207.                 string name;
  208.                 cout << "Name to delete? ";
  209.                 cin >> name;
  210.                 if(Animal.Delete(name)==false)
  211.                     cout << "Not found" <<endl;
  212.                 break;
  213.             }
  214.             case 3:// Displays all
  215.             {
  216.                 Animal.Display();
  217.                 break;
  218.             }
  219.             case 4: //Quit, couldn't find any method, other than exit(0);
  220.             {
  221.                 cout << "Thank you for using the program and See you later!";
  222.                 goOn = false;
  223.                 break;
  224.             }
  225.         }
  226.     }
  227.     return 0;
  228. }
Advertisement
Add Comment
Please, Sign In to add comment