用C、C++和Python编写程序计算圆、矩形或三角形的面积

281 阅读3分钟

问题说明

我们需要用C、C++和Python编写一个程序,能够根据用户的选择和输入返回圆、矩形和三角形的面积。

首先,用户可以选择他/她想要计算面积的几何形状。然后根据所选的几何形状要求用户提供该形状的边或半径并计算其面积。

为了计算圆形、矩形和三角形的面积,我们将使用以下公式。

圆的面积 = π r 2

长方形的面积=width*length

三角形的面积(三条边分别为a和c)=√[s(s-a)(s-b)(s-c)] ,其中s=(a+b+c)/2

计算圆、长方形或三角形面积的C程序,取决于用户的选择

#include <stdio.h>
#include <math.h>

int main()
{
	float area, radius, width, length, a, b, c,s;
	int choice;
	
	printf("**********Choose an Option:*********\n ");
	printf("1: Area of the Circle\n ");
	printf("2: Area of the Rectangle\n ");
	printf("3: Area of the Triangle\n ");
	
	//input the geometry shape
	scanf("%d", &choice);
	
	//for circle
	if(choice==1)
	{
		printf("Enter the radius of the Circle: ");
		scanf("%f", &radius);
		area = M_PI*radius*radius;
		printf("The area of the circle with radius %f is %.2f", radius, area);
	}
	
	//for rectangle
	
	if(choice ==2)
	{
		printf("Enter the Width of the rectangle: ");
		scanf("%f", &width);
		printf("Enter the length of the rectangle: ");
		scanf("%f", &length);
		
		area = width*length;
		printf("The area of the rectangle is %.2f", area);
	}
	//area of triangle using heron's formula
	if(choice ==3)
	{
		printf("Enter the 3 sides of triangle eg (12 13 14):  ");
		scanf("%f %f %f", &a, &b, &c);
		s = (a+b+c)/2;
		area = sqrt(s*(s-a)*(s-b)*(s-c));
		printf("The area of the triangle is %.2f", area);
	}
    return 0;
}

输出

**********Choose an Option:*********
1: Area of the Circle
2: Area of the Rectangle
3: Area of the Triangle
3
Enter the 3 sides of triangle eg (12 13 14): 3 7 6
The area of the triangle is 8.94

根据用户的选择,计算圆、长方形或三角形的面积的C++程序

#include
#include
using namespace std;

int main()
{
	float area, radius, width, length, a, b, c,s;
	int choice;
	
	cout<<"**********Choose an Option:*********\n ";
	cout<<"1: Area of the Circle\n ";
	cout<<"2: Area of the Rectangle\n ";
	cout<<"3: Area of the Triangle\n "; 
       
        //input the geometry shape 
        cin>>choice;
	
	//for circle
	if(choice==1)
	{
		cout<<"Enter the radius of the Circle: "; 
                cin>>radius;
		area = M_PI*radius*radius;
		cout<<"The area of the circle with radius "<< radius<<" is "<< area;
	}
	
	//for rectangle 
	
	if(choice ==2)
	{
		cout<<"Enter the Width of the rectangle: "; 
                cin>>width;
		cout<<"Enter the length of the rectangle: "; 
                cin>>length;
		
		area = width*length;
		cout<<"The area of the rectangle is "<< area;
	}
	//area of triangle using heron's formula
	if(choice ==3)
	{
		cout<<"Enter the 3 sides of triangle eg (12 13 14): "; 
                cin>>a>>b>>c;
		s = (a+b+c)/2;
		area = sqrt(s*(s-a)*(s-b)*(s-c));
		cout<<"The area of the triangle is "<< area;
	}
    return 0;
}

输出

**********Choose an Option:*********
1: Area of the Circle
2: Area of the Rectangle
3: Area of the Triangle
2
Enter the Width of the rectangle: 12
Enter the length of the rectangle: 6
The area of the rectangle is 72

根据用户的选择计算圆、矩形或三角形的面积的Python程序

import math

print("**********Choose an Option:********* ");
print("1: Area of the Circle");
print("2: Area of the Rectangle ");
print("3: Area of the Triangle ");

# input the geometry shape
choice = int(input(""))

# area of the circle
if choice==1:
    # input circle radius
    radius = float(input("Enter the radius of the Circle: "))
    area = math.pi*pow(radius,2)
    print(f"The area of the circle with radius {radius} is : {round(area,2)}")

#area of the rectangle
if choice==2:
    width =float(input("Enter the width of the rectangle: "))
    length = float(input("Enter the length of the rectangle: "))
    area = width*length
    print(f"The area of the ractangle is {round(area,2)}")

# area of triangle using heron's formula
if choice ==3:
    a, b , c = map(float, input("Enter the 3 sides of triangle eg (12 13 14):").split())
    s= (a+b+c)/2
    area = math.sqrt(s*(s-a)*(s-b)*(s-c))
    print(f"The area of the triangle is {round(area,2)}")

输出

**********Choose an Option:*********
1: Area of the Circle
2: Area of the Rectangle
3: Area of the Triangle
1
Enter the radius of the Circle: 12
The area of the circle with radius 12.0 is : 452.39

结束语!

在这个编程教程中,我们讨论了如何用C、C++和Python写一个程序来找出一个圆、矩形或三角形的面积。这个程序非常简单,而且是根据用户的选择来工作。

为了计算圆的面积,它要求用户输入圆的半径。对于长方形,它要求输入宽度和长度。而对于三角形,它接受三条边的长度。