int main() { char exp[] = "a+b*(c^d-e)^(f+g*h)-i"; printf("\tInfix expression: %s", exp); infixToPostfix(exp); return 0; }
void Eval::compileExpression (const std::string& e) { // Reduce e to a vector of tokens. Lexer l (e); l.ambiguity (_ambiguity); std::string token; Lexer::Type type; while (l.token (token, type)) { if (_debug) context.debug ("Lexer '" + token + "' " + Lexer::type_name (type)); _compiled.push_back (std::pair <std::string, Lexer::Type> (token, type)); } // Parse for syntax checking and operator replacement. if (_debug) context.debug ("[1;37;42mFILTER[0m Infix " + dump (_compiled)); infixParse (_compiled); if (_debug) context.debug ("[1;37;42mFILTER[0m Infix parsed " + dump (_compiled)); // Convert infix --> postfix. infixToPostfix (_compiled); if (_debug) context.debug ("[1;37;42mFILTER[0m Postfix " + dump (_compiled)); }
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() { 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; }
void Eval::evaluateInfixExpression (const std::string& e, Variant& v) const { // Reduce e to a vector of tokens. Lexer l (e); l.ambiguity (_ambiguity); std::vector <std::pair <std::string, Lexer::Type> > tokens; std::string token; Lexer::Type type; while (l.token (token, type)) tokens.push_back (std::pair <std::string, Lexer::Type> (token, type)); // Parse for syntax checking and operator replacement. if (_debug) context.debug ("[1;37;42mFILTER[0m Infix " + dump (tokens)); infixParse (tokens); if (_debug) context.debug ("[1;37;42mFILTER[0m Infix parsed " + dump (tokens)); // Convert infix --> postfix. infixToPostfix (tokens); if (_debug) context.debug ("[1;37;42mFILTER[0m Postfix " + dump (tokens)); // Call the postfix evaluator. evaluatePostfixStack (tokens, v); }
void main() { int i,j,t; struct stack s; char post[20],infix[20]; init(&s); push(&s,'5'); push(&s,'2'); i=pop(&s); j=peep(&s); if(s.top!=-1) { printf("Popped element = %d\n ",i); printf("Top element = %d\n ",j); } printf("Enter the infix expression:\n"); scanf("%s",infix); infixToPostfix(infix,post); printf("Postfix statement:\n"); printf("%s",post); /*printf("Enter postfix expression:\n "); scanf("%s",post);*/ t=evaluate(post); printf("\nThe result is %d ",t); }
/** * @param expression: a vector of strings; * @return: an integer */ int evaluateExpression(vector<string> &expression) { if (expression.empty()) { return 0; } vector<string> postfix; infixToPostfix(expression, postfix); return evaluatePostfixExpression(postfix); }
/** * Calculates the line and stores the result to destination variable. * While doing operations, prints intermediate step information for stacks * and postfix expressions. * @param codeLine The code line which will be calculated. */ void calculateLine(struct code_line *codeLine) { printf("----> Arithmetic Expression: %s\n", codeLine->lineContentInfix); printf("--> Infix-Postfix Conversion\n"); infixToPostfix(codeLine); printf("--> Parsing Postfix Expression\n"); parsePostfixExpression(codeLine, codeLine->lineContentInfix[0]); printf("----> Operations completed for this arithmetic expression.\n\n\n"); }
int main (int argc, char* argv[]) { stack myStack; stack evalStack; createStack(myStack); createStack(evalStack); printf("Please enter an infix expression"); scanInfix(); infixToPostfix(myStack); printf("%d", evaluate(evalStack)); }
//testing for the asm functions int main() { String exp = "( ( AX + ( BY * C ) ) / ( D4 - E ) )"; std::cout << "Infix: \n" << exp << std::endl; std::cout << "Postfix: \n" << infixToPostfix(exp) << std::endl; exp = " ( ( ( A + B ) + ( C + D ) ) + ( E + F ) )"; std::cout << "Infix: \n" << exp << std::endl; std::cout << "Postfix: \n" << infixToPostfix(exp) << std::endl; exp = "( A + B )"; std::cout << "Infix: \n" << exp << std::endl; std::cout << "Postfix: \n" << infixToPostfix(exp) << std::endl; exp = "( A + ( B + C ) )"; std::cout << "Infix: \n" << exp << std::endl; std::cout << "Postfix: \n" << infixToPostfix(exp) << std::endl; exp ="( ( H * ( ( ( ( A + ( ( B + C ) * D ) ) * F ) * G ) * E ) ) + J )"; std::cout << "Infix: \n" << exp << std::endl; std::cout << "Postfix: \n" << infixToPostfix(exp) << std::endl; }
int main() { char expr[MAX]; // expressao infixa char post[MAX]; // expressao posfixa Pilha p; int i; while (scanf("%s", expr) != EOF) { inicializaPilha(&p); infixToPostfix(expr, &p, post); printf("%s\n", post); } // fim while return 0; }
void Eval::compileExpression ( const std::vector <std::pair <std::string, Lexer::Type>>& precompiled) { _compiled = precompiled; // Parse for syntax checking and operator replacement. if (_debug) context.debug ("[1;37;42mFILTER[0m Infix " + dump (_compiled)); infixParse (_compiled); if (_debug) context.debug ("[1;37;42mFILTER[0m Infix parsed " + dump (_compiled)); // Convert infix --> postfix. infixToPostfix (_compiled); if (_debug) context.debug ("[1;37;42mFILTER[0m Postfix " + dump (_compiled)); }
int main() { char exp[20]; printf("enter the expression you want to check\n"); scanf("%s",exp); if(expression(exp)==0) //This condition will check whether equation is valid w.r.t. brackets or not { printf("Congratulation, Now hopefully result will be 100% correct\n"); } else printf("Sorry man, your entered equation is not valid,Now there are uncertain chances for result to be correct unless you didn't use brackets in your eqaution\n"); printf("PostFix form of your Infix Equation is:\n"); infixToPostfix(exp); printf("Thanks for using Program\nHit Any key to quiet\n"); getch(); printf("\n"); return 0; }
int main(){ double f; node*root; double a; char infix[max], postfix[max],temp; printf("Enter f(x) operator: "); scanf("%s",infix); printf("Enter x value: "); scanf("%lf",&f); infixToPostfix(infix,postfix); printf("in Reverse Polish notation: "); puts(postfix); printf("The result: %0.1lf\n",calculator(postfix,f)); root=createTree(postfix); printf("print Tree: "); printTree(root); printf("\n"); printf("The result is using Binary Expression: "); printf("%0.1lf\n",countTree(root,f)); }
int main(int argv, char *argc[]) { char *expression = " (1 * 2 + 5 )+2 + (1*5+5)+ 3"; char *p; //if (argv != 2) { // puts("Usage: a.exe {algorith expression(must valid)}"); // return 1; //} p = infixToPostfix(expression); printf("The infix expression is: %s\n", expression); puts("+++++++++++++++++++++++++++++++++++++++++++++++"); printf("The Postfix expression is: %s\n", p); if (strchr(expression, '=')) printf("%s %d\n", expression, processPostfix(p)); else printf("%s = %d\n", expression, processPostfix(p)); if (p != NULL) free(p); return 0; }
//read in valid infix from file and //convert to postfix int main() { std::ifstream in; in.open("data3-1.txt"); std::ofstream out; out.open("data.txt"); while(!in.eof()) { String line; line.resize(64); getLine(in,line); //The last time through, it is not EOF, so it stores, //a blank string if (!in.eof()) { //Don't need the last semicolon and whitespace String exp = line.substr(0,line.length()-3); out <<"Infix: " << exp << std::endl; out <<"Postfix: " << infixToPostfix(exp) << std::endl; out << "\n"; } } std::cout << "Writing output to data.txt.\n"; }
// Driver program to test above functions int main() { char exp[] = "a+b*(c^d-e)^(f+g*h)-i"; infixToPostfix(exp); return 0; }
int calculator(const string &s) { return evaluatePostfix(infixToPostfix(s)); }
double Calculator::evaluatePostfix(const char * postfix) { // TODO: uses the stack to evaluate an input string in postfix notation. // If a divide-by-zero error occurs, throw a custom exception and stop. postfix = infixToPostfix(postfix); Stack<double> stackOfDoubles; for (unsigned int i = 0; i < strlen(postfix); i++) // Unsigned int to avoid mismatch { char token = postfix[i]; if (isdigit(token)) { double operand = 0; double convert = 0; int countHowManyDecPlaces = 0; bool isDecimal = false; // FLAG while (i < strlen(postfix) && (isdigit(postfix[i]) || postfix[i] == '.')) { // if it's a decimal if (postfix[i] == '.') { i++; isDecimal = true; continue; } else { operand *= 10; convert = postfix[i] - '0'; operand = operand + convert; i++; if (isDecimal) { countHowManyDecPlaces++; } } } if (countHowManyDecPlaces != 0) // Handling of decimal inputs { // 1 decimal place if (countHowManyDecPlaces == 1) operand = operand / 10; // 2 decimal places if (countHowManyDecPlaces == 2) operand = operand / 100; // 3 decimal places if (countHowManyDecPlaces == 3) operand = operand / 1000; // 4 decimal places if (countHowManyDecPlaces == 4) operand = operand / 10000; // 5 decimal places if (countHowManyDecPlaces == 5) operand = operand / 100000; } i--; // Decrement i inorder to stay return back to track stackOfDoubles.push(operand); } else if (isOperator(token)) { double operand2 = stackOfDoubles.top(); stackOfDoubles.pop(); // Get two operands to prepare for calculator functions double operand1 = stackOfDoubles.top(); stackOfDoubles.pop(); if (operand2 == 0 && (postfix[i] == '/' || postfix[i] == '%')) { throw DivideByZero(); return 0; } if (token == '+') // Perform the operation: operand1 chr operand2 stackOfDoubles.push(operand1 + operand2); if (token == '-') // Perform the operation: operand1 chr operand2 stackOfDoubles.push(operand1 - operand2); if (token == '*') // Perform the operation: operand1 chr operand2 stackOfDoubles.push(operand1 * operand2); if (token == '/') // Perform the operation: operand1 chr operand2 stackOfDoubles.push(operand1 / operand2); } } double answer; answer = stackOfDoubles.top(); return answer; }
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; }