C#求圆周长以及面积
时间: 2023-02-05 18:20:09 浏览: 43
C 是一种编程语言,被广泛应用于软件开发、操作系统、科学计算、嵌入式系统等领域。它的语法简洁、功能强大,可以用来开发各种类型的应用程序。C 语言是一种静态类型、编译型语言,所以程序在编译时会进行类型检查,这使得程序更加安全可靠。同时,C 语言也提供了较为丰富的低级接口,可以让开发者更加精细地控制计算机的硬件和软件资源。
相关问题
CSharp求圆周长以及面积
求圆的周长:
```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))
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)