Pagini recente » Cod sursa (job #1356130) | Cod sursa (job #51634) | Cod sursa (job #277119) | Cod sursa (job #2490271) | Cod sursa (job #3140677)
#include <iostream>
#include <string>
using namespace std;
bool CONSTANTS[26] = {0};
bool Or(string &str, int &position, string &temp_expr);
bool And(string &str, int &position, string &temp_expr);
bool ParantezeNotConst(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 = ParantezeNotConst(str, position, temp_expr);
bool Nr2;
while (temp_expr == "AND"){
Nr2 = ParantezeNotConst(str, position, temp_expr);
return (Nr1 && Nr2);
}
return Nr1;
}
bool ParantezeNotConst(string &str, int &position, string &temp_expr){
bool Nr1;
if (str[position] == '('){
position += 1;
Nr1 = Or(str, position, temp_expr);
position += 1;
} else {
string ConstantOrTermOrExpression;
bool FoundExpression = false;
while (FoundExpression == false) {
ConstantOrTermOrExpression = "";
while (str[position] != ' ' && str[position] != ')') {
ConstantOrTermOrExpression += str[position];
position += 1;
}
position += 1;
if (ConstantOrTermOrExpression == "NOT") {
ConstantOrTermOrExpression = "";
while (str[position] != ' ' && str[position] != ')') {
ConstantOrTermOrExpression += str[position];
position += 1;
}
Nr1 = !CONSTANTS[ConstantOrTermOrExpression[0] - 'A'];
} else if (ConstantOrTermOrExpression == "TRUE") {
Nr1 = true;
break;
} else if (ConstantOrTermOrExpression == "FALSE") {
Nr1 = false;
break;
} else if (ConstantOrTermOrExpression.size() == 1) {
Nr1 = CONSTANTS[ConstantOrTermOrExpression[0] - 'A'];
}
if (str[position] == ')') {
position += 1;
if (str[position] == ' ') {
position += 1;
}
if (FoundExpression == false)
return Nr1;
}
if (ConstantOrTermOrExpression == "AND" || ConstantOrTermOrExpression == "OR") {
FoundExpression = true;
}
temp_expr = ConstantOrTermOrExpression;
}
}
return Nr1;
}
int main(){
string expression;
getline(cin, expression);
int changes;
cin >> changes;
string change;
cin >> change;
for (int i = 0; i < change.size(); i++){
CONSTANTS[change[i] - 'A'] = !CONSTANTS[change[i] - 'A'];
int position = 0;
string temp_expr = "0";
cout << Or(expression, position, temp_expr);
}
}