使用堆栈将字符串格式的十进制转换为双精度

时间:2019-04-01 06:10:31

标签: c++ list type-conversion stack

该项目目前应在今晚到期,到目前为止,我已经尽力了。如果能就如何继续进行工作获得任何指导,将不胜感激。我为一个项目涵盖了这个主题:“编写一个程序,该程序读取一个由正整数或正十进制数字组成的字符串,并将该数字转换为数字格式。如果该字符串由十进制数字组成,则该程序必须使用堆栈将十进制数字转换为数字格式。”

首先,我对堆栈如何将字符串格式的十进制转换为数字格式的十进制感到困惑。在程序内部如何工作?第二,由于某些原因,我创建的代码无法正常工作,我不确定原因。

我试图调查在堆栈溢出和其他网站上提出的问题,但没有人能回答我的问题。


#include <iostream>
#include <cassert>
#include <string>


using namespace std;


template <class Type>
class stackADT
{
public:
    virtual void initializeStack() = 0;
    virtual bool isEmptyStack() const = 0;
    virtual bool isFullStack() const = 0;
    virtual void push(const Type& newItem) = 0;
    virtual Type top() const = 0;
    virtual void pop() = 0;
};

template <class Type>
class stackType: public stackADT<Type>
{
private:
    int maxStackSize;
    int stackTop;

public:
    Type *list;
    void initializeStack()
    {
        stackTop = 0;
        cout << "stackTop " << stackTop << endl;
    }

    void print()
    {
        for(int i=0; i<stackTop; i++)
        {
            cout << list[i] << endl;
        }
    }

    bool isEmptyStack() const
    {
        return(stackTop == 0);
    }

    bool isFullStack() const
    {
        return(stackTop == maxStackSize);
    }

    void push(const Type& newItem)
    {
        if (!isFullStack())
        {
            list[stackTop] = newItem;
            stackTop++;
        }
        else
        {
            cout << "Cannot add to a full stack." << endl;
        }
        cout << "stacktop: " << stackTop << endl;

        system("pause");
    }

    Type top() const
    {
        assert(stackTop != 0); //if stack is empty, terminate the program.
        return list[stackTop - 1];
    }

    Type getList() const
    {
        assert(stackTop != 0); //if stack is empty, terminate the program.
        return *list;
    }

    void pop()
    {
        if (!isEmptyStack())
            stackTop--;
        else
            cout << "Cannot remove from an empty stack." << endl;

        cout << "pop: " << stackTop << endl;
    }

    stackType(int stackSize = 100)
    {
        if (stackSize <= 0)
        {
            cout << "Size of the array to hold the stack must be positive." << endl;
            cout << "Creating an array of size 100." << endl;
            maxStackSize = 100;
        }
        else
        {
            maxStackSize = stackSize;
            // cout << "maxStackSize "  << maxStackSize << endl;
        }

        stackTop = 0;
        list = new Type[maxStackSize];
    }

    stackType(const stackType<Type>& otherStack)
    {
        list = NULL;
        copyStack(otherStack);
    }

    ~stackType()
    {
        delete [] list;
    }

    const stackType<Type>& operator=(const stackType<Type>& otherStack)
    {
        if (this != &otherStack)
        {
            copyStack(otherStack);
        }
        return *this;
    }

    bool operator==(const stackType<Type>& otherStack) const
    {
        if (this == &otherStack)
        {
            return true;
        }
        else
        {
            if (stackTop != otherStack.stackTop)
            {
                return false;
            }
            else
            {
                for (int i = 0; i < stackTop; i++)
                {
                    if (list[i] != otherStack.list[i])
                    {
                        return false;
                    }
                    return true;
                }
            }
        }
    }

    void copyStack(const stackType<Type>& otherStack)
    {
        delete [] list;
        maxStackSize = otherStack.maxStackSize;
        stackTop = otherStack.stackTop;

        list = new Type[maxStackSize];

        //copy otherStack into this stack.
        for (int j = 0; j < stackTop; j++)
        {
            list[j] = otherStack.list[j];
        }
    }
};

