SUBANGKAR

Class_Bus

Sep 29th, 2016
91
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "Automobile.h"
  2. #include <cmath>
  3.  
  4. #define pii 3.1416
  5.  
  6. class Bus : public AutomobileWithManualXmission
  7. {
  8. public:
  9.  
  10.     Bus();
  11.  
  12.     void Move();
  13.     void SetDirections()
  14.     {
  15.         directionX = sin((totalAngle * pii / 180));
  16.         directionY = cos((totalAngle * pii / 180));
  17.     }
  18.     void SetMinAndMaxSpeedOfThisGear(int, int);
  19.     void TurnLeft();
  20.     void TurnRight();
  21.     void IncreaseSpeed();
  22.     void DecreaseSpeed();
  23.     void ShiftGearUp();
  24.     void ShiftGearDown();
  25.  
  26. private:
  27.  
  28.     int fuelEfficiency = 4;
  29.     int speedChangeRate = 6;
  30.     double angle = 15;
  31.     double totalAngle = 0;
  32.     int minSpd = 0;
  33.     int maxSpd = 0;
  34.     int GearSpeedRange[6][2] = { { 0,0 },{ 0,20 },{ 10,30 },{ 25,45 },{ 35,60 },{ 50,80 } };
  35. };
  36.  
  37.  
  38. Bus::Bus() : AutomobileWithManualXmission("Bus")
  39. {
  40.     SetInitialPosition(0, 0);
  41.     SetInitialDirection(0, 1);
  42.     speed = 0;
  43.  
  44.     fuel = 80;
  45.  
  46.     gearPosition = 0;
  47. }
  48.  
  49. void Bus::TurnLeft()
  50. {
  51.     totalAngle = totalAngle - angle;
  52.  
  53.     SetDirections();
  54. }
  55.  
  56. void Bus::TurnRight()
  57. {
  58.     totalAngle = totalAngle + angle;
  59.  
  60.     SetDirections();
  61. }
  62.  
  63. void Bus::IncreaseSpeed()
  64. {
  65.     if (speed < maxSpd)
  66.         speed = speed + speedChangeRate;
  67.  
  68.     if (speed > maxSpd) speed = maxSpd;
  69. }
  70.  
  71. void Bus::DecreaseSpeed()
  72. {
  73.     if (speed > minSpd)
  74.         speed = speed - speedChangeRate;
  75.  
  76.     if (speed < minSpd) speed = minSpd;
  77. }
  78.  
  79. void Bus::Move()
  80. {
  81.     if (fuel > 0)
  82.     {
  83.         fuel = fuel - (speed / 3600) / fuelEfficiency; // distance per second / fuelEfficiency
  84.  
  85.         posX = posX + (directionX * speed / 3.6 * 100 ); // because posX is asked to keep in cm
  86.         posY = posY + (directionY * speed / 3.6 * 100);
  87.     }
  88.  
  89.     else if (fuel <= 0) fuel = 0, speed = 0;
  90. }
  91.  
  92. void Bus::SetMinAndMaxSpeedOfThisGear(int min,int max) // updates the min and max speed range with change of gear
  93. {
  94.     minSpd = min;
  95.     maxSpd = max;
  96. }
  97.  
  98. void Bus::ShiftGearUp()
  99. {
  100.     if (gearPosition < 5)
  101.         ++gearPosition;
  102.    
  103.     SetMinAndMaxSpeedOfThisGear(GearSpeedRange[gearPosition][0], GearSpeedRange[gearPosition][1]);
  104. }
  105.  
  106. void Bus::ShiftGearDown()
  107. {
  108.     if (gearPosition > 0)
  109.         --gearPosition;
  110.  
  111.     SetMinAndMaxSpeedOfThisGear(GearSpeedRange[gearPosition][0], GearSpeedRange[gearPosition][1]);
  112.  
  113.     if (speed > maxSpd) speed = maxSpd;
  114.  
  115. }
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. #include <conio.h>
  137. #include <Windows.h>
  138. #include <ctime>
  139. #include <iostream>
  140. using namespace std;
  141.  
  142.  
  143.  
  144. int main()
  145. {
  146.     Bus bus;
  147.  
  148.     double x = 0, y = 0;
  149.     char c;
  150.  
  151.     while (true)
  152.     {
  153.         bus.GetPosition(x, y);
  154.         printf("%15s : %s\n", "Vehicle Type", bus.GetType());
  155.         printf("%15s : ( %.9g , %.9g )\n", "Position (X,Y)", x, y);
  156.         printf("%15s : %.5f\n", "Remaining Fuel", bus.GetFuel());
  157.         printf("%15s : %.5g\n", "Velocity", bus.GetSpeed());
  158.         printf("%15s : %d\n", "Gear Position", bus.GetGearPosition());
  159.         bus.GetDirection(x, y);
  160.         printf("%15s : x=%.5f y=%.5f\n", "Direction", x, y);
  161.  
  162.         if (_kbhit())
  163.         {
  164.             c = _getch();
  165.             cout << " >> " << c << endl;
  166.             switch (c) {
  167.             case 'a':
  168.                 bus.TurnLeft();
  169.                 break;
  170.             case 'd':
  171.                 bus.TurnRight();
  172.                 break;
  173.             case 'z':
  174.                 bus.ShiftGearUp();
  175.                 break;
  176.             case 'x':
  177.                 bus.ShiftGearDown();
  178.                 break;
  179.             case 'w':
  180.                 bus.IncreaseSpeed();
  181.                 break;
  182.             case 's':
  183.                 bus.DecreaseSpeed();
  184.                 break;
  185.             }
  186.         }
  187.  
  188.         bus.Move();
  189.         Sleep(300); // for making delay
  190.         system("CLS");
  191.     }
  192.  
  193.     return 0;
  194. }
RAW Paste Data