I am taking a intro to java course and my assignment is to create a simplified f
ID: 3859590 • Letter: I
Question
I am taking a intro to java course and my assignment is to create a simplified forth interpreter. I need to create a stack in java which will serve as part one of my overall class project to create a reverse polish notation calculator. Again for this part I only need to create the stack array and test it. This is what I have so far:
package rpnCalculator;
public class ArrayStack {
int maxSize;
int top;
double[]scaleRating = new double[10];
public ArrayStack(int n)
{
maxSize = n;
scaleRating = new double[maxSize];
top = 0;
}
public boolean empty()
{
if(top == 0)
{
return true;
}else {
return false;
}
}
public void push(double dbl)
{
if(top < maxSize)
{
scaleRating[top] = dbl;
top++;
}
else {
System.out.println("Stack overflow!");
}
}
public double pop()
{
if(this.empty())
{
double temp = this.peek();
scaleRating[top-1] = 0;
top--;
return temp;
}
else {
return 0;
}
}
public double peek()
{
if(top > 0)
{
return scaleRating[top - 1];
}else {
return 0;
}
}
}
I do not think I have done this correctly because the instructions say to use a private double but I seem to be writing the code for an array of Strings. Is there any way I can get some assistance in what the array should look like in the stack for a double element, rather than String?
Thank you
Explanation / Answer
Answer:
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class stack
{
int input[5];
int peek;
public:
stack()
{
peek=-1;
}
void push(int x)
{
if(peek > 4)
{
cout <<"stack over flow";
return;
}
input[++peek]=x;
cout <<"inserted" <<x;
}
void pop()
{
if(peek <0)
{
cout <<"stack under flow";
return;
}
cout <<"deleted" <<input[peek--];
}
void display()
{
if(peek<0)
{
cout <<" stack empty";
return;
}
for(int i=peek;i>=0;i--)
cout <<input[i] <<" ";
}
};
main()
{
int option;
stack st;
while(1)
{
cout <<" 1.push 2.pop 3.display 4.exit Enter ur optionoice";
cin >> option;
swipeektion(option)
{
case 1: cout <<"enter the element";
cin >> option;
st.push(option);
break;
case 2: st.pop(); break;
case 3: st.display();break;
case 4: exit(0);
}
}
return (0);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.