UVA 1596 Bug Hunt

题意:给出一段程序,输出第一个出现bug的位置。

程序有两个格式:一种是定义一个数组,并规定数组大小;一种是对数组元素进行赋值。 

Bug有两种:一种是数组越界,一种是使用未初始化的变量。

思路:模拟判断

如果是第一种语句,就给丢进map给个编号并记录数组大小。

第二种语句用递归判断一下,因为可以嵌套类似这样a[a[1]]。

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <utility>
using namespace std;

#define rep(i,j,k) for (int i=j;i<=k;i++)
#define Rrep(i,j,k) for (int i=j;i>=k;i--)
#define Clean(x,y) memset(x,y,sizeof(x))

const int maxn = 1001;

char temp[100];
string str;

int ans;
int arraynum;

map<string,int> pos;      //为数组名编号
map<int,int> value[maxn]; //每个数组的值是多少
map<int,bool> flag[maxn]; //每个数组的值是否被初始化
int ind[maxn]; // 最大下标

int getnum(char* st,char *ed)
{
    int ans = 0;
    for(char *i = st;i<=ed;i++)
        if ( isdigit( *i ) ) ans = ans * 10 + *i - '0';
        else break;
    return ans;
}

int getvalue(char *st,char *ed,bool& f)
{
    if ( !f ) return 0;
    char *s = st;
    int ans;
    if ( isdigit( *s ) )
        ans = getnum(st,ed);
    else
    {
        int k = strchr(st,'[') - st;
        string arrayname = string(st,st+k);
        int Ind = getvalue( st+k+1,ed,f );
        k = pos[arrayname];
        if( Ind< ind[ k ] && flag[ k ][Ind]  )
            ans = value[k][Ind];
        else
        {
            f = false;
            ans = 0;
        }
    }
    return ans;
}

bool init()
{
    gets(temp);
    if ( temp[0] == '.' ) return false;
    arraynum = 0;
    ans = 0;
    bool ok = true;
    while( temp[0]!='.' )
    {
        if ( ok )
        {
            if ( strchr(temp,'=') == NULL )
            {
                arraynum++;
                flag[arraynum].clear();
                value[arraynum].clear();
                int k = strchr(temp,'[') - temp;
                str = string(&temp[0],&temp[k]);
                pos[str] = arraynum;
                ind[arraynum] = getnum(temp+k+1,temp+strlen(temp)-1);
            }
            else
            {
                string arrayname;
                int Ind;
                int Val;
                bool vaild = true;
                int k = strchr(temp,'[') - temp;
                arrayname = string(&temp[0],&temp[k]);
                int p = strchr(temp,'=') - temp;
                Ind = getvalue(temp+k+1,temp+p,vaild);
                Val = getvalue(temp+p+1,temp+strlen(temp)-1,vaild);
                k = pos[arrayname];
                if ( !vaild || Ind>=ind[k] ) ok = false;
                else
                {
                    flag[k][Ind] = true;
                    value[k][Ind] = Val;
                }
            }
            ans++;
        }
        gets(temp);
    }
    if ( ok ) ans = 0;
    return true;
}

int main()
{
    while( init() )
    {
        cout<<ans<<endl;
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值