数组溢出检查

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

Array Overflow Check

问题

以下是翻译好的内容:

  1. public void overflowCheck(int temp)
  2. {
  3. if(stackArraySize/stackArray.length > 100)
  4. {
  5. System.out.println("栈溢出");
  6. }
  7. else if(stackArraySize/stackArray.length < 100)
  8. {
  9. System.out.println("栈未满");
  10. }
  11. else
  12. {
  13. System.out.println("栈已满");
  14. }
  15. }
  1. Data stackTest = new Data(10, false); // 创建一个新的栈并确认它不是队列
  2. int stackSize = stackTest.size;
  3. stackTest.push(10); // 将 10 推入栈
  4. stackTest.push(20); // 将 20 推入栈
  5. stackTest.push(30); // 将 30 推入栈
  6. stackTest.push(40); // 将 40 推入栈
  7. stackTest.push(50); // 将 50 推入栈
  8. stackTest.push(60); // 将 60 推入栈
  9. stackTest.push(70); // 将 70 推入栈
  10. stackTest.push(80); // 将 80 推入栈
  11. stackTest.push(90); // 将 90 推入栈
  12. stackTest.push(100); // 将 100 推入栈
  13. stackTest.push(110); // 将 110 推入栈
  14. stackTest.push(120); // 将 120 推入栈
  15. stackTest.overflowCheck(stackSize);
  1. int size; // 初始化大小
  2. int stackArray[]; // 初始化数组
  3. int top; // 初始化栈顶
  4. int stackArraySize;
  5. public Data(int size, boolean isArrayQueue) // 构造函数
  6. {
  7. if(isArrayQueue)
  8. {
  9. len = 0;
  10. Queue = new int[size];
  11. front = -1;
  12. rear = -1;
  13. }
  14. else
  15. {
  16. this.size = size;
  17. this.stackArray = new int[size];
  18. this.top = -1;
  19. }
  20. }
  21. public boolean isFull() // 检查是否满
  22. {
  23. return(size-1 == top);
  24. }
  25. public boolean isEmpty() // 检查是否为空
  26. {
  27. return(top == -1);
  28. }
  29. public void push(int pushedElement) // 将元素推入栈,只要isFull为假
  30. {
  31. if(!isFull()) // 检查栈是否已满
  32. {
  33. top++; // 增加栈顶
  34. stackArray[top] = pushedElement; // 将元素放入数组[top]
  35. System.out.println("推入的元素是:" + pushedElement);
  36. }
  37. else // 如果栈已满,告诉用户。
  38. {
  39. System.out.println("栈已满。");
  40. }
  41. }
  42. public int pop() // 从栈中弹出元素,只要isEmpty为假
  43. {
  44. if(!isEmpty()) // 检查栈是否已空
  45. {
  46. int originalTop = top; // 将原始栈顶值存储到originalTop中
  47. top--;// 减少栈顶
  48. System.out.println("弹出的元素是:" + stackArray[originalTop]);
  49. return stackArray[originalTop]; // 再次返回originalTop
  50. }
  51. else // 如果栈为空,告诉用户。
  52. {
  53. System.out.println("栈为空。");
  54. return -1;
  55. }
  56. }
  57. public int top() // 查看栈并返回顶部的元素
  58. {
  59. if(!this.isEmpty()) // 如果栈有值,返回顶部元素。
  60. {
  61. return stackArray[top];
  62. }
  63. else // 如果栈为空,告诉用户。
  64. {
  65. System.out.println("栈为空");
  66. return -1;
  67. }
  68. }
英文:

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

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

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

  1. public void overflowCheck(int temp)
  2. {
  3. if(stackArraySize/stackArray.length &gt; 100)
  4. {
  5. System.out.println(&quot;stack is overflowing&quot;);
  6. }
  7. else if(stackArraySize/stackArray.length &lt; 100)
  8. {
  9. System.out.println(&quot;stack is not full&quot;);
  10. }
  11. else
  12. {
  13. System.out.println(&quot;stack is full&quot;);
  14. }
  15. }

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

  1. Data stackTest = new Data(10, false); //create a new stack &amp; confirm it&#39;s not a queue
  2. int stackSize = stackTest.size;
  3. stackTest.push(10); //push 10 into the stack
  4. stackTest.push(20); //push 20 into the stack
  5. stackTest.push(30); //push 30 into the stack
  6. stackTest.push(40); //push 40 into the stack
  7. stackTest.push(50); //push 50 into the stack
  8. stackTest.push(60); //push 60 into the stack
  9. stackTest.push(70); //push 70 into the stack
  10. stackTest.push(80); //push 80 into the stack
  11. stackTest.push(90); //push 90 into the stack
  12. stackTest.push(100); //push 100 into the stack
  13. stackTest.push(110); //push 110 into the stack
  14. stackTest.push(120); //push 120 into the stack
  15. 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

  1. int size; //initialize size
  2. int stackArray[]; //initialize array
  3. int top; //initialize top
  4. int stackArraySize;
  5. public Data(int size, boolean isArrayQueue) //constructor
  6. {
  7. if(isArrayQueue)
  8. {
  9. len = 0;
  10. Queue = new int[size];
  11. front = -1;
  12. rear = -1;
  13. }
  14. else
  15. {
  16. this.size = size;
  17. this.stackArray = new int[size];
  18. this.top = -1;
  19. }
  20. }
  21. public boolean isFull() //check if it&#39;s full
  22. {
  23. return(size-1 == top);
  24. }
  25. public boolean isEmpty() //check if it&#39;s empty
  26. {
  27. return(top == -1);
  28. }
  29. public void push(int pushedElement) //push an element into the stack, as long as isFull is false
  30. {
  31. if(!isFull()) //check if the stack is already full
  32. {
  33. top++; //increment top
  34. stackArray[top] = pushedElement; //set array[top] to the pushedElement
  35. System.out.println(&quot;The pushed element is: &quot; + pushedElement);
  36. }
  37. else //if the stack is full, tell the user.
  38. {
  39. System.out.println(&quot;The stack is full.&quot;);
  40. }
  41. }
  42. public int pop() //pop an element from the stack, as long as isEmpty is false
  43. {
  44. if(!isEmpty()) //check is the stack is already empty.
  45. {
  46. int originalTop = top; //store original top value into originalTop
  47. top--;//decrement top
  48. System.out.println(&quot;The popped element is: &quot; + stackArray[originalTop]);
  49. return stackArray[originalTop]; //return the originalTop again
  50. }
  51. else //if the stack is empty, tell the user.
  52. {
  53. System.out.println(&quot;The stack is empty.&quot;);
  54. return -1;
  55. }
  56. }
  57. public int top() //peek into the stack &amp; return the element on the top
  58. {
  59. if(!this.isEmpty()) //if the stack has values, return the top element.
  60. {
  61. return stackArray[top];
  62. }
  63. else //if the stack is empty, tell the user.
  64. {
  65. System.out.println(&quot;The stack is empty&quot;);
  66. return -1;
  67. }
  68. }

答案1

