-
Notifications
You must be signed in to change notification settings - Fork 1
/
CPlace.cpp
122 lines (109 loc) · 1.99 KB
/
CPlace.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "CPlace.h"
CPlace::CPlace(string a_name, string a_colorset)
{
name = a_name;
colorset = a_colorset;
}
CPlace::CPlace(string a_name, string a_colorset, CValue a_value)
{
name = a_name;
colorset = a_colorset;
values.push_back(a_value);
}
void CPlace::addValue(CValue a_value)
{
values.push_back(a_value);
}
void CPlace::addFromTransition(CTransition *transition)
{
froms.push_back(transition);
}
void CPlace::addToTransition(CTransition *transition)
{
tos.push_back(transition);
}
int CPlace::putToken(CValue a_value)
{
if (colorset != a_value.colorset)
{
cout << "putToken Error." << endl;
return -1;
}
for (int i = 0; i < values.size(); i ++)
{
if (CValue::isIdentical(values[i], a_value) == 1)
{
values[i].number += a_value.number;
return 1;
}
}
values.push_back(a_value);
return 1;
}
int CPlace::okTakeToken(CValue a_value)
{
if (colorset != a_value.colorset)
{
cout << "okTakeToken Error." << endl;
return -1;
}
for (int i = 0; i < values.size(); i ++)
{
if (CValue::isIdentical(values[i], a_value) == 1)
{
if (values[i].number < a_value.number)
return 0;
else
{
return 1;
}
}
}
return 0;
}
int CPlace::takeToken(CValue a_value)
{
if (colorset != a_value.colorset)
{
cout << "takeToken Error." << endl;
return -1;
}
for (int i = 0; i < values.size(); i ++)
{
if (CValue::isIdentical(values[i], a_value) == 1)
{
if (values[i].number < a_value.number)
return 0;
else
{
values[i].number -= a_value.number;
if (values[i].number == 0)
{
values.erase(&values[i]);
}
return 1;
}
}
}
return 0;
}
string CPlace::itos(int i)
{
char cBuffer[10];
itoa(i, cBuffer, 10);
string s(cBuffer);
return s;
}
string CPlace::toString()
{
string strResult;
strResult += ("name:" + name);
strResult += (" colorset:" + colorset + "\n");
for (int i = 0; i < values.size(); i ++)
{
strResult += ("value-" + itos(i) + ": ");
strResult += values[i].toString();
strResult += "\n";
}
return strResult;
}