数据结构试验---网络打印机
main.cpp
View Code
#include <iostream>
#include <cstdlib>
#include <stdexcept>
#include <string>
#include "simulator.h"
#include "fifo.h"
using namespace std;
int main (int argc, char *argv[]) {
if (argc != 2) {
cerr << "Usage: " << argv[0] << " data-file\n";
system("pause");
return EXIT_FAILURE;
}
try {
string file = argv[1];
simulator *s = new fifo(2);
s->simulate(file);
delete s;
}
catch(exception& e) {
cerr << e.what() << endl;
}
catch(...) {
cerr << "Unknown exception caught!" << endl;
}
return EXIT_FAILURE;
}
simulator.h
View Code
#include <iostream>
using namespace std;
#include "job.h"
job::job (int number_of_pages, string user) {
this->number_of_pages = number_of_pages;
this->user = user;
}
job::job () {
this->number_of_pages = -1;
this->user = "";
}
string job::getuser() const {
return user;
}
int job::getnumpages() const {
return number_of_pages;
}
istream &operator>>(istream &stream, job &j) {
stream >> j.number_of_pages;
stream >> j.user;
return stream;
}
ostream &operator<<(ostream &stream, job &j) {
int pages = j.number_of_pages;
stream << pages << " page"
<< ((pages == 1) ? "" : "s")
<< " from " << j.user;
return stream;
}
simulator.cpp
View Code
#include <iostream>
#include <fstream>
using namespace std;
#include "simulator.h"
#include "event.h"
simulator::simulator(int seconds_per_page) {
this->seconds_per_page = seconds_per_page;
}
simulator::~simulator() {}
void simulator::loadworkload (string file) {
ifstream in;
// Open workload file
in.open (file.c_str());
if (!in) {
cerr << "Couldn't open input file, workload empty" << endl;
return;
}
// Load workload into simulator
while (1) {
event e;
in >> e;
if (in.eof())
break;
addevent (e);
}
}
void simulator::addevent(event e) {
workload.push(e);
}
job.h
View Code
#ifndef JOB_H
#define JOB_H
#include <iostream>
#include <string>
using namespace std;
class job {
//private:
private:
string user;
int number_of_pages;
public:
job();
job(int number_of_pages, string user);
string getuser() const;
int getnumpages() const;
friend istream &operator>>(istream &stream, job &j);
friend ostream &operator<<(ostream &stream, job &j);
};
#endif
job.cpp
View Code
#include <iostream>
using namespace std;
#include "job.h"
job::job (int number_of_pages, string user) {
this->number_of_pages = number_of_pages;
this->user = user;
}
job::job () {
this->number_of_pages = -1;
this->user = "";
}
string job::getuser() const {
return user;
}
int job::getnumpages() const {
return number_of_pages;
}
istream &operator>>(istream &stream, job &j) {
stream >> j.number_of_pages;
stream >> j.user;
return stream;
}
ostream &operator<<(ostream &stream, job &j) {
int pages = j.number_of_pages;
stream << pages << " page"
<< ((pages == 1) ? "" : "s")
<< " from " << j.user;
return stream;
}
event.h
View Code
#ifndef EVENT_H
#define EVENT_H
#include <iostream>
using namespace std;
#include "job.h"
class event {
private:
job j;
int wait_until;
public:
event();
event(job j, int wait_until);
job getjob() const;
int arrival_time() const;
friend istream &operator>>(istream &stream, event &e);
friend ostream &operator<<(ostream &stream, event &e);
};
#endif
event.cpp
View Code
#include <iostream>
using namespace std;
#include "event.h"
event::event (job j, int wait_until) {
this->j = j;
this->wait_until = wait_until;
}
event::event () {
this->wait_until = -1;
}
job event::getjob() const {
return j;
}
int event::arrival_time() const {
return wait_until;
}
istream &operator>>(istream &stream, event &e) {
stream >> e.wait_until;
stream >> e.j;
return stream;
}
ostream &operator<<(ostream &stream, event &e) {
stream << e.wait_until << " " << e.j;
return stream;
}
fifo.h
View Code
#include "simulator.h"
class fifo : public simulator
{
protected:
queue<event> work_wait;
//int seconds_per_page;
public:
fifo(int seconds_per_page):simulator(seconds_per_page){}
void simulate(string file);
~fifo(){}
//private:
};
fifo.cpp
View Code
#include<iostream>
#include"fifo.h"
using namespace std;
void fifo::simulate(string file)
{
int num = 0;
int counter = 0;
loadworkload(file);
double wait_time = workload.front().arrival_time();
double allwait_time = 0;
bool ifwork = false;
cout<<"FIFO Simulation\n"<<endl;
while(true)
{
if(!workload.empty())
{
event e = workload.front();
while(e.arrival_time()==counter)
{
cout<<" Arriving: "<<e.getjob().getnumpages()<<" pages form "
<<e.getjob().getuser()<<" at "<<e.arrival_time()<<" seconds "<<endl;
workload.pop();
num++;
work_wait.push(e);
if(!workload.empty())
e = workload.front();
else break;
}
}
if(wait_time==counter) ifwork = false;
if(ifwork==false)
{
if(!work_wait.empty())
{
event e = work_wait.front();
if(wait_time==counter)
{
cout<<" Servicing: "<<work_wait.front().getjob().getnumpages()<<" pages form "
<<work_wait.front().getjob().getuser()<<" at "<<wait_time<<" seconds"<<endl;
allwait_time +=(wait_time-e.arrival_time());
wait_time=e.getjob().getnumpages()*seconds_per_page+wait_time;
work_wait.pop();
ifwork = true;
}
}
}
if(ifwork==false&&workload.empty()&&work_wait.empty())
break;
counter++;
}
cout<<endl<<" Total jobs : "<<num<<endl<<" Aggregate latency : "<<allwait_time<<" seconds"
<<endl<<" Mean latency : "<<(double)(allwait_time/num)<<" seconds"<<endl;
system("pause");
}
posted on 2011-08-30 21:33 NewPanderKing 阅读(450) 评论(0) 编辑 收藏 举报