使用结构表示 X-Y 平面直角坐标系上的点,编写主程序顺序读入三个点坐标,调用函数判别由这三个点的 连线构成的图形是否为三角形。要求函数使用三个点结构体作为形参。

代码:

#include<iostream>
using namespace std;
struct position
{
	double x;
	double y;
};
void compare(position,position,position);
void main()
{
	position point1;
	position point2;
	position point3;
	cout<<"first position:x/y:";
	cin>>point1.x>>point1.y;
	cout<<"second position:x/y:";
	cin>>point2.x>>point2.y;
	cout<<"third position:x/y:";
	cin>>point3.x>>point3.y;
	compare(point1,point2,point3);
}
void compare(position point1,position point2,position point3)
{
	if ((point1.x==point2.x&&point2.x==point3.x)||(point1.y==point2.y&&point2.y==point3.y))
		cout<<"not rectangular"<<endl;
	else
		cout<<"is rectangular"<<endl;
	
}

结果:
在这里插入图片描述

可以按照以下步骤编写程序: 1. 定义平面直角坐标系上结构体。 ```c typedef struct { double x; double y; } Point; ``` 2. 读入 n 个坐标,存储到结构体数组中。 ```c int n; printf("请输入的数量:"); scanf("%d", &n); Point points[n]; for (int i = 0; i < n; i++) { printf("请输入第%d个坐标(用空格隔开):", i + 1); scanf("%lf %lf", &points[i].x, &points[i].y); } ``` 3. 对这些的 x 轴坐标排序。可以使用冒泡排序或快速排序等算法,这里演示冒泡排序的方法。 ```c for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (points[j].x > points[j+1].x) { // 交换两个坐标 Point temp = points[j]; points[j] = points[j+1]; points[j+1] = temp; } } } ``` 4. 输出排序后的的 x 轴坐标。 ```c printf("按 x 轴坐标从小到大排序后的坐标如下:\n"); for (int i = 0; i < n; i++) { printf("(%lf, %lf)\n", points[i].x, points[i].y); } ``` 完整代码如下: ```c #include <stdio.h> typedef struct { double x; double y; } Point; int main() { int n; printf("请输入的数量:"); scanf("%d", &n); Point points[n]; for (int i = 0; i < n; i++) { printf("请输入第%d个坐标(用空格隔开):", i + 1); scanf("%lf %lf", &points[i].x, &points[i].y); } for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (points[j].x > points[j+1].x) { // 交换两个坐标 Point temp = points[j]; points[j] = points[j+1]; points[j+1] = temp; } } } printf("按 x 轴坐标从小到大排序后的坐标如下:\n"); for (int i = 0; i < n; i++) { printf("(%lf, %lf)\n", points[i].x, points[i].y); } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值