简体   繁体   English

C#三角形类型计算和面积计算

[英]C# Triangle type calculation and area calculation

I am trying to solve or be pointed in the right direction. 我正在尝试解决或指出正确的方向。 I am having difficulty determining where to place my Area formula in the Triangle Class (not the main). 我在确定将我的Area公式放在Triangle Class(不是主要的)中的位置时遇到困难。 Area can only have a 'get' and not a 'set'. 区域只能具有“获取”,而不能具有“集合”。

Next issue is identifying the type of triangle based on the inputed side and if it is a 'right' triangle, appending the 'type' with '-right' for example (isosceles-right). 下一个问题是根据输入的一面来确定三角形的类型,如果它是“右”三角形,则将“类型”后面附加“-右”(等腰-右)。 I have an enum for the triangle types. 我有一个三角形类型的枚举。

I'm not looking for the direct answer to solve this but rather some help and coaching to help better build my skills 我不是在寻找解决这个问题的直接答案,而是在寻求一些帮助和指导以帮助我提高技能

Here is the class structure I have generated so far in C#, please keep in mind it is not complete. 这是我到目前为止在C#中生成的类结构,请记住它并不完整。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TriangleCheck;

namespace TriangleCheck
{
    public class Triangle
    {
        private StringBuilder _ErrorMsg;
        private int[] _Sides;
        private const int _nSides = 3;
        private int _Area;

        public Triangle(int[] Sides)
        {
            //Track amunt of errors recieved.
            int nErrors = 0;

            //Make sure ErrorMsg is cleared
            _ErrorMsg = new StringBuilder();

            //Did I get _nSides? If not, append to ErrorMsg and throw exception
            if(Sides.Length != _nSides)
            {
                _ErrorMsg.Append(string.Format("Expected {0} sides but recieved {1}", _nSides, Sides.Length));
                nErrors += 1;
            }

            //Is each side positive? If not, append to ErrorMsg and throw exception
            for (int i = 0; i < Sides.Length; i++)
            {
                if (Sides[i] <= 0)
                {
                    _ErrorMsg.Append(string.Format("{0} side is not a postive integer", Sides[i]));
                    nErrors += 1;
                }
            }

            //Set input from user to private property _Sides
            _Sides = Sides;
            _Area = Area;
        }

        public int Area
        {
            get { return _Area; }
            private set
            {
                int parameter = 
            }
        }


        public string ErrorMsg
        {
            get
            { return ErrorMsg.ToString();}
        }

        public bool IsRight
        {
            get
            {
                return ;
            }
        }

        public int Sides
        {
            get
            { return _Sides; }

            set
            {
                if (value > 0)
                {
                    _Sides = value;
                }
                else
                    throw new ArgumentOutOfRangeException("Value must be postive!");
            }
        }

        public TriangleTypes TriangleTypes
        {
            get
            {
                throw new System.NotImplementedException();
            }

            set
            {
            }
        }

        public void ScaleUp(int[] ScaleFactor)
        {
            throw new System.NotImplementedException();
        }

        public override string ToString()
        {
            return "A Triangle with sides " + _Sides + " is Type: " + TriangleTypes + " with Area:" + Area;
        }
    }
}

You mention that you can't set the Area property... it looks like you're trying to enforce that by making a private set , but why not just exclude the set leaving it as a read-only property? 您提到不能设置Area属性...看起来您正在尝试通过创建private set来强制实施该private set ,但是为什么不仅仅排除该set而将其保留为只读属性呢?

The Area formula could go a couple places; 面积公式可能会出现两个位置; the key is that it is derived from the sides but only matters when someone asks for it. 关键是它来自双方,但仅在有人要求时才重要。 So you could reasonably: 因此,您可以合理地:

  • Apply the formula and update internal state every time sides changes 每次变化时,应用公式并更新内部状态
  • Apply the formula and return the value every time someone does a get operation on Area 每次有人对Area进行get操作时,应用公式并返回值

Remember the point of getter and setter being functions is that they could contain logic to execute (to fully update internal state in setter, or to calculate the value of a read-only derived property). 请记住,getter和setter作为函数的要点是它们可以包含要执行的逻辑(以完全更新setter中的内部状态,或计算只读派生属性的值)。

More sophisticated patterns exist if performance of the area calculation were very worrisome, but I wouldn't get into that at this point. 如果面积计算的性能令人担忧,则可以使用更复杂的模式,但目前我不打算讨论。

As for determining if the triangle is right... if it is, which side must be the hypotenuse? 至于确定三角形是否正确...如果正确,斜边必须在哪一侧? What relationship do you know between the length of the hypotenuse and the lengths of the other sides, if the triangle is right? 如果三角形正确,您知道斜边的长度与另一边的长度之间有什么关系?

using System;

namespace ConsoleApp
{
    class Program
    {
        static void Main()
        {
            var t = new Triangle(2, 3, 5);

            //var Triangle = new Triangle(2);          // won't compile as no Triangle constructor can be found that takes 1 integer
            //var Triangle = new Triangle(2, 3, 5, 7); // won't compile as no Triangle constructor can be found that takes 4 integers
            //var Triangle = new Triangle(2, -3, 5);   // won't compile as the 2nd value is negative - and we've asked for unsigned for all 3 values

            Console.WriteLine("The triangle ({0}, {1}, {2}) has an area of {3}.", t.A, t.B, t.C, t.area());

            Console.ReadKey();
        }
    }

    public class Triangle
    {
        public uint A { get; set; }
        public uint B { get; set; }
        public uint C { get; set; }

        public Triangle(uint a, uint b, uint c)
        {
            this.A = a;
            this.B = b;
            this.C = c;
        }

        public uint area()
        {
            return A * B * C; // this needs fixing ...
        }
    }
}

Isn't this roughly what you are trying to achieve with your Triangle class - a way of stopping it being used incorrectly with too few or incorrect types of arguments. 这不是您要尝试使用Triangle类实现的大致目标吗?这是一种防止因参数太少或类型错误而导致不正确使用它的方法。 This one only allows 3 positive (uint) integers. 这个仅允许3个正(uint)整数。 Nothing else will comple - which is what you want. 没有其他东西可以完成-这就是您想要的。 Sorry if I have misunderstood. 对不起,如果我误会了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM