【项目1:分段函数求值】从键盘输入x的值(要求为实型),根据以下公式计算并输出x和y 的值。
要求采用两种解法完成,解法1用if-else语句,解法2主体用switch语句(注意到分段的关键点处,x的值均是偶数,这是可以利用的条件)。
提示:在这个公式中,x<0是非法的输入,程序中应该做出处理。
测试要求:运行至少5次程序,测试程序在所有可能的执行流程中,是否存在错误的处理。
下面是参考的报告格式。
上机内容:用if-else语句和switch语句求解分段函数
上机目的:学会分支结构程序设计
/* 作者:littlebeat
时间:2013.8.1
*/
//if-else形式的解法
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
float x,y;
cout<<"Please input x=";
while(cin>>x){
if(x>=10) y=1/(1+x);
else
if(x>=6) y=sqrt(x+1);//开方函数。
else
if(x>=2) y=x*x+1;
else
if(x>=0) y=x;
else
{cout<<"x为非法值!"<<endl;return 0;}//添加return 0;使得执行完该语句后不在继续向后执行。
cout<<"y="<<y<<endl;
}
return 0;
}
//switch语句形式的解法
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
float x,y;
cout<<"Input x=";
while(cin>>x){
if(x<0){cout<<"x是非法的!";return 0;}
int n=int(x/2);
switch (n){
case 0: y=x;cout<<"y="<<y<<endl;break;
case 1: y=x*x+1;cout<<"y="<<y<<endl;break;
case 2: y=x*x+1;cout<<"y="<<y<<endl;break;
case 3: y=sqrt(1+x);cout<<"y="<<y<<endl;break;
case 4: y=sqrt(1+x);cout<<"y="<<y<<endl;break;
default: y=1/(1+x);cout<<"y="<<y<<endl;break;
}
}
return 0;
}
【项目2:个人所得税计算器】编写选择结构程序,输入个人月收入总额,计算出他本月应缴税款和税后收入
(1)用if语句的嵌套完成程序设计;
(2)用switch语句完成程序设计;!!!!这种方式没有想出来。
//作者:littlebeat
//时间:2013.8.1
#include <iostream>
using namespace std;
int main( )
{
double dSalary,dTax=0,dNetIncome=0;
cout<<"请输入您本月的收入总额(元):";
cin>>dSalary;
// 下面求解应缴个人所和税dTax和税后收入dNetIncome
if(dSalary<=3500){dTax=0;dNetIncome=dSalary;}
else{
double money=dSalary-3500;
if(money<1500){dTax=money*0.03;dNetIncome=dSalary-dTax;}
else
if(money<4500){dTax=money*0.1-105;dNetIncome=dSalary-dTax;}
else
if(money<9000){dTax=money*0.2-555;dNetIncome=dSalary-dTax;}
else
if(money<35000){dTax=money*0.25-1005;dNetIncome=dSalary-dTax;}
else
if(money<55000){dTax=money*0.3-2755;dNetIncome=dSalary-dTax;}
else
if(money<80000){dTax=money*0.35-5505;dNetIncome=dSalary-dTax;}
else
{dTax=money*0.45-13505;dNetIncome=dSalary-dTax;}
}
cout<<"您本月应缴个人所和税 "<<dTax<<" 元,税后收入是 "<<dNetIncome<<" 元。\n";
cout<<"依法纳税,共享繁荣。谢谢使用!\n";
return 0;
}
//非常巧妙的解法switch
#include <iostream>
using namespace std;
int main( )
{
double m=0,s=0,f=0; //m为总收入,s为需要缴税的部分,f为税额
int t=0; //t用做判断
cout<<"你丫的一个月挣多少银子???"<<'\n';
cin>>m;
if (m<=3500)
cout<<"你不需要缴税,谢谢使用,请努力工作争取加工资!";
else
{
s=m-3500;
t=(s>=1500)+(s>=4500)+(s>=9000)+(s>=35000)+(s>=55000)+(s>=80000); //这是亮点,条件表达式为真,其值为1,t表示符合条件的个数
switch(t)
{
case 6:f=s*0.45-13505;break;
case 5:f=s*0.35-5505;break;
case 4:f=s*0.30-2755;break;
case 3:f=s*0.25-1005;break;
case 2:f=s*0.20-555;break;
case 1:f=s*0.10-105;break;
case 0:f=s*0.03;break;
}
cout<<"您的税后收入为: "<<m-f<<'\n';
cout<<'\n';
cout<<"* * * * * * * * * * *"<<'\n';
cout<<"* 爱国护税,人人有责 *"<<'\n';
cout<<"* * * * * * * * * * *"<<'\n';
}
return 0;
}
【项目3:利息计算器】输入存款金额并选择存款种类,计算出利息(不计利息税)和本息合计。要求使用switch语句,根据选择的存款种类,确定利率和存期后计算。
提示:如果是活期存款,需要补充输入天数。利息=金额×年利率×存期(单位:年,活期一年按360天计)。利率使用2011年7月7日公布的最新年利率:活期 0.50%,3个月 3.10%,6个月 3.30%,一年 3.50%,二年 4.40%,三年 5.00%,五年 5.50%。//作者:littlebeat
//时间:2013.8.1
#include <iostream>
using namespace std;
int main()
{
cout<<"欢迎使用利息计算器!"<<endl;
cout<<"请输入存款金额:";
double money,interest;
cin>>money;
cout<<"=====存款期限====="<<endl;
cout<<"1. 活期存款"<<endl;
cout<<"2. 3个月"<<endl;
cout<<"3. 6个月"<<endl;
cout<<"4. 一年"<<endl;
cout<<"5. 二年"<<endl;
cout<<"6. 三年"<<endl;
cout<<"7. 五年"<<endl;
int order;
cout<<"请输入存款期限的代号:";
cin>>order;
switch(order){
case 1: int day;cout<<"请输入存款天数:";cin>>day;interest=money*0.005*day/360;break;
case 2:interest=money*0.031*3/12;break;
case 3:interest=money*0.033*6/12;break;
case 4:interest=money*0.035;break;
case 5:interest=money*0.044*2;break;
case 6:interest=money*0.05*3;break;
case 7:interest=money*0.055*5;break;
default: cout<<"选择存款类型错误!"<<endl<<"感谢您的使用,欢迎下次光临!"<<endl;return 0;
}
cout<<"到期利息为:"<<interest<<"元,"<<"本息合计:"<<money+interest<<"元"<<endl;
cout<<"感谢您的使用,欢迎下次光临!"<<endl;
return 0;
}
【项目4:本月有几天?】输入年、月,输出本月有多少天。合理选择分支语句完成设计任务。
输入样例1:2004 2输出结果1:本月29天
输入样例2:2010 4
输出结果2:本月30天
//作者:littlebeat
//时间:2013.8.1
#include <iostream>
using namespace std;
int main()
{
int year,month;
cout<<"请输入年份和月份:";
cin>>year>>month;
switch(month){
case 1:cout<<"本月31天。"<<endl;
case 2:if(year%4!=0)
cout<<"本月28天。"<<endl;
else
if(year%100!=0)cout<<"本月29天"<<endl;
else
if(year%400==0)cout<<"本月29天。"<<endl;
else
cout<<"本月28天"<<endl;
break;
case 3:cout<<"本月31天。"<<endl;
case 4:cout<<"本月30天。"<<endl;
case 5:cout<<"本月31天。"<<endl;
case 6:cout<<"本月30天。"<<endl;
case 7:cout<<"本月31天。"<<endl;
case 8:cout<<"本月31天。"<<endl;
case 9:cout<<"本月30天。"<<endl;
case 10:cout<<"本月31天。"<<endl;
case 11:cout<<"本月30天。"<<endl;
case 12:cout<<"本月31天。"<<endl;
}
return 0;
}