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

xiaoxiao2021-02-28  111

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); }
转载请注明原文地址: https://www.6miu.com/read-56137.html

最新回复(0)