Advertisement
tiffprag

DSA Asgn 2 int main(binary search problem)

Nov 7th, 2019
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.47 KB | None | 0 0
  1. #include <iostream>
  2. #define MAX 5
  3. #include <iomanip>
  4. #include <stdlib.h> //for exit() function
  5. using namespace std;
  6.  
  7. class Reservation{
  8. private:
  9.   int bookingNo;
  10.   string name;
  11.   int phoneNo;
  12.   int paxNo;
  13.   int time;
  14.   int date;
  15.  
  16. public:
  17.   Reservation(){
  18.     bookingNo = 1;
  19.     name = "Tan Phit Huan";
  20.     phoneNo = 0123456777;
  21.     paxNo = 1;
  22.     time = 1500;
  23.     date = 20190101;
  24.   }
  25.   Reservation(int bNo, string na, int phNo, int pxNo, int t, int da){
  26.     bookingNo = bNo;
  27.     name = na;
  28.     phoneNo = phNo;
  29.     paxNo = pxNo;
  30.     time = t;
  31.     date = da;
  32.   }
  33.   void setBookingNo(int bNo){
  34.     bookingNo = bNo;
  35.   }
  36.   int getBookingNo(){
  37.     return bookingNo;
  38.   }
  39.   void setName(string na){
  40.     name = na;
  41.   }
  42.   string getName(){
  43.     return name;
  44.   }
  45.   void setPhoneNo(int phNo){
  46.     phoneNo = phNo;
  47.   }
  48.   int getPhoneNo(){
  49.     return phoneNo;
  50.   }
  51.   void setPaxNo(int pxNo){
  52.     paxNo = pxNo;
  53.   }
  54.   int getPaxNo(){
  55.     return paxNo;
  56.   }
  57.   void setTime(int t){
  58.     time = t;
  59.   }
  60.   int getTime(){
  61.     return time;
  62.   }
  63.   void setDate(long da){
  64.     date = da;
  65.   }
  66.   int getDate(){
  67.     return date;
  68.   }
  69.   void display(){
  70.     int monthNYear = date/100;
  71.  
  72.     int year = date/10000;
  73.     int month = monthNYear-(year*100);
  74.     int day = date - (monthNYear*100);
  75.  
  76.     cout<<"| "<<setfill(' ')<<bookingNo<<"   "
  77.     <<left<<setw(30)<<name
  78.     <<right<<setw(10)<<setfill('0')<<phoneNo
  79.     <<setw(12)<<setfill(' ')<<paxNo<<setw(6)<<setfill(' ')<<" "
  80.     <<setw(2)<<setfill('0')<< day <<"/"<<setw(2)<<setfill('0')<< month <<"/"<<setw(4)<<setfill('0')<< year<<setw(7)<<setfill(' ')<<" "
  81.     <<setw(4)<<setfill('0')<< time <<" |"<<endl;
  82.   }
  83. };
  84.  
  85. Reservation records[MAX];
  86.  
  87. //Option 1: Display All sorted by...
  88. void displayAll();
  89. //Option 1: Search & Display...
  90. void searchNDisplay();
  91.  
  92. int main(){
  93.   for(int i = 0; i<MAX; i++){
  94.     records[i].setBookingNo(i+1);
  95.   }
  96.  
  97.   records[0].setName("Tan Phit Huan");
  98.   records[1].setName("Khong Ziv Hale");
  99.   records[2].setName("Rex Luah");
  100.   records[3].setName("Lee Yeu Gor");
  101.   records[4].setName("Tiffany Pragasam");
  102.  
  103.   for(int i = 0; i<MAX; i++){
  104.     records[i].setPhoneNo(123456777+i);
  105.   }
  106.  
  107.   records[0].setPaxNo(2);
  108.   records[1].setPaxNo(5);
  109.   records[2].setPaxNo(7);
  110.   records[3].setPaxNo(4);
  111.   records[4].setPaxNo(10);
  112.  
  113.   records[0].setDate(20191212);
  114.   records[1].setDate(20191111);
  115.   records[2].setDate(20191111);
  116.   records[3].setDate(20191212);
  117.   records[4].setDate(20191211);
  118.  
  119.   for(int i = 0; i<MAX; i++){
  120.     records[i].setTime(1200+(i*100));
  121.   }
  122.  
  123.   char choice;
  124.   cout << "===============RESTAURANT RESERVATION SYSTEM II===============" << endl;
  125.   cout << "Please enter your selection" << endl <<
  126.     "1. Display All Sorted by..." << endl <<
  127.     "2. Search & Display..." << endl <<
  128.     "3. Given...display all..." << endl <<
  129.     "4. Exit" << endl;
  130.  
  131.   cout << "Choice: ";
  132.   cin >> choice;
  133.   cin.clear();
  134.   cin.ignore(1, '\n');
  135.   system("CLS");
  136.  
  137.   switch (choice) {
  138.  
  139.   //Display All Sorted by...
  140.   case '1':
  141.     displayAll();
  142.  
  143.   //Search...
  144.   case '2':
  145.     searchNDisplay();
  146.  
  147.   //Given...display all
  148.   case '3':
  149.  
  150.  
  151.   case '4':
  152.     exit(1);
  153.  
  154.   default:
  155.     cout << choice << " is not valid choice" << endl;
  156.     main();
  157.   }
  158.  
  159.   return 0;
  160. }
  161.  
  162. //Bubble sort by Name
  163. void bubbleSortName(Reservation A[]);
  164. //Bubble sort by Date
  165. void bubbleSortDate(Reservation A[]);
  166.  
  167. //Option 1 : Display All sorted by...
  168. void displayAll(){
  169.   char choice;
  170.   cout << "-----------------=DISPLAY ALL=------------------" << endl;
  171.   cout << "Display all sorted by..." << endl <<
  172.   "1. Name" << endl <<
  173.   "2. Date" << endl<<
  174.   "3. Back to Main Menu"<< endl;
  175.   cout << "Choice: ";
  176.   cin >> choice;
  177.   cin.clear();
  178.   cin.ignore(1, '\n');
  179.   system("CLS");
  180.  
  181.   switch (choice) {
  182.  
  183.   //Display all sorted by name
  184.   case '1':
  185.     bubbleSortName(records);
  186.  
  187.     cout<<setfill(' ')<<setw(5)<<"| No. "
  188.     <<left<<setw(30)<<"Name"
  189.     <<setw(5)<<"Phone No."
  190.     <<right<<setw(16)<<"Pax No."
  191.     <<setw(10)<<"Date"
  192.     <<setw(16)<<"Time |"<<endl
  193.     <<"|"<<right<<setfill('=')<<setw(86)<<"|"<<endl;
  194.    
  195.     for(int i = 0; i<MAX; i++){
  196.       records[i].display();
  197.     }
  198.  
  199.     break;
  200.  
  201.   //Display all sorted by date
  202.   case '2':
  203.     bubbleSortDate(records);
  204.  
  205.     cout<<setfill(' ')<<setw(5)<<"| No. "
  206.     <<left<<setw(30)<<"Name"
  207.     <<setw(5)<<"Phone No."
  208.     <<right<<setw(16)<<"Pax No."
  209.     <<setw(10)<<"Date"
  210.     <<setw(16)<<"Time |"<<endl
  211.     <<"|"<<right<<setfill('=')<<setw(86)<<"|"<<endl;
  212.    
  213.     for(int i = 0; i<MAX; i++){
  214.       records[i].display();
  215.     }
  216.  
  217.     break;
  218.  
  219.   case '3':
  220.     main();
  221.  
  222.   default:
  223.     cout << choice << " is not valid choice" << endl;
  224.     displayAll();
  225.   }
  226.  
  227.   cout << "Press enter to continue . . .";
  228.   cin.clear();
  229.   cin.ignore(1000, '\n');
  230.   system("CLS");
  231.   main();
  232. }
  233.  
  234. //Bubble sort by Name
  235. void bubbleSortName(Reservation A[]){
  236.     int i,j;
  237.     Reservation current;
  238.     for(i=0; i<MAX;i++){  
  239.       for (j=0; j<MAX-1; j++){  
  240.         if(A[j].getName()>A[j+1].getName()){              
  241.           current = A[j];              
  242.           A[j] = A[j+1];              
  243.           A[j+1] = current;          
  244.         }        
  245.       }          
  246.     }
  247. }
  248.  
  249. //Bubble sort by Date
  250. void bubbleSortDate(Reservation A[]){
  251.     int i,j;
  252.     Reservation current;
  253.     for(i=0; i<MAX;i++){  
  254.       for (j=0; j<MAX-1; j++){  
  255.         if(A[j].getDate()>A[j+1].getDate()){              
  256.           current = A[j];              
  257.           A[j] = A[j+1];              
  258.           A[j+1] = current;          
  259.         }        
  260.       }          
  261.     }
  262. }
  263.  
  264. //Bubble sort by Phone Number
  265. void bubbleSortPhoneNo(Reservation A[]){
  266.     int i,j;
  267.     Reservation current;
  268.     for(i=0; i<MAX;i++){  
  269.       for (j=0; j<MAX-1; j++){  
  270.         if(A[j].getPhoneNo()>A[j+1].getPhoneNo()){              
  271.           current = A[j];              
  272.           A[j] = A[j+1];              
  273.           A[j+1] = current;          
  274.         }        
  275.       }          
  276.     }
  277. }
  278.  
  279. //Search Phone No Record
  280. int binarySearch(Reservation A[], int key, int low, int high);
  281.  
  282. //Option 2 : Search...
  283. void searchNDisplay(){
  284.   char choice;
  285.   int foundIndex;
  286.   cout << "-----------------=SEARCH & DISPLAY=------------------" << endl;
  287.   cout << "Search..." << endl <<
  288.   "1. Phone Number" << endl <<
  289.   "2. Name" << endl<<
  290.   "3. Back to Main Menu"<< endl;
  291.   cout << "Choice: ";
  292.   cin >> choice;
  293.   cin.clear();
  294.   cin.ignore(1, '\n');
  295.   system("CLS");
  296.  
  297.   switch (choice) {
  298.  
  299.   //Search by Phone Number
  300.   case '1':
  301.   int phoneNo;
  302.   cout<<"Enter phone number : ";
  303.   cin>>phoneNo;
  304.   bubbleSortPhoneNo(records);
  305.   foundIndex = binarySearch(records, phoneNo, 0, MAX-1);
  306.   cout<<foundIndex<<endl;
  307.  
  308.   //Search by Name
  309.   case '2':
  310.  
  311.  
  312.   case '3':
  313.     main();
  314.  
  315.   default:
  316.     cout << choice << " is not valid choice" << endl;
  317.     displayAll();
  318.   }
  319.  
  320.   cout << "Press enter to continue . . .";
  321.   cin.clear();
  322.   cin.ignore(1000, '\n');
  323.   system("CLS");
  324.   main();
  325. }
  326.  
  327. //Search Phone No Record
  328. int binarySearchPhoneNo(Reservation A[], int key, int low, int high){    
  329.   int middle; //the middle index number of the array    
  330.   while (low <= high ){      
  331.     middle = (low+high)/2;
  332.     if(key==A[middle].getPhoneNo()){
  333.       return middle;
  334.  
  335.     }else if(key<A[middle].getPhoneNo()){
  336.       high = middle-1; //reset high index to left side
  337.  
  338.     }else{
  339.       low = middle+1; //reset low index to right side
  340.  
  341.     }
  342.   }
  343.  
  344.   return -1;   //key not found
  345. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement