Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. //define a stop class
  8. class stop
  9. {
  10.     public:
  11.         int dist; //distance from the previous stop
  12.         int tArrive; //time of arrival
  13.         int tDepart; //time of departure
  14.  
  15.         int getWaitTime(void){ //the time the tram waits at this stop
  16.             return tDepart-tArrive;
  17.         }
  18.  
  19.         bool isWait(void){ // return whether or not the tram waits at this stop
  20.             return (getWaitTime()>0);
  21.         }
  22. };
  23.  
  24. void drawLine(int L){ // draw a line of a set number of dashes
  25.     for (int i=0;i<=L;i++){
  26.         cout << "-";
  27.     }
  28.     cout << endl;
  29. }
  30.  
  31. float avg(stop * X, int M){ //calculate the average wait time
  32.     int R = 0;
  33.     for (int i=0;i<=M-2;i++){
  34.         R+=X[i].getWaitTime();
  35.     }
  36.     return R/M;
  37. }
  38.  
  39. int mx(stop * X, int M){ //maximum distance between two stops
  40.     int r = 0;
  41.     for (int i = 1;i<=M-1;i++){
  42.         if (X[i].dist>X[r].dist) r=i;
  43.     }
  44.     return r;
  45. }
  46.  
  47. bool ex(stop * X, int M){ //check if there exists a stop where the tram does not wait
  48.     for (int i = 1;i<=M-2;i++){
  49.         if (!(X[i].isWait())) return true;
  50.     }
  51.     return false;
  52. }
  53.  
  54. void coll(stop * X, int M, int * A, int &O){ //create an array of stops where the average speed has increased
  55.     int amt = 0;
  56.     int total_speed = 0;
  57.     int * avg_speed = new int[M];
  58.     for (int i=1;i<=M-1;i++){
  59.         total_speed += (X[i].dist)/(X[i].tArrive-X[i-1].tDepart);
  60.         avg_speed[i] = total_speed/i+1;
  61.         if (avg_speed[i]>avg_speed[i-1]){
  62.             amt++;
  63.             A[amt] = i;
  64.         }
  65.     }
  66.     O = amt;
  67. }
  68.  
  69. int main()
  70. {
  71.     int N; //the number of stops
  72.     cout << "Enter the number of stops: ";
  73.         do {
  74.             cin >> N;
  75.         }while((N<=1)&&!(cin.fail()));
  76.         N++;
  77.         stop stopList[N];
  78.         stopList[0].dist = 0;
  79.         stopList[0].tArrive = 0;
  80.         stopList[0].tDepart = 0;
  81.  
  82.     string inpt;
  83.     cout << "This program can read data from file (f) or from console input (c)." << endl;
  84.     cout << "Read data from: ";
  85.     do{
  86.     cin >> inpt;
  87.     }while( (cin.fail()) || !( (inpt == "c") || (inpt == "f") ) );
  88.  
  89.     if (inpt == "c"){
  90.         //Reading from console
  91.  
  92.         //define all stops:
  93.         for (int i=1;i<=N-1;i++){
  94.             cout << "Defining stop #" << i << ":" << endl;
  95.         //the distance between this and the previous stop, which must be more than zero
  96.             do {
  97.                 cout << "   Distance from previous stop: ";
  98.                 cin >> stopList[i].dist;
  99.             }while( (stopList[i].dist>=0) && (cin.fail()));
  100.         //the time of arrival cannot be sooner than the previous stop's
  101.             do {
  102.                 cout << "   Time of arrival: ";
  103.                 cin >> stopList[i].tArrive;
  104.             }while( !(stopList[i].tArrive>stopList[i-1].tArrive) && (cin.fail()) );
  105.         //the time of departure cannot be sooner than the previous stop's, and it cannot be sooner than the time of arrival
  106.             do {
  107.                 cout << "   Time of departure: ";
  108.                 cin >> stopList[i].tDepart;
  109.             }while(  !( (stopList[i].tDepart>stopList[i-1].tDepart) && (stopList[i].tDepart>=stopList[i].tArrive) )  && (cin.fail()) );
  110.         }
  111.     }
  112.     else{
  113.         //reading from file
  114.         cout << "Enter the file name to read from: ";
  115.         string fname;
  116.         cin >> fname;
  117.         string line;
  118.         ifstream workfile (fname.c_str());
  119.         if (workfile.is_open())
  120.         {
  121.             int i = 1;
  122.             while ( (getline (workfile,line)) && (i <= 1) )
  123.             {
  124.             cout << "Defining stop #" << i << ":" << endl;
  125.             stopList[i].dist = atoi(line.c_str());
  126.             cout << "   Distance from previous stop: " << stopList[i].dist << endl;
  127.             getline(workfile,line);
  128.             stopList[i].tArrive = atoi(line.c_str());
  129.             cout << "   Time of arrival: " << stopList[i].tArrive << endl;
  130.             getline(workfile,line);
  131.             stopList[i].tDepart = atoi(line.c_str());
  132.              cout << "   Time of departure: " << stopList[i].tDepart << endl;
  133.             i++;
  134.             }
  135.         workfile.close();
  136.         delete[] &i;
  137.     }
  138.     }
  139.  
  140.    //main menu
  141.    drawLine (20);
  142.    cout << "Please choose what to do." << endl;
  143.    cout << "1 - Get the average wait time" << endl;
  144.    cout << "2 - Get the pair of stops with the most distance between them" << endl;
  145.    cout << "3 - Check if there is a stop where the tram does not stop" << endl;
  146.    cout << "4 - Get all stops where the average tram speed is greater than at the previous stop" << endl;
  147.    cout << "5 - Exit program" << endl;
  148.    int C;
  149.    bool exit = false;
  150.    do { //main cycle
  151.     do {
  152.             cout << "Command: "; //crashes here when the main cycle runs for the second time
  153.             cin >> C;
  154.     }while(cin.fail());
  155.     switch (C) {
  156.     case 1:
  157.         cout << "The average wait time is: " << avg(stopList,N) << endl;
  158.         break;
  159.     case 2:
  160.         int m;
  161.         m = mx(stopList,N);
  162.         cout << "The biggest distance is " << stopList[m].dist << " between stops " << m << "and " << m-1 << "." << endl;
  163.         break;
  164.     case 3:
  165.         if (ex(stopList,N)) cout << "There is a stop where the tram does not stop";
  166.         else cout << "The tram stops at every station";
  167.         cout << "." << endl;
  168.         break;
  169.     case 4:
  170.         int S[N];
  171.         int M;
  172.         M = 0;
  173.         coll (stopList,N,S,M);
  174.         if (M>0){
  175.             cout << "The tram has increased its average speed compared to the last stop on the following occasions:" << endl;
  176.             for (int i = 0;i<=M-1;i++){
  177.                 cout << "Stop #" << S[i] << endl;
  178.             }
  179.         }
  180.         else cout << "The tram never increased its average speed." << endl;
  181.         break;
  182.     case 5:
  183.         exit = true;
  184.         break;
  185.     default:
  186.         cout << "Invalid command!" << endl;
  187.         break;
  188.     }
  189.  
  190.    }while(!(exit));
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement