本月还有几天
# include <iostream>
using namespace std;
int main()
{
int year,month,days;
count<<"请输入年份,月份";
cin>>year>>month;
if(year%4==0&&year%100!=0||year%400==0)
{if(month%2==0&&month!=2)
days=30;
else if (month%2!=0)
days=31;54
else(month==2)
days=29;
}
else
{
if(month%2==0&&month!=2)
days=30;
else if (month%2!=0)
days=31;
else (month==2)
days=28;
}
count<<year<<"年"<<month<<"月共有"<<days<<"天"<<endl
return 0;
}
分段
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int x;
double y;
cin>>x;
if(x<2) y=x;
else if(x<6) y=x*x+1;
else if(x<10) y=sqrt(x+1);
else y=1.0/(x+1);
cout<<y<<endl;
return 0;
}
本文提供了两个使用C++编写的程序实例:一是判断输入月份的天数,考虑到闰年因素;二是实现了一个分段函数计算器,根据不同输入区间计算输出值。通过这两个案例,读者可以学习到条件判断和基本数学函数的应用。
871





