Advertisement
skb50bd

Lab-8 [Bonus] Reversed Linked List

Aug 5th, 2015
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. int idgen = 0;
  8.  
  9. class Car{
  10. private:
  11.     int id;
  12.     string manufacturer;
  13.     float mileage;
  14.     string color;
  15.     Car *next;
  16. public:
  17.     Car(): id(++idgen), manufacturer("N/A"), mileage(0.0), color("N/A"), next(NULL) {}
  18.     Car(string menf, float mlg, string clr): id(++idgen), manufacturer(menf), mileage(mlg), color(clr), next(NULL) {}
  19.     ~Car() {}
  20.  
  21.     void show() {
  22.         cout << left << setw(15) << "ID " << ": " << left << id << endl;
  23.         cout << left << setw(15) << "Manufacturer " << ": " << left << manufacturer << endl;
  24.         cout << left << setw(15) << "Color " << ": " << left << color << endl;
  25.         cout << left << setw(15) << "Mileage " << ": " << left << mileage << endl << endl;
  26.     }
  27.  
  28.     void read(){
  29.         cout << "Enter info for Car " << id << endl;
  30.         cout << "Enter Manufacturer: ";
  31.         cin >> manufacturer;
  32.         cout << "Enter Color: ";
  33.         cin >> color;
  34.         cout << "Enter Mileage: ";
  35.         cin >> mileage;
  36.     }
  37.  
  38.     string getMan() { return manufacturer;}
  39.     string getColor() { return color;}
  40.     float getMile() { return mileage;}
  41.     void setNext(Car *C) { next = C;}
  42.     Car * getNext() { return next;}
  43.     void setMile(float m) { mileage = m;}
  44. };
  45.  
  46. void showToyota(Car *C){
  47.     Car *Current;
  48.     for(Current = C; Current != NULL; Current = Current -> getNext())
  49.         if(Current -> getMan() == "Toyota" || Current -> getMan() == "toyota")
  50.             Current -> show();
  51. }
  52.  
  53. void showRed(Car *C){
  54.     Car *Current;
  55.     for(Current = C; Current != NULL; Current = Current -> getNext())
  56.         if(Current -> getColor() == "Red" ||Current -> getColor() == "red")
  57.             Current -> show();
  58. }
  59.  
  60. void show25(Car *C){
  61.     Car *Current;
  62.     for(Current = C; Current != NULL; Current = Current -> getNext())
  63.         if(Current -> getMile() >= 25.0)
  64.             Current -> show();
  65. }
  66.  
  67. void setToyota(Car *C){
  68.     Car *Current;
  69.     for(Current = C; Current != NULL; Current = Current -> getNext())
  70.         if(Current -> getMan() == "Toyota" || Current -> getMan() == "toyota"){
  71.             Current -> setMile(40.0);
  72.             Current -> show();
  73.         }
  74. }
  75.  
  76.  
  77. int main(){
  78.     Car *Front = new Car();
  79.     Car *Current;
  80.     Car *Last;
  81.  
  82.     Current = Front;
  83.  
  84.     while(true) {
  85.         Last = Current;
  86.         Current -> read();
  87.  
  88.         char cont;
  89.         cout << "Do you want to continue (y/n)? ";
  90.         cin >> cont;
  91.         cout << endl;
  92.         if(cont == 'n' || cont == 'N') break;
  93.         Current = new Car();
  94.         Current -> setNext(Last);
  95.     }
  96.  
  97.     cout << endl << endl << "PRINTING ALL" << endl;
  98.     for(Current = Last; Current != NULL; Current = Current -> getNext())
  99.         Current -> show();
  100.  
  101.     cout << endl << endl << "PRINTING ALL TOYOTA" << endl;
  102.     showToyota(Last);
  103.     cout << endl << endl << "PRINTING ALL RED" << endl;
  104.     showRed(Last);
  105.     cout << endl << endl << "PRINTING ALL 25+" << endl;
  106.     show25(Last);
  107.     cout << endl << endl << "setting All TOYOTA 40" << endl;
  108.     setToyota(Last);
  109.  
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement