Điều tôi đang bối rối là về hàm isNumPalindrome(). Nó trả về một giá trị boolean của true hoặc false. Làm thế nào tôi có thể sử dụng để tôi có thể hiển thị nếu nó là một palindrome hay không. Ví dụ: if (isNumPalindrome == true) cout << "Your number is a palindrome"; else cout << "your number is not a palindrome.";
Tôi vừa học về các hàm C++; thế nào tôi có thể sử dụng nếu các câu lệnh trên các giá trị trả về hàm?
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
int askNumber();
bool isNumPalindrome();
int num, pwr;
int main()
{
askNumber();
return 0;
}
bool isNumPalindrome()
{
int pwr = 0;
if (num < 10)
return true;
else
{
while (num/static_cast<int>(pow(10.0, pwr)) >=10)
pwr++;
while (num >=10)
{
int tenTopwr = static_cast<int>(pow(10.0, pwr));
if ((num/tenTopwr) != (num% 10))
return false;
else
{
num = num % tenTopwr;
num = num/10;
pwr = pwr-2;
}
}
return true;
}
}
int askNumber()
{
cout << "Enter an integer in order to determine if it is a palindrome: " ;
cin >> num;
cout << endl;
if(isNumPalindrome(num))
{
cout << "It is a palindrome." ;
cout << endl;
}
else
{
cout << "It is not a palindrome." ;
cout << endl;
}
return num;
}
Wow, cho ai đó chỉ tìm hiểu về chức năng bạn đã viết khá đẹp C++. Làm tốt. –
Haha cảm ơn. Tôi chỉ có lớp học này vì vậy tôi dành nhiều ngày chỉ thực hành mã -_ -... – Sagistic
Đã chỉnh sửa: Hoạt động ngay bây giờ, nhưng vẫn phải sửa phần biến toàn cục. – Sagistic