Copyright (c) 2016,
烟台大学计算机与控制学院 All ringts reserved.
文件名称:项目3-点结构体
作 者:王兴振
完成日期:2016年12月16日
版 本 号:未知
题目描述:
下面定义了一个表示平面上一点的结构体类型:
struct Point
{
float x; //横坐标
float y; //纵坐标
};
(1)请编写程序,输入一点的横纵坐标,输出该点到原点的距离
(2)请编写程序,输入两点p1和p2的坐标,输出两点之间的距离,以及p1关于x轴的对称点,p2关于原点的对称点,运行结果如下图所示:
答 案(1):
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct Point
{
float x; //横坐标
float y; //纵坐标
};
int main()
{
float d;
struct Point Z;
printf("输入一个坐标:\n");
scanf("%f%f", &Z.x,&Z.y);
d=sqrt(Z.x*Z.x+Z.y*Z.y);
printf("它到原点的距离是:%.2f", d);
return 0;
}
答 案(2):
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct Point
{
float x; //横坐标
float y; //纵坐标
};
int main()
{
float d,a,s;
struct Point Z,M;
printf("输入P1坐标:");
scanf("%f%f", &Z.x,&Z.y);
printf("输入P2坐标:");
scanf("%f%f", &M.x,&M.y);
if(Z.x>M.x)
a=Z.x-M.x;
else
a=M.x-Z.x;
if(Z.y>M.y)
s=Z.y-M.y;
else
s=M.y-Z.y;
d=sqrt(a*a+s*s);
printf("它到原点的距离是:%.2f\n", d);
printf("P1关于x轴的对称点是(%.1f,%.1f)\n", Z.x,-Z.y);
printf("P2关于原点的对称轴是(%.1f,%.1f)\n", -M.x,-M.y);
return 0;
}