请 用c++设计一个程序包含点类 Po i n t 和一个 线 类 L i n e , L i n e 中 含 有 Po i n t 的 类对象。 L i n e 中有一个 判断 3 个点是 否 在同一 条直线 上的函 数。
时间: 2024-05-12 09:16:40 浏览: 8
好的,我明白了。以下是用 C 设计一个程序包含点类 Point 和一个线类 Line,Line 中含有 Point 的类对象。Line 中有一个判断3个点是否在同一条直线上的函数的代码:
```c
#include<stdio.h>
typedef struct point
{
int x,y;
}Point;
typedef struct line
{
Point p1,p2,p3;
}Line;
int IsOnSameLine(Line l)
{
int dx1=l.p1.x-l.p2.x, dy1=l.p1.y-l.p2.y;
int dx2=l.p1.x-l.p3.x, dy2=l.p1.y-l.p3.y;
if(dx1*dy2 == dx2*dy1)
return 1;
else
return 0;
}
int main()
{
Line l;
printf("Enter the value of points:\n");
scanf("%d%d%d%d%d%d",&l.p1.x,&l.p1.y,&l.p2.x,&l.p2.y,&l.p3.x,&l.p3.y);
if(IsOnSameLine(l))
printf("All Points are on the same line\n");
else
printf("Points are not on same line\n");
return 0;
}
```
以上代码演示了如何定义点类 Point 和线类 Line,并实现判断3个点是否在同一条直线上的函数 IsOnSameLine。在主函数中,用户可以通过键盘输入3个点的坐标值来测试程序。