Last executed input: "100000000", what wrong?


  • 0
    6

    here is my code:
    int calculate(char s)
    {
    char array[6];
    memset(array,0,6);
    bool flag=0;
    int extra;
    char * pTemp=s;
    char pEnd;
    int nTemp;
    int number1,number2,result;
    number1=0;
    result=0;
    while('\0' != (pTemp))
    {
    if(32 == (pTemp))
    {
    pTemp++;
    continue;
    }
    else
    {
    if(flag)
    {
    switch(
    (pTemp))
    {
    case '+':
    if(
    (pTemp+1) == 32)
    {
    while(
    (pTemp+1) == 32)
    {
    pTemp++;
    }
    }
    pTemp++;
    pEnd=pTemp;
    while(
    (pEnd +1) >= 48 && (pEnd+1) <= 57)
    {
    pEnd++;
    }
    memcpy(array,pTemp,pEnd-pTemp+1);
    number2=atoi(array);
    pTemp=pEnd;
    if(
    (pTemp+1) == 32)
    {
    do
    {
    pTemp++;
    }
    while((pTemp+1)==32);
    }
    if(
    (pTemp+1) == '' || (pTemp+1) =='/')
    {
    result=number1;
    number1=number2;
    extra=1;
    }
    else
    {
    number1=number1+number2;
    }
    break;
    case '-':
    if(
    (pTemp +1) == 32)
    {
    do
    {
    pTemp++;
    }
    while(
    (pTemp+1) == 32);

                        }
                        pTemp++;
                        pEnd=pTemp;
                        while(*(pEnd +1) >=48 && *(pEnd +1) <= 57)
                        {
                            pEnd++;
                        }
                        memcpy(array,pTemp,pEnd-pTemp+1);
                        number2=atoi(array);
                        pTemp=pEnd;
                        if(*(pTemp+1) == 32)
                        {
                            do
                            {
                                pTemp++;
                            }
                            while(*(pTemp+1) == 32);
                        }
                        if(*(pTemp+1)== '*' || *(pTemp+1) == '/')
                        {
                            result=number1;
                            number1=number2;
                            extra=2;
                        }
                        else
                        {
                            number1=number1-number2;
                        }
                       break;
                    case '*':
                       if(*(pTemp+1) == 32)
                       {
                           do
                           {
                               pTemp++;
                           }
                           while(*(pTemp+1) == 32);
                       }
                       pTemp++;
                       pEnd=pTemp;
                       while(*(pEnd+1) >= 48 && *(pEnd+1) <=57)
                       {
                           pEnd++;
                       }
                       memcpy(array,pTemp,pEnd-pTemp+1);
                       number2=atoi(array);
                       pTemp=pEnd;
                       if(extra == 1)
                       {
                           number1=result+number1*number2;
                           extra=0;
                       }
                       else
                       {
                           if(extra == 2)
                           {
                               number1=result-number1*number2;
                               extra=0;
                           }
                           else
                           {
                               number1=number1*number2;
                           }
                       }
                           pTemp=pTemp+2;
                        break;
                    case '/':
                        if(*(pTemp +1) == 32)
                        {
                            do
                            {
                                pTemp ++;
                            }
                            while(*(pTemp +1) == 32);
                        }
                        pTemp++;
                        pEnd=pTemp;
                        while(*(pEnd+1) >= 48 && *(pEnd+1) <= 57)
                        {
                            pEnd++;
                        }
                        memcpy(array,pTemp,pEnd-pTemp+1);
                        number2=atoi(array);
                        pTemp=pEnd;
                        if(extra == 1)
                        {
                            number1=result+number1/number2;
                            extra=0;
                        }
                        else
                        {
                            if(extra== 2)
                            {
                                number1=result-number1/number2;
                                extra=0;
                            }
                            else
                            {
                                number1=number1/number2;
                            }
                        }
                        pTemp=pTemp+2;
                        break;
                    default:
                        pTemp++;
                }
            }
            else
            {
                pEnd=pTemp;
                while(*(pEnd+1) >=48 &&  *(pEnd+1) <= 57)
                {
                    pEnd++;
                }
                memcpy(array,pTemp,pEnd-pTemp+1);
                number1=atoi(array);
                pTemp=pEnd;
                flag=1;
                pTemp++;
            }
        }
    }
    return number1;
    

    }
    thank you for your help


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.