用户输入一组选项和数据,进行与四边形有关的计算。 四边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最 后一个输入的顶点相邻。 选项包括: 1:输入四个点坐标,判断是否是四边形、平行四边形,判断结果输出 true/false,结果 之间以一个英文空格符分隔。 2:输入四个点坐标,判断是否是菱形、矩形、正方形,判断结果输出 true/false,结果 之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出"not a quadrilateral" 3:输入四个点坐标,判断是凹四边形(false)还是凸四边形(true),输出四边形周长、 面积,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出"not a quadrilateral" 4:输入六个点坐标,前两个点构成一条直线,后四个点构成一个四边形或三角形,输 出直线与四边形(也可能是三角形)相交的交点数量。如果交点有两个,再按面积从小到大 输出四边形(或三角形)被直线分割成两部分的面积(不换行)。若直线与四边形或三角形的 一条边线重合,输出"The line is coincide with one of the lines"。若后四个点不符合四边形或三 角形的输入,输出"not a quadrilateral or triangle"。 后四个点构成三角形的情况,假设三角形一条边上两个端点分别是 x、y,边线中间有 一点 z,另一顶点 s: 1)符合要求的输入,顶点重复或者 z 与 xy 都相邻,如:x x y s、x z y s、x y x s、s x y y。 此时去除冗余点,保留一个 x、一个 y。 2) 不符合要求的输入,z 不与 xy 都相邻,如:z x y s、x z s y、x s z y 5:输入五个点坐标,输出第一个是否在后四个点所构成的四边形(限定为凸四边形, 不考虑凹四边形)或三角形(判定方法见选项 4)的内部(若是四边形输出 in the quadrilateral/outof the quadrilateral,若是三角形输出 in the triangle/outof the triangle)。如果 点在多边形的某条边上,输出"on the triangle 或者 on the quadrilateral"。若后四个点不符合 四边形或三角形,输出"not a quadrilateral or triangle"。 ### 输入格式: 基本格式:选项+":"+坐标 x+","+坐标 y+" "+坐标 x+","+坐标 y。点的 x、y 坐标之间以英文"," 分隔,点与点之间以一个英文空格分隔。 ### 输出格式: 基本输出格式见每种选项的描述。 异常情况输出: 如果不符合基本格式,输出"Wrong Format"。 如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。 注意:输出的数据若小数点后超过 6 位,只保留小数点后 6 位,多余部分采用四舍五入规则 进到最低位。小数点后若不足 6 位,按原始位数显示,不必补齐。例如:1/3 的结果按格式 输出为 0.333333,1.0 按格式输出为 1.0选项 1、2、3 中,若四边形四个点中有重合点,输出"points coincide"。 选项 4 中,若前两个输入线的点重合,输出"points coincide"。 用c#来写
时间: 2025-03-15 11:02:59 浏览: 103
以下是基于您需求的 C# 实现代码示例,涵盖了各个选项的功能逻辑:
```csharp
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("请输入选项及坐标信息:");
string input = Console.ReadLine();
if (string.IsNullOrWhiteSpace(input))
throw new Exception("Wrong Format");
// 解析输入内容
int option = ParseOption(input);
List<Point> points = ParsePoints(input);
switch (option)
{
case 1:
HandleOption1(points);
break;
case 2:
HandleOption2(points);
break;
case 3:
HandleOption3(points);
break;
case 4:
HandleOption4(points);
break;
case 5:
HandleOption5(points);
break;
default:
Console.WriteLine("Invalid Option");
break;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message == "points coincide" ? "points coincide" : "Wrong Format");
}
}
private static double Distance(Point p1, Point p2) => Math.Sqrt((p1.X - p2.X) * (p1.X - p2.X) + (p1.Y - p2.Y) * (p1.Y - p2.Y));
private static bool IsQuadrilateral(List<Point> points)
{
for (int i = 0; i < 4; i++)
{
if (Distance(points[i], points[(i + 1) % 4]) <= 0 || Distance(points[0], points[^1]) <= 0)
return false;
}
return AreUnique(points);
}
private static bool AreUnique(List<Point> points)
{
HashSet<string> set = new HashSet<string>();
foreach (var point in points)
{
if (!set.Add(point.ToString()))
return false;
}
return true;
}
private static int ParseOption(string input) => int.Parse(input.Split(':')[0]);
private static List<Point> ParsePoints(string input)
{
var parts = input.Split(':')[1].Split(' ');
List<Point> points = new List<Point>();
foreach (var part in parts)
{
var coords = part.Split(',');
points.Add(new Point { X = Convert.ToDouble(coords[0]), Y = Convert.ToDouble(coords[1]) });
}
return points;
}
private static void HandleOption1(List<Point> points)
{
if (points.Count != 4) throw new Exception("wrong number of points");
if (!AreUnique(points)) throw new Exception("points coincide");
bool isQuad = IsQuadrilateral(points);
bool isParallelogram = CheckParallel(points);
Console.WriteLine($"{isQuad} {isParallelogram}");
}
private static void HandleOption2(List<Point> points)
{
if (points.Count != 4) throw new Exception("wrong number of points");
if (!IsQuadrilateral(points)) throw new Exception("not a quadrilateral");
if (!AreUnique(points)) throw new Exception("points coincide");
bool isRhombus = CheckAllSidesEqual(points);
bool isRectangle = CheckAngles90(points);
bool isSquare = isRhombus && isRectangle;
Console.WriteLine($"{isRhombus} {isRectangle} {isSquare}");
}
private static void HandleOption3(List<Point> points)
{
if (points.Count != 4) throw new Exception("wrong number of points");
if (!IsQuadrilateral(points)) throw new Exception("not a quadrilateral");
if (!AreUnique(points)) throw new Exception("points coincide");
bool isConvex = !CheckConcave(points);
double perimeter = GetPerimeter(points);
double area = CalculateArea(points);
Console.WriteLine($"{isConvex:F6} {perimeter:F6} {area:F6}");
}
private static void HandleOption4(List<Point> points)
{
if (points.Count != 6) throw new Exception("wrong number of points");
Line line = new Line(points[0], points[1]);
List<Point> shapePoints = points.GetRange(2, 4);
if (line.IsCoincide(shapePoints)) Console.WriteLine("The line is coincide with one of the lines");
else if (shapePoints.Count > 3 && !IsQuadrilateral(shapePoints)) Console.WriteLine("not a quadrilateral or triangle");
else
{
int intersectionCount = line.CountIntersections(shapePoints);
if (intersectionCount == 2)
{
Tuple<double, double> areas = line.DividedAreas(shapePoints);
Console.WriteLine($"{areas.Item1:F6}{areas.Item2:F6}");
}
else
Console.WriteLine(intersectionCount);
}
}
private static void HandleOption5(List<Point> points)
{
if (points.Count != 5) throw new Exception("wrong number of points");
Polygon polygon = new Polygon(points.GetRange(1, 4));
if (!polygon.IsValid()) Console.WriteLine("not a quadrilateral or triangle");
else
{
string result = polygon.CheckPointPosition(points[0]);
Console.WriteLine(result);
}
}
// 其他辅助方法省略...
}
public class Point
{
public double X { get; set; }
public double Y { get; set; }
public override string ToString() => $"{X},{Y}";
}
// 辅助类实现 省略...
```
---
###
阅读全文
相关推荐

















