【发布时间】:2019-08-22 06:10:26
【问题描述】:
这个项目今晚到期,到目前为止我已经尽了最大的努力。如果我能得到任何关于我应该如何继续工作的指导,我将不胜感激。我有一个我正在为一个项目介绍的主题:“编写一个程序,读取由正整数或正十进制数组成的字符串并将数字转换为数字格式。如果字符串由十进制数组成,则程序必须使用堆栈将十进制数转换为数字格式。”
首先,我对堆栈如何帮助将字符串格式的小数转换为数字格式的小数感到困惑。这将如何在程序内部工作?其次,我创建的代码由于某种原因无法正常工作,我不确定为什么。
我尝试查看堆栈溢出和其他网站上提出的问题,但没有任何问题可以回答我的问题。
#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+232Program 以退出代码结束:0
【问题讨论】:
-
“首先,我很困惑堆栈如何帮助将字符串格式的小数转换为数字格式的小数。” 我也是,什么是“数字格式”应该是什么意思? C++ 没有任何内置的十进制类 IIRC。
-
双倍或浮动
-
随心所欲地实现您的代码,每个 c++ 程序都使用一个堆栈 ...
-
我只能在这里冒险猜测。首先,如果允许,您可以使用
std::stack而不是自己实现。将所有数字压入堆栈,并逐个弹出并添加到最终输出中。当你不断寻找元素时,你必须除以10。 -
感谢@Wander3r 的评论。这让我开始思考如何重做我的代码,让它变得更好、更高效。
标签: c++ list type-conversion stack