C++作业3

项目2:本月有几天?

自选if语句的嵌套或/和switch语句完成程序设计

编程序,输入年份和月份,输出本月有多少天。合理选择分支语句完成设计任务。
样例输入1:2004 2
输出结果1:本月29天
样例输入2:2010 4
输出结果2:本月30天

#include <iostream>

using namespace std;

int main()
{
   int year,month;
   cout<<"请输入年份"<<endl;
   cin >>year ;
   cout<<"请输入月份"<<endl;
   cin >>month ;
   if((year%4==0&&year%100!=0)||(year%400==0))
   {
       if(month==2)
        cout<<"本月29天"<<endl;
    if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
        cout<<"本月31天"<<endl;
    if(month==4||month==6||month==9||month==11)
        cout<<"本月30天"<<endl;

   }
   else
    {
        if (month==2)
    cout<<"本月28天"<<endl;
    if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
        cout<<"本月31天"<<endl;
    if(month==4||month==6||month==9||month==11)
        cout<<"本月30天"<<endl;
   }
    return 0;
}

项目3:定期存款利息计算器

输入存款金额并选择存款种类,计算出利息(不计利息税)和本息合计。要求使用switch语句,根据选择的存款种类,确定利率和存期后计算。
提示:利息=金额×年利率×存期(单位:年,3个月为0.25年,6个月为0.5年)。
例如:1000元存6个月,利息=1000×0.033×0.5=16.5元
年利率:3个月  3.10%,6个月 3.30%,一年 3.50%,二年 4.40%,三年 5.00%,五年 5.50%。

#include <iostream>

using namespace std;

int main()
{
    double a,b,c;
    char d;
    cout<<"欢迎使用利息计算器!\n 请输入存款金额:"<<endl;
    cin>>a;
    cout<<"====存款期限==== \n 1. 3个月\n 2. 6个月\n 3. 一年\n 4. 二年\n 5. 三年\n 6. 五年"<<endl ;
    cout<<"请输入存款期限的代号:"<<endl;
    cin>>d;
    switch (d)
    {
        case '1':b=a*0.031*0.25;break ;
        case '2':b=a*0.033*0.5;break ;
        case '3':b=a*0.035*1;break;
        case '4':b=a*0.044*2;break;
        case '5':b=a*0.050*3;break;
        case '6':b=a*0.050*5;break;
        default :cout <<" error\n";
    }
    c=a+b;
    cout<<"到期利息为:"<<b<<"元,"<<"本息合计共:"<<c<<"元。"<<endl;
    cout<<"感谢您的使用,欢迎下次光临!"  <<endl;

    return 0;
}
项目4:多分数段函数求值

从键盘输入x的值(要求为实型),根据下面的公式计算并输出y的值。 

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    double x;
    cin >>x;

    if(x<2)
        cout<<"y="<<x;
    else if (2<=x&&x<6)
        cout<<"y="<<x*x+1;
    else if (6<=x&&x<10)
        cout<<"y="<<sqrt(x+1);
    else
        cout<<"y="<<1/(x+1);

    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值