基于所选形状的面积计算 [英] Area Calculation based on Shape selected

查看:103
本文介绍了基于所选形状的面积计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



最近我遇到了一个问题,要求我设计一个应用程序。该应用程序将具有N个具有上述形状的按钮(或按钮将具有特定形状,例如圆形,三角形)。当点击任何形状按钮时,将要求用户提供计算区域的信息(例如圆圈时的半径),然后将向用户显示所选形状的区域。



为了回答我接下来的问题,接下来的方法。



1.使用抽象方法 Area创建抽象类 Shape

2.所有其他形状都将实现此Shape类,并将实现Area方法以进行面积计算的专门计算



现在面试官问我如何使用上面的结构。



我告诉我在按钮的处理程序上我将创建特定形状类的对象并将调用Area方法它。



但是他说如果有1000个形状,那么你将需要创建1000个处理程序。即使你在其中创建单个处理程序,你将不得不有1000 if else条件来创建特定形状的对象?



我在此之后无法回答,我仍然想知道如何实现这个好的设计?



请帮帮我们。自从我接受本次采访以来,它的烦恼是什么才是理想的设计?

Hi All,

Recently I faced a question where I was asked to design an application. This application will have N number of buttons which have shapes mentioned on it(or buttons will be in particular shape e.g. Circle,Triangle). When any shape button is clicked user will be asked for information to calculate area(e.g. radius in case of circle) and then user will be presented with Area of the shape selected.

In order to answer the question I followed below approach.

1. Create abstract class Shape with abstract method Area()
2. All other shapes will implement this Shape class and will implement Area method for specialized calculation for area calculation

Now the interviewer asked me that how to use above structure.

I told that on the handler of button I will create object of particular shape's class and will invoke Area method with it.

But He told that if there are 1000 shapes then you will have to create 1000 handlers. even if you create single handler then inside it you will have to have 1000 if else conditions to create object of particular shape?

I could not answer after this and I'm still wondering how to achieve good design for this?

Please help me guys. Its bugging me since I gave this interview that what would be the ideal design for this?

推荐答案

我在评论中得到的是你应该使用泛型。假设我们有以下形状类:

What I was getting at in my comment was you should use generics. Let's say we have following classes for shapes:
public abstract class ShapeBase
{
    public abstract void Area();
}

public class Circle : ShapeBase
{
    public override void Area() { ... }
}

public class Triangle : ShapeBase
{
    public override void Area() { ... }
}

public class Square : ShapeBase
{
    public override void Area() { ... }
}



然后在您的表单中,您有一个通用事件处理程序:


Then in your form you have one generic event handler:

private void ButtonClick<TShape>() where TShape: ShapeBase, new()
{
    var shape = new TShape();
    shape.Area();
}



您使用不同的形状类型使用相应的按钮点击来注册此处理程序:


And you register this handler with the respective button clicks using different shape types:

buttonCircle.Click += (sender, args) => ButtonClick<Circle>();
buttonTriangle.Click += (sender, args) => ButtonClick<Triangle>();
buttonSquare.Click += (sender, args) => ButtonClick<Square>();



Voila单个处理程序,可以根据需要使用多种形状。你可以说你仍然需要注册一千个事件处理程序,但这不是你问题的一部分。


Voila a single handler used with as many shapes as you want. You can argue you still need to register one thousand event handlers but that was not part of your question.


你的问题让我头疼,在这里我试着找到一个通用的区域所有你需要的计算器是为你的形状传递坐标和边数(注意圆边是1)



我使用了多边形通用面积计算:



参考此处: http://www.mathopenref.com/coordpolygonarea .html [ ^ ]



Shape.cs

___________



Hi, Your question was breaking my heads , here I tried out to find a generic area calculator all you need is to pass co-ordinates and number of sides for your shape ( note in case of circle sides is 1)

I used polygon generic area calculation:

Refer here : http://www.mathopenref.com/coordpolygonarea.html[^]

Shape.cs
___________

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

namespace GenericAreaCalculator
{
    public class Shape
    {
        public Dictionary<int,int> x;
        public Dictionary<int,> y;

        public Shape()
        {
            x = new Dictionary<int,>();
            y = new Dictionary<int,>();
        }
    }
}





Area.cs

_______





Area.cs
_______

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

namespace GenericAreaCalculator
{
    public class Area:Shape
    {
        public float CalculateArea(Shape Shapes,int sides)
        {
            float area = 0;
             int x1 = 0;
             int y2 = 0;
             int y1 = 0;
             int x2 = 0;

            if (sides > 1)
            {
                for (int index = 1; index < sides; index++)
                {
                     x1 = Shapes.x[index];
                     y2 = Shapes.y[index + 1];
                     y1 = Shapes.y[index];
                     x2 = Shapes.x[index + 1];
                    area = area + (((x1 * y2)) - ((y1 * x2)));

                }

                 int xn = Shapes.x[sides];              
                 y1 = Shapes.y[1];
                 int yn = Shapes.y[sides];
                 x1 = Shapes.x[1];
                 area = (area + (((xn * y1)) - ((yn * x1))))/2;
                
            }
            else if (sides == 1)
            {
                area = (float)(3.14 * Shapes.x[0] * Shapes.x[0]);
            }
            

            

            return Math.Abs(area);
        }
    }
}





Program.cs

___________





Program.cs
___________

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

namespace GenericAreaCalculator
{
    class Program
    {
        static void Main(string[] args)
        {
            Shape shapeobj = new Shape();

            shapeobj.x.Add(1, 0);
            shapeobj.x.Add(2, 10);
            shapeobj.x.Add(3, 10);
            shapeobj.x.Add(4, 0);

            shapeobj.y.Add(1, 0);
            shapeobj.y.Add(2, 0);
            shapeobj.y.Add(3, 10);
            shapeobj.y.Add(4, 10);

            Area area = new Area();

            float ShapeArea = area.CalculateArea(shapeobj, 4);

        }
    }
}





注:此处坐标索引非常重要,按顺序制作



Note : Here Index of Co-ordinates have much importance , Make it in sequence


下面是一个简单的实现而不使用设计模式



我认为面试官问你关于观察者模式的实现



请参考这里关于观察者模式:



http://www.dofactory.com/net/observer-design-pattern [ ^ ]



https://msdn.microsoft.com/en-us/library/ee850490(ⅴ = vs.110).aspx [ ^ ]



The below is a simple implemtation without using Design Patterns

I think the interviewer asked you about implementation of Observer Pattern

Please refer here about Observer Pattern :

http://www.dofactory.com/net/observer-design-pattern[^]

https://msdn.microsoft.com/en-us/library/ee850490(v=vs.110).aspx[^]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ScreenShotDesktop
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            switch (btn.Text)
            {
                case "Circle" : //Calculate Area of Circle Here
                    break;
                case "Triangle": //Calculate Area of Triangle Here
                    break;
                    //And So on...
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            foreach (Button btn in this.Controls)
            {
                btn.Click += new EventHandler(button1_Click);
            }
        }
    }
}


这篇关于基于所选形状的面积计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