-
Notifications
You must be signed in to change notification settings - Fork 0
/
PHONGCACHC.cpp
156 lines (152 loc) · 2.35 KB
/
PHONGCACHC.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
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
#include <iostream>
#include <string>
using namespace std;
int bien[126];
int phu[126];
int dd[126];
void khoitao ()
{
int dem=1;
for (int i=97; i<=122; i++)
{
bien[i]=dem;
dem++;
phu[i]=0;
dd[i]=0;
}
}
int main ()
{
int t;
cin>>t;
for (int k=1; k<=t; k++)
{
string ex;
cin>>ex;
cout<<"Expression: "<<ex<<endl;
khoitao ();
int kt=1;
int N=ex.length();
while (1)
{
if (kt==0) break;
kt=0;
for (int i=0; i<N-1; i++)
{
string tg="";
for (int j=i; j<i+2; j++)
{
tg+=ex[j];
}
if (tg=="++")
{
if (i-1>=0 && ex[i-1]>='a' && ex[i-1]<='z')
{
int so=ex[i-1]-0;
phu[so]++;
}
else if (i+2<=N-1 && ex[i+2]>='a' && ex[i+2]<='z')
{
int so=ex[i+2]-0;
bien[so]++;
}
for (int j=i; j<N; j++)
{
ex[j]=ex[j+2];
}
for (int j=N-2; j<=N-1; j++)
{
ex[j]='\0';
}
kt=1;
N=N-2;
break;
}
else if (tg=="--")
{
if (i-1>=0 && ex[i-1]>='a' && ex[i-1]<='z')
{
int so=ex[i-1]-0;
phu[so]--;
}
else if (i+2<=N-1 && ex[i+2]>='a' && ex[i+2]<='z')
{
int so=ex[i+2]-0;
bien[so]--;
}
for (int j=i; j<N; j++)
{
ex[j]=ex[j+2];
}
for (int j=N-2; j<=N-1; j++)
{
ex[j]='\0';
}
N=N-2;
kt=1;
break;
}
}
}
int S=0;
if (ex[0]>='a' && ex[0]<='z')
{
int so=ex[0]-0;
S+=bien[so];
dd[so]=1;
for (int i=1; i<N; i++)
{
string tg="";
for (int j=i; j<i+2; j++)
{
tg+=ex[j];
}
if (tg[0]=='+')
{
int so=tg[1]-0;
S+=bien[so];
dd[so]=1;
}
else if (tg[0]=='-')
{
int so=tg[1]-0;
S-=bien[so];
dd[so]=1;
}
}
}
else
{
for (int i=0; i<N; i++)
{
string tg="";
for (int j=i; j<i+2; j++)
{
tg+=ex[j];
}
if (tg[0]=='+')
{
int so=tg[1]-0;
S+=bien[so];
dd[so]=1;
}
else if (tg[0]=='-')
{
int so=tg[1]-0;
S-=bien[so];
dd[so]=1;
}
}
}
cout<<"value = "<<S<<endl;
for (int i=97; i<=122; i++)
{
if (dd[i]==1)
{
char kitu=i+0;
cout<<kitu<<" = "<<bien[i]+phu[i]<<endl;
}
}
}
return 0;
}