第二章 分支控制

/实验2-7 比较大小/

#include<stdio.h>
void main()
{
	int a,b,c,t;
	printf("请输入3个整数:");
	scanf("%d%d%d",&a,&b,&c);
	if(b>c)
	{
		t=b;b=c;c=t;
	}
	if(a>b)
	{
		t=a;a=b;b=t;
	}
	if(a>c)
	{
		t=a;a=c;c=t;
	}
	printf("从小到大输出顺序为:%d<%d<%d\n",a,b,c);
}

/实验2-8 高速公路超速罚款/

/*实验2-8  高速公路超速罚款*/
#include<stdio.h>
int main()
{
	int a,b;
	double e;
	scanf("%d%d",&a,&b);
	e=(double)(a-b)*100/b;
	if(e<10)  printf("正常车速");
	else if(e<50)  printf("罚款200元");
	else  printf("吊销驾驶证");
	return 0;
}

/实验2-9 分段计算居民水费/

/*实验2-9  分段计算居民水费*/
#include<stdio.h>
int main()
{
	int x;
	double y;
	scanf("%d",&x);
	if(x<=0)  y=0;
	else if(x>0&&x<=15)  y=4*x/3;
	else if(x>15)  y=2.5*x-17.5;
	printf("%f\n",y);
	return 0;
}

/实验2-10 计算个人所得税/

/*实验2-10  计算个人所得税*/
#include<stdio.h>
int main()
{
	double s,r,t;
	scanf("%lf",&s);
	if(s<=1600)  t=0;
	else if(s>1600&&s<=2500)  t=(s-1600)*0.05;
    else if(s>2500&&s<=3500)  t=(s-1600)*0.10;
    else if(s>3500&&s<=4500)  t=(s-1600)*0.15;
    else if(s>4500)  t=(s-1600)*0.20;
	printf("%.2f\n",t);
	return 0;
}

/实验2-11 计算工资/

/*实验2-11 计算工资*/
#include<stdio.h>
int main()
{
	double salary;
	int year,s,time;
	scanf("%d%d",&time,&year);
		if(year>=5)  s=50;
		else   s=30;
		if(time<=40)  
			salary=s*time;
		else   
			salary=40*s+(time-40)*1.5*s;
		printf("salary=%.1f\n",salary);
		return 0;
}

/实验2-12 计算火车运行时间/

/*实验2-12  计算火车运行时间*/
#include<stdio.h>
int main()
{
	int m,n,a,b,c,d,x,y;
	scanf("%d%d",&m,&n);
	a=m/100;
	b=m%100;
    c=n/100;
	d=n%100;
	if(b<=d)
	{x=c-a;
	 y=d-b;
	}
	else
	{x=c-a-1;
	 y=60+d-b;
	}
	printf("traval time is %d hour(s) %d minute(s)\n",x,y);
	return 0;
}

/实验2-13 计算天数/

/*实验2-13 计算天数*/
#include<stdio.h>
int main()
{
	int y,m,d,leap;
    scanf("%d%d%d",&y,&m,&d);
	if((y%4==0&&y%100!=0)||(y%400==0))
		leap=1;
	else 
		leap=0;
	switch(m)
	{
	case 12:d+=30;
	case 11:d+=31;
	case 10:d+=30;
	case 9:d+=31;
	case 8:d+=31;
	case 7:d+=30;
	case 6:d+=31;
	case 5:d+=30;
    case 4:d+=31;
	case 3:d+=28+leap;
	case 2:d+=31;
	printf("this is the %dth of %d\n",d,y);
	}
	return 0;
}

/2-14简单计算器/

/*2-14简单计算器*/
#include<stdio.h>
int main()
{
	int a,b,result;
	char x;
	scanf("%d%c%d",&a,&x,&b);
	switch(x)
	{
	case'+':printf("result=%d\n",a+b);break;
	case'-':printf("result=%d\n",a-b);break;
	case'*':printf("result=%d\n",a*b);break;
	case'/':printf("result=%d\n",a/b);break;
    case'%':printf("result=%d\n",a%b);break;
	default:printf("illegal operator\n");
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值