-
Notifications
You must be signed in to change notification settings - Fork 1
/
printer.cc
201 lines (174 loc) · 4.52 KB
/
printer.cc
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
#include "printer.h"
#include <iostream>
#include <vector>
using namespace std;
Printer::Printer(unsigned int numStudents, unsigned int numVendingMachines,unsigned int numCouriers)
:numStudents(numStudents),numVendingMachines(numVendingMachines),numCouriers(numCouriers)
{
cout<<"Parent";
cout<<"\t";
cout<<"WATOff";
cout<<"\t";
cout<<"Names";
cout<<"\t";
cout<<"Truck";
cout<<"\t";
cout<<"Plant";
cout<<"\t";
for(int i=0;i<5;i++){
storages.push_back(storage('0',(unsigned int)0,(unsigned int)0,false));
}
for(unsigned int i=0;i<numStudents;i++){
cout<<"Stud"<<i<<"\t";
storages.push_back(storage('0',(unsigned int)0,(unsigned int)0,false));
}
for (unsigned int i=0;i<numVendingMachines;i++){
cout<<"Mach"<<i<<"\t";
storages.push_back(storage('0',(unsigned int)0,(unsigned int)0,false));
}
for (unsigned int i=0;i<numCouriers;i++){
cout<<"Cour"<<i<<"\t";
storages.push_back(storage('0',(unsigned int)0,(unsigned int)0,false));
}
cout<<endl;
totalCount=5+numStudents+numVendingMachines+numCouriers;
for (unsigned int i=0;i<totalCount;i++){
cout<<"*******"<<"\t";
}
cout<<endl;
}
//print all blocks, and reset values
void Printer::flush(){
bool haveData=false;
//check if any data there
for (unsigned int i=0;i<totalCount;i++){
if(storages[i].flag) haveData=true;
}
if(!haveData) return;
for(unsigned int i=0;i<totalCount;i++){
if(storages[i].flag){//only print if there is data
storages[i].flag=false;
cout<<storages[i].state;
if(storages[i].value1==NULL){
cout<<"\t";
continue;
}
cout<<storages[i].value1;
if(storages[i].value2==NULL){
cout<<"\t";
continue;
}
cout<<','<<storages[i].value2;
//cout<<"\t";
}
cout<<"\t";
}
cout<<endl;
}
//just reset all blocks, so clear a line
void Printer::reset(){
for(int i=0;i<totalCount;i++){
storages[i].flag=false;
}
}
_Mutex void Printer::helper( unsigned int index, char state, unsigned int value1, unsigned int value2) {
//check if state is F first
//if it is, just print it.
if(state=='F'){
flush();
for(unsigned int i=0;i<totalCount;i++){
if(index==i){
cout<<"F"<<"\t";
}
else{
cout<<"..."<<"\t";
}
}
cout<<endl;
return;
}
if(storages[index].flag){//flush and store information
flush();
}
storages[index]=(storage(state,value1,value2,true));
}
void Printer::debug(){
cout<<"ddddebug"<<endl;
for (int i=0;i<storages.size();i++){
cout<<storages[i].state<<" "<<storages[i].value1<<" "<<storages[i].value2<<storages[i].flag<<endl;
}
}
void Printer::print(Kind kind,char state){
unsigned int myindex=getindex(kind,0);
//debug();
helper(myindex,state,NULL,NULL);
}
void Printer::print( Kind kind, char state, int value1 ){
helper(getindex(kind,0),state,value1,NULL);
}
void Printer::print( Kind kind, char state, int value1, int value2 ){
helper(getindex(kind,0),state,value1,value2);
}
void Printer::print( Kind kind, unsigned int lid, char state ){
helper(getindex(kind,lid),state,NULL,NULL);
}
void Printer::print( Kind kind, unsigned int lid, char state, int value1 ){
helper(getindex(kind,lid),state,value1,NULL);
}
void Printer::print( Kind kind, unsigned int lid, char state, int value1, int value2 ){
//cout<<"vvv::"<<value1<<" "<<value2<<endl;
helper(getindex(kind,lid),state,value1,value2);
}
Printer::~Printer(){
cout<<"***********************"<<endl;
}
unsigned int Printer::getindex(Kind kind,unsigned int index){
//unsigned int value;
// WATCardOffice, NameServer, Truck, BottlingPlant,
//Student, Vending, Courier
switch(kind){
case Parent:
return 0;
break;
case WATCardOffice:
return 1;
break;
case NameServer:
return 2;
break;
case Truck:
return 3;
break;
case BottlingPlant:
return 4;
break;
case Student:
return 5+index;
break;
case Vending:
return 5+numStudents+index;
break;
case Courier:
//default:
return 5+numStudents+numVendingMachines+index;
break;
}
return 0;
}
/*
void uMain::main(){
Printer p (3,3,3);
p.print(Printer::Parent,'F');
//p.print(Printer::Truck,'d',14,15);
//p.print(Printer::Courier,2,'c');
//p.print(Printer::Courier,2,'c',12,23);
//p.print(Printer::Courier,'c',2);
//p.print(Printer::Courier,'c',2);
p.print(Printer::WATCardOffice,'S');
p.print(Printer::NameServer,'S');
p.print(Printer::Truck,'S');
p.print(Printer::BottlingPlant,'S');
p.print(Printer::BottlingPlant,'S');
p.print(Printer::Parent,'F');
}
*/