数组溢出检查

huangapple go评论63阅读模式
英文:

Array Overflow Check

问题

以下是翻译好的内容:

public void overflowCheck(int temp)
{
    if(stackArraySize/stackArray.length > 100)
    {
        System.out.println("栈溢出");
    }
    else if(stackArraySize/stackArray.length < 100)
    {
       System.out.println("栈未满");
    }
    else
    {
       System.out.println("栈已满");
    }
}
Data stackTest = new Data(10, false); // 创建一个新的栈并确认它不是队列
int stackSize = stackTest.size;
stackTest.push(10); // 将 10 推入栈
stackTest.push(20); // 将 20 推入栈
stackTest.push(30); // 将 30 推入栈
stackTest.push(40); // 将 40 推入栈
stackTest.push(50); // 将 50 推入栈
stackTest.push(60); // 将 60 推入栈
stackTest.push(70); // 将 70 推入栈
stackTest.push(80); // 将 80 推入栈
stackTest.push(90); // 将 90 推入栈
stackTest.push(100); // 将 100 推入栈
stackTest.push(110); // 将 110 推入栈
stackTest.push(120); // 将 120 推入栈

stackTest.overflowCheck(stackSize);
int size; // 初始化大小
int stackArray[]; // 初始化数组
int top; // 初始化栈顶
int stackArraySize;
public Data(int size, boolean isArrayQueue) // 构造函数
{
    if(isArrayQueue)
    {
        len = 0;
        Queue = new int[size];
        front = -1;
        rear = -1;
    }
    else
    {
        this.size = size;
        this.stackArray = new int[size];
        this.top = -1;
    }
}
public boolean isFull() // 检查是否满
{
    return(size-1 == top);
}
public boolean isEmpty() // 检查是否为空
{
    return(top == -1);
}
public void push(int pushedElement) // 将元素推入栈,只要isFull为假
{
    if(!isFull()) // 检查栈是否已满
    {
        top++; // 增加栈顶
        stackArray[top] = pushedElement; // 将元素放入数组[top]
        System.out.println("推入的元素是:" + pushedElement);
    }
    else // 如果栈已满,告诉用户。
    {
        System.out.println("栈已满。");
    }
}
public int pop() // 从栈中弹出元素,只要isEmpty为假
{
    if(!isEmpty()) // 检查栈是否已空
    {
        int originalTop = top; // 将原始栈顶值存储到originalTop中
        top--;// 减少栈顶
        System.out.println("弹出的元素是:" + stackArray[originalTop]);
        return stackArray[originalTop]; // 再次返回originalTop
    }
    else // 如果栈为空,告诉用户。
    {
        System.out.println("栈为空。");
        return -1;
    }
}
public int top() // 查看栈并返回顶部的元素
{
    if(!this.isEmpty()) // 如果栈有值,返回顶部元素。
    {
        return stackArray[top];
    }
    else // 如果栈为空,告诉用户。
    {
        System.out.println("栈为空");
        return -1;
    }
}
英文:

I'm trying to check whether or not an array is overflowing.
This is the logic that I'm using:

/*
store the length of the stackArray[] into a temp. int.
compare the size of the temp int to the stackArray[] via 3 tests.
If the stackArray[]&#39;s size is greater than the int&#39;s size,
then say that the array is overflowing, and that it doesn&#39;t have any more space to enter elements into.
If the stackArray[]&#39;s size is less than the int&#39;s size,
then say that the stackArray[] is underflowing and that it still has space to enter some elements into it.
If the stackArray[]&#39;s size is equal to the int&#39;s size,
then say that the array is full.
*/

To implement this logic, here's the method I'm using:

public void overflowCheck(int temp)
{
if(stackArraySize/stackArray.length &gt; 100)
{
System.out.println(&quot;stack is overflowing&quot;);
}
else if(stackArraySize/stackArray.length &lt; 100)
{
System.out.println(&quot;stack is not full&quot;);
}
else
{
System.out.println(&quot;stack is full&quot;);
}
}

But when I run this on my array, I keep getting "stack is not full. Here's my array:

Data stackTest = new Data(10, false); //create a new stack &amp; confirm it&#39;s not a queue
int stackSize = stackTest.size;
stackTest.push(10); //push 10 into the stack
stackTest.push(20); //push 20 into the stack
stackTest.push(30); //push 30 into the stack
stackTest.push(40); //push 40 into the stack
stackTest.push(50); //push 50 into the stack
stackTest.push(60); //push 60 into the stack
stackTest.push(70); //push 70 into the stack
stackTest.push(80); //push 80 into the stack
stackTest.push(90); //push 90 into the stack
stackTest.push(100); //push 100 into the stack
stackTest.push(110); //push 110 into the stack
stackTest.push(120); //push 120 into the stack
stackTest.overflowCheck(stackSize);

I should be getting an output of "stack is overflowing", but I keep getting "stack is not full".

Is this because my logic is flawed or my execution is flawed?
How do I fix this?

full class

    int size; //initialize size
int stackArray[]; //initialize array
int top; //initialize top
int stackArraySize;
public Data(int size, boolean isArrayQueue) //constructor
{
if(isArrayQueue)
{
len = 0;
Queue = new int[size];
front = -1;
rear = -1;
}
else
{
this.size = size;
this.stackArray = new int[size];
this.top = -1;
}
}
public boolean isFull() //check if it&#39;s full
{
return(size-1 == top);
}
public boolean isEmpty() //check if it&#39;s empty
{
return(top == -1);
}
public void push(int pushedElement) //push an element into the stack, as long as isFull is false
{
if(!isFull()) //check if the stack is already full
{
top++; //increment top
stackArray[top] = pushedElement; //set array[top] to the pushedElement
System.out.println(&quot;The pushed element is: &quot; + pushedElement);
}
else //if the stack is full, tell the user.
{
System.out.println(&quot;The stack is full.&quot;);
}
}
public int pop() //pop an element from the stack, as long as isEmpty is false
{
if(!isEmpty()) //check is the stack is already empty.
{
int originalTop = top; //store original top value into originalTop
top--;//decrement top
System.out.println(&quot;The popped element is: &quot; + stackArray[originalTop]);
return stackArray[originalTop]; //return the originalTop again
}
else //if the stack is empty, tell the user.
{
System.out.println(&quot;The stack is empty.&quot;);
return -1;
}
}
public int top() //peek into the stack &amp; return the element on the top
{
if(!this.isEmpty()) //if the stack has values, return the top element.
{
return stackArray[top];
}
else //if the stack is empty, tell the user.
{
System.out.println(&quot;The stack is empty&quot;);
return -1;
}
}

答案1

得分: 1

你需要修改你的逻辑并在push和pop方法中更新stackArraySize的值你没有在任何地方更新这个值

public class Data {

    int size; // 初始化大小
    int stackArray[]; // 初始化数组
    int top; // 初始化栈顶
    int stackArraySize;
    int len;
    int[] Queue;
    int front;
    int rear;

    public Data(int size, boolean isArrayQueue) // 构造函数
    {
        if (isArrayQueue) {
            len = 0;
            Queue = new int[size];
            front = -1;
            rear = -1;
        } else {
            this.size = size;
            this.stackArray = new int[size];
            this.top = -1;
        }
    }

    public boolean isFull() // 检查是否已满
    {
        return (size - 1 == top);
    }

    public boolean isEmpty() // 检查是否为空
    {
        return (top == -1);
    }

    public void push(int pushedElement) // 将元素推入栈中,只要isFull为false
    {


        if (!isFull()) // 检查栈是否已满
        {
            top++; // 增加栈顶
            stackArray[top] = pushedElement; // 将array[top]设置为pushedElement
            stackArraySize = top; // 更新stackArraySize的值为top
            System.out.println("已推入的元素为:" + pushedElement);
        } else // 如果栈已满,告诉用户。
        {
            stackArraySize++;
            System.out.println("栈已满。");
        }
    }

