bangkok transportation calculate

25
Report on Calculate Transportation Made by Karunyapas Dangruan 55090005 Present to Dr. Ukrit Watchareeruetai 13016209: Object-oriented Concepts and Programming Second Semester, 2012 King Mongkut’s Institute of Technology Lardkrabang

Upload: karav-nomercy

Post on 11-Aug-2015

35 views

Category:

Travel


0 download

TRANSCRIPT

Report on Calculate TransportationMade by

Karunyapas Dangruan 55090005Present to

Dr. Ukrit Watchareeruetai

13016209: Object-oriented Concepts and Programming

Second Semester, 2012

King Mongkut’s Institute of Technology Lardkrabang

How to:

1. Select start station and destination station

2. Click Calculate

3. Instructions and price will show below

Figure 1 BTS_getstation

Figure 2 MRT::MRT

Figure

3BTS_Cal

Figure 4 BTS_readfile

Figure 5 BTS::BTS

Figure 6 Train readfile

Figure 7 MRT_cal

Figure 8 ARL ARL

Figure 9 ARL cal

Figure 10 Mainwin::Mainwin

Figure 11 Mainwin cal

Source codes:

Train.hpp

#include <iostream>#include <string>#include <vector>#include <fstream>using namespace std;

class Train{public:virtual vector <string> getstation();virtual void readfile(string filename);protected:

ifstream f;vector <string> st;};

class BTS :public Train{public:BTS();void readfile(string filename);vector <string> getstation();int cal(int in1,int in2);private:vector <int> n;vector <string> ns;vector <int> e;vector <string> es;vector <int> w;vector <string> ws;vector <int> s;vector <string> ss;};class MRT :public Train{public:MRT();int cal(int d);};

class ARL :public Train{public:ARL();int cal(int d);

};

Train.cpp

#include "Train.hpp"

void Train::readfile(string filename){f.open(filename.c_str());while(f!=NULL){string name;getline(f,name);st.push_back(name);}}vector <string> Train::getstation(){return st;}BTS::BTS(){readfile("BTS.train");}void BTS::readfile(string filename){f.open(filename.c_str());while(f!=NULL){int index;string name;char type;f>>type>>index;getline(f,name);if(type=='n'){n.push_back(index);ns.push_back(name);}if(type=='s'){

s.push_back(index);ss.push_back(name);}if(type=='e'){e.push_back(index);es.push_back(name);}if(type=='w'){w.push_back(index);ws.push_back(name);}else continue;}}vector <string> BTS::getstation(){vector<string> out;for(int i=0;i<ns.size();i++)out.push_back(ns[i]);for(int i=1;i<es.size()-1;i++)out.push_back(es[i]);for (int i=0;i<ws.size()-1;i++)out.push_back(ws[i]);for (int i=1;i<ss.size();i++)out.push_back(ss[i]);return out;}int BTS::cal(int in1,int in2){int num=0,sum=15;if((in1<8)&&(in2<8)) num+=abs(n[in1-1]-n[in2-1]);else if ((in1>8)&&(in2>8)&&(in1<23)&&(in2<23)) num+=abs(e[in1-8]-e[in2-8]);else if ((in1>23)&&(in2>23))num+=abs(s[in1-23]-s[in2-23]);else{if(in2<8)num+=n[in2-1];else if((in2>8)&&(in2<23))num+=e[in2-8];else if(in2==23)num+=1;else if((in2>23)&&(in2<34))num+=e[in2-23];

if(in1<8)num+=n[in1-1];if((in1>8)&&(in1<23))num+=e[in1-8];else if(in1==23)num+=1;else if((in1>23)&&(in1<34))num+=e[in1-23];}if (num==0||num==1)return 15;else if (num<8) {for (int i=0; i<num; i++) {if (i%2==1) sum+=5;}return sum;} else return 40;}MRT::MRT(){readfile("MRT.train");}int MRT::cal(int d){int sum=16;int p[] = {0,2,2,2,3,2,2,2,3,2,2,2};if (d<13) {for (int i=0; i<d; i++) {sum+=p[i];}return sum;} else return 40;}

ARL::ARL(){readfile("ARL.train");}

int ARL::cal(int d){int sum=15;for (int i=1; i<d; i++)sum+=5;return sum;}

Mainwindow.cpp

#include "mainwindow.h"#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow){ui->setupUi(this);

vector <string> bts=b.getstation();for(int i=0;i<bts.size();i++){string str=bts[i];QString name=QString::fromStdString(str);ui->Start->addItem(name);ui->Destination->addItem(name);}vector <string> mrt=m.getstation();for(int i=0;i<mrt.size()-1;i++){string str=mrt[i];QString name=QString::fromStdString(str);ui->Start->addItem(name);ui->Destination->addItem(name);}vector <string> arl=a.getstation();for(int i=0;i<arl.size()-1;i++){string str=arl[i];QString name=QString::fromStdString(str);ui->Start->addItem(name);ui->Destination->addItem(name);}}

