项目2:本月有几天?
#include<iostream>
using namespace std;
int main( )
{
int year,month,day;
cout<<"请输入年份和月份(中间用空格隔开):";
cin>>year>>month;
switch(month)
{
case 1: day=31;break;
case 2:if((year%4==0)&&(year%100!=0)||(year%400==0))
{
day=29;break;
}
else
{
day=28;break;
}
case 3: day=31;break;
case 4: day=30;break;
case 5: day=31;break;
case 6: day=30;break;
case 7: day=31;break;
case 8: day=31;break;
case 9: day=30;break;
case 10: day=31;break;
case 11: day=30;break;
case 12: day=31;break;
}
if (month>=1&&month<=12)
cout<<"本月有"<<day<<"天"<<endl;
else
cout<<"输入月份有错!"<<endl;
return 0;
}
项目3:定期存款利息计算器
#include<iostream>
using namespace std;
int main( )
{
double Money,Interest,Sum;
int x;
cout<<"欢迎使用利息计算器!"<<endl; cout<<"请输入存款金额:";
cin>>Money;
cout<<"======存款期限======"<<endl;
cout<<"1. 3个月"<<endl;
cout<<"2. 6个月"<<endl;
cout<<"3. 一年"<<endl;
cout<<"4. 两年"<<endl;
cout<<"5. 三年"<<endl;
cout<<"6. 五年"<<endl;
cout<<"请输入存款期限的代号:";
cin>>x;
switch(x)
{
case 1:Interest=Money*0.031*0.25;break;
case 2:Interest=Money*0.033*0.5;break;
case 3:Interest=Money*0.035*1;break;
case 4:Interest=Money*0.044*2;break;
case 5:Interest=Money*0.05*3;break;
case 6:Interest=Money*0.055*5;break;
}
Sum=Money+Interest;
cout<<"到期利息为:"<<Interest<<"元,本息合计共"<<Sum<<"元。"<<endl;
cout<<"感谢您的使用,欢迎下次光临!"<<endl;
return 0;
}
项目4:多分数段函数求值
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double x,y;
cout <<"请输入x的值:";
cin >>x;
if(x<2) y=x;
else if(x>=2&&x<6) y=x*x+1;
else if(x>=6&&x<10) y=sqrt(x+1);
else y=1/(x+1);
cout <<"y="<<y<<endl;
return 0;
}