-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCNFExp.cpp
More file actions
171 lines (158 loc) · 3.77 KB
/
Copy pathCNFExp.cpp
File metadata and controls
171 lines (158 loc) · 3.77 KB
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
//
// CNFExp.cpp
// ROBDD
//
// Created by Mengdi Wang on 13-5-16.
// Copyright (c) 2013年 Mengdi Wang. All rights reserved.
//
#include "CNFExp.h"
#include <string>
void CNFExp::ProduceStack(std::string formula)
{
formula+="#";
memset(mystack, 0, (size+2)*sizeof(char));
memset(ex, 0, size*sizeof(char));
int top =0; //符号辅助 栈指针
mystack[top] = '#';
position = 0; //后缀表达式 下标
int i=0; //遍历表达式
char ch = formula[i];
while(ch != '#')
{
switch(ch)
{
case'(':
top++;
mystack[top] = ch;
break;
case')':
while(mystack[top]!='(')
{
ex[position] = mystack[top];
top--;
position++;
}
top--;
break;
case'|':
case'&':
while(top!=0 && mystack[top]!='(')
{
ex[position]=mystack[top];
top--;
position++;
}
top++;
mystack[top]=ch;
break;
case'!':
top++;
mystack[top]=ch;
break;
case'#':
while(top!=0)
{
ex[position]=mystack[top];
top--;
position++;
}
break;
default:
ex[position]=ch;
position++;
}
i++;
ch = formula[i];
}
//表达式处理完毕,将当前所有操作数出栈
while(mystack[top] != '#')
{
ex[position]=mystack[top];
top--;
position++;
}
#ifdef DD
printf("转化为后缀表达为:\n");
for(i=0;i<position;i++)
{
printf("%c",ex[i]);
}
printf("\n");
#endif
}
//-------------------------------------------
void CNFExp::Setv(char var, int value)
{
for(int i=0;i<position;i++)
{
if (ex[i] == var)
ex[i] = '0' + value;
}
}
//-------------------------------------------
void CNFExp::Setvbyn(int varn, int value)// 1=='a'
{
for(int i=0;i<position;i++)
{
if (ex[i] == 'a'-1+varn)
ex[i] = '0' + value;
}
}
//-------------------------------------------
bool CNFExp::AllApply()
{
for(int i=0;i<position;i++)
{
if (ex[i] != '&' && ex[i] != '|' && ex[i] != '!' && ex[i] != '!'
&& ex[i] != '1' && ex[i] != '0')
return false;
}
return true;
}
//---------------------------------------------
bool CNFExp::GetValue()
{
if ( !AllApply() )
return -1;
int top =0; //符号辅助 栈指针
mystack[top] = '#';
int i=0; //遍历后缀表达式
char ch = ex[i];
char CalCharHere;
bool Here =false;
char CalCharBefore;
bool Before = false;
while(i!= position)
{
switch(ch)
{
case'|':
CalCharHere = mystack[top];
Here = (CalCharHere == '1') ? true:false;
top--;
CalCharBefore = mystack[top];
Before = (CalCharBefore == '1') ? true:false;
mystack[top]= (Here || Before) ? '1' : '0' ;
break;
case'&':
CalCharHere = mystack[top];
Here = (CalCharHere == '1') ? true:false;
top--;
CalCharBefore = mystack[top];
Before = (CalCharBefore == '1') ? true:false;
mystack[top]= (Here && Before) ? '1' : '0' ;
break;
case'!':
CalCharHere = mystack[top];
Here = (CalCharHere == '1') ? true:false;
mystack[top]= Here ? '0' : '1' ;
break;
default:
top++;
mystack[top]= ch;
}
i++;
ch = ex[i];
}
return mystack[top] =='1';
}