Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
pop()工作不正常 这里是一个堆栈上的C++代码。忽略这里的额外代码 #include<iostream> using namespace std; class mystack { private: int top; int size; int * s; public: void initialize() { top=-1; cin>>size; s=new int[size]; } ~mystack(){delete [] s;} void push() { int x; if(top==size-1) cout<<"stack overflow!"<<endl; else { cout<<"Enter element to be pushed:"; cin>>x; top++; s[top]=x; cout<<s[top]<<endl; } } int pop() { int p=s[top]; if(top==-1) return 0; else { top--; return p; } } int maxsize() { return size; } int isempty() { if(top==-1) return 0; else return 1; } void display() { int i,p=top; cout<<s[0]<<endl; for(i=0;i<=p;i++) cout<<s[i]<<endl; } }; int main() { int n,i; cout<<"Enter no. of stacks:"; cin>>n; mystack * st=new mystack[n]; for(i=0;i<n;i++) { cout<<"Enter size of stack "<<i+1<<":"; st[i].initialize(); } int c,s; while(1) { cout<<"*****Operations*****"<<endl; cout<<"1.Push 2.Pop 3.Maxsize 4.isempty 5.Display 6.Quit"<<endl; cout<<"Enter your choice:"; cin>>c; if(n>1) { cout<<"Operation on which stack:"; cin>>s; } else s=1; if(c==1) st[s-1].push(); else if(c==2) { if(st[s-1].pop()==0) cout<<"stack underflow!"<<endl; else cout<<st[s-1].pop()<<endl; } else if(c==3) cout<<st[s-1].maxsize()<<endl; else if(c==4) { if(st[s-1].isempty()==0) cout<<"True"<<endl; else cout<<"False"<<endl; } else if(c==5) st[s-1].display(); else if(c==6) break; else { cout<<"Wrong input!"<<endl; continue; } } return 0; } #包括 使用名称空间std; 类mystack { 私人: int top; 整数大小; int*s; 公众: void initialize() { top=-1; cin>>尺寸; s=新整数[大小]; } ~mystack(){delete[]s;} 无效推送() { int x; 如果(顶部==尺寸-1) cout_C++_Class - Fatal编程技术网 >尺寸; s=新整数[大小]; } ~mystack(){delete[]s;} 无效推送() { int x; 如果(顶部==尺寸-1) cout,c++,class,C++,Class" /> >尺寸; s=新整数[大小]; } ~mystack(){delete[]s;} 无效推送() { int x; 如果(顶部==尺寸-1) cout,c++,class,C++,Class" />

pop()工作不正常 这里是一个堆栈上的C++代码。忽略这里的额外代码 #include<iostream> using namespace std; class mystack { private: int top; int size; int * s; public: void initialize() { top=-1; cin>>size; s=new int[size]; } ~mystack(){delete [] s;} void push() { int x; if(top==size-1) cout<<"stack overflow!"<<endl; else { cout<<"Enter element to be pushed:"; cin>>x; top++; s[top]=x; cout<<s[top]<<endl; } } int pop() { int p=s[top]; if(top==-1) return 0; else { top--; return p; } } int maxsize() { return size; } int isempty() { if(top==-1) return 0; else return 1; } void display() { int i,p=top; cout<<s[0]<<endl; for(i=0;i<=p;i++) cout<<s[i]<<endl; } }; int main() { int n,i; cout<<"Enter no. of stacks:"; cin>>n; mystack * st=new mystack[n]; for(i=0;i<n;i++) { cout<<"Enter size of stack "<<i+1<<":"; st[i].initialize(); } int c,s; while(1) { cout<<"*****Operations*****"<<endl; cout<<"1.Push 2.Pop 3.Maxsize 4.isempty 5.Display 6.Quit"<<endl; cout<<"Enter your choice:"; cin>>c; if(n>1) { cout<<"Operation on which stack:"; cin>>s; } else s=1; if(c==1) st[s-1].push(); else if(c==2) { if(st[s-1].pop()==0) cout<<"stack underflow!"<<endl; else cout<<st[s-1].pop()<<endl; } else if(c==3) cout<<st[s-1].maxsize()<<endl; else if(c==4) { if(st[s-1].isempty()==0) cout<<"True"<<endl; else cout<<"False"<<endl; } else if(c==5) st[s-1].display(); else if(c==6) break; else { cout<<"Wrong input!"<<endl; continue; } } return 0; } #包括 使用名称空间std; 类mystack { 私人: int top; 整数大小; int*s; 公众: void initialize() { top=-1; cin>>尺寸; s=新整数[大小]; } ~mystack(){delete[]s;} 无效推送() { int x; 如果(顶部==尺寸-1) cout

