紫书章五习题九 Bug Hunt UVA 1596

题意:一共有两种bug需要你找(自己不要想太多)
一共有两种操作
1.数组的声明,这个是没有bug的。注意只是声明,没有初始化
2.赋值操作 a[10]=2或a[10]=a[2]。这里bug会出现在用了没有初始化的值,超过数组的大小,用了没有声明的变量
还学到了
temp.find_first_of(‘[‘)//返回第一个[的下标
temp.find_last_of(‘]’)//返回最后一个】的下标
如果find没有找到值得话,就为string::npos
然后还有一个输入流的使用~

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <sstream>

using namespace std;
map<string , int > Map1;//里面存被定义了的数组及长度
map<string, map<int ,int > > Map2;//数组对应的相应下标有没有被初始化
int cheak(string temp)
{
    if(temp[0]>='0'&&temp[0]<='9')
    {
        stringstream ss;
        ss<<temp;
        int len;
        ss>>len;
        return len;
    }
    else
    {
        string aname=temp.substr(0,temp.find_first_of('['));
        string ain=temp.substr(temp.find_first_of('[')+1,temp.find_last_of(']'));
        int i=cheak(ain);
        if(i<0) return -1;
        if(Map1.count(aname)==0)
            {return -1;}
        else
        {
            if(Map2[aname].count(i)==0)
                {return -1;}
            else return Map2[aname][i];
        }
    }
}
int read()
{
    string str;
    int num=0,ans=0;
    while(getline(cin,str))
    {
        if(str=="."&&num==0)
            return 0;
        else if(str==".")
        {
            printf("%d\n",ans);num=0;
            ans=0;
            Map1.clear();
            Map2.clear();
        }
        else{
            num++;
            if(ans==0)
            {
                if(str.find("=")!=string::npos)
                {
                    string arr=str.substr(0,str.find("="));
                    string brr=str.substr(str.find("=")+1);
                    string aname = arr.substr(0,arr.find_first_of("["));
                    string ain = arr.substr(arr.find_first_of("[")+1,arr.find_last_of("]"));
                    int i=cheak(ain);
                    int j=cheak(brr);
                    int flag=0;
                    if(i<0||j<0) ans=num;
                    else if(Map1.count(aname)==0)
                        ans=num;
                    else
                    {
                        if(Map1[aname]<i+1)
                            ans=num;
                        else
                            Map2[aname][i]=j;
                    }
                }
                else{
                    string aname=str.substr(0,str.find_first_of("["));
                    string anum=str.substr(str.find_first_of("[")+1,str.find_last_of("]")-str.find_first_of("[")-1);
                    stringstream ss;
                    ss<<anum;
                    int len;
                    ss>>len;
                    Map1[aname]=len;
                }
            }
        }
    }
}
int main()
{
   //freopen("E:\\input.txt","r",stdin);
    while(read())
    {

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值