Pagini recente » Cod sursa (job #18029) | Cod sursa (job #2588656) | Cod sursa (job #1570920) | Cod sursa (job #671115) | Cod sursa (job #3140685)
#include <fstream>
#include <string>
using namespace std;
ifstream fin("bool.in");
ofstream fout("bool.out");
bool CONSTANTS[26] = {0};
bool Or(string &str, int &position, string &temp_expr);
bool And(string &str, int &position, string &temp_expr);
bool ParantezeConst(string &str, int &position, string &temp_expr);
bool Non(string &str, int &position, string &temp_expr);
bool Or(string &str, int &position, string &temp_expr){
bool Nr1 = And(str, position, temp_expr);
bool Nr2;
while (temp_expr == "OR") {
Nr2 = And(str, position, temp_expr);
return (Nr1 || Nr2);
}
return Nr1;
}
bool And(string &str, int &position, string &temp_expr){
bool Nr1 = Non(str, position, temp_expr);
bool Nr2;
while (temp_expr == "AND"){
Nr2 = Non(str, position, temp_expr);
return (Nr1 && Nr2);
}
return Nr1;
}
bool Non(string &str, int &position, string &temp_expr){
bool Nr1 = ParantezeConst(str, position, temp_expr);
int NotCount = 0;
while (temp_expr == "NOT"){
NotCount += 1;
Nr1 = ParantezeConst(str, position, temp_expr);
}
for (int i = 1; i <= NotCount; i++){
Nr1 = !Nr1;
}
return Nr1;
}
bool ParantezeConst(string &str, int &position, string &temp_expr){
bool Nr1;
bool FoundNumber = false;
if (str[position] == '('){
position += 1;
Nr1 = Or(str, position, temp_expr);
position += 1;
} else {
string ConstantOrTermOrExpression;
while (ConstantOrTermOrExpression != "AND" && ConstantOrTermOrExpression != "OR" && ConstantOrTermOrExpression != "NOT" && FoundNumber == false) {
ConstantOrTermOrExpression = "";
while (str[position] != ' ' && str[position] != ')' && str[position] != '(' && position < str.size()) {
ConstantOrTermOrExpression += str[position];
position += 1;
}
if (position < str.size()) {
position += 1;
}
if (ConstantOrTermOrExpression == "TRUE") {
Nr1 = true;
break;
} else if (ConstantOrTermOrExpression == "FALSE") {
Nr1 = false;
break;
} else if (ConstantOrTermOrExpression.size() == 1) {
Nr1 = CONSTANTS[ConstantOrTermOrExpression[0] - 'A'];
FoundNumber = true;
}
if (str[position] == ')' || position == str.size()) {
position += 1;
if (str[position] == ' ') {
position += 1;
}
}
if (ConstantOrTermOrExpression == "AND" || ConstantOrTermOrExpression == "OR" || ConstantOrTermOrExpression == "NOT") {
temp_expr = ConstantOrTermOrExpression;
break;
}
temp_expr = ConstantOrTermOrExpression;
}
}
return Nr1;
}
int main(){
string expression;
getline(fin, expression);
int changes;
fin >> changes;
string change;
fin >> change;
for (int i = 0; i < change.size(); i++){
CONSTANTS[change[i] - 'A'] = !CONSTANTS[change[i] - 'A'];
int position = 0;
string temp_expr = "0";
fout << Or(expression, position, temp_expr);
}
}