C语言中的多堆栈程序在经过一定数量的有意溢出和最高级的自动删除后显示不正确的元素
这是我现在正在开发的C程序,它应该允许用户一次最多处理5个堆栈,如果堆栈已满,它将在添加用户指定的数字之前自动删除toppermost项。然而,它一直受到一个海怪的困扰,在3个或更多不同的堆栈被故意溢出后,从而触发自动记忆功能,“显示所有堆栈中的所有元素”功能将开始显示不正确的结果 要么我错过了什么,要么我错过了一个闹鬼海怪 源代码:C语言中的多堆栈程序在经过一定数量的有意溢出和最高级的自动删除后显示不正确的元素,c,C,这是我现在正在开发的C程序,它应该允许用户一次最多处理5个堆栈,如果堆栈已满,它将在添加用户指定的数字之前自动删除toppermost项。然而,它一直受到一个海怪的困扰,在3个或更多不同的堆栈被故意溢出后,从而触发自动记忆功能,“显示所有堆栈中的所有元素”功能将开始显示不正确的结果 要么我错过了什么,要么我错过了一个闹鬼海怪 源代码: #include <stdio.h> #include <malloc.h> #define MAX 4 int stack[MAX],
#include <stdio.h>
#include <malloc.h>
#define MAX 4
int stack[MAX], topA = -1;
int stackb[MAX], topB = -1;
int stackc[MAX], topC = -1;
int stackd[MAX], topD = -1;
int stacke[MAX], topE = -1;
void pushA(int val)
{
if (topA == MAX)
{
int vala = val;
printf("\n Bin 0 full! Removing the toppest one and proceeds to add the specified item");
val = 0;
val = stack[topA];
topA--;
stack[topA + 1] = vala;
topA++;
vala = 0;
}
else
{
stack[topA + 1] = val;
topA++;
}
}
int popA()
{
int val;
if (topA == -1)
{
printf("\n Underflow");
}
else
{
val = stack[topA];
topA--;
}
return val;
}
void display_stackA()
{
int i;
if (topA == -1)
printf("\n Stack A is empty");
else
{
for (i = topA; i >= 0; i--)
printf("\t %d", stack[i]);
}
}
void pushB(int val)
{
if (topB == MAX)
{
int valb = val;
printf("\n Bin 1 full! Removing the toppest one and proceeds to add the specified item");
val = 0;
val = stackb[topB];
topB--;
stackb[topB + 1] = valb;
topB++;
valb = 0;
}
else
{
stackb[topB + 1] = val;
topB++;
}
}
int popB()
{
int val;
if (topB == -1)
{
printf("\n Underflow");
}
else
{
val = stackb[topB];
topB--;
}
}
void display_stackB()
{
int i;
if (topB == -1)
printf("\n Stack B is Empty");
else
{
for (i = topB; i >= 0; i--)
printf("\t %d", stackb[i]);
}
}
void pushC(int val)
{
if (topC == MAX)
{
int valc = val;
printf("\n Bin 2 full! Removing the toppest one and proceeds to add the specified item");
val = 0;
val = stackc[topC];
topC--;
stackc[topC + 1] = valc;
topC++;
valc = 0;
}
else
{
stackc[topC + 1] = val;
topC++;
}
}
int popC()
{
int val;
if (topC == -1)
{
printf("\n Underflow");
}
else
{
val = stackc[topC];
topC--;
}
}
void display_stackC()
{
int i;
if (topC == -1)
printf("\n Stack C is Empty");
else
{
for (i = topC; i >= 0; i--)
printf("\t %d", stackc[i]);
}
}
void pushD(int val)
{
if (topD == MAX)
{
int vald = val;
printf("\n Bin 3 full! Removing the toppest one and proceeds to add the specified item");
val = 0;
val = stackd[topD];
topD--;
stackd[topD + 1] = vald;
topD++;
vald = 0;
}
else
{
stackd[topD + 1] = val;
topD++;
}
}
int popD()
{
int val;
if (topD == -1)
{
printf("\n Underflow");
}
else
{
val = stackd[topD];
topD--;
}
}
void display_stackD()
{
int i;
if (topD == -1)
printf("\n Stack D is Empty");
else
{
for (i = topD; i >= 0; i--)
printf("\t %d", stackd[i]);
}
}
void pushE(int val)
{
if (topE == MAX)
{
int vale = val;
printf("\n Bin 4 full! Removing the toppest one and proceeds to add the specified item");
val = 0;
val = stacke[topE];
topE--;
stacke[topE + 1] = vale;
topE++;
vale = 0;
}
else
{
stacke[topE + 1] = val;
topE++;
}
}
int popE()
{
int val;
if (topE == -1)
{
printf("\n Underflow");
}
else
{
val = stacke[topE];
topE--;
}
}
void display_stackE()
{
int i;
if (topE == -1)
printf("\n Stack E is Empty");
else
{
for (i = topE; i >= 0; i--)
printf("\t %d", stacke[i]);
}
}
int main()
{
int option, options, val;
val = 0;
do
{
printf("\n -----Menu----- ");
printf("\n 1. PUSH a element");
printf("\n 2. POP a element");
printf("\n 3. Display all items");
printf("\n 4. Exit");
printf("\n Enter your choice");
scanf("%d", &option);
if (option == 1)
{
printf("\n Select ID no (0-4)");
scanf("%d", &options);
printf("\n Enter the value to push on your selected id:");
scanf("%d", &val);
if (options == 0)
{
pushA(val);
}
if (options == 1)
{
pushB(val);
}
if (options == 2)
{
pushC(val);
}
if (options == 3)
{
pushD(val);
}
if (options == 4)
{
pushE(val);
}
}
if (option == 2)
{
printf("\n Select ID no (0-4) to pop");
scanf("%d", &options);
if (options == 0)
{
printf("\n Toppest item popped from ID 0");
popA();
}
if (options == 1)
{
printf("\n Toppest item popped from ID 1");
popB();
}
if (options == 2)
{
printf("\n Toppest item popped from ID 2");
popC();
}
if (options == 3)
{
printf("\n Toppest item popped from ID 3");
popD();
}
if (options == 4)
{
printf("\n Toppest item popped from ID 4");
popE();
}
}
if (option == 3)
{
printf("\n The contents of ID 0 are :\n");
display_stackA();
printf("\n The contents of ID 1 are :\n");
display_stackB();
printf("\n The contents of ID 2 are :\n");
display_stackC();
printf("\n The contents of ID 3 are :\n");
display_stackD();
printf("\n The contents of ID 4 are :\n");
display_stackE();
}
} while (option != 4);
return 0;
}
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice1
Select ID no (0-4)0
Enter the value to push on your selected id:1
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice1
Select ID no (0-4)0
Enter the value to push on your selected id:2
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice1
Select ID no (0-4)0
Enter the value to push on your selected id:3
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice1
Select ID no (0-4)0
Enter the value to push on your selected id:4
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice1
Select ID no (0-4)0
Enter the value to push on your selected id:5
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice3
The contents of ID 0 are :
5 4 3 2 1
The contents of ID 1 are :
Stack B is Empty
The contents of ID 2 are :
Stack C is Empty
The contents of ID 3 are :
Stack D is Empty
The contents of ID 4 are :
Stack E is Empty
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice1
Select ID no (0-4)0
Enter the value to push on your selected id:6
Bin 0 full! Removing the toppest one and proceeds to add the specified item
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice3
The contents of ID 0 are :
6 4 3 2 1
The contents of ID 1 are :
Stack B is Empty
The contents of ID 2 are :
Stack C is Empty
The contents of ID 3 are :
Stack D is Empty
The contents of ID 4 are :
Stack E is Empty
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice1
Select ID no (0-4)1
Enter the value to push on your selected id:1
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice1
Select ID no (0-4)1
Enter the value to push on your selected id:2
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice1
Select ID no (0-4)1
Enter the value to push on your selected id:3
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice1
Select ID no (0-4)1
Enter the value to push on your selected id:4
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice1
Select ID no (0-4)1
Enter the value to push on your selected id:5
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice1
Select ID no (0-4)1
Enter the value to push on your selected id:6
Bin 1 full! Removing the toppest one and proceeds to add the specified item
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice3
The contents of ID 0 are :
6 4 3 2 1
The contents of ID 1 are :
6 4 3 2 1
The contents of ID 2 are :
Stack C is Empty
The contents of ID 3 are :
Stack D is Empty
The contents of ID 4 are :
Stack E is Empty
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice1
Select ID no (0-4)2
Enter the value to push on your selected id:1
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice3
The contents of ID 0 are :
6 4 3 2 1
The contents of ID 1 are :
6 4 3 2 1
The contents of ID 2 are :
1
The contents of ID 3 are :
Stack D is Empty
The contents of ID 4 are :
Stack E is Empty
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice1
Select ID no (0-4)2
Enter the value to push on your selected id:2
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice1
Select ID no (0-4)2
Enter the value to push on your selected id:3
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice3
The contents of ID 0 are :
6 4 3 2 1
The contents of ID 1 are :
6 4 3 2 1
The contents of ID 2 are :
3 2 1
The contents of ID 3 are :
Stack D is Empty
The contents of ID 4 are :
Stack E is Empty
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice1
Select ID no (0-4)2
Enter the value to push on your selected id:4
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice1
Select ID no (0-4)2
Enter the value to push on your selected id:5
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice1
Select ID no (0-4)2
Enter the value to push on your selected id:6
Bin 2 full! Removing the toppest one and proceeds to add the specified item
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice3
The contents of ID 0 are :
6 4 3 2 1
The contents of ID 1 are :
6 4 3 2 6
The contents of ID 2 are :
6 4 3 2 1
The contents of ID 3 are :
Stack D is Empty
The contents of ID 4 are :
Stack E is Empty
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice1
Select ID no (0-4)3
Enter the value to push on your selected id:1
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice1
Select ID no (0-4)3
Enter the value to push on your selected id:2
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice1
Select ID no (0-4)3
Enter the value to push on your selected id:3
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice1
Select ID no (0-4)3
Enter the value to push on your selected id:4
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice1
Select ID no (0-4)3
Enter the value to push on your selected id:5
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice1
Select ID no (0-4)3
Enter the value to push on your selected id:6
Bin 3 full! Removing the toppest one and proceeds to add the specified item
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice3
The contents of ID 0 are :
6 4 3 2 6
The contents of ID 1 are :
1 4 3 2 6
The contents of ID 2 are :
6 4 3 2 1
The contents of ID 3 are :
6 4 3 2 1
The contents of ID 4 are :
Stack E is Empty
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice1
Select ID no (0-4)4
Enter the value to push on your selected id:1
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice1
Select ID no (0-4)4
Enter the value to push on your selected id:2
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice1
Select ID no (0-4)4
Enter the value to push on your selected id:3
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice1
Select ID no (0-4)4
Enter the value to push on your selected id:4
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice1
Select ID no (0-4)4
Enter the value to push on your selected id:5
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice1
Select ID no (0-4)4
Enter the value to push on your selected id:6
Bin 4 full! Removing the toppest one and proceeds to add the specified item
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice3
The contents of ID 0 are :
6 4 3 2 6
The contents of ID 1 are :
1 4 3 2 6
The contents of ID 2 are :
6 4 3 2 6
The contents of ID 3 are :
6 4 3 2 1
The contents of ID 4 are :
6 4 3 2 1
-----Menu-----
1. PUSH a element
2. POP a element
3. Display all items
4. Exit
Enter your choice
您的堆栈只能容纳4个项目(因为
#define MAX 4
和int stackN[MAX];
),但您无法正确防止溢出。例如,如果操纵堆栈A并按下37,topA
为0;按41键,topA为1;按43键,topA为2;按47键,topA为3(堆栈已满),但按51键时,没有达到topA==MAX
条件,因此堆栈溢出-谁知道会覆盖什么!(它可能是topN
值之一;它可能是另一个堆栈的一部分;两者都不好,也都不是您想要的。)
我有一个网站给你-
最简单的修复方法可能是更改topN
的语义,以便将其初始化为0,而不是-1
。然后必须在所有函数中调整边缘条件
您确实需要避免使用太多的函数(3个函数各有5个副本)。这是一个更重要的重写。您还应该使用一个结构来描述每个堆栈。如果你还没有学会结构,你可以原谅(但是你应该使用一个包含
top
值的数组作为堆栈数据的2D数组。这将使所需的修复更容易;你可以有五分之一的地方进行系统编辑。你的堆栈只能容纳4个项目(因为\define MAX 4
和int stackN[MAX];
)但是您没有正确地防止溢出。例如,如果您操作堆栈A并按下37,则topA
为0;按下41和topA
为1;按下43和topA
为2;按下47和topA
为3(堆栈已满),但当您按下51键时,您没有达到topA==MAX
条件,因此您的堆栈溢出-覆盖谁知道是什么!(它可能是topN
值之一;它可能是另一个堆栈的一部分;两者都不好,也不是您想要的。)
我有一个网站给你-
最简单的修复方法可能是更改topN
的语义,使其初始化为0,而不是-1
。然后必须调整所有函数中的边缘条件
你真的需要避免有这么多的函数(3个函数各有5个副本)。这是一个更重要的重写。你还应该用一个结构来描述每个堆栈。如果你还没有学会结构,你就可以原谅了(但是你应该使用一个由
top
值组成的数组作为堆栈数据的2D数组。这将使所需的修复更加容易;你可以有五分之一的地方进行系统编辑。正如Jonathan所说,一个声明为4个元素的表只能使用0到3的索引。这是我的好日子,我给你一个简明的版本具有5个堆栈所共有的函数的sion。我还没有完全测试过,但它将为您提供一个很好的练习,让您理解它并根据Jonathan的建议进行更正。此代码仍然可以改进。轮到您了
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <assert.h>
#define STACK_QTY 5
#define MAX 4
int stack[STACK_QTY][MAX];
int top[STACK_QTY];
void push(int index, int val)
{
assert(index < STACK_QTY);
if(top[index] == (MAX - 1)) {
printf("\n Bin %d full! Removing the toppest one and proceeds to add "
"the specified item", index);
stack[index][top[index]] = val;
}
else {
stack[index][++top[index]] = val;
}
}
int pop(int index)
{
assert(index < STACK_QTY);
int val = -1;
if(top[index] == -1) {
printf("\n Underflow");
}
else {
val = stack[index][top[index]--];
}
return val;
}
void display_stack(int index)
{
assert(index < STACK_QTY);
int i;
if(top[index] == -1) {
printf("\n Stack %c is empty", index + 'A');
}
else {
for(i = top[index]; i >= 0; i--) {
printf("\t %d",stack[index][i]);
}
}
}
void option1(void)
{
unsigned int index = -1;
int value;
printf("\n Select ID no (0-%d)", STACK_QTY - 1);
scanf("%u",&index);
if (index < STACK_QTY){
printf("\n Enter the value to push on your selected id:");
scanf("%u",&value);
push(index, value);
}
else {
printf("error");
}
}
void option2(void)
{
unsigned int index;
printf("\n Select ID no (0-%d) to pop", STACK_QTY - 1);
scanf("%u",&index);
if (index < STACK_QTY){
printf("\n Toppest item popped from ID %d", index);
printf("\nvalue = %d\n", pop(index));
}
else {
printf("error");
}
}
void option3(void)
{
int i;
for (i = 0; i < STACK_QTY; ++i) {
printf("\n The contents of ID %d are :\n", i);
display_stack(i);
}
}
int main()
{
int option;
for (int i = 0; i < STACK_QTY; ++i) {
top[i] = -1;
}
do {
printf("\n -----Menu----- ");
printf("\n 1. PUSH a element");
printf("\n 2. POP a element");
printf("\n 3. Display all items");
printf("\n 4. Exit");
printf("\n Enter your choice");
scanf("%d", &option);
switch(option) {
case 1: option1(); break;
case 2: option2(); break;
case 3: option3(); break;
}
}
while(option != 4);
return 0;
}
#包括
#包括
#包括
#包括
#定义堆栈数量5
#定义最大值4
整数堆栈[堆栈数量][MAX];
int top[堆叠数量];
无效推送(int索引,int val)
{
断言(索引<堆栈数量);
如果(顶部[索引]==(最大值-1)){
printf(“\n仓位%d已满!删除最上面的仓位并继续添加”
“指定项目”,索引);
堆栈[索引][顶部[索引]]=val;
}
否则{
堆栈[索引][++顶部[索引]]=val;
}
}
int pop(int索引)
{
断言(索引<堆栈数量);
int val=-1;
如果(顶部[索引]=-1){
printf(“\n下溢”);
}
否则{
val=堆栈[索引][顶部[索引]-];
}
返回val;
}
无效显示\u堆栈(整数索引)
{
断言(索引<堆栈数量);
int i;
如果(顶部[索引]=-1){
printf(“\n堆栈%c为空”,索引+A”);
}
否则{
对于(i=top[index];i>=0;i--){
printf(“\t%d”,堆栈[索引][i]);
}
}
}
无效期权1(无效)
{
无符号整数索引=-1;
int值;
printf(“\n选择ID号(0-%d)”,堆栈数量-1);
scanf(“%u”和索引);
如果(索引<堆栈数量){
printf(“\n输入所选id上的值:”);
scanf(“%u”,和值);
push(索引、值);
}
否则{
printf(“错误”);
}
}
无效期权2(无效)
{
无符号整数索引;
printf(“\n选择要弹出的ID号(0-%d)”,堆栈数量-1);
scanf(“%u”和索引);
如果(索引<堆栈数量){
printf(“\n从ID%d弹出的最上面的项目”,索引);
printf(“\n值=%d\n”,pop(索引));
}
否则{
printf(“错误”);
}
}
无效选择权3(无效)
{
int i;
对于(i=0;i<堆栈数量++i){
printf(“\n ID%d的内容为:\n”,i);
显示堆栈(i);
}
}
int main()
{
int选项;
对于(int i=0;i<堆栈数量++i){
top[i]=-1;
}
做{
printf(“\n-----菜单------”);
printf(“\n 1.推送一个元素”);
printf(“\n 2.POP a元素”);
printf(“\n 3.显示所有项目”);
printf(“\n 4.退出”);
printf(“\n输入您的选择”);
scanf(“%d”,选项(&O);
开关(选件){
案例1:选项1();中断;
案例2:选项2();中断;
案例3:选择权