主题:哪位高手能帮我Debug这个程序(栈操作的)
[code]
#include<iostream.h>
#include<string.h>
#include<stdlib.h>
#define StackSize 128
#define Error (-1)
/*用栈实现括号匹配*/
/*程序调试了很久,老是出现出栈错误*/
/*输入"(8+5)*8"后,显示栈为空,找了半天了*/
class hyStack{
public:
hyStack(){
top=-1;
}
int isEmpty(void);
int isFull(void);
int Push(char);
char Pop(void);
int getTop(void);
int PrintMatchedPairs(char*);
private:
char data[128];
int top;
};
int hyStack::isEmpty(void){
return -1==top;
}
int hyStack::isFull(void){
return (top==StackSize-1);
}
int hyStack::Push(char ch){
if(isFull()){
cout<<"栈满了"<<endl;
return Error;
}
data[++top]=ch;
cout<<"Top="<<top<<endl;
return 1;
}
char hyStack::Pop(void){
if(isEmpty()){
cout<<"栈为空"<<endl;
return 0;
}
return data[top--];
}
int hyStack::getTop(void){
if(isEmpty()){
cout<<"栈为空"<<endl;
return Error;
}
return data[top];
}
int hyStack::PrintMatchedPairs(char *expr){
char ch,tmp;
int n=0,len;
len=strlen(expr);
while( (ch=expr[n])!='\0'&&(n<len) ){
if(ch=='('||ch=='['||ch=='{'){
Push(ch);
n++;
}
else if('/'==ch){
ch=expr[++n];
if(ch=='*'){
n++;
for(;expr[n]!='\0';n++){
if(expr[n]=='*'&&expr[n+1]=='/'){
n++;
break;
}
}
if(expr[n]=='\0'){
cout<<"出错/**/没有成对出现"<<endl;
return Error;
}
}
else {
continue;
}
}
else if(ch==')'){
tmp=Pop();
if(tmp!='('){
cout<<"error ()"<<endl;
return Error;
}
}
else if(ch==']'){
tmp=Pop();
if(tmp!='['){
cout<<"error []"<<endl;
return Error;
}
}
else if(ch=='}'){
tmp=Pop();
if(tmp!='}'){
cout<<"error {}"<<endl;
return Error;
}
}
else n++;
}
if(isEmpty())
cout<<"OK"<<endl;
else cout<<"error not isEmpty"<<endl;
}
int main(){
char expr[128];
hyStack hystr;
cin.getline(expr,128);
hystr.PrintMatchedPairs(expr);
system("pause");
return 0;
}
[/code]
[em10][em10][em10][em10]
#include<iostream.h>
#include<string.h>
#include<stdlib.h>
#define StackSize 128
#define Error (-1)
/*用栈实现括号匹配*/
/*程序调试了很久,老是出现出栈错误*/
/*输入"(8+5)*8"后,显示栈为空,找了半天了*/
class hyStack{
public:
hyStack(){
top=-1;
}
int isEmpty(void);
int isFull(void);
int Push(char);
char Pop(void);
int getTop(void);
int PrintMatchedPairs(char*);
private:
char data[128];
int top;
};
int hyStack::isEmpty(void){
return -1==top;
}
int hyStack::isFull(void){
return (top==StackSize-1);
}
int hyStack::Push(char ch){
if(isFull()){
cout<<"栈满了"<<endl;
return Error;
}
data[++top]=ch;
cout<<"Top="<<top<<endl;
return 1;
}
char hyStack::Pop(void){
if(isEmpty()){
cout<<"栈为空"<<endl;
return 0;
}
return data[top--];
}
int hyStack::getTop(void){
if(isEmpty()){
cout<<"栈为空"<<endl;
return Error;
}
return data[top];
}
int hyStack::PrintMatchedPairs(char *expr){
char ch,tmp;
int n=0,len;
len=strlen(expr);
while( (ch=expr[n])!='\0'&&(n<len) ){
if(ch=='('||ch=='['||ch=='{'){
Push(ch);
n++;
}
else if('/'==ch){
ch=expr[++n];
if(ch=='*'){
n++;
for(;expr[n]!='\0';n++){
if(expr[n]=='*'&&expr[n+1]=='/'){
n++;
break;
}
}
if(expr[n]=='\0'){
cout<<"出错/**/没有成对出现"<<endl;
return Error;
}
}
else {
continue;
}
}
else if(ch==')'){
tmp=Pop();
if(tmp!='('){
cout<<"error ()"<<endl;
return Error;
}
}
else if(ch==']'){
tmp=Pop();
if(tmp!='['){
cout<<"error []"<<endl;
return Error;
}
}
else if(ch=='}'){
tmp=Pop();
if(tmp!='}'){
cout<<"error {}"<<endl;
return Error;
}
}
else n++;
}
if(isEmpty())
cout<<"OK"<<endl;
else cout<<"error not isEmpty"<<endl;
}
int main(){
char expr[128];
hyStack hystr;
cin.getline(expr,128);
hystr.PrintMatchedPairs(expr);
system("pause");
return 0;
}
[/code]
[em10][em10][em10][em10]