浙大版《数据结构(第2版)》题目集习题3.8 符号配对 (20分) 易理解版。

代码容易理解,难处都做了解释,沉住心就能ac这道题,代码看着多,实际上很少,因为中间段是可以封装的,我为了加强理解所以没有选择封装。

#include<bits/stdc++.h> 
using namespace std;
const int maxsize = 1000;
char Data[maxsize];//保存符号
int len = 0;

char c_stack[maxsize];//一个比较简易的栈
int top = -1;

int main()
{
	//筛选数据,记录符号到Data字符数组里
    string s;
    while(getline(cin,s))//getline(cin,s)为输入一行数据,并赋值给字符串s
    {
        if(s[0]=='.')//倘若某行的首字母为'.',那么停止录入数据
        {
            break;
        }
        int s_len=s.length();//.length()为计算某字符串长度,并返回
        for(int i=0;i<s_len;i++)
        {
            char cfront=s[i],ctail=s[i+1];//这个方便记录符号/*与*/
            
            if(s[i]=='(' || s[i]=='[' || s[i]=='{' || s[i]==')' || s[i]==']' || s[i]=='}')
            {
                Data[len++]=s[i];
            }else if(cfront=='/'&&ctail=='*'){
                Data[len++]='<';//为方便操作,把/*记为<,下段同理
                i++;
            }else if(cfront=='*'&&ctail=='/'){
                Data[len++]='>';
                i++;
            }
        }
    }
    //从Data中读入数据,并利用栈来进行配对,里面的思想见文章末尾文字
    for(int i=0;i<len;i++)
    {
        if(Data[i]=='('||Data[i]=='['||Data[i]=='{'||Data[i]=='<'){
            c_stack[++top]=Data[i];
        }else if(Data[i]==')'){
            if(top<0){
                cout<<"NO"<<endl;
                cout<<"?-)"<<endl;
                return 0;
            }else if(c_stack[top]=='('){
                top--;
            }else{
                cout<<"NO"<<endl;
                if(c_stack[top]='<'){
                    cout<<"/*-?"<<endl;
                }else{
                    cout<<c_stack[top]<<"-?"<<endl;
                }
                return 0;
            }
        }else if(Data[i]==']'){
            if(top<0){
                cout<<"NO"<<endl;
                cout<<"?-]"<<endl;
                return 0;
            }else if(c_stack[top]=='['){
                top--;
            }else{
                cout<<"NO"<<endl;
                if(c_stack[top]=='<'){
                    cout<<"/*-?"<<endl;
                }else{
                    cout<<c_stack[top]<<"-?"<<endl;
                }
                return 0;
            }
        }else if(Data[i]=='}'){
            if(top<0){
                cout<<"NO"<<endl;
                cout<<"?-}"<<endl; 
                return 0;
            }else if(c_stack[top]=='{'){
                top--;
            }else{
                cout<<"NO"<<endl;
                if(c_stack[top]=='<'){
                    cout<<"/*-?"<<endl;
                }else{
                    cout<<c_stack[top]<<"-?"<<endl;
                }
                return 0;
            }
        }else{
            if(top<0){
                cout<<"NO"<<endl;
                cout<<"?-*/"<<endl;
                return 0;
            }else if(c_stack[top]=='<'){
                top--;
            }else{
                cout<<"NO"<<endl;
                cout<<c_stack[top]<<"-?"<<endl;
                return 0;
            }
        }
    }
    //代码工作到这里可能会出现,第一个录入的字符还未配对。
    if(top==0){
        if(c_stack[top]=='<'){
            cout<<"NO"<<endl;
            cout<<"/*-?"<<endl;
            return 0;
        }else{
            cout<<"NO"<<endl;
            cout<<c_stack[top]<<"-?"<<endl;
            return 0;
        }
    }else{
        cout<<"YES"<<endl;
    }
    return 0;
}

中间段的代码思路:
读入符号,
栈空,说明没有与之配对的,按照题目要求输出,并结束程序;
栈不空,且栈顶元素与该符号配对,那么栈顶元素出栈,继续执行下个Data里的符号;
栈不空,但栈顶元素不与之配对,按照题目要求输出,并结束程序。
代码自己写一遍就有体会了。
中间段可以封装减少代码量。
ps.代码参考:https://www.jianshu.com/p/b392a03b962b

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值