    public int pop() // 从栈中弹出元素,只要isEmpty为false
    {
        if (!isEmpty()) // 检查栈是否已空。
        {
            int originalTop = top; // 将原始栈顶值存储在originalTop中
            top--;// 减少栈顶
            System.out.println("已弹出的元素为:" + stackArray[originalTop]);
            stackArraySize = top;
            return stackArray[originalTop]; // 再次返回originalTop
        } else // 如果栈为空,告诉用户。
        {
            System.out.println("栈已空。");
            return -1;
        }
    }

    public int top() // 查看栈顶并返回顶部元素
    {
        if (!this.isEmpty()) // 如果栈中有值,返回顶部元素。
        {
            return stackArray[top];
        } else // 如果栈为空,告诉用户。
        {
            System.out.println("栈已空");
            return -1;
        }
    }

    public void overflowCheck(int temp) {
        if (stackArraySize >= temp) {
            System.out.println("栈溢出");
        } else if (stackArraySize < temp) {
            System.out.println("栈未满");
        } else {
            System.out.println("栈已满");
        }
    }

    public static void main(String args[]) {
        Data stackTest = new Data(10, false); // 创建一个新的栈并确认它不是队列
        int stackSize = stackTest.size;
        stackTest.push(10); // 将10推入栈中
        stackTest.push(20); // 将20推入栈中
        stackTest.push(30); // 将30推入栈中
        stackTest.push(40); // 将40推入栈中
        stackTest.push(50); // 将50推入栈中
        stackTest.push(60); // 将60推入栈中
        stackTest.push(70); // 将70推入栈中
        stackTest.push(80); // 将80推入栈中
        stackTest.push(90); // 将90推入栈中
        stackTest.push(100); // 将100推入栈中
        stackTest.push(110); // 将110推入栈中
        stackTest.push(120); // 将120推入栈中

        stackTest.overflowCheck(stackSize);

        stackTest.pop();
        stackTest.pop();
        stackTest.pop();
        stackTest.pop();
        stackTest.pop();
        stackTest.pop();
        stackTest.pop();
        stackTest.pop();
        stackTest.pop();
        stackTest.pop();
        stackTest.pop();
        stackTest.overflowCheck(stackSize);
    }

}
英文:

you have to modified your logic and update the value of stackArraySize in push and pop method you are not updating this value anywhere

public class Data {
int size; // initialize size
int stackArray[]; // initialize array
int top; // initialize top
int stackArraySize;
int len;
int[] Queue;
int front;
int rear;
public Data(int size, boolean isArrayQueue) // constructor
{
if (isArrayQueue) {
len = 0;
Queue = new int[size];
front = -1;
rear = -1;
} else {
this.size = size;
this.stackArray = new int[size];
this.top = -1;
}
}
public boolean isFull() // check if it&#39;s full
{
return (size - 1 == top);
}
public boolean isEmpty() // check if it&#39;s empty
{
return (top == -1);
}
public void push(int pushedElement) // push an element into the stack, as
// long as isFull is false
{
if (!isFull()) // check if the stack is already full
{
top++; // increment top
stackArray[top] = pushedElement; // set array[top] to the
stackArraySize=top;					// pushedElement
System.out.println(&quot;The pushed element is: &quot; + pushedElement);
} else // if the stack is full, tell the user.
{
stackArraySize++;
System.out.println(&quot;The stack is full.&quot;);
}
}
public int pop() // pop an element from the stack, as long as isEmpty is
// false
{
if (!isEmpty()) // check is the stack is already empty.
{
int originalTop = top; // store original top value into originalTop
top--;// decrement top
System.out.println(&quot;The popped element is: &quot; + stackArray[originalTop]);
stackArraySize=top;	
return stackArray[originalTop]; // return the originalTop again
} else // if the stack is empty, tell the user.
{
System.out.println(&quot;The stack is empty.&quot;);
return -1;
}
}
public int top() // peek into the stack &amp; return the element on the top
{
if (!this.isEmpty()) // if the stack has values, return the top element.
{
return stackArray[top];
} else // if the stack is empty, tell the user.
{
System.out.println(&quot;The stack is empty&quot;);
return -1;
}
}
public void overflowCheck(int temp) {
if (stackArraySize&gt;= temp) {
System.out.println(&quot;stack is overflowing&quot;);
} else if (stackArraySize&lt;temp) {
System.out.println(&quot;stack is not full&quot;);
} else {
System.out.println(&quot;stack is full&quot;);
}
}
public static void main(String args[]) {
Data stackTest = new Data(10, false); // create a new stack &amp; confirm
// it&#39;s not a queue
int stackSize = stackTest.size;
stackTest.push(10); // push 10 into the stack
stackTest.push(20); // push 20 into the stack
stackTest.push(30); // push 30 into the stack
stackTest.push(40); // push 40 into the stack
stackTest.push(50); // push 50 into the stack
stackTest.push(60); // push 60 into the stack
stackTest.push(70); // push 70 into the stack
stackTest.push(80); // push 80 into the stack
stackTest.push(90); // push 90 into the stack
stackTest.push(100); // push 100 into the stack
stackTest.push(110); // push 110 into the stack
stackTest.push(120); // push 120 into the stack
stackTest.overflowCheck(stackSize);
stackTest.pop(); 
stackTest.pop(); 
stackTest.pop(); 
stackTest.pop(); 
stackTest.pop(); 
stackTest.pop(); 
stackTest.pop(); 
stackTest.pop(); 
stackTest.pop();
stackTest.pop(); 
stackTest.pop(); 
stackTest.overflowCheck(stackSize);
}
}

