Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cstdlib>
- using namespace std;
- //define a stop class
- class stop
- {
- public:
- int dist; //distance from the previous stop
- int tArrive; //time of arrival
- int tDepart; //time of departure
- int getWaitTime(void){ //the time the tram waits at this stop
- return tDepart-tArrive;
- }
- bool isWait(void){ // return whether or not the tram waits at this stop
- return (getWaitTime()>0);
- }
- };
- void drawLine(int L){ // draw a line of a set number of dashes
- for (int i=0;i<=L;i++){
- cout << "-";
- }
- cout << endl;
- }
- float avg(stop * X, int M){ //calculate the average wait time
- int R = 0;
- for (int i=0;i<=M-2;i++){
- R+=X[i].getWaitTime();
- }
- return R/M;
- }
- int mx(stop * X, int M){ //maximum distance between two stops
- int r = 0;
- for (int i = 1;i<=M-1;i++){
- if (X[i].dist>X[r].dist) r=i;
- }
- return r;
- }
- bool ex(stop * X, int M){ //check if there exists a stop where the tram does not wait
- for (int i = 1;i<=M-2;i++){
- if (!(X[i].isWait())) return true;
- }
- return false;
- }
- void coll(stop * X, int M, int * A, int &O){ //create an array of stops where the average speed has increased
- int amt = 0;
- int total_speed = 0;
- int * avg_speed = new int[M];
- for (int i=1;i<=M-1;i++){
- total_speed += (X[i].dist)/(X[i].tArrive-X[i-1].tDepart);
- avg_speed[i] = total_speed/i+1;
- if (avg_speed[i]>avg_speed[i-1]){
- amt++;
- A[amt] = i;
- }
- }
- O = amt;
- }
- int main()
- {
- int N; //the number of stops
- cout << "Enter the number of stops: ";
- do {
- cin >> N;
- }while((N<=1)&&!(cin.fail()));
- N++;
- stop stopList[N];
- stopList[0].dist = 0;
- stopList[0].tArrive = 0;
- stopList[0].tDepart = 0;
- string inpt;
- cout << "This program can read data from file (f) or from console input (c)." << endl;
- cout << "Read data from: ";
- do{
- cin >> inpt;
- }while( (cin.fail()) || !( (inpt == "c") || (inpt == "f") ) );
- if (inpt == "c"){
- //Reading from console
- //define all stops:
- for (int i=1;i<=N-1;i++){
- cout << "Defining stop #" << i << ":" << endl;
- //the distance between this and the previous stop, which must be more than zero
- do {
- cout << " Distance from previous stop: ";
- cin >> stopList[i].dist;
- }while( (stopList[i].dist>=0) && (cin.fail()));
- //the time of arrival cannot be sooner than the previous stop's
- do {
- cout << " Time of arrival: ";
- cin >> stopList[i].tArrive;
- }while( !(stopList[i].tArrive>stopList[i-1].tArrive) && (cin.fail()) );
- //the time of departure cannot be sooner than the previous stop's, and it cannot be sooner than the time of arrival
- do {
- cout << " Time of departure: ";
- cin >> stopList[i].tDepart;
- }while( !( (stopList[i].tDepart>stopList[i-1].tDepart) && (stopList[i].tDepart>=stopList[i].tArrive) ) && (cin.fail()) );
- }
- }
- else{
- //reading from file
- cout << "Enter the file name to read from: ";
- string fname;
- cin >> fname;
- string line;
- ifstream workfile (fname.c_str());
- if (workfile.is_open())
- {
- int i = 1;
- while ( (getline (workfile,line)) && (i <= 1) )
- {
- cout << "Defining stop #" << i << ":" << endl;
- stopList[i].dist = atoi(line.c_str());
- cout << " Distance from previous stop: " << stopList[i].dist << endl;
- getline(workfile,line);
- stopList[i].tArrive = atoi(line.c_str());
- cout << " Time of arrival: " << stopList[i].tArrive << endl;
- getline(workfile,line);
- stopList[i].tDepart = atoi(line.c_str());
- cout << " Time of departure: " << stopList[i].tDepart << endl;
- i++;
- }
- workfile.close();
- delete[] &i;
- }
- }
- //main menu
- drawLine (20);
- cout << "Please choose what to do." << endl;
- cout << "1 - Get the average wait time" << endl;
- cout << "2 - Get the pair of stops with the most distance between them" << endl;
- cout << "3 - Check if there is a stop where the tram does not stop" << endl;
- cout << "4 - Get all stops where the average tram speed is greater than at the previous stop" << endl;
- cout << "5 - Exit program" << endl;
- int C;
- bool exit = false;
- do { //main cycle
- do {
- cout << "Command: "; //crashes here when the main cycle runs for the second time
- cin >> C;
- }while(cin.fail());
- switch (C) {
- case 1:
- cout << "The average wait time is: " << avg(stopList,N) << endl;
- break;
- case 2:
- int m;
- m = mx(stopList,N);
- cout << "The biggest distance is " << stopList[m].dist << " between stops " << m << "and " << m-1 << "." << endl;
- break;
- case 3:
- if (ex(stopList,N)) cout << "There is a stop where the tram does not stop";
- else cout << "The tram stops at every station";
- cout << "." << endl;
- break;
- case 4:
- int S[N];
- int M;
- M = 0;
- coll (stopList,N,S,M);
- if (M>0){
- cout << "The tram has increased its average speed compared to the last stop on the following occasions:" << endl;
- for (int i = 0;i<=M-1;i++){
- cout << "Stop #" << S[i] << endl;
- }
- }
- else cout << "The tram never increased its average speed." << endl;
- break;
- case 5:
- exit = true;
- break;
- default:
- cout << "Invalid command!" << endl;
- break;
- }
- }while(!(exit));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement