为什么当我输入一个大数字时,输出会一遍又一遍地重复?

How come when I type a large number the output just repeats itself over and over?

本文关键字:一遍 输出 数字 输入 一个 为什么      更新时间:2023-10-16

为什么当我输入一个大数字时,输出会一遍又一遍地重复自己?

#include "stack.h"
#include <string>
#include <sstream>
#include <math.h>
using namespace std;
void convertBinaryToDecimal(string num, MyStack<int>& thestack);
int main() {
MyStack<int> thestack;
int input = -22222;
while (true) {
cout << "enter -999 to quit, Enter your binarty number: ";
cin >> input;
if (input == -999) { 
cout << "Thank you, see you next time." << endl;
system("Pause");
break; 
}
else {
string str = to_string(input);
convertBinaryToDecimal(str, thestack);
}
}
}
void convertBinaryToDecimal(string num, MyStack<int>& thestack) {
int remainder = 0;
int count = 0;
int dec_value = 0;
int n;
stringstream number(num);
number >> n;
while (n != 0) {
remainder = n % 10;
n /= 10;
dec_value += remainder * pow(2, count);
count++;
}
thestack.push(dec_value);
int value = thestack.top();
thestack.pop();
cout << "The equivalent decimal number is: " << value << endl;
}

.cpp文件^

#ifndef STACK_H_INCLUDED
#define STACK_H_INCLUDED
#include <iostream>
#include <string>
using namespace std;

template<class Type>
class MyStack {
public:
int count;
MyStack(int mysize = 100) {
list = new int[mysize];
maxStackSize = mysize;
stackTop = 0;
count = 0;
}
MyStack(const MyStack<Type>& thestack) {
copyStack(thestack);
}
~MyStack() {
delete[] list;
}
void initializeStack();
bool isEmptyStack() const;
bool isFullStack() const;
Type push(const Type& newItem);
Type top()const;
void pop();

const MyStack<Type>& operator=(const MyStack<Type>& thestack) {
copyStack(thestack);
return *(this);
}

private:
int maxStackSize;
int stackTop;
Type* list;
void copyStack(const MyStack<Type>&);
};
template<class Type>
void MyStack<Type>::initializeStack() {
stackTop = 0;
}
template<class Type>
bool MyStack<Type>::isEmptyStack() const {
return (stackTop == 0);
}
template<class Type>
bool MyStack<Type>::isFullStack() const {
return (stackTop == maxStackSize);
}
template<class Type>
Type MyStack<Type>::push(const Type& newItem) {
if (isFullStack()) {
return NULL;
}
list[stackTop] = newItem;
stackTop++;
return list[stackTop - 1];
}
template<class Type>
Type MyStack<Type>::top()const {
if (isEmptyStack()) {
return NULL;
}
return list[stackTop - 1];
}
template<class Type>
void MyStack<Type>::pop() {
if (isEmptyStack()) {
}
else {
stackTop--;
}
}
template<class Type>
void MyStack<Type>::copyStack(const MyStack<Type>& thestack) {
delete[] list;
list = new Type[thestack.maxStackSize];
for (int i = 0; i < thestack.maxStackSize; i++) {
list[i] = thestack.list[i];
}
stackTop = thestack.stackTop;
maxStackSize = thestack.maxStackSize;
}

#endif//STACK_H_INCLUDED

头文件^

当我键入像"101010100100101010"这样的大二进制文件时,输出会一遍又一遍地重复。当我键入较小的二进制文件(如"1010"(时,它很好,并给了我正确的输出。

有什么想法吗?我很确定它只是崩溃。
编辑:我一直在测试它,它在 10 位数字后中断。

嗯。我认为主要问题是您将二进制数从用户读取到int变量中。因此,如果用户输入 1000,您希望它是 8,但实际上它是 1000(十进制(。然后将 1000(十进制(转换为字符串,然后是"1000"。这当然不好,也是造成您问题的原因之一。

整数变量可以将值保存到给定边界。您可以在C++中通过以下方式找到答案

#include <limits>
#include <iostream>
int main()
{
std::cout << std::numeric_limits<int>::max() << "n";
return 0;
}

结果取决于机器。32 位硬件上的可能值为:2147483647

所以,现在你输入一大串'1'和'0',例如"101010100100101010",你希望读取二进制文件。但是你试着把它放在一个整数中。但是十进制数101010100100101010不适合整数,因此您的完整预期功能将无法按预期工作。

解决方案是将用户的值读取到std::string中,而不是读取到int

然后,您可以将std::string中的二进制数据转换为小数。

为此,您可以使用现有的内置函数,例如:

#include <string>
#include <iostream>
int main()
{
const std::string testBinary{"101010100100101010"};
std::cout << std::stoul(testBinary,0,2) << "n";
return 0;
}

或者通过迭代字符串进行简单的转换:

#include <string>
#include <iostream>
#include <algorithm>
int main()
{
const std::string testBinary{"101010100100101010"};
unsigned long decimalValue{0};
std::for_each(testBinary.rbegin(),testBinary.rend(), [&decimalValue] (const char& c) { decimalValue |= (c-'0'); decimalValue <<= 1; });
std::cout << decimalValue << "n";
return 0;
}

此功能根本不需要堆栈。我不确定,你为什么要把它放进去。也许出于学术目的。

顺便说一下,您可以使用以下命令检查"二进制字符串"的正确性

if (std::all_of(testBinary.begin(), testBinary.end(), [](const char& c){ return c=='1' || c=='0';}))