int main()
{
    string s;
    char c;
    bool found;
    int b = 0;

    string j = "";
    stackType<double> stack;

    cout<<"Would you like to convert an integer(i) or a decimal(d)?"<<endl;
    cin>>c;

    switch (c) {
        case 'i' :
        case 'I' : {
            cout << "Please enter your integer in string format: ";
            cin >> s;

            b = atoi(s.c_str());
            break;
        }
        case 'd' :

        case 'D' : {

            cout << "Please enter your decimal in string format: ";
            cin >> s;

            found = false;

            int q = 0;

            while(found == false) {

                if(s[q] == '.') {
                    found = true;

                }
                else {
                    q++;
                }

            }


            for (int i = 0; i <q; i++) {
                char p = s[i];



                j += p;

                for (int m = 0; m<q-i; m++) {

                    j += '0';

                }
                double k = stof(j);
                stack.push(k);
                j.clear();

            }

            break;
        }
        default: {
            cout <<"Wrong input. Please enter i or d for integer or decimal: ";
             cin>>c;
            break;
        }
    }

    cout << "Here is your string in integer or decimal format: ";
    double t = 0;

    if(c == 'i') {

        cout << b;

    }
    else if(c == 'd') {

        for(int i = 0; i < stack.top(); i++){

            t += stack.list[i];

        }

        cout << t;



    }



    return 0;
}


我希望输出是我输入时正确打印出的数字,但输出是:

您要转换整数(i)还是十进制(d)? d 请以字符串格式输入小数:1025.56 堆栈数:1 sh:暂停:找不到命令 栈顶:2 sh:暂停:找不到命令 栈顶:3 sh:暂停:找不到命令 堆栈数:4 sh:暂停:找不到命令 这是您的整数或十进制格式的字符串:9.74742e + 232程序结束于退出代码:0

1 个答案:

答案 0 :(得分:1)

让输入字符串为2345.6789。由于它是字符串,因此其各个字符都存储在内存中的连续位置中。现在,让我们依次访问它们,并执行以下算法。

首先,求整数部分

int Acc = 0;
Loop 1: Repeat until Input == Decimal point

Input = '2'; Acc = 10 * Acc + (Input - '0') = 10 x 0 + 2 = 2
Input = '3'; Acc = 10 * Acc + (Input - '0') = 10 x 2 + 3 = 23
Input = '4'; Acc = 10 * Acc + (Input - '0') = 10 x 23 + 4 = 234
Input = '5'; Acc = 10 * Acc + (Input - '0') = 10 x 234 + 5 = 2345
Input = '.' (Decimal point); exit Loop1

接下来,用小数部分的数字填充堆栈。

auto stack = std::stack<int>
Loop2: Repeat until Input == End of string

Input = '6'; stack.push (Input - '0');
Input = '7'; stack.push (Input - '0');
Input = '8'; stack.push (Input - '0');
Input = '9'; stack.push (Input - '0');
Input = End of string; exit Loop2

接下来,从堆栈中弹出数字,并评估小数部分。

double Acc2 = 0;
Loop 3: repeat until stack.empty()

Acc2 = (Acc2 + stack.top()) / 10 = (0 + 9) / 10 = 0.9; stack.pop();
Acc2 = (Acc2 + stack.top()) / 10 = (0.9 + 8) / 10 = 0.89; stack.pop();
Acc2 = (Acc2 + stack.top()) / 10 = (0.89 + 7) / 10 = 0.789; stack.pop();
Acc2 = (Acc2 + stack.top()) / 10 = (0.789 + 6) / 10 = 0.6789; stack.pop();

最后,将整数部分添加到小数部分。

Result = Acc + Acc2

这是一个纯粹的学术问题。向你的老师转达我的问候。