U E D R , A S I H C RSS

CPP_Study_2005_1/Basic Bus Simulation/남상협

CPP_Study_2005_1/BasicBusSimulation/남상협

  • 작성자 : 01 남상협
  • 일시 : 05.08.12 10:43

데이터

~cpp 
100 
20 30 25 50 30 24 50

Source

~cpp 
//Simulation.cpp
#include <fstream>
#include <vector>
#include <iostream>
#include "BusSimulation.h"
using namespace std;

void main()
{
	ifstream fin("data.txt");
	BusSimulation busSimulation(fin);
	busSimulation.readBusData();
	busSimulation.readTimeInput(cin,cout);
	busSimulation.printResult(cout);
}

~cpp 
//BusSimulation.h
#ifndef _BUS_SIMULATION_H_
#define	_BUS_SIMULATION_H_
#include <iostream>
#include <vector>
#include <fstream>
#include "Bus.h"
using namespace std;
class BusSimulation {
private:
	vector<Bus> m_buses;
	ifstream &m_fin;
	double m_roundDistance;
public:
	BusSimulation(ifstream &fin) : m_fin(fin) {}
	void readBusData();
	istream& readTimeInput(istream &in, ostream &out);
	void increaseTime(int time);
	ostream& printResult(ostream &out);
};
#endif

~cpp 
//BusSimulation.cpp
#include <iostream>
#include <fstream>
#include <vector>
#include "BusSimulation.h"


void BusSimulation::readBusData()
{
	m_fin>>m_roundDistance;
	double velocity=0;
	while(m_fin>>velocity)
	{
		Bus bus(velocity,m_roundDistance);
		m_buses.push_back(bus);
	}
}
void BusSimulation::increaseTime(int time)
{
	for(vector<Bus>::iterator it=m_buses.begin(); it!=m_buses.end(); ++it)
		it->increaseTime(time);

}
ostream& BusSimulation::printResult(ostream &out)
{
	for(vector<Bus>::iterator it=m_buses.begin(); it!=m_buses.end(); ++it)
		out<< it->getPosition()<<"\n";;

	return out;
}

istream& BusSimulation::readTimeInput(istream &in, ostream &out)
{
	int minute = 0;
	int hour = 0;
	out<<"몇시간 후의 버스 위치를 보시겠습니까?"<<"\n"<<"시간 : ";
	in>>hour;
	out<<"분 : ";
	in>>minute;
	increaseTime(hour*3600 + minute*60);
	return in;
}

~cpp 
//Bus.h
#ifndef _BUS_H_
#define _BUS_H_
class Bus {
private:
	int m_velocity; // Km/h
	double m_position; // Killo meter
	int m_roundDistance; // Killo meter
	int m_time; // second
public:
	Bus(int velocity, int roundDistance) : 
	  m_velocity(velocity), m_roundDistance(roundDistance) {m_time=0; m_position=0;}
	  void increaseTime(int time) {m_time+=time; m_position+=(time*m_velocity/3600)%m_roundDistance;}
	  double getPosition(){return m_position;}
};
#endif

thread

  • 쩝.
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:22:42
Processing time 0.0136 sec