1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
#include <iostream>
#include <fstream>
#include <cassert>
#include <cstdlib>
#include <string>
using namespace std;
struct table_party
{
string name; //party name or table number
int num; //number of chair or people
int hour; // hour table opened or party arrived
int min; //min table opened or party arrived
};
class Queue
{
public:
static const int CAPACITY = 50;
typedef table_party QUEUE_TYPE;
Queue();
void insert(const QUEUE_TYPE&entry);
table_party remove();
bool is_empty() const;
bool is_full() const;
private:
QUEUE_TYPE data[CAPACITY];
int front, rear, count;
};
Queue::Queue()
{
count = 0;
front = -1;
rear = -1;
}
void Queue::insert(const QUEUE_TYPE& entry)
{
assert(count < CAPACITY);
rear = (rear + 1) % CAPACITY; // bump rear notice neat use of mod
data[rear] = entry; // put in elt
count++;
if (front == -1) front=0; // if queue was empty - must set front
}
table_party Queue::remove()
{
QUEUE_TYPE answer;
assert(count > 0); // check theres something there
answer = data[front]; // front marks first elt
count--;
if (count == 0)
{
rear = -1; // if removed last elt must reset rear
front = -1;
}
else front = (front + 1) % CAPACITY; // bump up front
return answer;
}
bool Queue::is_empty() const
{
return (count == 0);
}
bool Queue::is_full() const
{
return count>=CAPACITY;
}
void time_waited(int &a, int &b, int &c, int &d)//prints time parked & charges
{
int hour_diff;
int min_diff;
if (d<b)//if departing minutes is less than arriving minutes
{
hour_diff = c-a-1;
min_diff = d-b+60;
}
else
{
hour_diff = c-a;
min_diff = d-b;
}
cout<<"Your wait time was "<<hour_diff<<" hour(s) and "<<min_diff<<" minutes."<<endl;
}
int main ()
{
Queue table_waiting_for_a_party;
Queue party_waiting_for_a_table;
table_party t;
table_party p;
table_party ttemp;
table_party ptemp;
char code;
string starting_table;
string starting_party_name;
fstream textfile;
textfile.open("data5.txt");
textfile>>code;
while(!textfile.eof())
{
cout<<"========================================="<<endl;
if (code == 'P') //if party arrives
{
textfile>> p.name;
if (p.name == "NOBODY")
{
break;
}
else
{
textfile>> p.num;
textfile>> p.hour;
textfile>> p.min;
cout<<p.name<<" party of "<<p.num<<" arrives at "<<p.hour<<":";
if (p.min<10) cout<<"0"<<p.min<<endl;
else cout<<p.min<<endl;
if(table_waiting_for_a_party.is_empty())
{
cout <<"No table is available."<<endl;
}
else //if there are table available
{
ttemp = table_waiting_for_a_party.remove();
starting_table=ttemp.name;
while(p.num!=ttemp.num)
{
table_waiting_for_a_party.insert(ttemp);
ttemp = table_waiting_for_a_party.remove();
if (p.num == ttemp.num)
{
break;
}
if (ttemp.name == starting_table)
{
break;
}
}
if (p.num == ttemp.num)
{
cout <<"Your wait time is 0"<<endl;
}
else
{
party_waiting_for_a_table.insert(p);
}
}
}
}
else //if code = T, table opens up
{
textfile>>t.name;
textfile>>t.num;
textfile>>t.hour;
textfile>>t.min;
cout<<"Table: "<<t.name<<" seating "<<t.num<<" arrives at "<<t.hour<<":";
if (t.min<10) cout<<"0"<<t.min<<endl;
else cout<<t.min<<endl;
if(party_waiting_for_a_table.is_empty())//no party is waiting
{
cout<<"";
}
else //if there are parties waiting, search throught the party list
{
ptemp = party_waiting_for_a_table.remove(); //ptemp = first party in line
starting_party_name=ptemp.name;
while(t.num!=ptemp.num) //while chair & party don't match
{
party_waiting_for_a_table.insert(ptemp); //add the party to the tail
ptemp = party_waiting_for_a_table.remove(); //get new first party in line
if (t.num == ptemp.num)// if found matching table, break
{
break;
}
if (ptemp.name == starting_party_name)//if exhausted list, break
{
break;
}
}
if (t.num == ptemp.num) // if this table matches a party in the waitlist
{
cout <<ptemp.name<<" party of "<<ptemp.num<<", your table is ready"<<endl;
time_waited(ptemp.hour, ptemp.min, t.hour, t.min);
}
else
{
table_waiting_for_a_party.insert(t);
}
}
}
textfile>> code;
}
system("pause");
}
|