Question

I NEED SAMPLE PRINT OUT AS WELL AS CODE PLEASE!!!! Objectives: To gain experience with stacks....

I NEED SAMPLE PRINT OUT AS WELL AS CODE PLEASE!!!!

Objectives:

To gain experience with stacks.

Documentation:

Explain the purpose of the program as detail as possible - 8%.

Develop a solution for the problem and mention algorithms to be used -12%

List data structures to be used in solution. - 5%.

Give a description of how to use the program and expected input/output - 5%

Explain the purpose of each class you develop in the program. - 5%.

Programming:

For each method, give the pre and post conditions and invariant, if any - 10%

Program execution according to the requirements given 50%

Naming of program as required 5%

Description of Program

You are to write a program name calc.java that evaluates an infix expression entered by the user. The expression may contain the following tokens:
(1)   Integer constants (a series of decimal digits).
(2)   x (representing a value to be supplied later).
(4)   Binary operators (+, -, *, / and %).
(5)   Parentheses
        

Spaces between tokens are allowed but not required. The program will convert the expression to postfix (RPN) form and display the converted expression.
The following example illustrates the behavior of the program (user input is in bold and red):
Porgram output is in bold and green.

Enter infix expression: (7 + 1) * (8 – 2) / 4
Converted expression: 7 1 + 8 2 - * 4 /
The result is: 12


Now, solve the postfix expression and return a result. You must now use an operand stack to process the postfix expression. Display this result on the screen as well as the postfix expression as shown above.
If the infix expression contains an error of any kind, the program must display the message Error in expression (with an optional explanation) and then terminate. The following examples illustrate various types of errors:

Enter infix expression: 1 2 +
Error in expression!! No operator between operands. Also last token must be an operand.

Enter infix expression: 10.4
Error in expression!! Cannot accept floating point numbers.

Enter infix expression: 1 ( + 2)
Error in expression!! No operator between operand and left parentheses.

Enter infix expression: 5 – (x – 2))
Error in expression!! No matching left parentheses for a right parentheses.

Enter infix expression: 1 ** 2
Error in expression!! The * operator cannot be preceded by a * operator.

The output of your program must match the format illustrated in this example.

Here are some other additional requirements for this program:

(1)   You must use stack objects during the translation from infix to postfix.

(2)   Operators must have the correct precedence and associativity. Binary operators * , / and % takes precedence over binary + and -. And processing is always done from left to right.

What to turn in


Turn in the calc.java and clac.class OR calc.jar in the folder A#4 in iCollege no later than 11:00 p.m. on the due date. You must put the program name, your name, the course number (CSC 2720) and the date at the begining of the program along with all documentation.

Hints:


1.   Do the program in stages. First, get the infix to postfix conversion working for binary operators. Use the examples from the hand out as a starting point for the program, but keep in mind that this code doesn’t handle associativity properly.

3.   To detect errors in the infix expression, you will need to check for several situations:

      A binary operator is preceded by an operator or an operand is preceded by an
      operand.
            An illegal character is encountered (such as a period).
            The last token in the expression is not an operand.
            There is no left parentheses anywhere in the stack when a right parentheses is
             encountered.
            The stack contains a left parenthesis when the expression ends.

4. Use a string to store the postfix expression. Use a stack of characters
     during the translation from infix to postfix.

5. Use the isDigit() function to test whether a character is a digit.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The required code is as follows:

ItoP.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ItoP;

