(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); }