ANTI-BLOT系统解决方案中的错误(spoj)

时间:2015-07-14 19:25:11

标签: c++

你好,我们必须转换一个像" 43 + machula0 = 163" 如43 + 120 = 163.means我们必须扫描表达式,然后找到缺少的部分。我的程序运行良好而不放入while循环,但它显示错误,当我用循环运行它。

#include<iostream>  
#include<cstring>  
#include<cctype>  
#include<cstdlib>  

using namespace std;  

void convert(char *a,int size)  
   {


    int i=0;  
    char c =a[i];  
    int f1,f2,f3,n1,n2,s=0; //f1,f2,f3 are flags to check machula 
    f1=0;f2=0;f3=0;n1=0;n2=0;  


    while(c!='+')//to store no. before plus in n1
    {
        c=a[i];
       if(isalpha(c))//to check whether the character is alphabet or not
        {
            f1=1;
        }
        else if(isdigit(c))//to check whether the character is digit
        {
            int a = c-'0';
            n1=n1*10 + a;
        }
        i++;
    }
    while(c!='=')//to store no. before plus in n2
    {
        c=a[i];
        if(isalpha(c))
        {
             f2=1;
        }
        else if(isdigit(c))
        {
            int k = c-'0';
            n2=n2*10 + k;
        }
        i++;

    }
    while(i!=size)//to store no. before plus in s
    {
        c=a[i];
        if(isalpha(c))
       {
             f3=1;
        }
        else if(isdigit(c))
        {
            int h = c-'0';
            s=s*10 + h;
        }
        i++;
    }

    if(f3==1)
   {
        s=n1+n2;
        cout<<n1<<" + "<<n2<<" = "<<s<<endl;

    }
    else 
    {
        if(f1==1)
        {
            n1=s-n2;
            cout<<n1<<" + "<<n2<<" = "<<s<<endl;
        }
        else if(f2==1)
        {
            n2=s-n1;
            cout<<n1<<" + "<<n2<<" = "<<s<<endl;
        }
        else
        {
            cout<<n1<<" + "<<n2<<" = "<<s<<endl;
        }
    }}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {

    char *a;
    a= new char[10000];
    cin.getline(a,10000);
    int size = strlen(a);
    convert(a,size);
    delete []a;
    }
    return 0;
}

1 个答案:

答案 0 :(得分:0)

第二个想法我确实有一些有用的输入。如果你手动输入,可以输入如下输入:

1 
43 + machula0 = 163" as 43 + 120 = 163

这会导致问题

int main()
{
    int t;
    cin>>t; // gets up to the end of the number typed.
            // It does not get the carriage return from hitting enter to trigger the input.
    while(t--)
    {

        char *a; // Not relevant to the problem, but this is an awesomely bad idea. 
                 // Use a string
        a= new char[10000];
        cin.getline(a,10000); // returns instantly with an empty string because of the 
                              // left-over enter used above to get the number.
        int size = strlen(a);
        convert(a,size); // most of convert does not check size, so instantly out of 
                         // array bounds and crash
        delete []a;
    }
    return 0;
}

对于这样的输入有效:

1 43 + machula0 = 163" as 43 + 120 = 163

更好的解决方案:

void convert(string &a)
{
    for (char c: a)
    {
        //parse logic goes here
    }
    // output logic goes here
}

int main()
{
    int t;
    cin >> t;
    cin.get();
    while (t--)
    {

        string line;
        if (getline(cin, line))
        {
            convert(line);
        }
    }
    return 0;
}

不幸的是,转换功能需要更多的工作。最明显的是,对于字符串的结尾几乎完全没有测试。