这个堆栈存数据有问题 求大神调试 (我是菜鸟)
#include<iostream>
#include<ctype.h>
using namespace std;
const int SIZE =3;
class stack{
int a[SIZE];
int top;
public:
stack(){top=0;}
void push(int a);
void pop();
bool isempty(){return top==0;}
bool isfull(){return top==SIZE;}
void showstack();
};
void stack::push(int b){
if (isfull())
cout<<"the stack is full\n";
return;
a[top]=b;
top++;
}
void stack::pop(){
if(isempty())
cout<<"it is empty\n";
return ;
int temp=a[--top];
cout<<temp;}
void stack::showstack(){
for(int i=0;i<top;i++)
cout<<a[i];
}
int main()
{
stack ss;
char ch;
bool flag=true;
cout<<"put i to push\n";
cout<<"put p to pop \n";
cout<<"put s to show the stack\n";
cout<<"put e to end\n";
cin>>ch;
ch=char(ch-32);
while(flag)
{
switch(ch)
{ int temp;
case 'I':
cout<<"please input the data:\n";
cin>>temp;
ss.push(temp);
break;
case 'P':
cout<<"we will push the data:\n";
ss.pop();
cout<<temp;
break;
case 'S':
ss.showstack();
break;
case 'E':
flag=false;
cout<<"we will endl";
default:
cout<<"you put worry char try it again\n";
}
cin>>ch;
ch=char(ch-32);
}
}
[解决办法]
指出你两个错误
if (isfull())
cout<<"the stack is full\n";
return;
要加上大括号
if (isfull())
{
cout<<"the stack is full\n";
return;
}
这里也要加上大括号
if(isempty())
cout<<"it is empty\n";
return ;
[解决办法]
楼上说的对,主要问题是没加大括号。我改了一下你看看
// duizhan.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<ctype.h>
using namespace std;
const int SIZE =10;
class stack{
int a[SIZE];
int top;
public:
stack():top(0){};
void push(int a);
void pop();
bool isempty(){return (top == 0);}
bool isfull(){return (top == SIZE);}
void showstack();
};
void stack::push(int b){
if (isfull())
{
cout<<"the stack is full"<<endl;
return;
}
a[top]=b;
top++;
}
void stack::pop()
{
if(isempty())
{
cout<<"it is empty"<<endl;
return ;
}
int temp=a[--top];
cout<<temp<<endl;
}
void stack::showstack(){
for(int i=0;i<top;i++)
cout<<a[i]<<endl;
}
int main()
{
stack ss;
char ch;
bool flag=true;
cout<<"put i to push\n";
cout<<"put p to pop \n";
cout<<"put s to show the stack\n";
cout<<"put e to end\n";
cin>>ch;
fflush(stdin);
ch=char(ch-32);
while(flag)
{
switch(ch)
{
int temp;
case 'I':
cout<<"please input the data:\n";
cin>>temp;
ss.push(temp);
break;
case 'P':
cout<<"we will push the data:\n";
ss.pop();
break;
case 'S':
ss.showstack();
break;
case 'E':
flag=false;
cout<<"we will endl\n";
default:
cout<<"you put worry char try it again\n";
}
cout<<"put i to push\n";
cout<<"put p to pop \n";
cout<<"put s to show the stack\n";
cout<<"put e to end\n";
cin>>ch;
fflush(stdin);
ch=char(ch-32);
}
}