int main() { int choice; char infix[50], *postfix; do { printf("(1) Convert infix to postfix expression and evaluate\n"); printf("(2) Evaluate postfix expression\n"); printf("(3) Quit\n"); printf("Enter selection (1, 2, or 3): "); scanf("%d", &choice); getchar(); // read and ignore newline if (choice == 1) { printf("Enter Infix Expression: "); fgets(infix, 50, stdin); infix[strlen(infix) - 1] = '\0'; // trim off newline character postfix = infixToPostfix(infix); printf("Postfix: %s\n", postfix); printf("Evaluates to: %d\n", evaluatePostfix(postfix)); } else if (choice == 2) { printf("Enter Postfix Expression: "); fgets(infix, 50, stdin); infix[strlen(infix) - 1] = '\0'; printf("Evaluates to: %d\n", evaluatePostfix(infix)); } else if (choice == 3) { printf("Bye.\n"); } else { printf("Invalid selection. Try again...\n"); } } while (choice != 3); return 0; }
int main() { char exp[] = "231*+9-"; printf ("Value of %s is %d", exp, evaluatePostfix(exp)); return 0; }
int main() { Expression* infix; Expression* postfix; std::string* evaluatedExpression; char newline[80]; char nextChar; std::fstream expressions; expressions.open("TestExpressions.txt"); infix = new Expression; infix->read(expressions); while(expressions.good()) { postfix = new Expression; evaluatedExpression = new std::string; infix->print(std::cout); convertToPostfix(*infix, *postfix); postfix->print(std::cout); evaluatePostfix(*postfix, *evaluatedExpression); std::cout << *evaluatedExpression << "\n"; delete infix; delete postfix; delete evaluatedExpression; expressions.getline(newline, 79, '\n'); // move past end of line nextChar = expressions.peek(); // peek next character to check if at end of file infix = new Expression; infix->read(expressions); } delete infix; expressions.close(); return 0; }
int main() { std::string infix_expression; int column_width = 16; std::cout <<std::left <<std::setw(column_width) <<"infix" <<std::left <<std::setw(column_width) <<"prefix" <<std::left <<std::setw(column_width) <<"postfix" <<std::left <<std::setw(column_width) <<"value" <<"\n\n"; while (getline(std::cin, infix_expression)) { std::string post_fix_expression; std::string pre_fix_expression; double post_fix_evaluation; post_fix_expression = infixToPostfix(infix_expression); pre_fix_expression = postfixToPrefix(post_fix_expression); post_fix_evaluation = evaluatePostfix(post_fix_expression); std::string::iterator new_end = std::remove_if(infix_expression.begin(), infix_expression.end(), isspace); std::cout <<std::left <<std::setw(column_width) <<std::string(infix_expression.begin(), new_end) <<std::left <<std::setw(column_width) <<pre_fix_expression <<std::left <<std::setw(column_width) <<post_fix_expression <<std::left <<std::setw(column_width) <<post_fix_evaluation <<std::endl; } return 0; }
int main() { char exp[100]; printf("Enter the postfix expression to be evaluated:\n"); scanf("%s", exp); printf ("Value of expression %s is %d\n", exp, evaluatePostfix(exp)); return 0; }
double Calculator::evaluate(const char * infix) { // TODO: Calculate the value of a mathematical expression given in infix notation. // Evaluate postfix double answer; answer = evaluatePostfix(infix); return answer; }
int calculator(const string &s) { return evaluatePostfix(infixToPostfix(s)); }
command_stream_t make_command_stream (int (*get_next_byte) (void *), void *get_next_byte_argument) { /* FIXME: Replace this with your implementation. You may need to add auxiliary functions and otherwise modify the source code. You can also use external functions defined in the GNU C Library. */ // error (1, 0, "command reading not yet implemented"); // return 0; //get all input chars to buf char* buf = malloc(65536); int size = 65536; int end_buf = 0; int i; int val; for (; (val = get_next_byte(get_next_byte_argument)) != EOF; ) { char value = (char) val; if (end_buf >= size) { char* t_buf = malloc(size *= 2); copy(buf, t_buf, size); free(buf); buf = t_buf; } buf[end_buf++] = value; } buf[end_buf++] = ' '; //handle errors if (!basicSyntax(buf)) error(1, 0, "basic syntax error!"); //tokenize buffer tokentype t = NULL_TOKEN; char* tokchars = malloc(128); int cwp = 0; //current word pointer bool commenting; //true or false: are we commenting? token* start, *end; //represents linked list of tokens token** andstart = &start; token** andend = &end; start = end = NULL; for (i = 0; i < end_buf;) { char c = buf[i]; if (c == '#') { if (t != NULL_TOKEN) addToken_make(&andstart, &andend, t, tokchars, cwp, 0); t = NULL_TOKEN; free(tokchars); tokchars = malloc(128); cwp = 0; commenting = 1; i++; } else if (c == '\n' && commenting) { commenting = 0; i++; } else if (commenting) { i++; continue; } else if (c == '\r') continue; else if (c == '&') { if (t != NULL_TOKEN) addToken_make(&andstart, &andend, t, tokchars, cwp, 0); t = NULL_TOKEN; token* andtoken = malloc(sizeof(token)); andtoken->t = AND_TOKEN; andtoken->sub = cur_shellnum; addToken(&start, &end, andtoken); t = NULL_TOKEN; i += 2; free(tokchars); tokchars = malloc(128); cwp = 0; } else if (c == '|') { if (t != NULL_TOKEN) addToken_make(&andstart, &andend, t, tokchars, cwp, 0); t = NULL_TOKEN; if (buf[i+1] == '|') { token* ortoken = malloc(sizeof(token)); ortoken->t = OR_TOKEN; ortoken->sub = cur_shellnum; addToken(&start, &end, ortoken); t = NULL_TOKEN; i += 2; } else { token* pipetoken = malloc(sizeof(token)); pipetoken->t = PIPE_TOKEN; pipetoken->sub = cur_shellnum; addToken(&start, &end, pipetoken); t = NULL_TOKEN; i += 1; } free(tokchars); tokchars = malloc(128); cwp = 0; } else if (c == '(' || c == ')' || c == '<' || c == '>' || c == ';' || c == '\n') { if (t != NULL_TOKEN) addToken_make(&andstart, &andend, t, tokchars, cwp, 0); t = NULL_TOKEN; addToken_make(&andstart, &andend, tokentypeOf(c), 0, 0, (c == '(' || c == ')')); free(tokchars); tokchars = malloc(128); cwp = 0; i++; } else if (c == ' ' || c == '\t' || !c) { if (t == WORD_TOKEN) tokchars[cwp++] = c; else { free(tokchars); tokchars = malloc(128); cwp = 0; t = NULL_TOKEN; } i++; } else if (c == '!' || c == '%' || c == '+' || c == ',' || c == '-' || c == '.' || c == '/' || c == ':' || c == '@' || c == '^' || c == '_' || ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || ('0' <= c && c <= '9')) { t = WORD_TOKEN; tokchars[cwp++] = c; i++; } else { int xx = 0; error (1, 0, "unrecognized character"); } } if (t != NULL_TOKEN) addToken_make(&andstart, &andend, t, tokchars, cwp, 0); //clean token stream first token* clean; token** andclean = &clean; for (clean = start; clean; clean = clean->next) //start by removing adjacent newlines and checking syntax of semicolon, pipe, and, and or if (clean->t == NEWLINE_TOKEN) while (clean->next && clean->next->t == NEWLINE_TOKEN) { clean->next = clean->next->next; if (clean->next) clean->next->prev = clean; } else if (clean->t == SEMICOLON_TOKEN || clean->t == PIPE_TOKEN || clean->t == AND_TOKEN || clean->t == OR_TOKEN) { if (!clean->prev || clean->prev->t == NEWLINE_TOKEN) error (1, 0, "illegal semicolon or operator syntax"); } for (clean = start; clean; clean = clean->next) //then check operator syntax once more if (clean->t == AND_TOKEN || clean->t == OR_TOKEN || clean->t == PIPE_TOKEN) { if (!clean->next || clean->next->t == SEMICOLON_TOKEN || !clean->prev || clean->prev->t == SEMICOLON_TOKEN) error (1, 0, "illegal operator syntax"); if (clean->next->t == NEWLINE_TOKEN) { token* temp = clean->next; for (; temp && temp->t == NEWLINE_TOKEN; temp = temp->next); if (!temp || temp->t == SEMICOLON_TOKEN) error (1, 0, "illegal operator syntax"); } } else if (clean->t == LESS_TOKEN || clean->t == GREATER_TOKEN) if (!clean->prev || !clean->next || clean->prev->t != WORD_TOKEN || clean->next->t != WORD_TOKEN) error(1, 0, "bad redirection!"); for (clean = start; clean; clean = clean->next) //then get rid of all newlines { if (clean->t == NEWLINE_TOKEN) { if ((clean->prev != NULL && clean->prev->t != WORD_TOKEN && clean->prev->t != RIGHT_PAREN_TOKEN) || (clean->next != NULL && clean->next->t != WORD_TOKEN && clean->next->t != LEFT_PAREN_TOKEN)) //function as whitespace { token* p = clean->prev; token* n = clean->next; p->next = n; if (n) n->prev = p; continue; } else //function as semicolon clean->t = SEMICOLON_TOKEN; } if (clean->t == SEMICOLON_TOKEN) while (clean->next && clean->next->t == SEMICOLON_TOKEN) { clean->next = clean->next->next; if (clean->next) clean->next->prev = clean; } } if (start && start->t == SEMICOLON_TOKEN) { start = start->next; start->prev = 0; } //change token stream from infix to postfix token* finalTokenStream = infixToPostfix(start); command_stream* strm = evaluatePostfix(finalTokenStream); return strm; }