gpt4 book ai didi

c++ - 有没有办法在遇到 `\n` 时停止 std::cin

转载 作者:行者123 更新时间:2023-11-27 23:56:37 25 4
gpt4 key购买 nike

除了一件事,我编写的代码运行良好。我编写此代码的任务是将数据作为空格分隔的 double 字符串输入到程序中。并且它们的精度可能大于 10^-25。所以我创建了自己的类(class)来处理这个问题。

问题是,当我编写代码时,每次按下 enter 时,我都会通过手动向控制台输入两个值来测试它,这样我的程序就可以理解一个 double 的结束位置和另一个开始的位置(它正在寻找一个'\n' 基本上)。

现在我真的需要调整此代码以处理我的任务输入(空格分隔的 double 列表,如 2.521 32.12334656 23.21 ....)。但是我在重载的 >> 运算符中遇到了 getline 问题。它只是吃掉 '\n' 字符并开始寻找更多输入。我能让它工作的唯一方法是手动输入值并在最后一个值之后手动输入一个额外的空格,然后才按回车键。

我请求你的帮助。

完整代码如下:

#include <iostream>
#include <string>
#include <algorithm>


class BigNumber {
private:
std::string fullPart;
std::string floatPart;

public:
BigNumber() : fullPart("0"), floatPart("0") {}


friend std::ostream & operator << (std::ostream & os, const BigNumber & bn);
friend std::istream & operator >> (std::istream & os, BigNumber & bn);

void operator+=(BigNumber & bn);
};

int main()
{
BigNumber bn, bntemp;

while (std::cin >> bntemp)
{
bn += bntemp;

if (std::cin.peek() == '\n')
break;
}

std::cout << bn << std::endl;
return 0;
}

void addFullPart(const std::string & add, std::string & add_to)
{
auto addConv = std::stold(add);
auto addToConv = std::stold(add_to);

auto newFull = std::to_string(addConv + addToConv);
add_to = std::string(newFull.begin(), std::find(newFull.begin(), newFull.end(), '.'));
}

bool carryReminder(std::string & add_to, int32_t indx_from)
{
for (auto curr = indx_from; curr >= 0; --curr)
{
if (add_to[curr] != '9')
{
++(add_to[curr]);
return true;
}
else
add_to[curr] = '0';
}

return false;
}

std::pair<std::string, int32_t> addFloatPart(std::string & add, std::string & add_to)
{
std::string resultFloat;

int32_t reminderReturn{};

// don't forget to reverse str
if (add.size() != add_to.size())
{
// add remaining 0's
if (add.size() < add_to.size())
{
while (add.size() != add_to.size())
{
auto tempBigger = add_to.back();
add_to.pop_back();
resultFloat.push_back(tempBigger);
}
}
else
{
while (add.size() != add_to.size())
{
auto tempBigger = add.back();
add.pop_back();
resultFloat.push_back(tempBigger);
}
}
}

// now they are equal and have a form of 120(3921) 595

for (int32_t i = add_to.size() - 1; i >= 0; --i)
{
int32_t add_toDigit = add_to[i] - '0';
int32_t addDigit = add[i] - '0';

if (add_toDigit + addDigit >= 10)
{
resultFloat.append(std::to_string((add_toDigit + addDigit) - 10));
// we have a remainder
if (i == 0 || !carryReminder(add_to, i - 1))
reminderReturn = 1;
}
else
{
resultFloat.append(std::to_string(add_toDigit + addDigit));
}
}

std::reverse(resultFloat.begin(), resultFloat.end());

return std::make_pair(resultFloat, reminderReturn);

}


std::ostream & operator<<(std::ostream & os, const BigNumber & bn)
{
os << bn.fullPart << "." << bn.floatPart;
return os;
}

std::istream & operator>>(std::istream & is, BigNumber & bn)
{
std::string temp;
std::getline(is, temp, ' ');

auto fullPartTemp = std::string(temp.begin(), std::find(temp.begin(), temp.end(), '.'));
auto floatPartTemp = std::string(std::find(temp.begin(), temp.end(), '.') + 1, temp.end());

bn.floatPart = floatPartTemp;
bn.fullPart = fullPartTemp;

return is;
}

void BigNumber::operator+=(BigNumber & bn)
{
auto pair = addFloatPart(bn.floatPart, floatPart);

floatPart = pair.first;

if (pair.second > 0)
addFullPart(std::to_string(std::stoi(bn.fullPart) + 1), fullPart);
else
addFullPart(bn.fullPart, fullPart);
}

最佳答案

我建议你先用getline阅读一行。然后你可以做一个istringstream并使用你的 >>在那上面。具体来说,您可以添加 #include <sstream>并更改 main功能如下:

int main()
{
BigNumber bn, bntemp;

std::string temp;
std::getline(std::cin, temp);
std::istringstream ln(temp);
while (ln.good()) {
ln >> bntemp;
bn += bntemp;
}

std::cout << bn << std::endl;
return 0;
}

关于c++ - 有没有办法在遇到 `\n` 时停止 std::cin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42237747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com