如何在 c++ 中正确从正整数中获取偶数和奇数?
How to get even & odd numbers correctly from a positive integer in c++?
我是C++新手...我无法从正整数中获取偶数和奇数。但由于某种原因,它无法正确显示它。因此,在我的 if-else 语句中,我使用mod
运算符,并使用偶数even = even * 10 + remainder;
,而我试图做的奇数是:odd = odd * 10 + remainder;
..我收到错误
二进制表达式的操作数无效。
所以,任何人都可以通过从正整数中获取偶数和奇数来帮助我解决问题。谢谢。
这是我的代码:
#include <iostream>
#include <fstream> // for file stream.
using namespace std;
int main() {
// Variables
int x, reversedNumber, remainder;
// Creating String variables and setting them to empty string.
string even = "", odd = "";
// Creating a file.
ofstream out;
out.open("outDataFile.txt");
// creating a character variable
// for the user input if they want to use the program again.
char ch;
do {
// Even number.
even = "";
// Odd number.
odd = "";
// Reversed number
reversedNumber = 0;
// Prompt the user to enter a positive integer.
cout << "nEnter a positive integer and press <Enter> ";
// Validate user input.
// if (cin >> x && x < 0) {
//
// } else {
// cout << "Invalid entry, Try again." << endl;
// cin.clear();
// while (cin.get() != 'n');
// }
// Display Results to the screen
cout << "the original number is " << x << "n";
// Display results in the text file.
out << "the original number is " << x << "n";
// Display number reversed.
cout << "the number reversed ";
// Display number reversed in text file.
out << "the number reversed ";
//Reversing the integer.
while (x != 0) {
remainder = x % 10;
reversedNumber = reversedNumber * 10 + remainder;
// Display on screen
cout << remainder << " ";
// Display in text file.
out << remainder << " ";
x /= 10;
}
// Display the results on screen and in the text file.
cout << "n";
out << "n";
// Reading the reverse numbers result.
while (reversedNumber != 0) {
remainder = reversedNumber % 10;
// Checking if the number is even or odd.
if (remainder % 2 == 0) {
// even = even * 10 + remainder;
} else {
// odd = odd * 10 + remainder;
}
reversedNumber /= 10;
}
//Displaying the even numbers.
if (even != "") {
cout << "the even digits are " << even << "n";
out << "the even digits are " << even << "n";
}
// If it is not even then display..
else {
cout << "There are no even digits n";
out << "There are no even digits n";
}
//Display the odd results.
if (odd != "") {
cout << "the odd digits are " << odd << "n";
out << "the odd digits are " << odd << "n";
}
// If its not odd then display.
else {
cout << "There are no odd digits n";
out << "There are no odd digits n";
}
// just a divider to divide the results inside text file.
out << "----------------- n";
// Prompt the user if they want to use the program again.
cout << "nDo you like to continue/repeat? (Y/N):";
// get the input from user.
cin >> ch;
if ((ch == 'Y') || (ch == 'y')) {
} else {
cout << "nGoodbye!" << endl;
}
} while (ch == 'y' || ch == 'Y');
// close the text file.
out.close();
return 0;
}
在将数字附加到字符串之前将其转换为字符串:
while (reversedNumber != 0) {
remainder = reversedNumber % 10;
// Checking if the number is even or odd.
if (remainder % 2 == 0) {
even += std::to_string(remainder);
} else {
odd += std::to_string(remainder);
}
reversedNumber /= 10;
}
如果您对使用std::to_string
感到不舒服,那么您可以将even
和odd
作为整数,只需执行以下操作:
int even = 0;
int odd = 0;
...
while (reversedNumber != 0) {
remainder = reversedNumber % 10;
// Checking if the number is even or odd.
if (remainder % 2 == 0) {
even *= 10 + remainder;
} else {
odd *= 10 + remainder;
}
reversedNumber /= 10;
}
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 使用指针从C++中的数组中获取最大值
- 如何获取std::result_of函数的返回类型
- 如何在openssl-ecc中获取十六进制格式的私钥
- 使用Unreal C++获取VR耳机的世界位置/方向
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 从C字符串中获取奇怪的字符串长度
- 为什么我的for循环不能正确获取argv
- 从python中调用C++函数并获取返回值
- 如何获取一个数字的前3位
- 获取字符串的长度并将其分配给数组
- 无法获取菜单选择以运行函数.C++
- 数组长度,为什么从命令行获取时不能使用它?
- Boost Spirit,获取迭代器内部语义动作
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- 具有默认值的引用获取函数
- xmake总是报告:错误:无法获取cxx的程序,为什么
- 在c++中获取大文件的大小