pop()工作不正常 这里是一个堆栈上的C++代码。忽略这里的额外代码 #include<iostream> using namespace std; class mystack { private: int top; int size; int * s; public: void initialize() { top=-1; cin>>size; s=new int[size]; } ~mystack(){delete [] s;} void push() { int x; if(top==size-1) cout<<"stack overflow!"<<endl; else { cout<<"Enter element to be pushed:"; cin>>x; top++; s[top]=x; cout<<s[top]<<endl; } } int pop() { int p=s[top]; if(top==-1) return 0; else { top--; return p; } } int maxsize() { return size; } int isempty() { if(top==-1) return 0; else return 1; } void display() { int i,p=top; cout<<s[0]<<endl; for(i=0;i<=p;i++) cout<<s[i]<<endl; } }; int main() { int n,i; cout<<"Enter no. of stacks:"; cin>>n; mystack * st=new mystack[n]; for(i=0;i<n;i++) { cout<<"Enter size of stack "<<i+1<<":"; st[i].initialize(); } int c,s; while(1) { cout<<"*****Operations*****"<<endl; cout<<"1.Push 2.Pop 3.Maxsize 4.isempty 5.Display 6.Quit"<<endl; cout<<"Enter your choice:"; cin>>c; if(n>1) { cout<<"Operation on which stack:"; cin>>s; } else s=1; if(c==1) st[s-1].push(); else if(c==2) { if(st[s-1].pop()==0) cout<<"stack underflow!"<<endl; else cout<<st[s-1].pop()<<endl; } else if(c==3) cout<<st[s-1].maxsize()<<endl; else if(c==4) { if(st[s-1].isempty()==0) cout<<"True"<<endl; else cout<<"False"<<endl; } else if(c==5) st[s-1].display(); else if(c==6) break; else { cout<<"Wrong input!"<<endl; continue; } } return 0; } #包括 使用名称空间std; 类mystack { 私人: int top; 整数大小; int*s; 公众: void initialize() { top=-1; cin>>尺寸; s=新整数[大小]; } ~mystack(){delete[]s;} 无效推送() { int x; 如果(顶部==尺寸-1) cout,c++,class,C++,Class,既然你还没有回到这一点,我想你已经发现了你的逻辑错误 这是我发现的一个错误。可能还有更多错误,我不再寻找了 在下面的代码中,pop()被调用了多少次 else if(c==2) { if(st[s-1].pop()==0) cout<<"stack underflow!"<<endl; else cout<<st[s-1].pop()<<endl;

既然你还没有回到这一点,我想你已经发现了你的逻辑错误

这是我发现的一个错误。可能还有更多错误,我不再寻找了

在下面的代码中,pop()被调用了多少次

  else if(c==2)
    {
        if(st[s-1].pop()==0)
            cout<<"stack underflow!"<<endl;
        else
            cout<<st[s-1].pop()<<endl;
    }
else如果(c==2)
{
if(st[s-1].pop()==0)

coutI建议解决1堆栈的问题,更好地理解代码的作用。一些一般性建议:将输入逻辑与推送/初始化等分开,在数组访问之前检查
top==-1
,并且应该有一个初始化东西的构造函数。在这里“top--;return p;”你应该首先保存你想要返回的内容,然后根据你的推送量递减。但是,首先阅读上面的建议。我很快就完成了代码(/dev/pts/2I忘了给kudo的-编译的代码,它运行了,您有一个简单的逻辑错误…但是您可以尝试在命令循环中键入quit.oofdah。也就是说,不要使用堆栈修改操作(pop)来检测下溢。相反,使用状态检查,如isempty()。要考虑的另一个测试:当用户将1、2、3、0推入同一堆栈时会发生什么情况,然后弹出、弹出、弹出、弹出?这很好,我会更正它…但我认为这不是问题。当我弹出为什么返回top之前的元素?为什么返回top之前的元素?当(c==2)时,有两个对pop()的调用并且应该只有1。第一个在if子句中:“if(st[s-1].pop()==0)”第二个在cout中:“cout”