计算正多边形面积问题
Calculate the area of a regular polygon issue
我正在尝试编写一些代码来使用此公式 (sides*length^2)/4*tan(Pi/sides) 计算正多边形的面积我的代码看起来像
double area = (nSides * Math.pow(sLength, 2)) /
4 * Math.tan ((Math.PI) / nSides);
我预计 nSides == 5 和 sLength == 6.5,我应该得到 72.69017017488385 但它输出 38.370527260283126。
您需要为分母使用附加括号
(4 * Math.tan ((Math.PI) / nSides))
否则 java 认为您将整个表达式乘以 Math.tan ((Math.PI) / nSides)
。
double area = (nSides * Math.pow(sLength, 2)) /
(4 * Math.tan ((Math.PI) / nSides));
叫做Order of Operations. The Java documentation says it unclearly here:
The operators in the following table are listed according to precedence order
问题是除法和乘法具有相同的优先级。
Operators on the same line have equal precedence. ... All binary operators except for the assignment operators are evaluated from left to right;
因此,鉴于它们具有相同的优先级,除以 4 将在乘以 Math.tan
之前进行计算。您需要通过插入一组额外的括号来明确指定顺序:
double area = (nSides * Math.pow(sLength, 2)) /
(4 * Math.tan ((Math.PI) / nSides));
万一有人需要使用 OOP 概念计算正多边形的面积和周长,这是我的尝试。有人问我这样的学校作业,这就是我试图解决的问题。我是一名学生,想成为一名程序员,乐于学习和分享知识。
正多边形是具有 3 个或更多边的形状。每边的长度必须相等。计算面积的公式根据边数不同
Square area = length of side * length of side;
Triangle = length of side * length of side * sqrt (3) / 4
在 C# 中,我的代码如下所示,但我尽可能多地应用了 oop 的概念,并且我还实现了工厂设计模式。我也会在java中编码。
这是 IRegularPolygon 接口,在这个问题中我使用了接口而不是抽象数据类型。
public interface IRegularPolygon
{
int NumberOfSides { get; set; }
double SideLength { get; set; }
double getPerimeter();
double getArea();
}
// you can also use abstract class the code is as follows
public abstract class RegularPolygon
{
public int NumberOfSides { get; set; }
public double SideLength { get; set; }
public double getPerimeter()
{
return NumberOfSides * SideLength;
}
public double getArea();
}
//The Square class that implement IRegularPolygon interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RegularPolygon.BL
{
public class Square : IRegularPolygon
{
public int NumberOfSides { get; set; }
public double SideLength { get; set; }
public Square(double SideLength)
{
this.NumberOfSides=4;
this.SideLength = SideLength;
}
/// <summary>
/// Get the Perimeter of the square
/// </summary>
/// <returns></returns>
public double getPerimeter()
{
return NumberOfSides * SideLength;
}
/// <summary>
/// Get the area of a square
/// </summary>
/// <returns></returns>
public double getArea()
{
return SideLength * SideLength;
}
public override string ToString()
{
return String.Format("{0}-{1}", "Area of Sqare:" + getArea(), "Perimeter Of Sqaure :" + getPerimeter());
}
}
}
//The EquilateralTriangle class that implement IRegularPolygon interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RegularPolygon.BL
{
public class EquilateralTriangle : IRegularPolygon
{
public int NumberOfSides { get; set; }
public double SideLength { get; set; }
public EquilateralTriangle(double SideLength)
{
this.NumberOfSides=3;
this.SideLength = SideLength;
}
/// <summary>
/// Get the Perimeter of EquilateralTriangle
/// </summary>
/// <returns></returns>
public double getPerimeter()
{
return NumberOfSides * SideLength;
}
/// <summary>
/// Get the area of EquilateralTriangle
/// </summary>
/// <returns></returns>
public double getArea()
{
return SideLength * SideLength * Math.Sqrt(3) / 4;
}
public override string ToString()
{
return String.Format("{0}-{1}", "Area of Sqare:" + getArea(), "Perimeter Of Sqaure :" + getPerimeter());
}
}
}
//The RegularPentagon class that implement IRegularPolygon interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RegularPolygon.BL
{
public class RegularPentagon : IRegularPolygon
{
public int NumberOfSides { get; set; }
public double SideLength { get; set; }
public double Apothem { get; set; }
public RegularPentagon(double SideLength, double Apothem)
{
this.NumberOfSides = 5;
this.SideLength = SideLength;
this.Apothem = Apothem;
}
/// <summary>
/// Get the Perimeter of RegularPentagon
/// </summary>
/// <returns></returns>
public double getPerimeter()
{
return NumberOfSides * SideLength;
}
/// <summary>
/// Get the area of RegularPentagon
/// </summary>
/// <returns></returns>
public double getArea()
{
return 2.5 * SideLength * Apothem;
}
public override string ToString()
{
return String.Format("{0}-{1}", "Area of Sqare:" + getArea(), "Perimeter Of Sqaure :" + getPerimeter());
}
}
}
// RegPolygonFactory class is a factory class that instantiate object based on shape
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RegularPolygon.BL
{
public class RegPolygonFactory
{
/// <summary>
/// Create instance of an object based on the type of shape
/// </summary>
/// <param name="shapeType"></param>
/// <param name="side"></param>
/// <returns></returns>
public IRegularPolygon getShape(string shapeType, double sideLength)
{
return getShape(shapeType, sideLength, 0.0);
}
public IRegularPolygon getShape(string shapeType, double sideLength, double apothem)
{
if (shapeType == null) throw new ArgumentNullException("shapeType");
if (sideLength < 0.0) throw new ArgumentOutOfRangeException(sideLength.ToString());
if (apothem < 0.0) throw new ArgumentOutOfRangeException(apothem.ToString());
else if (shapeType.Equals("SQUARE"))
{
return new Square(sideLength);
}
else if (shapeType.Equals("EQUILATERALTRIANGLE"))
{
return new EquilateralTriangle(sideLength);
}
//shapeType.equalsIgnoreCase("REGULARPENTAGON")
else if (shapeType.Equals("REGULARPENTAGON"))
{
return new RegularPentagon(sideLength, apothem);
}
return null;
}
}
}
//For unit Test: To test for instance square
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RegularPolygon.BL;
namespace RegularPolygonTest.BL
{
[TestClass]
public class SquareTest
{
RegPolygonFactory regularPolygon = new RegPolygonFactory();
/// <summary>
/// Test the Area Of Square with valid input(side=4)
/// </summary>
[TestMethod]
public void getAreaOfSquare_ValidArgument()
{
//Arrange
double sideLength = 4;
var square = regularPolygon.getShape("SQUARE", sideLength);
var expected = sideLength * sideLength;
//Act
var actual = square.getArea();
//Assert
Assert.AreEqual(expected, actual);
}
/// <summary>
/// Test the Perimeter Of Square with valid input(side=6, edges=4)
/// </summary>
[TestMethod]
public void getPerimeterOfSquare_ValidAregument()
{
//Arrange
double sideLength = 6;
double NumberOfSides = 4;
var square = regularPolygon.getShape("SQUARE", sideLength);
var expected = sideLength * NumberOfSides;
//Act
var actual = square.getPerimeter();
//Assert
Assert.AreEqual(expected, actual);
}
/// <summary>
/// Test the Perimeter Of Square with invalid input(edges=6)
/// </summary>
[TestMethod]
public void getPerimeterOfSquare_InValidPostiveAregument()
{
//Arrange
double sideLength = 5;
double NumberOfSides = 6;
var square = regularPolygon.getShape("SQUARE", sideLength);
var expected = sideLength * NumberOfSides;
//Act
var actual = square.getPerimeter();
//Assert
Assert.AreEqual(expected, actual);
}
}
}
我正在尝试编写一些代码来使用此公式 (sides*length^2)/4*tan(Pi/sides) 计算正多边形的面积我的代码看起来像
double area = (nSides * Math.pow(sLength, 2)) /
4 * Math.tan ((Math.PI) / nSides);
我预计 nSides == 5 和 sLength == 6.5,我应该得到 72.69017017488385 但它输出 38.370527260283126。
您需要为分母使用附加括号
(4 * Math.tan ((Math.PI) / nSides))
否则 java 认为您将整个表达式乘以 Math.tan ((Math.PI) / nSides)
。
double area = (nSides * Math.pow(sLength, 2)) /
(4 * Math.tan ((Math.PI) / nSides));
叫做Order of Operations. The Java documentation says it unclearly here:
The operators in the following table are listed according to precedence order
问题是除法和乘法具有相同的优先级。
Operators on the same line have equal precedence. ... All binary operators except for the assignment operators are evaluated from left to right;
因此,鉴于它们具有相同的优先级,除以 4 将在乘以 Math.tan
之前进行计算。您需要通过插入一组额外的括号来明确指定顺序:
double area = (nSides * Math.pow(sLength, 2)) /
(4 * Math.tan ((Math.PI) / nSides));
万一有人需要使用 OOP 概念计算正多边形的面积和周长,这是我的尝试。有人问我这样的学校作业,这就是我试图解决的问题。我是一名学生,想成为一名程序员,乐于学习和分享知识。
正多边形是具有 3 个或更多边的形状。每边的长度必须相等。计算面积的公式根据边数不同
Square area = length of side * length of side;
Triangle = length of side * length of side * sqrt (3) / 4
在 C# 中,我的代码如下所示,但我尽可能多地应用了 oop 的概念,并且我还实现了工厂设计模式。我也会在java中编码。
这是 IRegularPolygon 接口,在这个问题中我使用了接口而不是抽象数据类型。
public interface IRegularPolygon
{
int NumberOfSides { get; set; }
double SideLength { get; set; }
double getPerimeter();
double getArea();
}
// you can also use abstract class the code is as follows
public abstract class RegularPolygon
{
public int NumberOfSides { get; set; }
public double SideLength { get; set; }
public double getPerimeter()
{
return NumberOfSides * SideLength;
}
public double getArea();
}
//The Square class that implement IRegularPolygon interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RegularPolygon.BL
{
public class Square : IRegularPolygon
{
public int NumberOfSides { get; set; }
public double SideLength { get; set; }
public Square(double SideLength)
{
this.NumberOfSides=4;
this.SideLength = SideLength;
}
/// <summary>
/// Get the Perimeter of the square
/// </summary>
/// <returns></returns>
public double getPerimeter()
{
return NumberOfSides * SideLength;
}
/// <summary>
/// Get the area of a square
/// </summary>
/// <returns></returns>
public double getArea()
{
return SideLength * SideLength;
}
public override string ToString()
{
return String.Format("{0}-{1}", "Area of Sqare:" + getArea(), "Perimeter Of Sqaure :" + getPerimeter());
}
}
}
//The EquilateralTriangle class that implement IRegularPolygon interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RegularPolygon.BL
{
public class EquilateralTriangle : IRegularPolygon
{
public int NumberOfSides { get; set; }
public double SideLength { get; set; }
public EquilateralTriangle(double SideLength)
{
this.NumberOfSides=3;
this.SideLength = SideLength;
}
/// <summary>
/// Get the Perimeter of EquilateralTriangle
/// </summary>
/// <returns></returns>
public double getPerimeter()
{
return NumberOfSides * SideLength;
}
/// <summary>
/// Get the area of EquilateralTriangle
/// </summary>
/// <returns></returns>
public double getArea()
{
return SideLength * SideLength * Math.Sqrt(3) / 4;
}
public override string ToString()
{
return String.Format("{0}-{1}", "Area of Sqare:" + getArea(), "Perimeter Of Sqaure :" + getPerimeter());
}
}
}
//The RegularPentagon class that implement IRegularPolygon interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RegularPolygon.BL
{
public class RegularPentagon : IRegularPolygon
{
public int NumberOfSides { get; set; }
public double SideLength { get; set; }
public double Apothem { get; set; }
public RegularPentagon(double SideLength, double Apothem)
{
this.NumberOfSides = 5;
this.SideLength = SideLength;
this.Apothem = Apothem;
}
/// <summary>
/// Get the Perimeter of RegularPentagon
/// </summary>
/// <returns></returns>
public double getPerimeter()
{
return NumberOfSides * SideLength;
}
/// <summary>
/// Get the area of RegularPentagon
/// </summary>
/// <returns></returns>
public double getArea()
{
return 2.5 * SideLength * Apothem;
}
public override string ToString()
{
return String.Format("{0}-{1}", "Area of Sqare:" + getArea(), "Perimeter Of Sqaure :" + getPerimeter());
}
}
}
// RegPolygonFactory class is a factory class that instantiate object based on shape
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RegularPolygon.BL
{
public class RegPolygonFactory
{
/// <summary>
/// Create instance of an object based on the type of shape
/// </summary>
/// <param name="shapeType"></param>
/// <param name="side"></param>
/// <returns></returns>
public IRegularPolygon getShape(string shapeType, double sideLength)
{
return getShape(shapeType, sideLength, 0.0);
}
public IRegularPolygon getShape(string shapeType, double sideLength, double apothem)
{
if (shapeType == null) throw new ArgumentNullException("shapeType");
if (sideLength < 0.0) throw new ArgumentOutOfRangeException(sideLength.ToString());
if (apothem < 0.0) throw new ArgumentOutOfRangeException(apothem.ToString());
else if (shapeType.Equals("SQUARE"))
{
return new Square(sideLength);
}
else if (shapeType.Equals("EQUILATERALTRIANGLE"))
{
return new EquilateralTriangle(sideLength);
}
//shapeType.equalsIgnoreCase("REGULARPENTAGON")
else if (shapeType.Equals("REGULARPENTAGON"))
{
return new RegularPentagon(sideLength, apothem);
}
return null;
}
}
}
//For unit Test: To test for instance square
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RegularPolygon.BL;
namespace RegularPolygonTest.BL
{
[TestClass]
public class SquareTest
{
RegPolygonFactory regularPolygon = new RegPolygonFactory();
/// <summary>
/// Test the Area Of Square with valid input(side=4)
/// </summary>
[TestMethod]
public void getAreaOfSquare_ValidArgument()
{
//Arrange
double sideLength = 4;
var square = regularPolygon.getShape("SQUARE", sideLength);
var expected = sideLength * sideLength;
//Act
var actual = square.getArea();
//Assert
Assert.AreEqual(expected, actual);
}
/// <summary>
/// Test the Perimeter Of Square with valid input(side=6, edges=4)
/// </summary>
[TestMethod]
public void getPerimeterOfSquare_ValidAregument()
{
//Arrange
double sideLength = 6;
double NumberOfSides = 4;
var square = regularPolygon.getShape("SQUARE", sideLength);
var expected = sideLength * NumberOfSides;
//Act
var actual = square.getPerimeter();
//Assert
Assert.AreEqual(expected, actual);
}
/// <summary>
/// Test the Perimeter Of Square with invalid input(edges=6)
/// </summary>
[TestMethod]
public void getPerimeterOfSquare_InValidPostiveAregument()
{
//Arrange
double sideLength = 5;
double NumberOfSides = 6;
var square = regularPolygon.getShape("SQUARE", sideLength);
var expected = sideLength * NumberOfSides;
//Act
var actual = square.getPerimeter();
//Assert
Assert.AreEqual(expected, actual);
}
}
}