括号匹配问题

 没用模版写的,也没考虑多于的情况,练手。

 //括号配对问题
#include<iostream>
using namespace std;
class stack
{
      private:
       int *a;
       int top;
      public:
             stack(int sz=100);
             void Push(const int &x);
             bool Pop(int &);
};
      void stack::Push(const int &x)
      {
           a[++top]=x;
      }
      bool stack::Pop(int &x)
      {
           x=a[top--];
           return true;
      }
      stack::stack(int sz)
      {
        top=-1;
        a=new int[sz];
      }
     int main()
     {
         cout<<"please enter a line"<<endl;
         char str[20];
         cin>>str; 
         stack ch;
         void C(char *,stack);
         C(str,ch);
         system("pause");
         return 0;
     }
     void C(char str[],stack s)
     {
          int i=0;int ch;
          for(i=0;i<strlen(str);i++)
          {
            if(str[i]=='(')
            s.Push(i+1);
            else if(str[i]==')')
             { if(s.Pop(ch))
             cout<<"第"<<ch<<"个左括号与第"<<i+1<<"右括号匹配"<<endl;
             }
          }
     } 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值