This repository has been archived by the owner on Jan 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpressionToChart.cpp
195 lines (166 loc) · 5.41 KB
/
ExpressionToChart.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
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
//
// ExpressionToChart.cpp
// mini_project
//
// Created by sxb on 2017/4/9.
// Copyright © 2017年 sxb. All rights reserved.
//
#include "ExpressionToChart.h"
using std::string;
bool ExprToChart::Isoperator(const char &ch){
for (int i=0; i<4; i++){
if (ch == expr_operator[i]){
return true;
}
}
return false;
}
bool ExprToChart::Errorcheck(const string& Tmpexpr){
for (int i=0; i<Tmpexpr.length(); i++){
if (isalpha(Tmpexpr[i])){
if (i+1 < Tmpexpr.length()){
if (Isoperator(Tmpexpr[i+1])==false && Tmpexpr[i+1] != ')') { throw SyntaxError{}; }
}
if (i!=0) {
if (Isoperator(Tmpexpr[i-1])==false && Tmpexpr[i-1] != '(') { throw SyntaxError{}; }
}
}
if (Tmpexpr[i] == '(' || Tmpexpr[i] == ')'){
S_Bracket.push_back(Tmpexpr[i]);
}
}
while(S_Bracket.empty()==false){
if ((S_Bracket.size() % 2 )!= 0) { throw ExprBracketError{}; }
if (S_Bracket.back() == ')') {
if (S_Bracket.front() == '('){
S_Bracket.pop_back();
S_Bracket.pop_front();
}else{
throw ExprBracketError{};
}
}
else { throw ExprBracketError{}; }
}
return true;
}
string ExprToChart::filter(const string& expr,const int& n){
string result="";
if (expr.length() == 0) { throw ExprEmptyError{}; }
for (int i=0; i<expr.length(); i++){
if (expr[i]==' ' || (expr[i]<='H' && expr[i]>='A') || Isoperator(expr[i]) || expr[i]=='(' || expr[i]==')' ){
if (expr[i] != ' '){
result += expr[i];
}
if (isalpha(expr[i])){
int count=0;
for (int k=0; k<Expralpha.size(); k++){
if (Expralpha[k] == expr[i]) count++;
}
if (count == 0) Expralpha.push_back(expr[i]);
}
}else
{
throw InvaidCharError{};
}
}
if (Expralpha.size() != n) {
throw WrongNumVarError{};
}
return result ;
}
int ExprToChart::GetPriority(const char &ch){
int i;
for (i=0; i<4; i++){
if (expr_operator[i] == ch) { break; }
}
return i+1 ;
}
string ExprToChart::ExprOperatorOut(const int& priority,bool Isbracket){
string result = "";
while(Exproperator.empty() == false && (GetPriority(Exproperator.top()) >= priority) ){
if (Exproperator.top() != '('){
result += Exproperator.top();
Exproperator.pop();
}
else{
if (Isbracket == true) { Exproperator.pop(); }
break;
}
}
return result;
}
string ExprToChart::ExprTran(const string& TmpExpr){
string result="";
for (int i=0; i<TmpExpr.length(); i++){
if (isalpha(TmpExpr[i])){
result += TmpExpr[i];
}
if (Isoperator(TmpExpr[i])){
if (Exproperator.empty()) { Exproperator.push(TmpExpr[i]); }
else{
if (GetPriority(Exproperator.top()) < GetPriority(TmpExpr[i])){
Exproperator.push(TmpExpr[i]);
}
else if (GetPriority(Exproperator.top()) >= GetPriority(TmpExpr[i])){
result += ExprOperatorOut(GetPriority(TmpExpr[i]),false);
Exproperator.push(TmpExpr[i]);
}
}
}
if (TmpExpr[i] == '(') { Exproperator.push(TmpExpr[i]); }
if (TmpExpr[i] == ')') { result += ExprOperatorOut(0, true); }
}
if (Exproperator.empty() == false) result += ExprOperatorOut(0, false);
return result;
}
string ExprToChart::CalTruthtable(const string& TmpExpr){
string result = "";
for (int i=0; i<TmpExpr.length(); i++){
if (isalpha(TmpExpr[i])){
ExprNum.push(VarNum[TmpExpr[i]-'A']);
}
if (Isoperator(TmpExpr[i])){
if (TmpExpr[i] == '~') {
int tmp = ~ExprNum.top();
ExprNum.pop();
ExprNum.push(tmp & 1);
}else{
int tmp_right = ExprNum.top();
ExprNum.pop();
if (ExprNum.empty()) { throw SyntaxError{}; }
int tmp_left = ExprNum.top();
ExprNum.pop();
switch (TmpExpr[i]) {
case '|': ExprNum.push((tmp_left|tmp_right)&1); break;
case '&': ExprNum.push((tmp_left&tmp_right)&1); break;
case '^': ExprNum.push((tmp_left^tmp_right)&1); break;
}
}
}
}
result += std::to_string(ExprNum.top());
ExprNum.pop();
if (!ExprNum.empty()) { throw SyntaxError{}; }
return result;
}
string ExprToChart::ExprTruthtable(const int& n,const string& TmpExpr){
string result = "";
for (int mark = (1 << n)-1; mark >=0 ; mark--){
for (int i=0; i<n; i++){
int tmp_ch = Expralpha[i] - 'A';
int tmp_data = (mark & (1 << tmp_ch)) >> tmp_ch ;
VarNum[tmp_ch] = tmp_data;
}
result += CalTruthtable(TmpExpr);
}
return result;
}
string ExprToChart::solve(int n, const string &expr){
string result="";
string tmp="";
tmp = filter(expr,n);
Errorcheck(tmp);
tmp = ExprTran(tmp);
result = ExprTruthtable(n,tmp);
return result;
}