MainWindow::~MainWindow(){delete ui;}

void MainWindow::calculate(){ostringstream outstr;int bz=b.getstation().size();

int mz=m.getstation().size();int az=a.getstation().size();if(stNUM==1&&desNUM==37)outstr<<"Walk from BTS "<<b.getstation()[0]<<" to MRT "<<m.getstation()[2];else if(stNUM==12&&desNUM==46)outstr<<"Walk from BTS "<<b.getstation()[11]<<" to MRT "<<m.getstation()[11];else if(stNUM==25&&desNUM==50)outstr<<"Walk from BTS "<<b.getstation()[24]<<" to MRT "<<m.getstation()[15];else if(desNUM==1&&stNUM==37)outstr<<"Walk from MRT "<<m.getstation()[2]<<" to BTS "<<b.getstation()[0];else if(desNUM==12&&stNUM==46)outstr<<"Walk from MRT "<<m.getstation()[11]<<" to BTS "<<b.getstation()[11];else if(desNUM==25&&stNUM==50)outstr<<"Walk from MRT "<<m.getstation()[15]<<" to BTS "<<b.getstation()[24];else if(desNUM==55&&stNUM==45)outstr<<"Walk from MRT "<<m.getstation()[11]<<" to Airport Link "<<a.getstation()[2];else if(desNUM==45&&stNUM==55)outstr<<"Walk from Airport Link "<<a.getstation()[2]<<" to MRT "<<m.getstation()[11];else if(desNUM==53&&stNUM==6)outstr<<"Walk from BTS "<<a.getstation()[0]<<" to Airport Link "<<b.getstation()[5];else if(stNUM==53&&desNUM==6)outstr<<"Walk from Airport Link "<<b.getstation()[5]<<" to BTS "<<a.getstation()[0];else if(stNUM<bz&&desNUM<bz){sumall+=b.cal(stNUM,desNUM);outstr<<"Take BTS from "<<b.getstation()[stNUM-1]<<" to "<<b.getstation()[desNUM-1]<<" \n\tCost "<<b.cal(stNUM,desNUM)<<" Bath.";}else if (stNUM<bz&&desNUM<(bz+mz)){outstr<<"Take BTS from "<<b.getstation()[stNUM-1]<<" to ";int ap = (b.cal(stNUM,1)+m.cal(abs((desNUM-35)-3)));int bp = (b.cal(stNUM,12)+m.cal(abs((desNUM-35)-12)));int cp = (b.cal(stNUM,25)+m.cal(abs((desNUM-35)-16)));if(ap==mincost(ap,bp,cp)){

outstr<<b.getstation()[0]<<"\n\tCost "<<b.cal(stNUM,1)<<" Bath.\n";outstr<<"\nThen take MRT from "<<m.getstation()[2]<<" to "<<m.getstation()[desNUM-35]<<"\n\tCost "<<m.cal(abs((desNUM-35)-3))<<" Bath.";sumall+=ap;}else if(bp==mincost(ap,bp,cp)){outstr<<b.getstation()[11]<<"\n\tCost "<<b.cal(stNUM,12)<<" Bath.\n";outstr<<"\nThen take MRT from "<<m.getstation()[12]<<" to "<<m.getstation()[desNUM-35]<<"\n\tCost "<<m.cal(abs((desNUM-35)-12))<<" Bath.";sumall+=bp;}else{outstr<<b.getstation()[24]<<"\n\tCost "<<b.cal(stNUM,25)<<" Bath.\n";outstr<<"\nThen take MRT from "<<m.getstation()[15]<<" to "<<m.getstation()[desNUM-35]<<"\n\tCost "<<m.cal(abs((desNUM-35)-16))<<" Bath.";sumall+=cp;}}

else if(stNUM<bz){outstr<<"Take BTS from "<<b.getstation()[stNUM-1]<<" to "<<b.getstation()[5]<<"\n\tCost "<<b.cal(stNUM,6);outstr<<"\nThen take Airport link from "<<a.getstation()[0]<<" to "<<a.getstation()[desNUM-53]<<"\n\tCost "<<a.cal(desNUM-53);sumall+=b.cal(stNUM,6)+a.cal(desNUM-53);}else if(stNUM<(bz+mz-1)&&desNUM<bz){outstr<<"Take MRT from "<<m.getstation()[stNUM-35]<<" to ";int ap = (b.cal(desNUM,1)+m.cal(abs((stNUM-35)-3)));int bp = (b.cal(desNUM,12)+m.cal(abs((stNUM-35)-12)));int cp = (b.cal(desNUM,25)+m.cal(abs((stNUM-35)-16)));if(ap==mincost(ap,bp,cp)){outstr<<m.getstation()[2]<<"\n\tCost "<<m.cal(abs((stNUM-35)-3))<<" Bath.\n";outstr<<"\nThen take BTS from "<<b.getstation()[0]<<" to "<<m.getstation()[desNUM-1]<<"\n\tCost "<<b.cal(1,desNUM)<<" Bath.";sumall+=ap;}

else if(bp==mincost(ap,bp,cp)){outstr<<m.getstation()[11]<<"\n\tCost "<<m.cal(abs((stNUM-35)-12))<<" Bath.\n";outstr<<"\nThen take BTS from "<<b.getstation()[11]<<" to "<<m.getstation()[desNUM-1]<<"\n\tCost "<<b.cal(12,desNUM)<<" Bath.";sumall+=bp;}else{outstr<<m.getstation()[15]<<"\n\tCost "<<m.cal(abs((stNUM-35)-16))<<" Bath.\n";outstr<<"\nThen take BTS from "<<b.getstation()[24]<<" to "<<m.getstation()[desNUM-1]<<"\n\tCost "<<b.cal(25,desNUM)<<" Bath.";sumall+=cp;}}else if(stNUM<(bz+mz-1)&&desNUM<(bz+mz-1)){outstr<<"Take MRT from "<<m.getstation()[stNUM-35]<<" to "<<m.getstation()[desNUM-35]<<" \n\tCost "<<m.cal(abs(stNUM-desNUM))<<" Bath.";sumall+=m.cal(abs(stNUM-desNUM));}else if(stNUM<(bz+mz-1)){outstr<<"Take MRT from "<<m.getstation()[stNUM-35]<<" to "<<m.getstation()[10]<<"\n\tCost "<<m.cal(abs((stNUM-35)-11));outstr<<"\nThen take Airport link from "<<a.getstation()[2]<<" to "<<a.getstation()[desNUM-53]<<"\n\tCost "<<a.cal(abs((desNUM-53)-2));sumall+=m.cal(abs((stNUM-35)-11))+a.cal(abs((desNUM-53)-2));}else if (stNUM>53&&desNUM<bz){outstr<<"Take Airport Link from "<<a.getstation()[stNUM-53]<<" to "<<a.getstation()[0]<<"\n\tCost "<<a.cal(stNUM-53);outstr<<"\nThen take BTS from "<<b.getstation()[5]<<" to "<<b.getstation()[desNUM-1]<<"\n\tCost "<<b.cal(desNUM,6);sumall+=b.cal(desNUM,6)+a.cal(stNUM-53);}else if(stNUM>53&&desNUM<(bz+mz-1)){outstr<<"Take Airport Link from "<<a.getstation()[stNUM-53]<<" to "<<a.getstation()[2]<<"\n\tCost "<<a.cal(abs((stNUM-53)-2));outstr<<"\nThen take MRT from "<<m.getstation()[10]<<" to "<<m.getstation()[desNUM-35]<<"\n\tCost "<<m.cal(abs(desNUM-35)-10);

sumall+=m.cal(abs(desNUM-35)-10)+a.cal(abs((stNUM-53)-2));}else if(stNUM<53&&desNUM<(bz+mz+az-2)){outstr<<"Take Airport Link from "<<a.getstation()[stNUM-53]<<" to "<<a.getstation()[desNUM-53]<<"\n\tCost "<<a.cal(abs(stNUM-desNUM))<<" Bath.";sumall+=a.cal(abs(stNUM-desNUM));}display=outstr.str();}void MainWindow::on_Start_activated(){stNUM=ui->Start->currentIndex();}

