用c++编程完成编写一个完整的程序函数funstring检查这个字符串是否为闰年是返回1不是返回0
#include <iostream>
#include <string>
using namespace std;
int fun(string str) {
int year = stoi(str);
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
return 1;
}
return 0;
}
int main() {
string str;
cout << "请输入一个年份:";
cin >> str;
if (fun(str) == 1) {
cout << str << "年是闰年" << endl;
} else {
cout << str << "年不是闰年" << endl;
}
return 0;
}
注意事项:
- 需要头文件
<string>和<iostream>。 - 函数
fun(string)中,需要将字符串转换为整数,使用stoi()函数。 - 判断闰年的公式:能被4整除但不能被100整除,或者能被400整除
原文地址: https://www.cveoy.top/t/topic/hfdm 著作权归作者所有。请勿转载和采集!