07.01-c

来源:互联网 发布:小众手表 知乎 编辑:程序博客网 时间:2024/06/07 22:43
【项目1:分段函数求值】
从键盘输入x的值(要求为实型),根据公式1计算并输出x和y的值。 

解法(1)if语句:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
double x,y;
printf("please enter x\n");
scanf("%lf",&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 if(x>=10)
{
y=1/(x+1);
}
else
{
printf("error\n");
}
printf("x=%lfy=%lf",x,y);
return 0;
}

解法2:switch语句

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
double x, y;
int a;
printf("please enter x\n");
scanf("%lf", &x);
a=x/2;
if (a<0)
a=0;

switch(a)
{
case 0:y=x;break;
case 1:
case 2:y=x*x+1;break;
case 3:
case 4:y=sqrt(x+1);break;
default:y=1/(x+1);
}

printf("x=%lf y=%lf",x,y);
return 0;
}
遗漏了负数情况。


【项目2:油量监控】
设计一个程序,用于赛车油量的监控。该程序在赛车油量偏低(少于1/4,即0.25)时,警示车手应该注意;在油箱接近满载(不低于3/4)时,提示提手不要停车。而对于其他情况下,不提示任何信息,以免车手分心。
请设计出这个程序来,输入油量刻度(0-1之内的数,如0.21),提示相应信息,如果需要的话。
提示:下面显示了几种情况,注意对程序的完整测试。

解法:

#include <stdio.h>
#include <stdlib.h>

int main()
{
printf("enter number read from the board(0-1):");
double x;
scanf("%lf",&x);
if(x<0.25)
{
printf("pay attention, low fuel!\n");
}
else if(x >= 0.75)
{
printf("high fuel,don't worry\n");
}

return 0;
}

【项目:个人所得税计算器if语句版】
编写选择结构程序,输入个人月收入总额,计算出他本月应缴税款和税后收入(计算办法见附:关于个人所得税的有关背景知识)。
(1)用if语句的嵌套完成;
(2)可以在下面程序的基本框架基础上完成,如需其他变量自行增加

附件:附:关于个人所得税的有关背景知识
计算方法:个人所得税=(收入总额-3500)*税率-速算扣除数
从2011年9月1日起,我国个人所得税起征点基数为3500元,超出部分按以下7级计算。

序号

收入范围

税率

速算扣除数

1

超过01500

3%

0

2

超过1500元至4500元的部分

10%

105

3

超过4500元至9000元的部分

20%

555

4

超过9,000元至35,000元的部分

25%

1005

5

超过35,000元至55,000元的部分

30%

2755

6

超过55,000元至80,000元的部分

35%

5505

7

超过80,000以上

45%

13505

例如:王某月收入总额3600元,个人所得税=(3600-3500)*3%=3元;
再例:李某月收入13500元,个人所得税=(13500-3500)*25%-1005=2500-1005=1495元。
更多了解速算扣除数,请找百度百科。

解法1:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
printf("please enter your salary\n");
double dSalary,dTax,dIncome;
scanf("%lf",&dSalary);
if((dSalary-3500)>=0 && (dSalary-3500)<1500)
{
dTax=(dSalary-3500)*0.03;
dIncome=dSalary-dTax;
}
else if((dSalary-3500)>=1500 && (dSalary-3500)<4500)
{
dTax=(dSalary-3500)*0.1-105;
dIncome=dSalary-dTax;
}
else if((dSalary-3500)>=4500 && (dSalary-3500)<9000)
{
dTax=(dSalary-3500)*0.2-555;
dIncome=dSalary-dTax;
}
else if((dSalary-3500)>=9000 && (dSalary-3500)<35000)
{
dTax=(dSalary-3500)*0.25-1005;
dIncome=dSalary-dTax;
}
else if((dSalary-3500)>=35000 && (dSalary-3500)<55000)
{
dTax=(dSalary-3500)*0.3-2755;
dIncome=dSalary-dTax;
}
else if((dSalary-3500)>=55000 && (dSalary-3500)<80000)
{
dTax=(dSalary-3500)*0.35-5505;
dIncome=dSalary-dTax;
}
else if(dSalary-3500>=80000)
{
dTax=(dSalary-3500)*0.45-13505;
dIncome=dSalary-dTax;
}
else
{
printf("you are not qualified to pay tax!\n");
}
printf("dTax=%lf dIncome=%lf",dTax,dIncome);
return 0;
}

解法2:

#include <stdio.h>
#include <stdlib.h>

int main()
{
printf("please enter your salary\n");
double dSalary,dTax,dIncome;
int a,b;
scanf("%lf",&dSalary);
if(dSalary-3500<0)
{
printf("you are not qualified to pay tax\n");
}
if(dSalary-3500<9000)
{
a=(dSalary-3500)/1500;
switch(a)
{
case (0):dTax=(dSalary-3500)*0.03;
dIncome=dSalary-dTax;break;
case (1):
case (2):dTax=(dSalary-3500)*0.1-105;
dIncome=dSalary-dTax;break;
case (3):
case (4):
case (5):dTax=(dSalary-3500)*0.1-105;
dIncome=dSalary-dTax;break;
}
}
else if(dSalary-3500>=9000)
{
b=(dSalary-3500)/5000;
switch(b)
{
case (0):
case (1):
case (2):
case (3):
case (4):
case (5):
case (6):dTax=(dSalary-3500)*0.25-1005;
dIncome=dSalary-dTax;break;
case (7):
case (8):
case (9):
case (10):dTax=(dSalary-3500)*0.3-2755;
dIncome=dSalary-dTax;break;
case (11):
case (12):
case (13):
case (14):
case (15):dTax=(dSalary-3500)*0.35-5505;
dIncome=dSalary-dTax;break;
default:dTax=(dSalary-3500)*0.45-13505;
dIncome=dSalary-dTax;break;
}

}

printf("dTax=%lf dIncome=%lf",dTax,dIncome);
return 0;
}

【项目:定期存款利息计算器】
输入存款金额并选择存款种类,计算出利息(不计利息税)和本息合计。要求使用switch语句,根据选择的存款种类,确定利率和存期后计算。
提示:利息=金额×年利率×存期(单位:年,3个月为0.25年,6个月为0.5年)。
例如:1000元存6个月,利息=1000×0.033×0.5=16.5元
利率使用2011年7月7日公布的年利率:3个月  3.10%,6个月 3.30%,一年 3.50%,二年 4.40%,三年 5.00%,五年 5.50%。
程序运行图参考下面的界面:

解法:

#include <stdio.h>
#include <stdlib.h>

int main()
{
printf("welcome to use interest calculator!\n");
printf("please enter the sum of the money you want to save:");
double dMoney,dInterest,dTotal;
double dRate,dYear;
scanf("%lf",&dMoney);
int a=0;
printf("::::::deposit period::::::\n");
printf("1. demand deposit\n");
printf("2. three months\n");
printf("3. six months\n");
printf("4. one year\n");
printf("5. two years\n");
printf("6. three years\n");
printf("7. five years\n");
printf("please choose the deposit period:");
scanf("%d",&a);
switch (a)
{
case 1:
dRate=0.0035;
int iDays=0;
printf("please enter the number of days:");
scanf("%d",&iDays);
dYear=iDays/365;
break;

case 2: dRate=0.031;dYear=0.25;break;
case 3: dRate=0.033;dYear=0.5;break;
case 4: dRate=0.035;dYear=1;break;
case 5: dRate=0.044;dYear=2;break;
case 6: dRate=0.05;dYear=3;break;
case 7: dRate=0.055;dYear=5;break;
default: printf("error!please retry!\n");
}
dInterest=dMoney*dRate*dYear;
dTotal=dMoney+dInterest;
printf("interest is:%.2lf the total money is:%.2lf\n",dInterest,dTotal);
printf("thank you for your use!");

return 0;
}

【项目:构造菜单】
在应用程序中,很多都将各种功能设计了“菜单”供用户选择,本项目做些体验。
请编写程序,显示“菜单”,由用户输入代号后,输出对所选项目的简短评论。运行结果参考下图:

解法:

#include <stdio.h>
#include <stdlib.h>

int main()
{
printf(":::::MENU:::::\n");
printf("1. 山西刀削面\n");
printf("2. 兰州拉面\n");
printf("3. 西安羊肉泡馍\n");
printf("4. 烟台焖子\n");
printf("5. 西北风\n");
int a;
printf("请输入你选择的代号:");
scanf("%d",&a);
switch (a)
{
case 1:printf("山西刀削面,一份");
case 2:printf("兰州拉面,二份");
case 3:printf("西安羊肉泡馍,3份");
case 4:printf("烟台焖子,5份");
case 5:printf("西北风,无数份");

}

return 0;
}






0 0
原创粉丝点击