得分: 1

  1. 你需要修改你的逻辑并在pushpop方法中更新stackArraySize的值你没有在任何地方更新这个值
  2. public class Data {
  3. int size; // 初始化大小
  4. int stackArray[]; // 初始化数组
  5. int top; // 初始化栈顶
  6. int stackArraySize;
  7. int len;
  8. int[] Queue;
  9. int front;
  10. int rear;
  11. public Data(int size, boolean isArrayQueue) // 构造函数
  12. {
  13. if (isArrayQueue) {
  14. len = 0;
  15. Queue = new int[size];
  16. front = -1;
  17. rear = -1;
  18. } else {
  19. this.size = size;
  20. this.stackArray = new int[size];
  21. this.top = -1;
  22. }
  23. }
  24. public boolean isFull() // 检查是否已满
  25. {
  26. return (size - 1 == top);
  27. }
  28. public boolean isEmpty() // 检查是否为空
  29. {
  30. return (top == -1);
  31. }
  32. public void push(int pushedElement) // 将元素推入栈中,只要isFull为false
  33. {
  34. if (!isFull()) // 检查栈是否已满
  35. {
  36. top++; // 增加栈顶
  37. stackArray[top] = pushedElement; // 将array[top]设置为pushedElement
  38. stackArraySize = top; // 更新stackArraySize的值为top
  39. System.out.println("已推入的元素为:" + pushedElement);
  40. } else // 如果栈已满,告诉用户。
  41. {
  42. stackArraySize++;
  43. System.out.println("栈已满。");
  44. }
  45. }
  46. public int pop() // 从栈中弹出元素,只要isEmpty为false
  47. {
  48. if (!isEmpty()) // 检查栈是否已空。
  49. {
  50. int originalTop = top; // 将原始栈顶值存储在originalTop中
  51. top--;// 减少栈顶
  52. System.out.println("已弹出的元素为:" + stackArray[originalTop]);
  53. stackArraySize = top;
  54. return stackArray[originalTop]; // 再次返回originalTop
  55. } else // 如果栈为空,告诉用户。
  56. {
  57. System.out.println("栈已空。");
  58. return -1;
  59. }
  60. }
  61. public int top() // 查看栈顶并返回顶部元素
  62. {
  63. if (!this.isEmpty()) // 如果栈中有值,返回顶部元素。
  64. {
  65. return stackArray[top];
  66. } else // 如果栈为空,告诉用户。
  67. {
  68. System.out.println("栈已空");
  69. return -1;
  70. }
  71. }
  72. public void overflowCheck(int temp) {
  73. if (stackArraySize >= temp) {
  74. System.out.println("栈溢出");
  75. } else if (stackArraySize < temp) {
  76. System.out.println("栈未满");
  77. } else {
  78. System.out.println("栈已满");
  79. }
  80. }
  81. public static void main(String args[]) {
  82. Data stackTest = new Data(10, false); // 创建一个新的栈并确认它不是队列
  83. int stackSize = stackTest.size;
  84. stackTest.push(10); // 将10推入栈中
  85. stackTest.push(20); // 将20推入栈中
  86. stackTest.push(30); // 将30推入栈中
  87. stackTest.push(40); // 将40推入栈中
  88. stackTest.push(50); // 将50推入栈中
  89. stackTest.push(60); // 将60推入栈中
  90. stackTest.push(70); // 将70推入栈中
  91. stackTest.push(80); // 将80推入栈中
  92. stackTest.push(90); // 将90推入栈中
  93. stackTest.push(100); // 将100推入栈中
  94. stackTest.push(110); // 将110推入栈中
  95. stackTest.push(120); // 将120推入栈中
  96. stackTest.overflowCheck(stackSize);
  97. stackTest.pop();
  98. stackTest.pop();
  99. stackTest.pop();
  100. stackTest.pop();
  101. stackTest.pop();
  102. stackTest.pop();
  103. stackTest.pop();
  104. stackTest.pop();
  105. stackTest.pop();
  106. stackTest.pop();
  107. stackTest.pop();
  108. stackTest.overflowCheck(stackSize);
  109. }
  110. }
英文:

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

  1. public class Data {
  2. int size; // initialize size
  3. int stackArray[]; // initialize array
  4. int top; // initialize top
  5. int stackArraySize;
  6. int len;
  7. int[] Queue;
  8. int front;
  9. int rear;
  10. public Data(int size, boolean isArrayQueue) // constructor
  11. {
  12. if (isArrayQueue) {
  13. len = 0;
  14. Queue = new int[size];
  15. front = -1;
  16. rear = -1;
  17. } else {
  18. this.size = size;
  19. this.stackArray = new int[size];
  20. this.top = -1;
  21. }
  22. }
  23. public boolean isFull() // check if it&#39;s full
  24. {
  25. return (size - 1 == top);
  26. }
  27. public boolean isEmpty() // check if it&#39;s empty
  28. {
  29. return (top == -1);
  30. }
  31. public void push(int pushedElement) // push an element into the stack, as
  32. // long as isFull is false
  33. {
  34. if (!isFull()) // check if the stack is already full
  35. {
  36. top++; // increment top
  37. stackArray[top] = pushedElement; // set array[top] to the
  38. stackArraySize=top; // pushedElement
  39. System.out.println(&quot;The pushed element is: &quot; + pushedElement);
  40. } else // if the stack is full, tell the user.
  41. {
  42. stackArraySize++;
  43. System.out.println(&quot;The stack is full.&quot;);
  44. }
  45. }
  46. public int pop() // pop an element from the stack, as long as isEmpty is
  47. // false
  48. {
  49. if (!isEmpty()) // check is the stack is already empty.
  50. {
  51. int originalTop = top; // store original top value into originalTop
  52. top--;// decrement top
  53. System.out.println(&quot;The popped element is: &quot; + stackArray[originalTop]);
  54. stackArraySize=top;
  55. return stackArray[originalTop]; // return the originalTop again
  56. } else // if the stack is empty, tell the user.
  57. {
  58. System.out.println(&quot;The stack is empty.&quot;);
  59. return -1;
  60. }
  61. }
  62. public int top() // peek into the stack &amp; return the element on the top
  63. {
  64. if (!this.isEmpty()) // if the stack has values, return the top element.
  65. {
  66. return stackArray[top];
  67. } else // if the stack is empty, tell the user.
  68. {
  69. System.out.println(&quot;The stack is empty&quot;);
  70. return -1;
  71. }
  72. }
  73. public void overflowCheck(int temp) {
  74. if (stackArraySize&gt;= temp) {
  75. System.out.println(&quot;stack is overflowing&quot;);
  76. } else if (stackArraySize&lt;temp) {
  77. System.out.println(&quot;stack is not full&quot;);
  78. } else {
  79. System.out.println(&quot;stack is full&quot;);
  80. }
  81. }
  82. public static void main(String args[]) {
  83. Data stackTest = new Data(10, false); // create a new stack &amp; confirm
  84. // it&#39;s not a queue
  85. int stackSize = stackTest.size;
  86. stackTest.push(10); // push 10 into the stack
  87. stackTest.push(20); // push 20 into the stack
  88. stackTest.push(30); // push 30 into the stack
  89. stackTest.push(40); // push 40 into the stack
  90. stackTest.push(50); // push 50 into the stack
  91. stackTest.push(60); // push 60 into the stack
  92. stackTest.push(70); // push 70 into the stack
  93. stackTest.push(80); // push 80 into the stack
  94. stackTest.push(90); // push 90 into the stack
  95. stackTest.push(100); // push 100 into the stack
  96. stackTest.push(110); // push 110 into the stack
  97. stackTest.push(120); // push 120 into the stack
  98. stackTest.overflowCheck(stackSize);
  99. stackTest.pop();
  100. stackTest.pop();
  101. stackTest.pop();
  102. stackTest.pop();
  103. stackTest.pop();
  104. stackTest.pop();
  105. stackTest.pop();
  106. stackTest.pop();
  107. stackTest.pop();
  108. stackTest.pop();
  109. stackTest.pop();
  110. stackTest.overflowCheck(stackSize);
  111. }
  112. }

