实验 4 在分支循环结构中调用自定义函数
2013-10-17 09:57
393 查看
/* 计算并输出多个用户应支付的电费 */ #include<stdio.h> int main(void) { int m,i; //m为用户的个数 double x,y; /* 定义x为用电量,y为电费 */ double cylinder(double x); //设置自定义函数 printf("请输入m"); scanf_s("%d",&m); for(i=1;i<=m;i++){ printf("请输入电量:\n"); scanf_s("%Lf",&x); if(x<0){ //当x小于0时,提示输入错误 printf("sorry,您的输入有误"); } else { y=cylinder(x); //定义函数 printf("y=%.3f\n",y); } } return 0; } double cylinder(double x) //调用函数 { double y; if(x<=50){ //将函数结果返回上面函数中 y=0.53*x; } else{ y=50*0.53+(x-50)*0.58; } return y; }
为了倡导居民节约用电,某省电力公司执行“阶梯电价”,安装一户一表的居民用户电价分两个“阶梯”:月用电量 50 千瓦时以内的,电价为 0.53 元/千瓦时;超过 50 千瓦时的用电量,电价上调 0.05 元/千瓦时。编写程序,输入用户的月用电量(千瓦时),计算并输出该用户应支付的电费(元)
/* 计算并输出用户应支付的电费 */ #include<stdio.h> int main(void) { int i; double x,y; /* 定义x为用电量,y为电费 */ printf("请输入电量:\n"); scanf_s("%Lf",&x); if(x<0){ printf("sorry,您的输入有误"); } else if(x<=50){ //若电量小于50,则y=0.53*x y=0.53*x; } else{ y=50*0.53+(x-50)*0.58; //电量大于50后,超出部分0.58元每瓦计算 printf("y=%.3f\n",y); } return 0; }
/* 利用循环计算多个圆柱体体积 */ #include<stdio.h> int main(void) { int n,i; double r,h,volume; //定义浮点型变量,r=半径,h=高,volume=体积 double cylinder(double r,double h); //定义函数 printf("请输入n"); //循环求n个圆柱体的体积 scanf_s("%d",&n); for(i=1;i<=n;i++){ printf("请输入圆柱体的半径和高:"); scanf_s("%lf%lf",&r,&h); if((r<=0)||(h<=0)){ //当半径或高小于等于0是,提示错误 printf("sorry,您的输入有误"); } else{ volume=cylinder(r,h); //否则,调用自定义函数结果 printf("volume=%.3f\n",volume); } } return 0; } double cylinder(double r,double h) //自定义函数的调用 { double result; result = 3.14*r*r*h; return result; }
相关文章推荐
- an concreate example
- 自定义函数替代file_get_contents获取远程数据
- 图解数据结构(1)——大圈表示法、动态数组和单向链表
- ANDROID笔记:ListView和CheckBox的简单使用
- pid_t的类型定义 【转】
- 汉化
- Oracle 细粒度审计
- suse 10 无法进入系统的原因及解决方法
- 转义字符
- Protobuf编译原理
- 使用UIImageView播放动画
- ace日志多种输出方式
- 图解数据结构(2)——栈
- 实验四-在分支循环结构中调用自定义函数
- 作业 2 分支、循环结构
- 作业2
- 缺陷跟踪系统BugTracker。 以下对使用
- 实验 4 在分支循环结构中调用自定义函数 计算多个用户的电费(元)。
- 练习1
- C# 序列化 与 反序列化