我需要重写程序以使用函数isPalindrome.它需要输入一个5位整数并返回一个布尔值(如果它是回文,则为true,如果不是则为false),并且它不能包含任何cout语句.如果没有cout功能,我不知道如何做到这一点.这是我的代码:
#include <iostream>
using namespace std;
int main()
{
int number, digit1, digit2, digit3, digit4, digit5;
cout << "\nEnter a 5-digit integer: ";
cin >> number;
//Break down input number into individual digits:
digit1 = number / 10000;
digit2 = number % 10000 / 1000;
digit3 = number % 10000 / 100;
digit4 = number % 10000 / 10;
digit5 = number % 10;
if ( digit1 == digit5 && digit2 == digit4 )
cout << number <<" is a palindrome.\n";
else
cout << number << " is not a palindrome.\n";
return 0;
}
int isPalindrome ()
{
}
Run Code Online (Sandbox Code Playgroud)
这应该有助于你开始(不会破坏太多的乐趣)
int main(){
//accept integer input using cin
if(isPalindrome(input))
cout << "yaay!";
else
cout << "nooouuu!";
}
bool isPalindrome (int input)
{
//test things here
return ( digit1 == digit5 && digit2 == digit4 )
// ^ returns true if condition satisfied
}
Run Code Online (Sandbox Code Playgroud)
此外,您分离数字的方式是不正确的.它应该是:
digit1 = number/10000 % 10;
digit2 = number/1000 % 10;
digit3 = number/100 % 10;
digit4 = number/10 % 10;
digit5 = number % 10;
Run Code Online (Sandbox Code Playgroud)
当然,上面应该是一个循环.