(一)本月有几天?
#include<iostream>
using namespace std;
int main()
{
int year,month,days;
cout<<"请输入年份";
cin>>year;
cout<<"请输入月份";
cin>>month;
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: days=31;break;
case 4:
case 6:
case 9:
case 11: days=30;break;
case 2:
if( (year%4==0 && year%100!=0) || year%400==0)
days=29;
else days=28;break;
}
cout<<"该年该月的天数为:"<<days<<endl;
return 0;
}
(二)定期存款利息计算器
#include<iostream>
using namespace std;
int main()
{
int m,n;
double r,t,s,i;
cout<<"欢迎使用利息计算器!"<<endl;
cout<<"请输入存款金额:";
cin>>m;
cout<<"======存款期限======"<<endl;
cout<<"1、3个月\n2、6个月\n3、一年\n4、二年\n5、三年\n6、五年\n"<<endl;
cout<<"请输入存款期限的代号:";
cin>>n;
switch(n)
{
case 1:t=0.25;r=0.0310;break;
case 2:t=0.50;r=0.0330;break;
case 3:t=1.00;r=0.0350;break;
case 4:t=2.00;r=0.0440;break;
case 5:t=3.00;r=0.0500;break;
case 6:t=5.00;r=0.0550;break;
}
i=m*t*r;
s=m+i;
cout<<"到期利息为:"<<i<<"本息合计共"<<s<<endl;
cout<<"感谢您的使用,欢迎下次光临!"<<endl;
return 0;
}
(三) 多分数段函数求值
#include <iostream>
#include <cmath>
using namespace std;
int main( )
{
double x,y;
cout<<"输入x的值:"<<endl;
cin>>x;
if(x<2)
y=x;
else if(x<6)
y=x*x+1;
else if(x<10)
y=sqrt(x+1);
else if(x>=10)
y=1/(x+1);
cout<<"y的值是"<<y<<endl;
return 0;
}