-
Notifications
You must be signed in to change notification settings - Fork 2
/
并行程序模拟.cpp
98 lines (85 loc) · 1.76 KB
/
并行程序模拟.cpp
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
#include<iostream>
#include<deque>
#include<vector>
#include<string>
#include<cctype>
#include<cstring>
const int maxn = 10000;
using namespace std;
int num, Q;
int w[5];
int var[30] = {0}, pos[maxn]={0};
int judge(string &s){
if(s.find("=")!=string::npos) return 0;
if(s.find("print")!=string::npos) return 1;
if(s.find("unlock")!=string::npos) return 3;
if(s.find("lock")!=string::npos) return 2;
if(s.find("end")!=string::npos) return 4;
}
int getnumber(string &s){
int pos = s.find_last_of(" ");
return stoi(s.substr(pos+1));
}
int main(){
while(cin>>num>>w[0]>>w[1]>>w[2]>>w[3]>>w[4]>>Q){
getchar();
memset(var, 0, sizeof(var));
memset(pos,0,sizeof(pos));
vector<string> cmd[maxn];
deque<int> ready, stop;
bool flag = false;
for(int i = 1; i<=num; i++){
while(1){
string s;
getline(cin ,s);
cmd[i].push_back(s);
if(s == "end"){
break;
}
}
ready.push_back(i);
}
while(!ready.empty()){
bool over = false;
int d = ready.front();
ready.pop_front();
int time = Q;
while(time>0){
string s = cmd[d][pos[d]];
int id = judge(s);
// cout<<s<<endl;
// int time = Q;
// if(time>0){
if(id == 0){
var[s[0]-'a'] = getnumber(s);
}else if(id == 1){
cout<<d<<": "<<var[s[s.size()-1] - 'a']<<endl;
}else if(id == 2){
if(flag){
stop.push_back(d);
over = true;
break;
}else{
flag = true;
}
}else if(id == 3){
flag = false;
if(!stop.empty()){
int g = stop.front();stop.pop_front();
ready.push_front(g);
}
}else if(id == 4){
over = true;
break;
}
// }else break;
time -= w[id];
pos[d]++;
}
if(!over){
ready.push_back(d);
}
}
}
return 0;
}