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



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

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