答案2

得分: 1

使用比率进行检查需要将其设置为双精度,否则类型转换将保持为默认整数。

话虽如此,你的 push 方法目前不允许在定义长度之后继续推入元素,而这对于下面的 overflowCheck 的工作是必需的。

class StackCheck {
    Data stackTest = new Data(10, false); // 创建一个新堆栈并确认它不是队列
    public static void main() {
        
        stackTest.push(10); // 将 10 推入堆栈
        stackTest.push(20); // 将 20 推入堆栈
        stackTest.push(30); // 将 30 推入堆栈
        stackTest.push(40); // 将 40 推入堆栈
        stackTest.push(50); // 将 50 推入堆栈
        stackTest.push(60); // 将 60 推入堆栈
        stackTest.push(70); // 将 70 推入堆栈
        stackTest.push(80); // 将 80 推入堆栈
        stackTest.push(90); // 将 90 推入堆栈
        stackTest.push(100); // 将 100 推入堆栈
        stackTest.push(110); // 将 110 推入堆栈
        stackTest.push(120); // 将 120 推入堆栈
    
        stackTest.overflowCheck();
    }
    
    public void overflowCheck() {   
        double ratio = stackArray.length/stackTest.size;
        if(ratio > 1){
            System.out.println("堆栈溢出");
        }
        else if(ratio == 1){
           System.out.println("堆栈已满");
        }
        else{
           System.out.println("堆栈未满");
        }
    }
}
英文:

Using a ratio to check needs it to be a double, otherwise typecast would keep it as default integer.

Having said that, your push method does not currently allow for elements to be pushed after length defined, which you would need for the below overflowCheck to work.

class StackCheck {
Data stackTest = new Data(10, false); //create a new stack &amp; confirm it&#39;s not a queue
public static void main() {
stackTest.push(10); //push 10 into the stack
stackTest.push(20); //push 20 into the stack
stackTest.push(30); //push 30 into the stack
stackTest.push(40); //push 40 into the stack
stackTest.push(50); //push 50 into the stack
stackTest.push(60); //push 60 into the stack
stackTest.push(70); //push 70 into the stack
stackTest.push(80); //push 80 into the stack
stackTest.push(90); //push 90 into the stack
stackTest.push(100); //push 100 into the stack
stackTest.push(110); //push 110 into the stack
stackTest.push(120); //push 120 into the stack
stackTest.overflowCheck();
}
public void overflowCheck() {   
double ratio = stackArray.length/stackTest.size;
if(ratio &gt; 1){
System.out.println(&quot;stack is overflowing&quot;);
}
else if(ratio == 1){
System.out.println(&quot;stack is full&quot;);
}
else{
System.out.println(&quot;stack is not full&quot;);
}
}
}

huangapple
  • 本文由 发表于 2020年7月25日 14:17:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63085041.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定