Pagini recente » Cod sursa (job #1769810) | Cod sursa (job #2815318) | Cod sursa (job #1861660) | Cod sursa (job #2917318) | Cod sursa (job #94728)
Cod sursa(job #94728)
#include <iostream>
#include <fstream>
#include <stack>
#include <string>
using namespace std;
bool chars[27];
stack<string> postFix;
void transform(string expresie)
{
if(expresie.length() == 0)
return;
if(expresie.substr(0, 1).compare(" ") == 0)
expresie = expresie.substr(1);
if(expresie.length() == 0)
return;
if(expresie.substr(0,1).compare("(") == 0)
{
int c = 0;
size_t i = 0;
for(; i < expresie.length(); ++i)
{
if(expresie.at(i) == '(')
++c;
if(expresie.at(i) == ')')
--c;
if(c == 0)
break;
}
transform(expresie.substr(1, i-1));
if(i < expresie.length())
transform(expresie.substr(i+1));
}
if(expresie.length() >= 3)
{
if(expresie.substr(0,3).compare("AND") == 0)
{
transform(expresie.substr(3));
postFix.push("AND");
}
if(expresie.substr(0,3).compare("NOT") == 0)
{
transform(expresie.substr(3));
postFix.push("NOT");
}
if(expresie.substr(0,2).compare("OR") == 0)
{
transform(expresie.substr(2));
postFix.push("OR");
}
}
if(expresie.length() > 1 && expresie.at(1) == ' ')
{
postFix.push(expresie.substr(0,1));
transform(expresie.substr(1));
}
if(expresie.length() == 1)
{
postFix.push(expresie);
}
if(expresie.length() >= 4 && expresie.substr(0,4).compare("TRUE") == 0)
{
postFix.push("TRUE");
transform(expresie.substr(4));
}
if(expresie.length() >= 5 && expresie.substr(0,5).compare("FALSE") == 0)
{
postFix.push("FALSE");
transform(expresie.substr(5));
}
}
bool getElement(stack<string> st)
{
string s = st.top();
st.pop();
if(s == "TRUE")
return true;
if(s == "FALSE")
return false;
if(s.length() == 1)
return chars[s.at(0)-'A'];
if(s.compare("AND") == 0)
return getElement(st) && getElement(st);
if(s.compare("OR") == 0)
return getElement(st) || getElement(st);
if(s.compare("NOT") == 0)
return !getElement(st);
}
int main(void)
{
ifstream in;
ofstream out;
in.open("bool.in");
out.open("bool.out");
string expresie;
char read[1001];
in.getline(read, 1001);
expresie.assign(read);
transform(expresie);
for(int i=0;i<27;++i)
chars[i] = false;
int n;
in >> n;
for(int i=0;i<n;++i)
{
char ch;
in >> ch;
chars[ch-'A'] = !chars[ch-'A'];
stack<string> work(postFix);
string s;
while(!work.empty())
{
s = work.top();
work.pop();
if(s.compare("AND") == 0)
{
bool b = getElement(work) && getElement(work);
if(b)
s = "TRUE";
else
s = "FALSE";
work.push(s);
}
if(s.compare("OR") == 0)
{
bool b = getElement(work) || getElement(work);
if(b)
s = "TRUE";
else
s = "FALSE";
work.push(s);
}
if(s.compare("NOT") == 0)
{
bool b = !getElement(work);
if(b)
s = "TRUE";
else
s = "FALSE";
work.push(s);
}
}
if(s == "TRUE")
out << "1";
else if(s == "FALSE")
out << "0";
else
if(chars[s.at(0)-'A'])
out << "1";
else
out << "0";
}
out << endl;
in.close();
out.close();
return 0;
}