Pagini recente » Cod sursa (job #2024491) | Cod sursa (job #2212939) | Cod sursa (job #12958) | Cod sursa (job #2005489) | Cod sursa (job #1007044)
#include <fstream>
#include <string>
#include <list>
#include <vector>
#include <stack>
inline void shunting_yard(const std::string &str, std::list<char> &expr){
std::stack<char> S;
for(unsigned c=0;c<str.size();++c){
if(str[c]=='t' || str[c]=='f' || (str[c]>='A'&&str[c]<='Z') ) expr.push_back(str[c]);
else if(str[c]=='n'){
while(!S.empty()&&S.top()=='n'){
expr.push_back('n');
S.pop();
}
S.push('n');
}
else if(str[c]=='a'){
while(!S.empty()&&(S.top()=='n'||S.top()=='a')){
expr.push_back(S.top());
S.pop();
}
S.push('a');
}
else if(str[c]=='o'){
while(!S.empty()&&(S.top()=='n'||S.top()=='a'||S.top()=='o')){
expr.push_back(S.top());
S.pop();
}
S.push('o');
}
else if(str[c]=='(') S.push('(');
else if(str[c]==')'){
while(S.top()!='('){
expr.push_back(S.top());
S.pop();
}
S.pop();
}
}
while(!S.empty()){
expr.push_back(S.top());
S.pop();
}
}
int main(){
std::ifstream fin("bool.in");
std::ofstream fout("bool.out");
std::string input;
std::getline(fin,input);
for(unsigned c=0;c<input.size()-1;++c){
if(input[c]=='N'&&input[c+1]=='O'){ input[c]='n'; input[c+1]=' '; input[c+2]=' '; c+=2;}
else if(input[c]=='O'&&input[c+1]=='R'){ input[c]='o'; input[c+1]=' '; ++c;}
else if(input[c]=='A'&&input[c+1]=='N'){ input[c]='a'; input[c+1]=' '; input[c+2]=' '; c+=2;}
else if(input[c]=='T'&&input[c+1]=='R'){ input[c]='t'; input[c+1]=' '; input[c+2]=' '; input[c+3]=' '; c+=3;}
else if(input[c]=='F'&&input[c+1]=='A'){
input[c]='f'; input[c+1]=' '; input[c+2]=' '; input[c+3]=' '; input[c+4]=' '; c+=4;
}
}
//t-true,f-false,n-not,a-and,o-or,A-Z-variables
std::list<char> expr;
shunting_yard(input,expr); //produce a reverse polish expression
int n; fin>>n;
std::vector<bool> val('t'+1,false);
val['t']=true;
while(n--){
char chx; fin>>chx; val[chx]= !val[chx];
std::stack<bool> rez;
for(auto it=expr.begin();it!=expr.end();++it){
if(*it=='n') rez.top()= !rez.top();
else if(*it=='a'){
bool b=rez.top(); rez.pop();
rez.top()=rez.top()&&b;
}
else if(*it=='o'){
bool b=rez.top(); rez.pop();
rez.top()=rez.top()||b;
}
else rez.push(val[*it]);
}
fout<<rez.top();
}
fout<<'\n';
}