mô-đun "khu vực. py"
import math
def calculateAreaTriangle[a,b,c]:
# calculate the semi-perimeter
s = [a + b + c] / 2
# calculate the area
area = [s*[s-a]*[s-b]*[s-c]]**0.5
return area
def calculateAreaCircle[r]:
return math.pi*r*r
def calculateAreaRectangle[w,h]:
return w*h
chủ yếu. py
import area
import math
def calculateVolumeCylinder[h,r]:
return math.pi * r * r * h
def calculateSurfaceAreaCylinder[h,r]:
return [[2*math.pi*r] * h] + [[math.pi*r**2]*2]
ch=-1
while ch!=5:
print["1. Calculate the area of a triangle"]
print["2. Calculate the area of a circle"]
print["3. Calculate the area of a rectangle"]
print["4. Calculate the area of the volume and surface area of a cylinder"]
print["5. Exit"]
ch=int[input["Your choice: "]]
if ch==1:
a = float[input['\nEnter the first side: ']]
b = float[input['Enter the second side: ']]
c = float[input['Enter the third side: ']]
a=area.calculateAreaTriangle[a,b,c]
print[f"\nThe area of the triangle: {a}\n"]
elif ch==2:
r = float[input['\nEnter the radius of the circle: ']]
a=area.calculateAreaCircle[r]
print[f"\nThe area of the circle: {a}\n"]
elif ch==3:
w = float[input['\nEnter the height of the rectangle: ']]
h = float[input['Enter the width of the rectangle: ']]
a=area.calculateAreaRectangle[w,h]
print[f"\nThe area of the circle: {a}\n"]
elif ch==4:
h = float[input['\nEnter the height of cylinder: ']]
r = float[input['Enter the radius of cylinder: ']]
v=calculateVolumeCylinder[h,r]
sa=calculateSurfaceAreaCylinder[h,r]
print[f"\nThe volume of the cylinder: {v}"]
print[f"The surface area of the cylinder: {sa}\n"]
đầu ra
Viết chương trình bằng C là Chương trình điều khiển bằng menu để tính diện tích của các hình dạng hình học khác nhau
Trình bày bằng hình ảnh
Giải pháp mẫu
Mã C
#include
void main []
{
int choice,r,l,w,b,h;
float area;
printf["Input 1 for area of circle\n"];
printf["Input 2 for area of rectangle\n"];
printf["Input 3 for area of triangle\n"];
printf["Input your choice : "];
scanf["%d",&choice];
switch[choice]
{
case 1:
printf["Input radious of the circle : "];
scanf["%d",&r];
area=3.14*r*r;
break;
case 2:
printf["Input length and width of the rectangle : "];
scanf["%d%d",&l,&w];
area=l*w;
break;
case 3:
printf["Input the base and hight of the triangle :"];
scanf["%d%d",&b,&h];
area=.5*b*h;
break;
}
printf["The area is : %f\n",area];
}
Đầu ra mẫu
Input 1 for area of circle Input 2 for area of rectangle Input 3 for area of triangle Input your choice : 1 Input radious of the circle : 5 The area is : 78.500000
Sơ đồ
Trình chỉnh sửa mã lập trình C
Cải thiện giải pháp mẫu này và đăng mã của bạn qua Disqus
Trước. Viết chương trình trong C để đọc bất kỳ Số tháng nào ở dạng nguyên và hiển thị số ngày của tháng này
Kế tiếp. Viết chương trình bằng C là Chương trình điều khiển bằng menu để thực hiện một phép tính đơn giản
Mức độ khó của bài tập này là gì?
Dễ dàng trung bình khóKiểm tra kỹ năng Lập trình của bạn với bài kiểm tra của w3resource
Theo dõi chúng tôi trên Facebook và Twitter để cập nhật thông tin mới nhất.
Lập trình C. Lời khuyên trong ngày
Lập trình C - Cái gì ??. ??. toán tử làm trong C?
??. là một trigraph có nghĩa là. Vì vậy, nó nói
!ErrorHasOccured[] || HandleError[];
mà, do ngắn mạch, tương đương với
if [ErrorHasOccured[]] HandleError[];
Giới thiệu. https. //chút. ly/3pqNxWw
- Xu hướng hàng tuần
- Bài tập lập trình Java cơ bản
- Truy vấn con SQL
- Bài tập cơ sở dữ liệu Adventureworks
- Bài tập cơ bản C# Sharp
- SQL COUNT[] với sự khác biệt
- Bài tập chuỗi JavaScript
- Xác thực biểu mẫu HTML JavaScript
- Bài tập bộ sưu tập Java
- hàm SQL COUNT[]
- Tham gia bên trong SQL
- Hàm JavaScript Bài tập
- Hướng dẫn Python
- Bài tập mảng Python
- Tham gia chéo SQL
- Bài tập về mảng Sharp trong C#