I have mapped out my program using numbers in place of letters for this program,
ID: 3723714 • Letter: I
Question
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:
B
C
TEMP1
A
TEMP1
TEMP2
D
E
TEMP3
TEMP2
TEMP3
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<value; 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();
}
}
Explanation / Answer
import java.io.IOException;
import java.util.Scanner;
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() {
if (topOfArray>=0){
return array[topOfArray--];//return array-1 when array is popped.Check before pop
}
else{
return (int) '';
}
}
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<value; i++) {
char start = stack.charAt(i);
if (start >= 'A' && start <= 'z') {
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;
else{
ArrayEvaluation e = new ArrayEvaluation();
System.out.println("Result: " + (char)e.find(userInput));//Convert int into character
}
}
}
public static String getString() throws IOException
{
Scanner input = new Scanner(System.in);
return input.nextLine();
}
}