习题 4.12 有4个圆塔,圆心分别为(2,2)、(-2,2)、(-2,-2)、(2,-2),圆半径为1,这4个塔的高度为10m,塔以外无建筑物。今输入任一点的坐标,求该点的建筑高度(塔外的高度为零)

来源:互联网 发布:淘宝网建达巧克力 编辑:程序博客网 时间:2024/06/07 02:00

C程序设计 (第四版) 谭浩强 习题4.12 个人设计

习题 4.12 有4个圆塔,圆心分别为(2,2)、(-2,2)、(-2,-2)、(2,-2),圆半径为1,这4个塔的高度为10m,塔以外无建筑物。今输入任一点的坐标,求该点的建筑高度(塔外的高度为零)。

xa)2+(yb)2=1

(a,b)

代码块

方法1:(利用条件选择结构)

#include <stdio.h>#include <math.h>int main(){    int x, y, h;    double p1, p2, p3, p4;    //输入坐标    printf("Please enter coordinate: ");    scanf("%d %d", &x, &y);    //4个圆塔的坐标方程    p1 = pow(x-2, 2) + pow(y-2, 2);    p2 = pow(x-2, 2) + pow(y+2, 2);    p3 = pow(x+2, 2) + pow(y-2, 2);    p4 = pow(x+2, 2) + pow(y+2, 2);    //判断坐标是否在圆塔内并输出结果    (p1<=1 || p2<=1 || p3<=1 || p4<=1) ? h = 10 : h = 0;    printf("The building height on this point is %d\n", h);    return 0;}

方法2:(利用函数的模块化设计)

#include <stdio.h>#include <math.h>void input();                             //定义输入函数void judge(int m, int n);                 //定义坐标判断函数int x, y;                                 //定义全局变量坐标值int main(){    input();                              //调用输入函数    judge(x, y);                          //调用坐标判断函数    return 0;}//输入函数void input(){    printf("Please enter coordinate: ");    scanf("%d %d", &x, &y);}//坐标判断函数void judge(int m, int n){    int h;    double p1, p2, p3, p4;    p1 = pow(m-2, 2) + pow(n-2, 2);    p2 = pow(m-2, 2) + pow(n+2, 2);    p3 = pow(m+2, 2) + pow(n-2, 2);    p4 = pow(m+2, 2) + pow(n+2, 2);    (p1<=1 || p2<=1 || p3<=1 || p4<=1) ? h = 10 : h = 0;    printf("The building height on this point is %d\n", h);}
阅读全文
0 0
原创粉丝点击