如何从堆栈末端删除0

How do I remove 0 from end of stack

本文关键字:删除 堆栈      更新时间:2023-10-16

函数处理字符串数组,并通过使用堆栈函数返回int值。在堆栈中,要推出的第一个数字是最后一个弹出的数字,因此循环从字符串的末端开始。它应该在字符串开始时删除任何0例如。004912500 => 4912500但是我不知道如何确保我的循环能够在开始时和0之后的0区分,因为炭都从背面推了。getvp返回void Pointer的整数值

int getDecimal (const char stringOfDigits [])
{
    Stack S;
    int *temp;
    VoidPtr item;
    // Loop to get int value of each element of char array
    for (int i = strlen(stringOfDigits)-1; i >= 0; i--)
        {
                if (isdigit(stringOfDigits[i]))
                    {
                        if (stringOfDigits[i] != '0')
                            {
                                temp = new int;
                                *temp = (stringOfDigits[i] - '0');
                                item = temp;
                                S.push(item);
                            }                       
                    }
        }
    while(!S.isEmpty())
        {
            item = S.pop();
            cout << S.getVP(item);
        }
    cout << endl;
}

在开始打印之前跳过0

int getDecimal (const char stringOfDigits [])
{
    Stack S;
    int *temp;
    VoidPtr item;
    int i = strlen(stringOfDigits)-1;
    // Loop to get int value of each element of char array
    for (; i >= 0; i--)
        {
                if (isdigit(stringOfDigits[i]))
                    {
                        if (stringOfDigits[i] != '0')
                            {
                                temp = new int;
                                *temp = (stringOfDigits[i] - '0');
                                item = temp;
                                S.push(item);
                            }                       
                    }
        }
    if(!S.isEmpty())
    {
        item = S.pop();
        //skip the 0's
        while((item == 0) && !S.isEmpty())
        {
            item = S.pop();
        }
        //output the first valid item, or a 0 if it's filled with 0 
        cout << S.getVP(item);
        while(!S.isEmpty())
        {
            item = S.pop();
            cout << S.getVP(item);
        }
    cout << endl;
}