void MainWindow::on_Destination_activated(){desNUM=ui->Destination->currentIndex();}

void MainWindow::change_status(QString &start,QString &des){stNUM=ui->Start->currentIndex()+1;desNUM=ui->Destination->currentIndex()+1;ui->Status->setText("From");ui->From_leb->setText(start);ui->Des_la->setText(des);}void MainWindow::on_pushButton_clicked(){sumall=0;change_status(ui->Start->currentText(),ui->Destination->currentText());calculate();ui->Sumt->setText(QString::number(sumall)+".00");ui->GT->setText(QString::fromStdString(display));}

int MainWindow::mincost(int x,int y,int z){vector<int> tmp;tmp.push_back(x);tmp.push_back(y);tmp.push_back(z);vector<int>::iterator pos;pos = min_element (tmp.begin(), tmp.end());

return *pos;}

Mainwindow.h

#ifndef MAINWINDOW_H#define MAINWINDOW_H

#include <QMainWindow>#include <string>#include <vector>#include <algorithm>#include <sstream>#include "Train.hpp"

using namespace std;

namespace Ui {class MainWindow;}

class MainWindow : public QMainWindow{Q_OBJECT

public:explicit MainWindow(QWidget *parent = 0);~MainWindow();void calculate();private slots:void on_Start_activated();void on_Destination_activated();void change_status(QString &start,QString &des);void on_pushButton_clicked();int mincost(int x,int y,int z);

private:Ui::MainWindow *ui;BTS b;MRT m;ARL a;int stNUM;int desNUM;int sumall;string display;

};

#endif // MAINWINDOW_H

Main.cpp

#include "mainwindow.h"#include <QApplication>

int main(int argc, char *argv[]){QApplication a(argc, argv);MainWindow w;w.show();

return a.exec();}