#include <algorithm> #include <deque> #include <iostream> #include <string> typedef std::deque< std::string > tokens_t; void pre_to_in( tokens_t* eq ) { if ( !eq->empty() ) { const std::string token = eq->front(); eq->pop_front(); if ( ( token == "+" ) || ( token == "-" ) || ( token == "/" ) || ( token == "*" ) ) { std::cout << "("; pre_to_in( eq ); std::cout << token; pre_to_in( eq ); std::cout << ")"; } else { std::cout << token; } } } using namespace std; struct node { int data; node *next; }*p = NULL, *top = NULL, *save = NULL, *ptr; void push(int x) { p = new node; p->data = x; p->next = NULL; if (top == NULL) { top = p; } else { save = top; top = p; p->next = save; } } char pop() { if (top == NULL) { cout<<"underflow!!"; } else { ptr = top; top = top->next; return(ptr->data); delete ptr; } } int main() { std::cout << "write formula in prefix notation" << std::endl; tokens_t tokens; std::copy( std::istream_iterator< std::string >( std::cin ), std::istream_iterator< std::string >(), std::back_inserter( tokens ) ); pre_to_in( &tokens ); for (int i = 0; i < strlen(x); i++) { if (x[i] >= 48 && x[i] <= 57) push(x[i]-'0'); else if (x[i] >= 42 && x[i] <= 47) { a=pop(); b=pop(); switch(x[i]) { case '+': push(a+b); break; case '-': push(a-b); break; case '*': push(a*b); break; case '/': push(a/b); break; } } } cout<<"ans is "<<pop()<<endl; getch(); } }
Write a C++ program that reads string expressions from an input file "prefix.txt" containing prefix expressions...
write a program in c++ Read an infix expression from an input file and convert to postfix. Instead of displaying directly on the screen, first place in a queue, and then display the contents of the queue on the screen. Precondition: The expression will be read from a file (input.txt) that contains a single line. There will be no spaces between the operands and the operators. The following operators are allowed: ( ) + - * / The normal rules...
C++: Learning Outcomes Implement two stacks and use them to implement an infix to prefix expression convertor Stacks A stack is an abstract data type which uses a sequential container and limits access to that container to one end. You may enter or remove from the container, but only at one end. Using the Linked List data structure from your last homework assignment, implement a Stack of type string. The Stack should only have one data member: the Linked List....
EVALUATING GENERAL INFIX EXPRESSIONS INTRODUCTION The notation in which we usually write arithmetic expressions is called infix notation; in it, operators are written between their operands: X + Y. Such expressions can be ambiguous; do we add or multiply first in the expression 5 + 3 * 2? Parentheses and rules of precedence and association clarify such ambiguities: multiplication and division take precedence over addition and subtraction, and operators associate from left to right. This project implements and exercises a stack-based algorithm that evaluates...
Objective To acquire expertise in stack manipulation and management, subroutine linkage and return conventions, and recursive procedures. Description You are to create a MIPS programming assignment that returns postfix representation of the input and create an expression tree. Your MIPS program should make use of the expression tree to store the input and provide, by means of an adequate traversal, the value for the expression. Your solution should be structured according to the following steps: Step 1: Convert expression from...
The second project involves completing and extending the C++ program that evaluates statements of an expression language contained in the module 3 case study. The statements of that expression language consist of an arithmetic expression followed by a list of assignments. Assignments are separated from the expression and each other by commas. A semicolon terminates the expression. The arithmetic expressions are fully parenthesized infix expressions containing integer literals and variables. The valid arithmetic operators are +, –, *, /. Tokens...
Write a PYTHON program that reads a file (prompt user for the input file name) containing two columns of floating-point numbers (Use split). Print the average of each column. Use the following data forthe input file: 1 0.5 2 0.5 3 0.5 4 0.5 The output should be: The averages are 2.50 and 0.5. a) Your code with comments b) A screenshot of the execution Version 3.7.2
Write a java program for the following: Your program reads an infix expression represented by a string S from the standard input (the keyboard). Then your program converts the infix expression into a postfix expression P using the algorithm. Next, your program evaluates the postfix expression P to produce a single result R. At last, your program displays the original infix expression S, the corresponding postfix expression P and the final result R on the standard output ( the screen...
For this project you will implement a simple calculator. Your calculator is going to parse infix algebraic expressions, create the corresponding postfix expressions and then evaluate the postfix expressions. The operators it recognizes are: +, -, * and /. The operands are integers. Your program will either evaluate individual expressions or read from an input file that contains a sequence of infix expressions (one expression per line). When reading from an input file, the output will consist of two files:...
Total point: 15 Introduction: For this assignment you have to write a c program that will take an infix expression as input and display the postfix expression of the input. After converting the postfix expression, the program should evaluate the expression from the postfix and display the result. What should you submit? Write all the code in a single file and upload the .c file. Problem We as humans write math expression in infix notation, e.g. 5 + 2 (the...
Write a java program to convert and print an infix expression to postfix expression. You can use Java stack methods. (Must read input from System.in) Your main method should be as follow: public static void main(String args[]) { intopost p = new intopost (); String iexp, pexp; //infix postfix expression try{ Scanner inf = new Scanner (System.in); // Read input from KB/ File while(inf.hasNext()){ // read next infix expression iexp = inf.next(); // Assume method name to convert infix...