一:求解某月天数
#include<iostream>
using namespace std;
int main()
{
int y,m,d;
cout<<"日历"<<endl;
cout<<"请输入年份";
cin>>y;
cout<<"请输入月份";
cin>>m;
switch(m)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: d=31;break;
case 4:
case 6:
case 9:
case 11: d=30;break;
case 2:
if( y%400==0 || (y%4==0 && y%100!=0))
d=29;
else d=28;break;
default : cout<<"Error"<<endl;
}
cout<<"这个月有"<<d<<"天"<<endl;
return 0;
}
<span style="line-height: 18px; font-family: Consolas, 'Courier New', Courier, mono, serif; background-color: inherit;">
</span>
<span style="background-color: inherit;">二:利息计算</span>
<span style="background-color: inherit;"></span><pre name="code" class="cpp">#include<iostream>
using namespace std;
int main()
{
cout<<"欢迎使用"<<endl;
cout<<"请输入存款金额:\0"<<" ";
int money,number;
double interest,rate,term,sum;
cin>>money;
cout<<"存款期限"<<endl;
cout<<"1. 3个月\n2. 6个月\n3. 一年\n4. 两年\n5. 三年\n6. 五年"<<endl;
cout<<"请输入存款期限(1-6):"<<endl;
cin>>number;
switch(number)
{
case 1: term=0.25;rate=0.031;break;
case 2: term=0.50;rate=0.033;break;
case 3: term=1.00;rate=0.035;break;
case 4: term=2.00;rate=0.044;break;
case 5: term=3.00;rate=0.050;break;
case 6: term=5.00;rate=0.055;break;
default : cout<<"无此选项"<<endl;
}
interest=money*term*rate;
sum=money+interest;
cout<<"到期利息为:"<<interest<<","<<"本息合计共:"<<sum<<"元"<<endl;
cout<<"非常感谢您的使用"<<endl;
return 0;
}
<span style="background-color: inherit;">三:多分段函数求值</span>
<span style="background-color: inherit;">
</span>
<span style="line-height: 18px; font-family: Consolas, 'Courier New', Courier, mono, serif; background-color: inherit;"></span><pre name="code" class="cpp">#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 y=1/(x+1);
cout<<"函数y的值为:"<<y<<endl;
return 0;
}