二进制字符串到整数堆栈
Binary String to Integer Stack
努力将字符串分离到包含 1 和 0 的堆栈中。目前我正在尝试遍历字符串并将它们解析为整数以添加到堆栈中。
我正在输入 1010,结果是 1010 , 010, 10, 0 而不是所需的堆栈是 1, 0, 1, 0
我已经使用 atoi 和 stoi 以及索引和 .at 方法来解决我仍然遇到同样问题的地方。
#include <iostream>
#include <string>
#include <stack>
using namespace std;
bool isParsableInt(string input) {
string nums = "1234567890";
string test = input;
int attempt;
try {
if (test == "") { return false; }
if (test[0] == '-') {
test = test.substr(1, test.length() - 1);
}
for (int i = 0; i < test.length(); i++) {
if (nums.find(test[i]) == string::npos) { return false; }
attempt = atoi(&test[i]); // String to integer
}
return true;
}
catch (...) { // Catches any error thrown
return false;
}
}
stack<int> createBinaryStack() {
string input;
stack<int> result = stack<int>();
while (true){
cout << "Enter a binary number : ";
cin >> input;
if (!isParsableInt(input)) {
cout << "Invalid Input found - Must be 1's & 0's" << endl;
continue;
}
if (count(input, '0') + count(input, '1') != input.length()) {
cout << "Invalid Number found - Must be 1's & 0's" << endl;
continue;
}
for (int i = 0; i < input.length(); i++) {
cout << stoi(&input.at(i)) << "\t"; // Issue on atoi and stoi functions do not seem to work
result.push(stoi(&input.at(i)));
}
cout << endl;
return result;
}
}
int binaryStackToDecimal(stack<int> stk){
int count = stk.size();
int total = 0;
for (int i = 0; i < count; i++) {
if (stk.top() != 1 && stk.top() != 0) {
return -1;
}
total += stk.top() * pow(2, i);
stk.pop();
}
return total;
}
int main(){
stack<int> stk = createBinaryStack();
while (!stk.empty()) {
cout << stk.top();
stk.pop();
}
cout << endl;
cout << binaryStackToDecimal(stk);
}
stoi(&input.at(i))
应该是
input.at(i) - '0'
或者,因为你只处理零和一,所以更简单
input.at(i) == '1'
也有效(许多其他变体也是如此)。
你的错误是采用了旨在将 数字序列 转换为数字(stoi
和 atoi
)的函数做的是转换一个数字。
努力将字符串分离到包含 1 和 0 的堆栈中。目前我正在尝试遍历字符串并将它们解析为整数以添加到堆栈中。 我正在输入 1010,结果是 1010 , 010, 10, 0 而不是所需的堆栈是 1, 0, 1, 0
我已经使用 atoi 和 stoi 以及索引和 .at 方法来解决我仍然遇到同样问题的地方。
#include <iostream>
#include <string>
#include <stack>
using namespace std;
bool isParsableInt(string input) {
string nums = "1234567890";
string test = input;
int attempt;
try {
if (test == "") { return false; }
if (test[0] == '-') {
test = test.substr(1, test.length() - 1);
}
for (int i = 0; i < test.length(); i++) {
if (nums.find(test[i]) == string::npos) { return false; }
attempt = atoi(&test[i]); // String to integer
}
return true;
}
catch (...) { // Catches any error thrown
return false;
}
}
stack<int> createBinaryStack() {
string input;
stack<int> result = stack<int>();
while (true){
cout << "Enter a binary number : ";
cin >> input;
if (!isParsableInt(input)) {
cout << "Invalid Input found - Must be 1's & 0's" << endl;
continue;
}
if (count(input, '0') + count(input, '1') != input.length()) {
cout << "Invalid Number found - Must be 1's & 0's" << endl;
continue;
}
for (int i = 0; i < input.length(); i++) {
cout << stoi(&input.at(i)) << "\t"; // Issue on atoi and stoi functions do not seem to work
result.push(stoi(&input.at(i)));
}
cout << endl;
return result;
}
}
int binaryStackToDecimal(stack<int> stk){
int count = stk.size();
int total = 0;
for (int i = 0; i < count; i++) {
if (stk.top() != 1 && stk.top() != 0) {
return -1;
}
total += stk.top() * pow(2, i);
stk.pop();
}
return total;
}
int main(){
stack<int> stk = createBinaryStack();
while (!stk.empty()) {
cout << stk.top();
stk.pop();
}
cout << endl;
cout << binaryStackToDecimal(stk);
}
stoi(&input.at(i))
应该是
input.at(i) - '0'
或者,因为你只处理零和一,所以更简单
input.at(i) == '1'
也有效(许多其他变体也是如此)。
你的错误是采用了旨在将 数字序列 转换为数字(stoi
和 atoi
)的函数做的是转换一个数字。