答案2

得分: 1

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

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

  1. class StackCheck {
  2. Data stackTest = new Data(10, false); // 创建一个新堆栈并确认它不是队列
  3. public static void main() {
  4. stackTest.push(10); // 将 10 推入堆栈
  5. stackTest.push(20); // 将 20 推入堆栈
  6. stackTest.push(30); // 将 30 推入堆栈
  7. stackTest.push(40); // 将 40 推入堆栈
  8. stackTest.push(50); // 将 50 推入堆栈
  9. stackTest.push(60); // 将 60 推入堆栈
  10. stackTest.push(70); // 将 70 推入堆栈
  11. stackTest.push(80); // 将 80 推入堆栈
  12. stackTest.push(90); // 将 90 推入堆栈
  13. stackTest.push(100); // 将 100 推入堆栈
  14. stackTest.push(110); // 将 110 推入堆栈
  15. stackTest.push(120); // 将 120 推入堆栈
  16. stackTest.overflowCheck();
  17. }
  18. public void overflowCheck() {
  19. double ratio = stackArray.length/stackTest.size;
  20. if(ratio > 1){
  21. System.out.println("堆栈溢出");
  22. }
  23. else if(ratio == 1){
  24. System.out.println("堆栈已满");
  25. }
  26. else{
  27. System.out.println("堆栈未满");
  28. }
  29. }
  30. }
英文:

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.

  1. class StackCheck {
  2. Data stackTest = new Data(10, false); //create a new stack &amp; confirm it&#39;s not a queue
  3. public static void main() {
  4. stackTest.push(10); //push 10 into the stack
  5. stackTest.push(20); //push 20 into the stack
  6. stackTest.push(30); //push 30 into the stack
  7. stackTest.push(40); //push 40 into the stack
  8. stackTest.push(50); //push 50 into the stack
  9. stackTest.push(60); //push 60 into the stack
  10. stackTest.push(70); //push 70 into the stack
  11. stackTest.push(80); //push 80 into the stack
  12. stackTest.push(90); //push 90 into the stack
  13. stackTest.push(100); //push 100 into the stack
  14. stackTest.push(110); //push 110 into the stack
  15. stackTest.push(120); //push 120 into the stack
  16. stackTest.overflowCheck();
  17. }
  18. public void overflowCheck() {
  19. double ratio = stackArray.length/stackTest.size;
  20. if(ratio &gt; 1){
  21. System.out.println(&quot;stack is overflowing&quot;);
  22. }
  23. else if(ratio == 1){
  24. System.out.println(&quot;stack is full&quot;);
  25. }
  26. else{
  27. System.out.println(&quot;stack is not full&quot;);
  28. }
  29. }
  30. }

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:

确定