2021.3.10
1.用函数实现分段函数的改写
#include<stdio.h>
void Z(int x);
int main()
{
int x;
scanf("%d",&x);
Z(x);
return 0;
}
void Z(int x)
{
int y;
if(x<0) y=-1;
else if(x=0) y=0;
else y=1;
printf("x=%d,y=%d\n",x,y);
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/dde5b1c81517f9a8a67b7caa1ced7e61.jpeg#pic_center)
2.用函数实现居民水费
#include<stdio.h>
float shuifei(float a);
int main()
{
float x,y;
scanf("%f",&x);
y=shuifei(x);
printf("当月用量为%f 该用户应支付的水费是%f元\n",x,y);
return 0;
}
float shuifei(float a)
{
float z;
if(a<=0)
z=0;
else if(a<=15)
z=4*a/3;
else
z=2.5*a-17.5;
return z;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9f549786b836db52017829e9546204dc.jpeg#pic_center)
3.函数改写百分制改五级制
#include<stdio.h>
int grade(int a);
int main()
{
int fenshu;
scanf("%d",&fenshu);
if(fenshu>100||fenshu<0)
printf("error\n");
grade (fenshu);
return 0;
}
int grade(int a)
{
int i;
char grade;
i=a/10;
switch(i)
{
case 10:
case 9:grade='A';break;
case 8:grade='B';break;
case 7:grade='C';break;
case 6:grade='D';break;
default:grade='E';break;
}
printf("成绩为%c\n",grade);
return a;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ae0bd6a7f1cfadfd9c97d9f7a6398ccf.jpeg#pic_center)
4.用函数实现计算天数
#include<stdio.h>
int A(int year,int month,int day);
int main()
{
int year,month,day;
scanf("%d %d %d",&year,&month,&day);
A(year,month,day);
return 0;
}
int A(int year,int month,int day)
{
int leap;
if((year%4==0&&year%100!=0)||(year%400==0))
leap=1;
else
leap=0;
switch(month)
{
case 12:day=day+30;
case 11:day=day+31;
case 10:day=day+30;
case 9:day=day+31;
case 8:day=day+31;
case 7:day=day+30;
case 6:day=day+31;
case 5:day=day+30;
case 4:day=day+31;
case 3:day=day+28+leap;
case 2:day=day+31;
}
printf("this is the %dth of %d\n",day,year);
return year,month,day;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/28f1b8b2c511adeae63c44fe07345d25.jpeg#pic_center)
5.用函数实现简易计算器
#include<stdio.h>
void Z(int opnd1,char optr,int opnd2);
int main()
{
int opnd1,opnd2;
char optr;
scanf("%d%c%d",&opnd1,&optr,&opnd2);
Z(opnd1,optr,opnd2);
return 0;
}
void Z(int opnd1,char optr,int opnd2)
{
int result;
switch(optr)
{
case'+':result=opnd1+opnd2;break;
case'-':result=opnd1-opnd2;break;
case'*':result=opnd1*opnd2;break;
case'/':result=opnd1/opnd2;break;
case'%':result=opnd1%opnd2;break;
}
printf("%d%c%d=%d\n",opnd1,optr,opnd2,result);
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/7e4b991032279baca86a19d68cd182ae.jpeg#pic_center)
6.用函数实现三个数排序从大到小
#include<stdio.h>
void Z(int a,int b,int c);
int main()
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
Z(a,b,c);
return 0;
}
void Z(int a,int b,int c)
{
int t;
if(a<b)
{
t=a;
a=b;
b=t;
}
if(a<c)
{
t=a;
a=c;
c=t;
}
if(b<c)
{
t=b;
b=c;
c=t;
}
printf("%d %d %d\n",a,b,c);
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6565c9d39d330500b35a8bbef4a02387.jpeg#pic_center)
7.用switch实现数字与星期数的转化
#include<stdio.h>
int week(int x);
int main()
{
int x;
scanf("%d",&x);
week(x);
return 0;
}
int week(int x)
{
switch(x)
{
case 1:printf("Monday");break;
case 2:printf("Tuesday");break;
case 3:printf("Wednesday");break;
case 4:printf("Thursday");break;
case 5:printf("Friday");break;
case 6:printf("Saturday");break;
case 7:printf("Sunday");break;
}
printf("\n");
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b893a2d18f2db57aac210a66ec4150a6.jpeg#pic_center)