CSharp求圆周长以及面积
时间: 2023-01-11 11:55:36 浏览: 90
求圆的周长:
```csharp
double r = 3.0; // 圆的半径
double circumference = 2 * Math.PI * r; // 圆周长=2*π*半径
```
求圆的面积:
```csharp
double r = 3.0; // 圆的半径
double area = Math.PI * r * r; // 圆面积=π*半径^2
```
注意:上述代码使用了 C# 中的 `Math.PI` 常量来表示圆周率 $\pi$。
另外,在 C# 中,可以使用 `System.Math` 类的 `Sqrt` 方法来求平方根,例如:
```csharp
double x = 2.0;
double y = Math.Sqrt(x); // y 的值为 1.4142135623730950488016887242097
```
相关问题
求矩形和圆形的面积周长
以下是使用C#和Python分别求解矩形和圆形的面积和周长的例子:
C#代码:
```csharp
using System;
public abstract class Shape
{
public abstract double GetArea();
public abstract double GetPerimeter();
}
public class Rectangle : Shape
{
private double length;
private double width;
public Rectangle(double l, double w)
{
length = l;
width = w;
}
public override double GetArea()
{
return length * width;
}
public override double GetPerimeter()
{
return 2 * (length + width);
}
}
public class Circle : Shape
{
private double radius;
public Circle(double r)
{
radius = r;
}
public override double GetArea()
{
return Math.PI * radius * radius;
}
public override double GetPerimeter()
{
return 2 * Math.PI * radius;
}
}
class Program
{
static void Main(string[] args)
{
// 使用多态求矩形的面积和周长,求圆形的面积和周长
Shape shape = new Circle(5);
double area = shape.GetArea();
double perimeter = shape.GetPerimeter();
Console.WriteLine("圆形的周长是{0},面积是{1}", perimeter, area);
shape = new Rectangle(3, 4);
area = shape.GetArea();
perimeter = shape.GetPerimeter();
Console.WriteLine("矩形的周长是{0},面积是{1}", perimeter, area);
}
}
```
Python代码:
```python
import math
class Shape:
def get_area(self):
pass
def get_perimeter(self):
pass
class Rectangle(Shape):
def __init__(self, length, width):
self.length = length
self.width = width
def get_area(self):
return self.length * self.width
def get_perimeter(self):
return 2 * (self.length + self.width)
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def get_area(self):
return math.pi * self.radius * self.radius
def get_perimeter(self):
return 2 * math.pi * self.radius
# 使用多态求矩形的面积和周长,求圆形的面积和周长
shape = Circle(5)
area = shape.get_area()
perimeter = shape.get_perimeter()
print("圆形的周长是{0},面积是{1}".format(perimeter, area))
shape = Rectangle(3, 4)
area = shape.get_area()
perimeter = shape.get_perimeter()
print("矩形的周长是{0},面积是{1}".format(perimeter, area))
```
c#用多态实现求圆的面积和周长(矩形,三角形)
以下是使用C#多态实现求圆的面积和周长、矩形的面积和周长、三角形的面积和周长的示例代码:
```csharp
using System;
// 定义一个抽象类Shape
abstract class Shape
{
public abstract double GetArea(); // 获取面积的抽象方法
public abstract double GetPerimeter(); // 获取周长的抽象方法
}
// 定义圆形类Circle,继承自Shape
class Circle : Shape
{
private double radius; // 圆的半径
public Circle(double radius)
{
this.radius = radius;
}
public override double GetArea()
{
return Math.PI * radius * radius; // 计算圆的面积
}
public override double GetPerimeter()
{
return 2 * Math.PI * radius; // 计算圆的周长
}
}
// 定义矩形类Rectangle,继承自Shape
class Rectangle : Shape
{
private double length; // 矩形的长度
private double width; // 矩形的宽度
public Rectangle(double length, double width)
{
this.length = length;
this.width = width;
}
public override double GetArea()
{
return length * width; // 计算矩形的面积
}
public override double GetPerimeter()
{
return 2 * (length + width); // 计算矩形的周长
}
}
// 定义三角形类Triangle,继承自Shape
class Triangle : Shape
{
private double side1; // 三角形的边长1
private double side2; // 三角形的边长2
private double side3; // 三角形的边长3
public Triangle(double side1, double side2, double side3)
{
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public override double GetArea()
{
double p = (side1 + side2 + side3) / 2; // 计算半周长
return Math.Sqrt(p * (p - side1) * (p - side2) * (p - side3)); // 计算三角形的面积
}
public override double GetPerimeter()
{
return side1 + side2 + side3; // 计算三角形的周长
}
}
// 测试代码
class Program
{
static void Main(string[] args)
{
// 创建一个圆形对象
Circle circle = new Circle(5);
Console.WriteLine("圆的面积:{0}", circle.GetArea());
Console.WriteLine("圆的周长:{0}", circle.GetPerimeter());
// 创建一个矩形对象
Rectangle rectangle = new Rectangle(4, 6);
Console.WriteLine("矩形的面积:{0}", rectangle.GetArea());
Console.WriteLine("矩形的周长:{0}", rectangle.GetPerimeter());
// 创建一个三角形对象
Triangle triangle = new Triangle(3, 4, 5);
Console.WriteLine("三角形的面积:{0}", triangle.GetArea());
Console.WriteLine("三角形的周长:{0}", triangle.GetPerimeter());
}
}
```
相关推荐
![text/x-c++; charset=iso-8859-1](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)