一、利息计算器
#include <iostream.h>
void main()
{
int a;
double b, c, d, f;
cout << "欢迎使用利息计算器!"<<"\n";
cout << "请输入存款金额:";
cin >> b;
cout << "======存款期限======" << "\n";
cout << "1. 3个月 " << "\n";
cout << "2. 6个月" << "\n";
cout << "3. 一年 " << "\n";
cout << "4. 二年" << "\n";
cout << "5. 三年" << "\n";
cout << "6. 五年" << "\n";
cout << "请输入存款期限的代号:";
cin >> a;
if (a>=1 && a <=6)
{
switch(a)
{
case 1:
c = 0.25;
d = 0.031;
break;
case 2:
c = 0.5;
d = 0.033;
break;
case 3:
c = 1;
d = 0.035;
break;
case 4:
c = 2;
d = 0.044;
break;
case 5:
c = 3;
d = 0.05;
break;
case 6:
c = 5;
d = 0.055;
break;
}
f = b * c * d;
cout << "到期利息为:" << f << "元,本息合计共"<< f + b <<"元。"<<"\n";
}
else
cout << "选择存款类型错误!"<<"\n";
cout << "感谢您的使用,欢迎下次光临!"<<"\n";
}
运行结果:
二、多分数段函数求值
#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;
if(x>=6 && x<10)
y=sqrt(x+1);
if(x>=10)
y=1/(x+1);
cout <<"输出y的值:"<<y<<endl;
}
return 0;
}
#include <iostream.h>
void main()
{
int year,month;
cout<<"请输入年份和月份:"<<endl;
cin>>year>>month;
if(month==2)
{
if(year%4==0&&year%100!=0||year%400==0)
cout<<month<<"月的天数为29天"<<endl;
else
cout<<month<<"月的天数为28天"<<endl;
}
else
if(month==4||month==6||month==9||month==11)
cout<<month<<"月的天数为30天"<<endl;
else
cout<<month<<"月的天数为31天"<<endl;
}
运行结果