int parse(string s){ StackInt stack; // for each character in the array for(unsigned int i=0; i < s.length(); i++) { if(s[i] == ' '){ if(!stack.empty()) // make sure theres no space between integers if(stack.top() >= 0) if(s[i+1] >= '0' && s[i+1] <= '9') return MAL; continue; // don't do anything on white space } else if(s[i] >= '0' && s[i] <= '9'){ // is int, add to stack int num = (int)s[i] - 48; if(!stack.empty()){ if(stack.top() >= 0){ // the previous int is the tens of current int num = num + stack.top()*10; stack.pop(); } } stack.push(num); } else if(s[i] == '>'){ // is >, add to stack if(stack.empty()) stack.push(RIGHT); else { // avoid throw exception if(stack.top() != CLOSE && stack.top() < 0) stack.push(RIGHT); else return MAL; // may not be preceeded by int or ) } } else if(s[i] == '<'){ // is <, add to stack if(stack.empty()) stack.push(LEFT); else { // avoid throw exception if (stack.top() != CLOSE && stack.top() < 0) stack.push(LEFT); else return MAL; // may not be preceeded by int or ) } } else if(s[i] == '*' || s[i] == '+'){ if(parseShift(stack) == 1) return MAL; // parseShift if(stack.empty()) return MAL; if(stack.top() < 0 && stack.top() != CLOSE) return MAL; if(s[i] == '*') stack.push(TIMES); else if(s[i] == '+') stack.push(PLUS); } else if(s[i] == '('){ if(!stack.empty() && (stack.top() >= 0 || stack.top() == CLOSE)) return MAL; stack.push(OPEN); } else if(s[i] == ')'){ if(parseShift(stack) == 1) return MAL; // parseShift if(parseExp(stack) == 1) return MAL; // parseExp } else return MAL; } if(parseShift(stack) == 1) return MAL; // parseShift, outside of () // last check for syntaxical errors int final; if(stack.top() >= 0) final = stack.top(); else return MAL;
int main(int argc, char const *argv[]) { ifstream IFileIn; ifstream IFileVars; int AND = -2; int OR = -3; int NOT = -4; int OpenParenth = -5; int OpenCount = 0; int ClosedCount = 0; int Answer = 0; int numberBeg; int numberEnd; string line; bool charBool; bool nextBool = NULL; bool currBool = NULL; string number; StackInt stack; map<string, bool> boolMap; map<string, bool>::iterator it; //stackint expressionStack; IFileVars.open(argv[1]); //open the variable expressions file if (IFileVars.fail()) { cout << "\n\n\nCould not open the file\n\n\n"; } else if (IFileVars.is_open()){ while ( getline (IFileVars,line)){ //read in each line character by character and store the keys and values separated by null spaces for (int i = 0; i < line.length(); ++i) { //find the end of each number if (isdigit(line[i]) != 0 && isdigit(line[i+1]) == 0){ numberEnd = i + 1; } //if the character is T or F store that as a bool if (line[i] == 'T'){ charBool = true; } if (line[i] == 'F'){ charBool = false; } } //store number into a string for (int j = 0; j < numberEnd; ++j){ number += line[j]; } //store the pair<string, bool> into the map boolMap.insert(pair<string, bool>(number, charBool)); //reset the number string number = ""; } IFileVars.close(); } //open the expr1.in file IFileIn.open(argv[2]); if (IFileIn.fail()){ cout << "Could not open file"; } else if(IFileIn.is_open()) { while(getline(IFileIn,line)){ Answer = 0; for (int i = 0; i < line.length(); ++i){ if (isdigit(line[i]) == 0 && isdigit(line[i+1]) != 0){ numberBeg = i +1; } if (isdigit(line[i]) != 0 && isdigit(line[i+1]) == 0){ numberEnd = i; for (int j = numberBeg; j <= numberEnd; ++j) { number += line[j]; } if (boolMap.count(number) == 1) { stack.push(boolMap[number]); } else if (boolMap.count(number) == 0 && Answer == 0){ Answer = 3; } //set the number string to null number = ""; } //store all of the boolean values and open parenthesis onto the stack if (line[i] == '&'){ stack.push(AND); } if (line[i] == '|'){ stack.push(OR); } if (line[i] == '~'){ stack.push(NOT); } if (line[i] == '('){ OpenCount += 1; stack.push(OpenParenth); } //evaluate at the ) character if (line[i] == ')' && stack.empty() != 0) { ClosedCount += 1; } if (line[i] == ')' && stack.empty() == 0){ ClosedCount += 1; if (stack.top() == AND || stack.top() == OR || stack.top() == NOT && Answer == 0) {Answer = 4;continue;} currBool = stack.top(); stack.pop(); while(stack.top() == -4){ stack.pop(); ~currBool; } while(!stack.empty()){ if (stack.top() == AND){ stack.pop(); if (stack.top() == AND || stack.top() == OR || stack.top() == OpenParenth || stack.top() == NOT){ if (Answer == 0){ Answer = 4; } } nextBool = stack.top(); stack.pop(); while(stack.top() == -4){ stack.pop(); ~nextBool; } if (stack.top() == OR && Answer == 0){ Answer = 4; continue; } currBool = currBool & nextBool; } if (stack.top() == OR){ stack.pop(); if (stack.top() == AND || stack.top() == OR || stack.top() == OpenParenth || stack.top() == NOT && Answer == 0){ if (Answer == 0){ Answer = 4; } } nextBool = stack.top(); stack.pop(); while(stack.top() == -4){ stack.pop(); ~nextBool; } if (stack.top() == AND && Answer == 0){ Answer = 4; continue; } currBool = currBool | nextBool; } if (stack.top() == OpenParenth){ stack.pop(); while(stack.top() == -4){ stack.pop(); ~currBool; } } } } } if (OpenCount != ClosedCount && Answer == 0){ Answer = 4; } if (Answer == 0 && line != "" && currBool == 1){ cout << "\nT"; }if (Answer == 0 && line != "" && currBool == 0){ cout << "\nF"; } else if (Answer == 3){ cout << "\nUnknown Variable"; } else if (Answer == 4){ cout << "\nMalformed"; } } } return 0; }
bool eval( int & answer, const string & line ) { bool shifter = false; int openparen = 0, leftshift = 0, rightshift = 0, ast = 0, plu = 0; StackInt st; int test1 = 0; string token; istringstream sin( line ); sin >> token; for(int i = 0; i < line.length(); i++) { if(shifter == true) { if(line[i] != '(' && !(line[i] >= '0' && line[i] <= '9')) { //cout << "Law Breaker: " << line[i] << endl; return false; } //shifter = false; } if(line[i] == '(') { openparen++; st.push(oparen); } else if(line[i] == '>' || line[i] == '<') { if(line[i] == '>') { rightshift++; } else if (line[i] == '<') { leftshift++; } while(line[i+1] == '>' || line[i+1] == '<') { if(line[i+1] == '>') { rightshift++; } else if (line[i+1] == '<') { leftshift++; } i++; } shifter = true; continue; } else if(line[i] == '*') { st.push(times); } else if(line[i] == '+') { st.push(plussign); } else if(line[i] >= '0' && line[i] <= '9') { test1 = (line[i] - 48); while(line[i+1] >= '0' && line[i+1] <= '9') { //cout << "Test 1: " << test1 << " Line i: " << line[i] << " i +1: " << line[i+1] << endl; test1 = (test1*10) + (line[i+1]-48); i++; } st.push(test1); if(shifter == true) { int power = 0; test1 = st.top(); st.pop(); if(rightshift < leftshift) { power = leftshift - rightshift; test1 = test1 * pow(2,power); //cout << test1 << endl; //st.push(test1); } else if (leftshift < rightshift) { power = rightshift - leftshift; test1 = test1 / pow(2, power); } st.push(test1); //cout << "TOP: " << st.top() << endl; leftshift = rightshift = 0; shifter = false; } } else if(line[i] == ')') { int lhs = 0, rhs = 0, oper = 0, temp = 0, val = -1; rhs = st.top(); st.pop(); //cout << "TEST SPOT : " << st.top() << endl; oper = st.top(); if( oper > 0) { return false; } else { st.pop(); lhs = st.top(); st.pop(); val = mathop(lhs, rhs, oper); if(val < 0) { return false; } if(st.top() == -1) { openparen--; st.pop(); } st.push(val); } } } if(openparen == 0) { answer = st.top(); st.pop(); if(st.empty()) { return true; } } else return false; /*cout << openparen << ", " << leftshift << ", " << rightshift << endl; cout << "Numbers: " << st.top() << endl; */ } // evaluate