C++primer plus the sixth edition of After-school programming exercises answer 12.5 and 12.6__linux

Source: Internet
Author: User
Tags rand setf
bank is the realization of the fifth question, BANK1 is the implementation of the sixth question 
#ifndef queue_h_ #define Queue_h_ class Customer {private:int arrive;
int processtime;

	Public:customer () {arrive=processtime=0;}
	void set (int when);
	int when () Const{return arrive}
	int ptime () Const{return processtime;}
	BOOL Operator!= (const Customer &c) const;

BOOL operator== (const Customer &c) const;

};

typedef Customer Item;
		Class Queue {private:struct node{item item;
	Node *next;
	};

	ENUM{Q_SIZE=10};
	Node *front;
	Node *rear;
	int items;
	const int qsize;


	int first_start_time; Queue (const queue &Q): qsize (0) {}//queue &operator= (const queue &q) {return *this;}
The class cannot be copied public:queue (int qs=q_size);
	Queue (Item &i);
	~queue ();
	void Clearline ();
	BOOL IsEmpty ();
	BOOL Isfull ();
	BOOL Insertqueue (const Item &i);
	BOOL Deletequeue (Item &i);
	int queuecount () const;
	int Cusnumbefore (const Item &i) const;
	void set_first_start_time (const int T); int wait_time_min (const Item &i,const int t);//Only the class member function can add a const, the member function cannot modify the classdata member}; #endif
#include <iostream> #include "queue.h" #include <cstdlib>//for rand () using namespace std;
	Queue::queue (int qs): qsize (QS) {front=rear=0;
	items=0;
first_start_time=0;
	} queue::~queue () {Node *temp;
		while (front!=null) {temp=front;
		front=front->next;
	Delete temp;
	} void Queue::clearline () {Node *temp;
		while (front!=null) {temp=front;
		front=front->next;
	Delete temp;
	} front=rear=0;
	items=0;
first_start_time=0;

BOOL Queue::isempty () {return items==0;}

BOOL Queue::isfull () {return items==qsize;}

int Queue::queuecount () const {return items;}
	BOOL Queue::insertqueue (const Item &i) {if (Isfull ()) return false;
	Node *add=new node;
	add->item=i;
	add->next=null;
	items++;
	if (front==null) Front=add;
	else rear->next=add;
	Rear=add;	
return true;
	BOOL Queue::d eletequeue (Item &i) {if (IsEmpty ()) return false;
	i=front->item;
	items--;
	Node *temp=front;
	front=front->next;
	Delete temp; if (items==0) Rear=null;
return true;
	the int queue::cusnumbefore (const Item &i) const {Node *temp=front;

	int n=1; Cout<<temp->item.when () <<endl<<temp->item.ptime () <<endl;//shows the customer's join time and processing time while (i!
		=temp->item) {if (temp==null) return-1;
		temp=temp->next;
	n++;

} return N;


} void Queue::set_first_start_time (const int t) {first_start_time=t} int queue::wait_time_min (const Item &i,const int T) {if (Cusnumbefore (i) ==-1) {cout<< "Customer I not in queue" <<end
		L
	return 0;
		else {Node *temp=front;
			int wait_time=0;
				while (temp->item!=i) {temp=temp->next;
			Wait_time+=temp->item.ptime ();

	return wait_time;
	} void Customer::set (int when) {Processtime=rand ()%3+1;
Arrive=when; BOOL Customer::operator!= (const Customer &c) Const {if (arrive==c.arrive) && (processtime==c.processtime
	) return false;
return true; BOOL customer::operator== (const Customer &c) Const {IF((arrive==c.arrive) && (processtime==c.processtime)) return true;
return false; }
<pre name= "code" class= "CPP" > #include <iostream> #include <cstdlib> #include <ctime> #include "

Queue.h "using namespace std;

const int min_per_hr=60;

BOOL Newcustomer (double x);

void Bank ();

void Bank1 ();

	int main () {Bank1 ();
return 0;

BOOL Newcustomer (double x) {return (rand () *x/rand_max<1);
	void Bank () {Srand (time (0));
cout<< "Case Study:bank of Heather Automatic teller\n";
	cout<< "Enter Maximum size of queue:";
int QS;
	cin>>qs;
	qs=10;

	Queue Line (QS);//Enter queue maximum length//cout<< "Enter the number of simulation hours:";
int hours;
	cin>>hours;
	hours=100;
	Long cyclelimit=min_per_hr*hours;//Enter program run time//cout<< "Enter the average number of customers per hour:";
Double Perhour;

	cin>>perhour;//Enter the number of customer arrivals per hour double min_per_cust;
	Item temp;
	Long turnaways=0;
	Long customers=0;
	Long served=0;
	Long sum_line=0;
	int wait_time=0;

	Long line_wait=0;

	Double average_wait_time; for (int i=1;i<105;i++) {turnaways=0;
		customers=0;
		Served=0;
		sum_line=0;
		wait_time=0;

		line_wait=0;
	Perhour=i;
	Min_per_cust=min_per_hr/perhour; for (int cycle=0;cycle<cyclelimit;cycle++) {if (Newcustomer (min_per_cust))//To determine whether there is a new user {if (line.isfull ()) turn
			aways++;
				else {customers++;//cout<< "insert a new customer" <<endl;
Temp.set (cycle);//user arrival time Line.insertqueue (temp);//Insert new user//cout<< "Now time" <<cycle<<endl;

cout<<customers<< "Processing Time" <<temp.ptime () <<endl; cout<<customers<< "Location:" <<line.
Cusnumbefore (temp) <<endl;
cout<< "Wait_time=" <<wait_time<<endl;
			cout<<customers<< "Wait Time:" << (Line.wait_time_min (temp,cycle) +wait_time) <<endl; } if ((wait_time<=0) &&! (
			Line.isempty ()) {//cout<< "Delete a customer" <<endl;
			Line.deletequeue (temp);
			Line.set_first_start_time (cycle);
			Wait_time=temp.ptime (); LiNe_wait+=cycle-temp.when ();
		served++;
		} if (wait_time>0) wait_time--;

		

Sum_line+=line.queuecount ();

	cout<< "ATM Run-time End" <<endl;
} if (customers>0) {//cout<< "Customers accepted:" <<customers<<endl;
cout<< "Customers served:" <<served<<endl;
cout<< "Turnaways:" <<turnaways<<endl;
cout<< "Average queue size:";
Cout.precision (2);
COUT.SETF (Ios_base::fixed,ios_base::floatfield);
cout<< (Double) sum_line/cyclelimit<<endl;
		cout<< "Average wait Time:"//<< (double) line_wait/served<< "minutes\n";
		Average_wait_time= (Double) (line_wait/served);
	if (average_wait_time<=1) {cout<<perhour<<endl;
		Break

	
	
	Line.clearline ();//Empty queue} else cout<< "No customers!\n";

} cout<< "done!\n";
	} void Bank1 () {Srand (time (0)); cout<< "Case Study:bank of Heather Automatic teller\n ";
	cout<< "Enter Maximum size of queue:";
int QS;
	cin>>qs;
	qs=10;

Queue line1 (QS);//Enter queue maximum length \ line2 (QS);

	cout<< "Enter The number of simulation hours:";
int hours;
	cin>>hours;
	hours=100;
	Long cyclelimit=min_per_hr*hours;//Enter program run time//cout<< "Enter the average number of customers per hour:";
Double Perhour;

	cin>>perhour;//Enter the number of customer arrivals per hour double min_per_cust;
	Item temp;
	Long turnaways=0;
	Long customers=0;
	Long served=0;
	Long sum_line=0;
	int wait_time1=0;
	int wait_time2=0;

	Long line_wait=0;

	Double average_wait_time;
		for (int i=1;i<100;i++) {turnaways=0;
		customers=0;
		Served=0;
		sum_line=0;
		wait_time1=0;
		wait_time2=0;

		line_wait=0;
	Perhour=i;
	Min_per_cust=min_per_hr/perhour; for (int cycle=0;cycle<cyclelimit;cycle++) {if (Newcustomer (min_per_cust))//To determine whether there is a new user {if (line1.isfull () &&amp
			; Line2.isfull ()) turnaways++; else {customers++;//cout<< "insert a new customer" <<eNdl
Temp.set (cycle)//user arrival time//Cout<<line1.queuecount () <<endl;
				Cout<<line2.queuecount () <<endl;
					if (Line1.queuecount () <=line2.queuecount ()) {//cout<< "line1 insert a new customer" <<endl;
					Endl
Line2.insertqueue (temp);/Otherwise, insert new user to Line2}//cout<< "Now time" <<cycle<<endl;

cout<<customers<< "Processing Time" <<temp.ptime () <<endl; cout<<customers<< "Location:" <<line.
Cusnumbefore (temp) <<endl;
cout<< "Wait_time=" <<wait_time<<endl;
			cout<<customers<< "Wait Time:" << (Line.wait_time_min (temp,cycle) +wait_time) <<endl; } if ((wait_time1<=0) &&! (
			Line1.isempty ())//handling LINE1 customer at Team head {//cout<< "line1 Delete a customer" <<endl;
			Line1.deletequeue (temp);
		Line1.set_first_start_time (cycle);	Wait_time1=temp.ptime ();
			Line_wait+=cycle-temp.when ();
		served++; } if ((wait_time2<=0) &&! (
			Line2.isempty ())//handling Line2 customer at Team head {//cout<< "line2 Delete a customer" <<endl;
			Line2.deletequeue (temp);
			Line2.set_first_start_time (cycle);
			Wait_time2=temp.ptime ();
			Line_wait+=cycle-temp.when ();
		served++;
		} if (wait_time1>0) wait_time1--;
		if (wait_time2>0) wait_time2--;

		

Sum_line+=line1.queuecount () +line2.queuecount ();

	cout<< "ATM Run-time End" <<endl;
} if (customers>0) {//cout<< "Customers accepted:" <<customers<<endl;
cout<< "Customers served:" <<served<<endl;
cout<< "Turnaways:" <<turnaways<<endl;
cout<< "Average queue size:";
Cout.precision (2);
COUT.SETF (Ios_base::fixed,ios_base::floatfield);
cout<< (Double) sum_line/cyclelimit<<endl; cout<< "Average wait Time:"//<< (double) line_wait/served<< "minutes\n";
		Average_wait_time= (Double) (line_wait/served);
	if (average_wait_time<=1) {cout<<perhour<<endl;
		Break

	
	
	Line1.clearline ();//Empty Queue line2.clearline ();//Empty queue} else cout<< "No customers!\n";

} cout<< "done!\n"; }


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.