Pagini recente » Cod sursa (job #1103531) | Borderou de evaluare (job #1888892) | Cod sursa (job #334656) | Cod sursa (job #2001571) | Cod sursa (job #2632771)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#include <string.h>
#include <stdlib.h>
using namespace std;
ifstream in ("bool.in");
ofstream out("bool.out");
char buff[1010];
bool val[260];
int poz, n, q;
vector <char> a;
stack <char> stk;
inline void pars(string & s)
{
while(buff[poz]==' ' && poz<n)
poz++;
while(buff[poz]>='A' && buff[poz]<='Z' && poz<n)
s+=buff[poz++];
if(s.empty() && poz<n) s=buff[poz++];
}
inline char getNext()
{
string s; pars(s);
if(s.size()==1)
{
if(s[0]=='(')
return '(';
else if(s[0]==')')
return ')';
else
return s[0];
}
if(s=="NOT") return '!';
else if(s=="AND") return '&';
else if(s=="OR") return '|';
else if(s=="TRUE") return '1';
else if(s=="FALSE") return '0';
return 0;
}
void creareFormaPrefixata();
bool calculare()
{
for(auto & x : a)
{
if(x>='A' && x<='Z')
stk.push(val[(int)x]);
else if(x=='0'||x=='1')
stk.push(x-'0');
else if(x=='!')
{
auto temp=stk.top(); stk.pop();
temp^=1;
stk.push(temp);
}
else if(x=='&')
{
auto temp1=stk.top(); stk.pop();
auto temp2=stk.top(); stk.pop();
stk.push(temp1&temp2);
}
else if(x=='|')
{
auto temp1=stk.top(); stk.pop();
auto temp2=stk.top(); stk.pop();
stk.push(temp1|temp2);
}
}
auto temp=stk.top();
stk.pop();
return temp;
}
int main()
{
in.getline(buff, 1010); n=strlen(buff);
creareFormaPrefixata();
in.getline(buff, 1010); q=atoi(buff);
in.getline(buff, 1010);
for(int i=0; buff[i]!='\0'; i++)
{
val[(int)buff[i]]^=1;
out<<(int)calculare();
}
return 0;
}
void creareFormaPrefixata()
{
char val=getNext();
while(val)
{
if((val>='A' && val<='Z')||val=='1'||val=='0') ///e numar
{
a.push_back(val);
val=getNext();
continue;
}
if(stk.empty())
stk.push(val);
else
{
if(val=='(')
stk.push('(');
else if(val==')')
{
while(stk.top()!='(')
a.push_back(stk.top()), stk.pop();
stk.pop();
}
else if(val=='!')
{
while(!stk.empty()&&stk.top()=='!')
a.push_back(stk.top()), stk.pop();
stk.push(val);
}
else if(val=='&')
{
while(!stk.empty()&&stk.top()=='&')
a.push_back(stk.top()), stk.pop();
stk.push(val);
}
else if(val=='|')
{
while(!stk.empty()&&(stk.top()=='&'||stk.top()=='|'))
a.push_back(stk.top()), stk.pop();
stk.push(val);
}
}
val=getNext();
}
while(!stk.empty())
a.push_back(stk.top()), stk.pop();
}