多段函数:
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
double x,y;
cout<<"x=";
cin>>x;
if(x>=10)
y=1/(x+1);
else if(x>=6)
y=sqrt(x+1);
else if(x>=2)
y=x*x+1;
else
y=x;
cout<<"y="<<y;
}
一个月有多少天
#include<iostream>
using namespace std;
int main()
{
int y,m,d;
cout<<"input y,m:";
cin>>y>>m;
if(m==1||m==3||m==5||m==7||m==8||m==10||m==12)
d=31;
else if(m==4||m==6||m==9||m==11)
d=30;
else if(y%4==0&&y%100!=0||y%400==0&&m==2)
d=29;
else
d=28;
cout<<"d="<<d;
}
计算利息
#include<iostream>
using namespace std;
int main()
{
int item;
double y,r,m,d;
cout<<"欢迎使用利息计算器";
cout<<"请输入要存款的金额(元):";
cin>>d;
cout<<"=====存款期限=====";
cout<<"1.3个月\n"<<"2.6个月\n"<<"3.一年";
cout<<"4.两年\n"<<"5.三年\n"<<"6.五年";
cin>>item;
switch(item)
{
case 1:r=0.031,y=0.25; break;
case 2:r=0.033,y=0.5 ; break;
case 3:r=0.035,y=1 ; break;
case 4:r=0.044,y=2 ; break;
case 5:r=0.050,y=3 ; break;
case 6:r=0.055,y=5 ; break;
}
m=y*d*r;
cout<<"m="<<m;
cout<<"m+d="<<m+d;
}