/**
*
* @author Akshay Bisht
*/
import java.util.*;
class ItoP
{
private int cnt=0;
private static Scanner sc;
public static void main(String []args)
{
System.out.println("Enter the string to convert of Postfix Ecpression :");
sc = new Scanner(System.in);
String in = sc.nextLine();
in = in.replaceAll("\\s","");
ItoP ip = new ItoP();
ip.convert(in);
}
public void convert(String in)
{
Stack<String> stck = new Stack<String>();
Queue<String> qu = new LinkedList<String>();
Queue<String> qu1 = new LinkedList<String>();
ItoP ip = new ItoP();
while(ip.cnt < in.length())
{
String tok= ip.getTok(ip,in);
if(ip.chckOper(tok))
{
qu.add(tok);
}
else if(ip.chckinOp(tok))
{
if(stck.isEmpty())
{
stck.push(tok);
}
else
{
while(!stck.isEmpty()&&(preceed((String)stck.peek())>=preceed(tok)))
{
String mystring = (String) stck.peek();
stck.pop();
qu.add(mystring);
}
stck.push(tok);
}
}
else if(tok.equals(")"))
{
while((String)stck.peek()!="(")
{
String mystr =(String)stck.peek();
stck.pop();
qu.add(mystr);
}
stck.pop();
}
else
{
stck.push(tok);
}
}
while(!stck.empty())
{
qu.add((String)stck.peek());
stck.pop();
}
while(!qu.isEmpty())
{
System.out.print(qu.peek()+" ");
qu1.add(qu.peek());
qu.remove();
}
boolean res = ip.chck(qu1);
if(res)
{
System.out.println("\nTrue");
}
else
{
System.out.println("\nFalse");
}
}
public boolean chck(Queue<String> qu1)
{
int cnt;
String temporary[] = new String[1000];
int checking[] = new int[1000];
int uu=0,qq,io;
int space = qu1.size();
cnt=space;
for(uu=0;uu<space;uu++)
{
temporary[uu]=qu1.element();
checking[uu]=1;
qu1.remove();
}
int pre;
int n=0 ,n1=0,res=0;
String op = null;
while(cnt>1)
{
for(uu=0;uu<space;uu++)
{
if(chckinOp(temporary[uu])&&checking[uu]==1&&temporary[uu].length()==1)
{
pre = preceed(temporary[uu]);
if(pre == 5)
{
for(qq=uu-1;qq>=0;qq--)
{
if(checking[qq]==1)
{
n1 = Integer.parseInt(temporary[qq]);
break;
}
}
for(io=qq-1;io>=0;io--)
{
if(checking[io]==1)
{
n = Integer.parseInt(temporary[io]);
break;
}
}
checking[qq]=0;
checking[io]=0;
checking[uu]=1;
cnt = cnt -2;
if(temporary[uu].equals(">"))
{
if(n>n1)
{
op = "true";
}
else
{
op = "false";
}
}
else if(temporary[uu].equals("<"))
{
if(n<n1)
{
op = "true";
}
else
{
op = "false";
}
}
temporary[uu]=op;
}
else
{
for(qq=uu-1;qq>=0;qq--)
{
if(checking[qq]==1)
{
n1 = Integer.parseInt(temporary[qq]);
break;
}
}
for(io=qq-1;io>=0;io--)
{
if(checking[io]==1)
{
n = Integer.parseInt(temporary[io]);
break;
}
}
if(pre == 10)
{
checking[qq]=0;
checking[io]=0;
checking[uu]=1;
cnt = cnt - 2 ;
res = n % n1;
}
else if(pre == 9)
{
checking[qq]=0;
checking[io]=0;
checking[uu]=1;
cnt = cnt -2;
if(temporary[uu].equals("/"))
res = n / n1;
if(temporary[uu].equals("*"))
res = n * n1;
}
else if(pre == 7)
{
checking[qq]=0;
checking[io]=0;
checking[uu]=1;
cnt = cnt -2;
if(temporary[uu].equals("+"))
res = n + n1;
if(temporary[uu].equals("-"))
res = n - n1;
}
temporary[uu]= Integer.toString(res);
}   
}
else if (chckinOp(temporary[uu])&&checking[uu]==1&&temporary[uu].length()==2)
{
pre = preceed(temporary[uu]);
if(pre == 5)
{
for(qq=uu-1;qq>=0;qq--)
{
if(checking[qq]==1)
{
n1 = Integer.parseInt(temporary[qq]);
break;
}
}
for(io=qq-1;io>=0;io--)
{
if(checking[io]==1)
{
n = Integer.parseInt(temporary[io]);
break;
}
}
checking[qq]=0;
checking[io]=0;
checking[uu]=1;
cnt = cnt -2;
if(temporary[uu].equals(">="))
{
if(n>=n1)
{
op = "true";
}
else
{
op = "false";
}
}
else if(temporary[uu].equals("<="))
{
if(n<=n1)
{
op = "true";
}
else
{
op = "false";
}
}
temporary[uu]=op;
}
else if(pre == 4)
{
for(qq=uu-1;qq>=0;qq--)
{
if(checking[qq]==1)
{
n1 = Integer.parseInt(temporary[qq]);
break;
}
}
for(io=qq-1;io>=0;io--)
{
if(checking[io]==1)
{
n = Integer.parseInt(temporary[io]);
break;
}
}
checking[qq]=0;
checking[io]=0;
checking[uu]=1;
cnt = cnt -2;
if(temporary[uu].equals("=="))
{
if(n==n1)
{
op = "true";
}
else
{
op = "false";
}
}
else if(temporary[uu].equals("!="))
{
if(n!=n1)
{
op = "true";
}
else
{
op = "false";
}
}
temporary[uu]=op;
}
else if(pre == 3)
{
String value1 = new String();
String value2 = new String();
for(qq=uu-1;qq>=0;qq--)
{
if(checking[qq]==1)
{
value2 = temporary[qq];
break;
}
}
for(io=qq-1;io>=0;io--)
{
if(checking[io]==1)
{
value1 = temporary[io];
break;
}
}
checking[qq]=0;
checking[io]=0;
checking[uu]=1;
cnt = cnt -2;
if(value1.equals(value2) && value1.equals("true"))
{
op = "true";
}
else
{
op = "false";
}
temporary[uu]=op;
}
else if(pre == 2)
{
String value1 = new String();
String value2 = new String();
for(qq=uu-1;qq>=0;qq--)
{
if(checking[qq]==1)
{
value2 = temporary[qq];
break;
}
}
for(io=qq-1;io>=0;io--)
{
if(checking[io]==1)
{
value1 = temporary[io];
break;
}
}
checking[qq]=0;
checking[io]=0;
checking[uu]=1;
cnt = cnt -2;
if(value1.equals("true") || value2.equals("true"))
{
op = "true";
}
else
{
op = "false";
}
temporary[uu]=op;
}
}
}
}
if(temporary[space-1]=="true")
return true;
else
return false;
}
public int preceed(String tok)
{
if(tok=="%")
{
return 10;
}
if(tok=="/")
{
return 9;
}
if(tok=="*")
{
return 9;
}
if(tok=="+")
{
return 7;
}
if(tok=="-")
{
return 7;
}
if(tok==">=" || tok=="<=" || tok=="<" || tok==">" )
{
return 5;
}
if(tok=="!="|| tok=="==" )
{
return 4;
}
if(tok=="&&")
{
return 3;
}
if(tok=="||")
{
return 2;
}
return 0;
}
public boolean chckOper(String tok)
{
int uu=0;
char tokend[] = new char[100];
tokend = tok.toCharArray();
for(uu=0;uu<tok.length();uu++)
{
if((tokend[uu]>=65 &&tokend[uu]<=90)||(tokend[uu]>=97&&tokend[uu]<=122)||tokend[uu]==':'||((tokend[uu]>=48)&&(tokend[uu]<=57)))
{
continue;
}
else
return false;
}
return true;
}
public boolean chckinOp(String tok)
{
if((tok.equals("+"))||(tok.equals("%"))||(tok.equals(">"))||
(tok.equals("<"))||(tok.equals("-"))||(tok.equals("*"))||
(tok.equals("/"))||(tok.equals("=="))||(tok.equals(">="))||
(tok.equals("<="))||(tok.equals("!="))||(tok.equals("&&"))||
(tok.equals("||")))
return true;
else
return false;
}
public String getTok(ItoP ip,String in)
{
int uu=ip.cnt;
int qq,io;
char str[] = new char[100];
str = in.toCharArray();
if((str[uu]>=65 &&str[uu]<=90)||(str[uu]>=97&&str[uu]<=122)||((str[uu]>=48)&&(str[uu]<=57)))
{
for(qq=uu;qq<in.length();qq++)
{
if((str[qq]>=65 &&str[qq]<=90)||(str[qq]>=97&&str[qq]<=122)||str[qq]==':'||((str[qq]>=48)&&(str[qq]<=57)))
{
continue;
}
else
{
break;
}
}
ip.cnt=qq;
char str1[] = new char[qq-uu];
for(io=uu;io<qq;io++)
{
str1[io-uu]=str[io];
}
String mystring = new String(str1);
return mystring;
}
else if(str[uu]=='(')
{
ip.cnt++;
return "(";
}
else if(str[uu]==')')
{
ip.cnt++;
return ")";
}
else if(str[uu]=='+')
{
ip.cnt++;
return "+";
}
else if(str[uu]=='-')
{
ip.cnt++;
return "-";
}
else if(str[uu]=='*')
{
ip.cnt = ip.cnt +1;
return "*";
}
else if(str[uu]=='%')
{
ip.cnt = ip.cnt +1;
return "%";
}
else if(str[uu]=='/')
{
ip.cnt = ip.cnt +1;
return "/";
}
else if(str[uu]=='=' && str[uu+1]=='=')
{
ip.cnt = ip.cnt +2;
return "==";
}
else if(str[uu]=='&' && str[uu+1]=='&')
{
ip.cnt = ip.cnt +2;
return "&&";
}
else if(str[uu]=='|' && str[uu+1]=='|')
{
ip.cnt = ip.cnt +2;
return "||";
}
else if(str[uu]=='>' && str[uu+1]=='=')
{
ip.cnt = ip.cnt +2;
return ">=";
}
else if(str[uu]=='!' && str[uu+1]=='=')
{
ip.cnt = ip.cnt +2;
return "!=";
}
else if(str[uu]=='<' && str[uu+1]=='=')
{
ip.cnt = ip.cnt +2;
return "<=";
}
else if(str[uu]=='>')
{
ip.cnt++;
return ">";
}
else if(str[uu]=='<')
{
ip.cnt++;
return "<";
}
return null;
}
}

