| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 478 人关注过本帖
标题:我的这个程序写的有很大问题,希望能给些提示
只看楼主 加入收藏
WorldOfGoo
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2008-11-25
收藏
 问题点数:0 回复次数:1 
我的这个程序写的有很大问题,希望能给些提示
这个程序是检验括号匹配的,但是其中括号匹配的概念是最邻近的连个括号是匹配的,如:“())”则默认第一个和第二个括号匹配。但程序中class Stack中如果把“ LinkNode<T> *top;”定义成protected域就会报错。。。。。希望能给些提示。。。
#include <iostream>
#include <sstream>

using namespace std;

template<class T>
struct LinkNode{
       T data;
       LinkNode<T>* link;
       LinkNode(LinkNode<T>* ptr=NULL){link=ptr;}
       LinkNode(const T& item,LinkNode<T> *ptr=NULL)
          {data=item;link=ptr;}
};

template<class T>
class Stack{
      public:
              Stack(){top=new LinkNode<T>;}
              bool IsEmpty()const
              {if(top->link==NULL)
                  return true;
               else
                  return false;}
              void Push(T n);
              T Pop();      
              LinkNode<T> *top;                        
};
 
template<class T>               
void Stack<T>::Push(T x){      
     top=new LinkNode<T>(x,top);
}

template<class T>
T Stack<T>::Pop(){
      if(IsEmpty()==true){
         cout<<"栈空"<<endl;
         return 0;
       }
      LinkNode<T>* del=top;
      top=top->link;
      T a=del->data;
      delete del;
      return a;
}           

int main(){
   
    char sr,c1,ch1='(', ch2='[', ch3='{', ch4=')', ch5=']', ch6='}';
    do{
    Stack<int>a;
    Stack<char>b;
    cout<<"Enter a List:";     
    string str;
    getline(cin,str);
    istringstream sin(str);
    int n=1;
    for(char s;sin>>s; ){  
                  if(s==ch1){
                     a.Push(n);
                     b.Push(ch1);
                     }
                  else if(s==ch2){
                     a.Push(n);
                     b.Push(ch2);
                     }
                  else if(s==ch3){
                     a.Push(n);
                     b.Push(ch3);
                     }
               
                  else if(s==ch4){
                     char m=b.Pop();
                     if(m!=ch1){
                        cout<<"没有与第"<<n <<"个右括号匹配的左小括号"<<endl;
                        b.Push(m);
                        }
                     else{
                        cout<<"第"<<a.top->data<<"与第"<<n<<"个小括号匹配"
                            <<endl;
                        a.Pop();     
                        }   
                         }
                  else if(s==ch5){
                     char j=b.Pop();
                     if(j!=ch2){
                        cout<<"没有与第"<<n <<"个右括号匹配的左中括号"<<endl;
                        b.Push(j);
                        }
                     else{
                        cout<<"第"<<a.top->data<<"与第"<<n<<"个中括号匹配"
                            <<endl;  
                         a.Pop();
                         }
                         }  
                  else if(s==ch6){
                     char k=b.Pop();
                     if(k!=ch3){
                        cout<<"没有与第"<<n <<"个右大括号匹配的左括号"<<endl;
                        b.Push(k);
                        }
                     else{
                        cout<<"第"<<a.top->data<<"与第"<<n<<"个大括号匹配"
                            <<endl;  
                        a.Pop();
                        }
                        }                                                
                 n++;
        }
      
       while(a.IsEmpty()==false){
                      cout<<"没有与第"<<a.Pop()<<"括号相匹配的括号"
                          <<endl;}
/**/           
        cout<<"once more?";
        cin>>sr;
        cin.get(c1);
        }while(sr=='y');   
         
         system("pause");
         return 0;
           
}
搜索更多相关主题的帖子: 提示 
2008-11-26 17:06
debroa723
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:23
帖 子:862
专家分:1954
注 册:2008-10-12
收藏
得分:0 
cout<<"第"<<a.top->data<<"与第"<<n<<"个小括号匹配"
...
cout<<"第"<<a.top->data<<"与第"<<n<<"个中括号匹配"
...
cout<<"第"<<a.top->data<<"与第"<<n<<"个大括号匹配"

!!!
如果是保护成员,a.top这样的访问是不允许的,写个Get函数吧,温习一下类相关知识最好.
2008-11-26 21:19
快速回复:我的这个程序写的有很大问题,希望能给些提示
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016584 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved