Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have mapped out my program using numbers in place of letters for this program, and it looks like it is successfully mapping out my

I have mapped out my program using numbers in place of letters for this program, and it looks like it is successfully mapping out my program. However, I am having a hard time transfering it over to letters and non-mathematic variables.

(Write a program that accepts a postfix expression containing single letter operands and the operators +, -, *, and / and prints a sequence of instructions to evaluate the expression and leaves the result in the register. Use variables of the form TEMPn as temporary variables. For example, using the postfix expression ABC*+DE-/ should print the following:

LD B

ML C

ST TEMP1

LD A

AD TEMP1

ST TEMP2

LD D

SB E

ST TEMP3

LD TEMP2

DV TEMP3

ST TEMP4)

class StackProblem {

private int[] array;

private int topOfArray;

private int maxArraySize;

public StackProblem(int m) { //constructor and parameters defined.

maxArraySize = m;

array = new int[maxArraySize];

topOfArray = -1;

}

public int popArray() {

return array[topOfArray--]; //return array-1 when array is popped.

}

public void pushArray(int i) {

array[++topOfArray] = i; //adds 1 element to array when push method occurs.

}

}

class ArrayEvaluation {

public int find(String stack) {

int value;

int result = 0;

value = stack.length();

StackProblem array = new StackProblem(value); //I need this expression to print out letters, and not a numerical value. How would i go about doing this?

for (int i=0; i

char start = stack.charAt(i);

if (start >= 'A' && start <= 'F') {

array.pushArray((int)(start -'0')); //push array from 0th position

}

else {

int a = array.popArray();

int b = array.popArray();

if (start == '+') {

result = a+b;

} else if (start == '-') {

result = b-a;

} else if (start == '*') {

result = a*b;

} else if (start == '/') {

result = b/a;

}

array.pushArray(result);

}

}

return array.popArray();

}

}

class DataStructuresProject1 {

public static void main (String args[]) throws IOException {

String userInput;

while (true) {

System.out.println("Please enter a valid postfix expression: ");

userInput = getString();

if (userInput.equals(""))

break;

ArrayEvaluation e = new ArrayEvaluation();

System.out.println("Result: " + e.find(userInput));

}

}

public static String getString() throws IOException

{

Scanner input = new Scanner(System.in);

return input.nextLine();

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part 3 Lnai 8726

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448440, 978-3662448441

More Books

Students also viewed these Databases questions