std::reverse
[这是我目前拥有的代码,但它始终输出0,我试图让它输出输入的反面,包括负数,例如-123425将为524321-:
#include<iostream>
using namespace std;
int main() {
int number;
bool negative;
cout << "Enter an integer: ";
cin >> number;
while (number != 0) {
number % 10;
number /= 10;
}
if (number < 0) {
negative = true;
number = -number;
cout << number << "-";
}
else {
negative = false;
}
cout << number << endl;
return EXIT_SUCCESS;
}
<< > 您有std::string
并用std::reverse
反转其内容会容易得多。std::reverse
#include <algorithm> // reverse
#include <cstdlib> // EXIT_SUCCESS
#include <iostream> // cin, cout, endl
#include <string> // string
using namespace std;
int main()
{
cout << "Enter an integer: ";
string number;
cin >> number;
reverse(number.begin(), number.end());
cout << number << endl;
return EXIT_SUCCESS;
}
的最后一行将指出0,因为数字必须为0才能退出while循环。cout << number << endl;
您有#include <algorithm> // reverse
#include <cstdlib> // EXIT_SUCCESS
#include <iostream> // cin, cout, endl
#include <string> // string
using namespace std;
int main()
{
cout << "Enter an integer: ";
string number;
cin >> number;
reverse(number.begin(), number.end());
cout << number << endl;
return EXIT_SUCCESS;
}
的最后一行将指出0,因为数字必须为0才能退出while循环。