一、项目2:本月有几天?
#include<iostream>
using namespace std;
int main()
{
int year,month,day;
cout<<"请输入年份和月份";
cin>>year>>month;
if(month==2)
{
if((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
day=29;
else
day=28;
}
else
{
if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
day=31;
else
{
if(month==4 || month==6 || month==9 || month==11)
day=30;
}
}
cout<<"本月有"<<day<<"天"<<endl;
return 0;
}
二、项目3:定期存款利息计算器
#include<iostream>
using namespace std;
int main()
{
int type;
double money, time, rate, lixi;
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 >> type;
if (type>=1 && type <=6)
{
switch(type)
{
case 1:
time = 0.25;
rate = 0.031;
break;
case 2:
time = 0.5;
rate = 0.033;
break;
case 3:
time = 1;
rate = 0.035;
break;
case 4:
time = 2;
rate = 0.044;
break;
case 5:
time = 3;
rate = 0.05;
break;
case 6:
time = 5;
rate = 0.055;
break;
}
lixi = money * time * rate;
cout << "到期利息为:" << lixi << "元,本息合计共"<< lixi + money <<"元。"<<endl;
}
else
cout << "选择存款类型错误!"<<endl;
cout << "感谢您的使用,欢迎下次光临!"<<endl;
return 0;
}
三、项目4:多分数段函数求值
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int x,y;
cout<<"输入x=";
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;
}