Pagini recente » Cod sursa (job #50788) | Cod sursa (job #303233) | Cod sursa (job #2464036) | Cod sursa (job #3207410) | Cod sursa (job #2369274)
#include <iostream>
#include<fstream>
#include<string>
using namespace std;
ifstream in("bool.in");
ofstream out("bool.out");
string expresion,to_change;
int len=0;
bool states[30];
int changes;
void read()
{
string aux;
getline(in,aux);
expresion="("+aux+")";
in>>changes;
in>>to_change;
len=expresion.length();
}
bool is_letter(char a)
{
return a>='A'&&a<='Z';
}
string get_next_expresion(int&to_start)
{
string formed="";
for(; to_start<len; to_start++)
{
if(is_letter(expresion[to_start]))
formed+=expresion[to_start];
else
break;
}
to_start--;
return formed;
}
bool check_not(int to_start)
{
if(expresion[to_start]=='N'&&expresion[to_start+1]=='O'&&expresion[to_start+2]=='T')
return true;
return false;
}
void apply_operator(bool&last,bool newval,string&oper)
{
if(oper=="AND")
last=last&&newval;
else if(oper=="OR")
last=last||newval;
}
bool is_operator(string&a)
{
if(a=="OR" || a=="AND")
return true;
return false;
}
bool get_value(string exp)
{
if(exp=="TRUE")
return true;
if(exp=="FALSE")
return false;
if(exp.length()==1)
{
char a=exp[0];
return states[a-'A'];
}
return false;
}
bool solve_paranthesis(int&to_start)
{
bool last_value=false;
string last_operator="OR";
bool nott=false;
for(; expresion[to_start]!=')'; to_start++)
{
if(is_letter(expresion[to_start]))
{
string exp=get_next_expresion(to_start);
if(is_operator(exp))
{
nott=false;
last_operator=exp;
}
else if(exp=="NOT")
nott=!nott;
else
{
bool val=get_value(exp);
if(nott)
val=!val;
apply_operator(last_value,val,last_operator);
nott=false;
}
}
else if(expresion[to_start]=='(')
{
to_start++;
bool val=solve_paranthesis(to_start);
if(nott)
val=!val;
apply_operator(last_value,val,last_operator);
nott=false;
}
}
return last_value;
}
int main()
{
read();
for(int i=0; i<changes; i++)
{
char ch=to_change[i];
states[ch-'A']=!states[ch-'A'];
int c=1;
out<<solve_paranthesis(c);
}
}