无法在C++中显示反转的数字
Unable to display a reversed number in C++
我在打印反向数字时遇到困难。该算法按预期工作,用于反转用户的输入,但无法正确显示:
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int rem, OriValue, InValue2 = 0, InValue = 0;//rem = remainder,InValue = User input
bool neg = false; // Boolean Variable to remember input value
//Request customer to enter a int value
cout << "Enter any decimal integer value >= 0:";
cin >> InValue;
OriValue = InValue;
if(InValue < 0)
{
neg = true;
InValue = -InValue;
cout << "Input value is negative :" << InValue <<"n";
}
else if (InValue > 0 )
cout << "Input value is positive:"<< InValue <<"n";
do
{
rem = (InValue % 10);
cout << "Remainder value:"<< rem << "n";
InValue = InValue / 10;
}
while (InValue != 0);
cout << OriValue << " in reverse is " << InValue << "n";
// Here is an example of the output:
// -123 in reverse 321
return 0;
}
我该如何解决这个问题?
这应该完成工作。检查代码中的注释
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int rem, OriValue, InValue2 = 0, InValue = 0;//rem = remainder,InValue = User input
int reverseVal = 0; // Add this variable to store the reverse number
bool neg = false; // Boolean Variable to remember input value
//Request customer to enter a int value
cout << "Enter any decimal integer value >= 0:";
cin >> InValue;
OriValue = InValue;
if(InValue < 0)
{
neg = true;
InValue = -InValue;
cout << "Input value is negative :" << InValue <<"n";
}
else if (InValue > 0 )
cout << "Input value is positive:"<< InValue <<"n";
do
{
rem = (InValue % 10);
cout << "Remainder value:"<< rem << "n";
InValue = InValue / 10;
// Add this to store
reverseVal = reverseVal*10 + rem;
}
while (InValue != 0);
cout << OriValue << " in reverse is " << reverseVal << "n";
// Here is an example of the output:
// -123 in reverse 321
return 0;
}
您没有将反转的变量存储在任何位置,而n == 0
是循环终止的时候。当您在循环结束后打印n
时,如您所见,它必须为0。解决方案是在划分n
时存储余数。
以下是实现这一目标的几个选项。
使用stringstream
:
#include <iostream>
#include <sstream>
using namespace std;
int main() {
int original_value, n;
stringstream ss;
cout << "Enter an integer >= 0:";
cin >> original_value;
n = original_value;
if (n < 0) {
n = -n;
ss << '-';
}
while (n > 0) {
ss << n % 10;
n /= 10;
}
cout << original_value << " in reverse is " << ss.str() << "n";
return 0;
}
使用reverse
:
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n = -1234;
string rev = to_string(n);
reverse(rev.begin() + (rev[0] == '-' ? 1 : 0), rev.end());
cout << rev << "n";
return 0;
}
样品运行:
Enter an integer >= 0: -1234
-1234 in reverse is -4321
如果末尾需要一个整数,则可以始终使用atoi
、istringstream
或stoi
从字符串中解析它。
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 在 txt 文件中显示前两个数字的程序
- 为什么strlen(s)与s的大小不同,为什么cout-char显示的是字符而不是数字
- 为什么 sf::Text 显示点而不是数字?
- 为什么断点显示数组的第二个值是一个大数字?额外学分工作
- 未来值公式显示疯狂巨大数字的输出
- 为什么当我在 c++ 中运行函数时,我的代码显示数字 53
- 代码以查找数组中的最大数字,但它仅将第一个数字显示为最大数字. 有人请告诉我为什么
- 无法在C++中显示反转的数字
- 如何执行此程序,将键入的数字显示为数字值的多次
- Cpp代码故障(显示意外数字)
- 如何让我的代码显示文件名不正确或文件中数字的平均值?
- 为什么C++显示错误的数字?
- C++显示两个区间之间的数字的程序检查一个数字是否可以表示为两个素数的总和
- C++编译器只显示数字,不显示正确的输出
- 输入的数字与代码显示的数字不同,在结构中使用数组,为什么?
- 如何计算和显示C++中5个数字的平均值?
- 以8位数字形式输入日期并以英文形式显示
- 为什么我的 rand() 数字显示为 0