Pagini recente » Cod sursa (job #1360119) | Cod sursa (job #2162259) | Cod sursa (job #1023877) | Cod sursa (job #2942120) | Cod sursa (job #1226837)
#include <fstream>
#include <iostream>
#include <stack>
#include <string>
#define ascii 65
#define NOF 26 //number of letters
using namespace std;
ifstream f1 ("bool.in"); //INTRARE
ofstream f2 ("bool.out"); //IESIRE
bool letters[NOF];
int opr;
stack<string> st;
stack<char> eval;
stack<int> rel;
string expr;
string aux;
string all;
char chr;
void op();
void test();
void evaluator();
int main(){
//FORMA POSTFIXATA
while(f1>>noskipws>>chr){
if(chr == '\n') break;
else if (chr == ' ') {
test();
}
else if (chr == '(') {
test();
aux = chr;
st.push(aux);
rel.push(0);
}
else if (chr == ')') {
test();
while(rel.top() > 0){
expr += st.top();
rel.pop();
st.pop();
}
rel.pop();
st.pop();
}
else all += chr;
}
if(!all.empty()){
test();
}
while(!st.empty()){
expr += st.top();
st.pop();
}
//END - expr - FORMA POSTFIXATA
//LETTERS VECTOR INIT
for(int i = 0; i < NOF; i++){
letters[i] = 0;
}
int n;
f1>>n;
f1>>chr;
for(int i = 0; i < n; i++){
char c;
f1>>c;
letters[c - ascii] = !letters[c - ascii];
evaluator();
}
return 0;
}
void test(){
int size = all.size();
if(size == 1){ //f2<<"da*"<<all<<"*";
expr += all;
}
else if(size > 1){ //f2<<"nu*"<<all<<"*";
if(all == "AND"){
opr = 2;
aux="*";
op();
}
else if(all == "OR"){
opr = 1;
aux="+";
op();
}
else if(all == "NOT"){
opr = 3;
aux="!";
op();
}
else if(all == "TRUE"){
expr += "1";
}
else if(all == "FALSE"){
expr += "0";
}
}
all.clear();
}
void op(){
while(!st.empty() && rel.top() >= opr){
//f2<<"da"<<aux<<opr<<"\n";
expr += st.top();
st.pop();
rel.pop();
}
st.push(aux);
rel.push(opr);
}
void evaluator(){
//EVALUATION OF EXPRESSION
int max_e = expr.size();
for(int i = 0; i < max_e; i++){
if(expr[i] >= ascii && expr[i] < ascii + NOF){
eval.push(expr[i]);
} else {
chr = '0';
if(expr[i] == '+'){
bool e1 = letters[eval.top() - ascii];
eval.pop();
bool e2 = letters[eval.top() - ascii];
eval.pop();
if(e1 || e2) chr = '1';
}
else if(expr[i] == '*'){
bool e1 = letters[eval.top() - ascii];
eval.pop();
bool e2 = letters[eval.top() - ascii];
eval.pop();
if(e1 && e2) chr = '1';
}
else if(expr[i] == '!') {
bool e = letters[eval.top() - ascii];
eval.pop();
if(!e) chr = '1';
}
eval.push(chr);
}
}
//if(eval.size() == 1) {
f2<<eval.top();
eval.pop();
//}
//else f2<<"errrrrrr";
}