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

Converting Decimal in String Format to a Double Using Stacks

本文关键字:十进制 转换 双精度 格式 堆栈 字符串      更新时间:2023-10-16

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

首先,我对堆栈如何帮助将字符串格式的小数转换为数字格式的小数感到困惑。这将如何在程序内部工作?其次,我创建的代码由于某种原因不起作用,我不确定为什么。

我尝试查看堆栈溢出和其他网站上提出的问题,但没有任何东西可以回答我的问题。


#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

设输入字符串为 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

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