Question

Create a C program Evaluate an expression from console input and print its result. -The format...

Create a C program Evaluate an expression from console input and print its result.

-The format of expressions: Number Symbol Number Symbol ... Symbol Number = Number

-Number refers to an integer and Symbol refers to either + , -, *, or / - * and / have higher priority over + and

-. E.g., given input “1+2*3+4=”, the result should be “11”; Hint: Use Polish Notation

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include<stdio.h>
char expr[20];//array to store entered expression
char stack[20];//store the postfix expression
int precedence(char a,char b)
{//returns true if precedence of operator a is more or equal to than that of b
    if(((a=='+')||(a=='-'))&&((b=='*')||(b=='/')))
    return 0;
    else
    return 1;
}
int i;
int ctr;
int top=-1;//top of postfix stack
int topOper=-1;//top of operator stack
int operate(int a,int b,char oper)
{
    int res=0;
switch(oper)
{
    case '+':res=a+b;break;
    case '-':res=a-b;break;
    case '*':res=a*b;break;//return result of evaluation
    case '/':res=a/b;break;
}
return res;
}
void postfixConvert()
{
char topsymb,operatorStack[20];
ctr=0;
while(expr[ctr]!='\0')
{//read till the end of input
if(expr[ctr]>='0'&&expr[ctr]<='9')
stack[++top]=expr[ctr];//add numbers straightaway
else
{
while(topOper>=0&&precedence(operatorStack[topOper],expr[ctr]))
{//check for the operators of higher precedence and then add them to stack
topsymb=operatorStack[topOper--];
stack[++top]=topsymb;
}
operatorStack[++topOper]=expr[ctr];
}
ctr++;
}
while(topOper>=0)//add remaining operators to stack
stack[++top]=operatorStack[topOper--];
printf("The Resulting Postfix expression for the given infix expression\n%s\n",stack);
}
int main()
{
printf("\t\tExpression Evaluator\n");
printf("This Program Evaluates Basic Expressions(without brackets) with arithmetic operations(+,-,*,/) on single digit operand length below 20\n");
printf("Enter the Expression\n");
scanf("%s",expr);
postfixConvert();//function to convert in postfix form
char oper;
int operand1,operand2;
ctr=0;
int result[2];//stack to keep storing results
int rTop=-1;//top of result stack
while(stack[ctr]!='\0')
{
oper=stack[ctr];
if(oper>='0'&&oper<='9')
result[++rTop]=(int)(oper-'0');//add numbers
else
{//if an operator is encountered than pop twice and push the result of operation to the stack
    operand1=result[rTop--];
    operand2=result[rTop--];
    result[++rTop]=operate(operand2,operand1,oper);
}
ctr++;
}
printf("The result of the expression is\n%d\n",result[0]);
getch();
}
Add a comment
Know the answer?
Add Answer to:
Create a C program Evaluate an expression from console input and print its result. -The format...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • . Use what you learned from our First Python Program to write a Python program that...

    . Use what you learned from our First Python Program to write a Python program that will carry out the following tasks. Ask user provide the following information: unit price (for example: 2.5) quantity (for example: 4) Use the following formula to calculate the revenue: revenue = unit price x quantity Print the result in the following format on the console: The revenue is $___. (for example: The revenue is $10.0.) . . Use the following formula to calculate the...

  • implement a class of infix calculators (Using C++). Consider a simple infix expression that consist of...

    implement a class of infix calculators (Using C++). Consider a simple infix expression that consist of single digit operands; the operators +, -, *, and / ; and parentheses. Assume that unary operators are illegal and that the expression contains no embedded spaces. Design and implement a class of infix calculators. Use the algorithms given in the chapter 6 to evaluate infix expressions as entered into the calculator. You must first convert the infix expression to postfix form and then...

  • Objective To acquire expertise in stack manipulation and management, subroutine linkage and retur...

    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...

  • please write c programming for this code below. Write a program to print a pattern of...

    please write c programming for this code below. Write a program to print a pattern of numbers separated by spaces for the given number of rows. At the time of execution, the program should print the message on the console as: Enter number of rows For example, if the user gives the input as: Enter number of rows : 4 then the program should print the result as: 232 34543 4567654

  • Implement a Java program using simple console input & output and logical control structures such that...

    Implement a Java program using simple console input & output and logical control structures such that the program prompts the user to enter 5 integer scores between 0 to 100 as inputs and does the following tasks For each input score, the program determines whether the corresponding score maps to a pass or a fail. If the input score is greater than or equal to 60, then it should print “Pass”, otherwise, it should print “Fail”. After the 5 integer...

  • Total point: 15 Introduction: For this assignment you have to write a c program that will...

    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...

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

  • write a C program that evaluate algebraic expressions over real numbers in Reverse Polish Notation (RPN)

    Your program must evaluate algebraic expressions over real numbers in Reverse Polish Notation (RPN). + (addition), - (subtraction), * (multiplication) and / (division) should be treated as arithmetic operations. Depending on the selection, numerical values must be entered and displayed in decimal, hexadecimal or binary form. Example: The expression ((3+4) *(7+1.6-12) -5) / (3-8.7)Will be calculated on your RPN calculator as 3 4 + 7 1.6 + 12 - * 5 - 3 8.7 - / Your RPN computer's user...

  • We as humans write math expression in infix notation, e.g. 5 + 2 (the operators are...

    We as humans write math expression in infix notation, e.g. 5 + 2 (the operators are written in-between the operands). In a computer’s language, however, it is preferred to have the operators on the right side of the operands, i.e. 5 2 +. For more complex expressions that include parenthesis and multiple operators, a compiler has to convert the expression into postfix first and then evaluate the resulting postfix. Write a program that takes an “infix” expression as input, uses...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT