Question

In: Computer Science

Consider simple infix expressions that consist of single-digit operands; the operators +, -, *, and /;...

Consider simple infix expressions that consist of single-digit operands; the operators +, -, *, and /; and the parentheses. Assume that unary operators are illegal and that the expression contains no embedded spaces. Design and implement a class of infix calculators. The class should have the following members:

Private members:

  1. a string to store an infix expression for evaluation.

  2. a private member function that checks if an infix expression is well formed. (A well formed expression consists of single-digit operands; the operators +, -, *, and /; and the parentheses.)

  3. a private member function that checks for balanced brackets of an expression using the algorithm studied.

  4. a private member function that converts an infix expression to a postfix expression using the algorithm studied.

  5. a private member function that determines the precedence of an operator

  6. a private member function that evaluates a postfix expression using the algorithm studied.

Public members:

  1. a default constructor.

  2. a function that sets the data member by an infix expression. The function must first check if the infix expression is well formed and balanced with brackets by calling private member function 2 and 3 before passing it to the data member. The function returns true if the operation is successfully performed. Otherwise it returns false to indicate the parameter expression is not valid.

  3. a function that evaluates the expression stored in the data member. This function should not have any parameters. The function should first convert the infix expression to its postfix form and then evaluate the resulting postfix expression.

Other requirements

#The program to be written in C++. After submit the answer, can you double check and test run the code then show it for me? Please read the requirements carefully, and I want the answer have to be written in C++. Please!!

  • Use the link-based Stack with exceptions.

  • Write a main function to test your calculators. Your program should allow the user to evaluate additional expressions until the user wants to end the program.

  • You should not assume that the expression is well-formed. The user is asked to reenter an expression if the expression entered is not well-formed.

  • You should not assume that the parentheses in the expression are balanced. The user is asked to reenter an expression if the expression entered is not balanced. Use the algorithm discussed to solve this problem.

Solutions

Expert Solution

/*the following is the code.This code is only for single digit calculator .Plase refer any other sourse to get the more digit.

//c++ program to infix calculator
#include <iostream>
#include<stdbool.h>
#include<string.h>
using namespace std;

//Stack initailisation
typedef struct{
int top;
int arr[50];
}Stack;

Stack s;
//stack push and pop function
void push(char item)
{
if(s.top >= 49)
{
return;
}
/*if(s.arr[s.top]>=0 && s.arr[s.top]<=9)
{
s.arr[s.top]*=10;
s.arr[s.top]+=item;
}*/
else
{
s.top = s.top+1;
s.arr[s.top] = item;
}
}

int pop()
{
int item ;
if(s.top <0)
{
exit(1);
}
else
{
item = s.arr[s.top];
s.top = s.top-1;
return(item);
}
}

// declare of class
class calculator{
private:
char infix[50]; //expression string
bool valid(char exp[]) //cheak expression well formed
{
for(int i=0;i<strlen(exp);i++)
{
char ch=exp[i];
if((is_operator(ch))!=1 && ch!='(' && ch!=')' && isdigit(ch)!=1)
return false;
}
return true;
}
bool valid_bracket(char exp[]) //cheak weather brackets are balanced
{
int flag=0;
for(int i=0;i<strlen(exp);i++)
{
if(exp[i]=='(')
flag++;
if(exp[i]==')')
flag--;
}
if(flag==0)
return true;
else
return false;
}
int precedence(char symbol) //precedence function
{
if(symbol == '*' || symbol == '/')
{
return(2);
}
else if(symbol == '+' || symbol == '-')
{
return(1);
}
else
{
return(0);
}
}
int is_operator(char symbol)
{
if(symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol =='-')
{
return 1;
}
else
{
return 0;
}
}
void infixTopostfix(char *postfix) //function to convert infix to postfix
{
int i, j;
char item;
char x;

push('(');
strcat(infix,")");
i=0;
j=0;
item=infix[i];
while(item != '\0')
{
if(item == '(')
{
push(item);
}
else if( isdigit(item) || isalpha(item))
{
*(postfix+j) = item;
j++;
}
else if(is_operator(item) == 1)
{
x=pop();
while((is_operator(x) == 1) && (precedence(x)>= precedence(item)))
{
*(postfix+j) = x;
j++;
x =pop();
}
push(x);
push(item);
}
else if(item == ')')
{
x =pop();
while(x != '(')
{
*(postfix+j) = x;
j++;
x =pop();
}
}
else
{
exit(1);
}
i++;
item = infix[i];
}
if(s.top>0)
{
exit(1);
}
*(postfix+j)= '\0';
}
int evaluate() //evaluatig postfix expression
{
char postfix[50];
infixTopostfix(postfix);
s.top=-1;
for(int i=0;i<strlen(postfix);i++)
{
char ch=postfix[i];
if(ch>=48 && ch<=57)
{
int x=ch-'0';
push(x);
}
if(ch=='*' || ch=='/' || ch=='+' || ch=='-')
{
int a,b,value;
a=pop();
b=pop();
switch (ch) {
case '*':
value=b*a;
break;
case '/':
value=b/a;
break;
case '+':
value=b+a;
break;
case '-':
value=b-a;
break;
}
push(value);
}
}
return pop();
}
public:
calculator() //default constructor
{
return;
}
int calc(char exp[]) //function that set datamember and evaluates
{
char postfix[50];
if(valid(exp)==false){
printf("The entered expression is not well formed.\n");
return 0;
}
if(valid_bracket(exp)==false)
{
printf("The enterned expression's brackets are not balanced.\n");
return 0;
}
strcpy(infix,exp);
int result=evaluate();
return result;
}
};

int main()
{
int choice;
s.top=-1;
char exp[50];
calculator cal;
a:printf("Choices\n1:Enter '1' to Evaluate a expression\n2:Enter any other number Quit\n" );
printf("Enter your choice:");
scanf("%d",&choice);
if(choice==1)
{
int result;
printf("Enter the expression\n");
scanf("%s",exp);
result=cal.calc(exp);
if(result==0)
{
goto a;
}
printf("*************%s=%d*****************\n\n\n\n",exp,result);
goto a;
}
else
{
return 0;
}
}


Related Solutions

Consider simple infix expressions that consist of single-digit operands; the operators +, -, *, and /;...
Consider simple infix expressions that consist of single-digit operands; the operators +, -, *, and /; and the parentheses. Assume that unary operators are illegal and that the expression contains no embedded spaces. Design and implement a class of infix calculators. The class should have the following members: Private members: a string to store an infix expression for evaluation. a private member function that checks if an infix expression is well formed. (A well formed expression consists of single-digit operands;...
C++ OOP Make a program to evaluate infix arithmetic expressions containing integer operands and the operators...
C++ OOP Make a program to evaluate infix arithmetic expressions containing integer operands and the operators + (addition), - (subtraction), * (multiplication), / (division) and pairs of parentheses, properly nested. Use the following two-stack algorithm (by E. W. Dijkstra): If the next token in the expression is an integer, push the integer onto the value stack. If the next token in the expression is an operator, If the operator stack is empty or the priority of the operator is greater...
C++ Data Structure Write a program to change following infix expressions to postfix expressions using a...
C++ Data Structure Write a program to change following infix expressions to postfix expressions using a stack a) D-B+C b) C*D+A*B c) (A*B)*C+D*F-C d) (A-4*(B-C)-D/E)*F
1) Consider the following infix expressions. What is the equivalent postfix (reverse Polish notation) expression? 16/(5+3)b)...
1) Consider the following infix expressions. What is the equivalent postfix (reverse Polish notation) expression? 16/(5+3)b) A*B+C*Dc) X × Y + W × Z + V × U 2) Consider the postfix (reverse Polish notation) 10 5 + 6 3 - /. What is the equivalent infix expression?
Consider four-digit numbers that consist of 0, 1, 2, 5, 6, and 9. a) How many...
Consider four-digit numbers that consist of 0, 1, 2, 5, 6, and 9. a) How many four-digit numbers can be formed from the digits 0, 1, 2, 5, 6, and 9 if each digit can be used only once? (the four-digit numbers can't start with 0). b) How many of those four-digit numbers are even? c) How many are greater than 2200?
Project [2]: Expressions and Operators Project Goals The goal of this project is to: 1. Get...
Project [2]: Expressions and Operators Project Goals The goal of this project is to: 1. Get students familiar with expressions and operators. Important Notes: 1. Formatting: Make sure that you follow the precise recommendations for the output content and formatting. For example, do not change the text from “You’re walking through the woods and come upon a chest. Do you open it? 1 – yes 2 - no” to “Do you open the chest? ”. Your assignment will be auto-graded...
We normally write arithmetical expressions using infix notation, meaning that the operator appears between its two...
We normally write arithmetical expressions using infix notation, meaning that the operator appears between its two operands, as in "4 + 5". In postfix notation, the operator appears after its operands, as in "4 5 +". Here is a slightly more complex postfix expression: "25 12 7 - 2 * /". The equivalent infix expression is: "25 / ((12 - 7) * 2)". The result of that expression should be 2.5 (beware integer division). Postfix expressions don't require parentheses. Write...
In a certain country license plates consist of zero or one digit followed by four or...
In a certain country license plates consist of zero or one digit followed by four or five uppercase letters from the Roman alphabet. (a) How many different license plates can the country produce? (b) How many license plates have no repeated letter? (c) How many license plates have at least one repeated letter? (d) What is the probability that a license plate has a repeated letter? (Round your answer to the nearest whole percent.)   %
Write the RE for identifiers that can consist of any sequence of letters (l) or digit...
Write the RE for identifiers that can consist of any sequence of letters (l) or digit (d) or "_" but the first char must be a letter and the last char cannot be a "_" (10)
Write the RE for identifiers that can consist of any sequence of letters (l) or digit...
Write the RE for identifiers that can consist of any sequence of letters (l) or digit (d) or "_" but the first char must be a letter and the last char cannot be a "_"
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT