c++作业3

本文提供了两个使用C++编写的程序实例:一是判断输入月份的天数,考虑到闰年因素;二是实现了一个分段函数计算器,根据不同输入区间计算输出值。通过这两个案例,读者可以学习到条件判断和基本数学函数的应用。
本月还有几天
# 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;  
}  


评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值