Output:

Please rate the answer if it helped....Thankyou

Hope it helps.....

Add a comment
Know the answer?
Add Answer to:
I NEED SAMPLE PRINT OUT AS WELL AS CODE PLEASE!!!! Objectives: To gain experience with stacks....
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
  • You are to write a program name expressionTree.java that evaluates an infix expression entered by the...

    You are to write a program name expressionTree.java that evaluates an infix expression entered by the user. The expression may contain the following tokens: (1) Integer constants (a series of decimal digits). (2)   One alphabetic character - "x" (representing a value to be supplied later). (3)   Binary operators (+, -, *, / and % (modulo)). (4)   Parentheses          You will parse the input expression creating an expression tree with the tokens, then use the postOrder tree traversal algorithm to extract...

  • JAVA, please You must write a robust program meaning that your program should not crash with...

    JAVA, please You must write a robust program meaning that your program should not crash with any given data. Data validation must be done any time that user enters an input. Write a program that 1. Gets an infix expression form the user and evaluate the expression using stack ADT a. Finds the postfix equivalent of the given infix expression b. Evaluate the created postfix expression. c. Note: your program should not crash when you enter an invalid expression such...

  • Here is the code I have so far. I'm trying to figure out how to implement...

    Here is the code I have so far. I'm trying to figure out how to implement a boolean and use precedence. The 2nd expression should be 14 but it comes out as 28 so I'm definitely not understanding. #include <stack> #include <iostream> #include <string> using namespace std; // Function to find precedence of // operators. int precedence(char op) {    if (op == '+' || op == '-')        return 1;    if (op == '*' || op ==...

  • Stacks are used by compilers to help in the process of evaluating expressions and generating machine...

    Stacks are used by compilers to help in the process of evaluating expressions and generating machine language code.In this exercise, we investigate how compilers evaluate arithmetic expressions consisting only of constants, operators and parentheses. Humans generally write expressions like 3 + 4and 7 / 9in which the operator (+ or / here) is written between its operands—this is called infix notation. Computers “prefer” postfix notation in which the operator is written to the right of its two operands. The preceding...

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

  • In C programming Language Write a version of the infix-to-postfix conversion algorithm. Write a program that converts an...

    In C programming Language Write a version of the infix-to-postfix conversion algorithm. Write a program that converts an ordinary infix arithmetic expression (assume a valid expression is entered) with single-digit integers For Example: Infix expression (6 + 2) * 5 - 8 / 4 to a postfix expression is  62+5*84/- The program should read the expression into character array infix and use the stack functions implemented in this chapter to help create the postfix expression in character array postfix. The...

  • Hello, please correct the following C++ CODE it is not working on visual studio: (I WILL RATE, NO...

    Hello, please correct the following C++ CODE it is not working on visual studio: (I WILL RATE, NO INCOMPLETE OR WRONG SOLUTIONS PLEASE) // CPP program to evaluate a given // expression where tokens are // separated by space. #include using namespace std; // Function to find precedence of // operators. int precedence(char op){ if(op == '+'||op == '-') return 1; if(op == '*'||op == '/') return 2; return 0; } // Function to perform arithmetic operations. int applyOp(int a,...

  • Python Issue Postfix notation (also known as Reverse Polish Notation or RPN in short) is a...

    Python Issue Postfix notation (also known as Reverse Polish Notation or RPN in short) is a mathematical notation in which operators follow all of its operands. It is different from infix notation in which operators are placed between its operands. The algorithm to evaluate any postfix expression is based on stack and is pretty simple: Initialize empty stack For every token in the postfix expression (scanned from left to right): If the token is an operand (number), push it on...

  • C++: Learning Outcomes Implement two stacks and use them to implement an infix to prefix expression...

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

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