2016-01-24 80 views
1

我想寫一點代碼來計算正則多邊形的面積與這個公式(邊*長度^ 2)/ 4 *譚(Pi /邊)我的代碼看起來像計算正多邊形問題的面積

double area = (nSides * Math.pow(sLength, 2))/
      4 * Math.tan ((Math.PI)/nSides); 

我預計,隨着nSides == 5和sLength == 6.5,我應該得到72.69017017488385但它輸出38.370527260283126代替。

回答

3

它叫做Order of Operations。 Java文檔說,這得不清楚here

下表中的運營商根據優先順序

問題上市是除法和乘法具有相同的優先級。

同一行上的運算符具有相同的優先級。 ...除賦值運算符外的所有二元運算符都從左到右進行求值;

因此,如果它們具有相同的優先級,那麼在乘以Math.tan之前,將除以4。你需要通過插入一組額外的括號明確指定的順序:

double area = (nSides * Math.pow(sLength, 2))/
       (4 * Math.tan ((Math.PI)/nSides)); 
0

您將需要使用除括號爲您分母

(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)); 
0

在任何情況下,一個需要使用OOP的概念,這裏是我試計算規則多邊形的面積和周長。有人問我這樣的學校任務,這就是我試圖解決的問題。我是一名學生,想成爲一名程序員,並樂於學習和分享知識。

正多邊形是一個三邊或更多邊的形狀。每一邊的長度必須相等。計算面積的公式根據邊的數量而不同。

方形區域=邊長×邊長;
三角=側* 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); 
     } 
    } 
} 
+0

實際上有一個特定的公式可以用來計算任意邊正則多邊形的面積:A = s^2 * n * cot(180°/ n)/ 2'。 –