为什么当我输入一个大数字时,输出会一遍又一遍地重复?
How come when I type a large number the output just repeats itself over and over?
为什么当我输入一个大数字时,输出会一遍又一遍地重复自己?
#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';}))
相关文章:
- 如何格式化我的文本文件以使其不会一遍又一遍地重复同一行?
- 为什么即使使用 for 循环遍历我的向量,它也没有输出到控制台?(C++)
- 尝试重载输出运算符时,我无法遍历对象向量
- 为什么当我输入一个大数字时,输出会一遍又一遍地重复?
- 如何遍历几个每小时一次的根(.root)文件,并将它们组合成更大的每日数据.root文件?
- 使用后序遍历递归的深度优先搜索会产生意外输出
- BFS遍历给出错误的输出
- 我如何一遍又一遍地向用户提问,直到他们输入正确的值字段?
- c++ 一遍又一遍地对函数返回相同的检查
- 一遍又一遍地构造相同的对象
- C++:如何一次输出两个字母的字符串
- C++ 在 while 循环中获取一次输出
- 有没有办法不在 c++ 中的参数中一遍又一遍地输入相同的数据类型
- 如何从while循环生成的一组输出中找到最大值
- 一遍又一遍地创建同一个文本文件
- 变量不计数,即使一遍又一遍地调用函数
- 如何避免一遍又一遍地编写相同的模板
- 一遍又一遍地传递对象的替代方法
- 一遍又一遍地验证文件时遇到问题
- 递归的迷宫遍历输出每一条完整的路径