我们有一些图形的边长数据,这些图形包括三角新和矩形,请你编写一个程序求出它们的面积。请你实现一个基础图形类Graph,然后实现三角形类Triangle和矩形类Rectangle

图形面积计算

我们有一些图形的边长数据,这些图形包括三角新和矩形,请你编写一个程序求出它们的面积。
请你实现一个基础图形类Graph,然后实现三角形类Triangle和矩形类Rectangle,继承自Graph。根据输入的边数实现不同的对象,并计算面积。
输入格式:
一行,一个整数n,表示图形个数。
n行,每行是用空格隔开的整数。
输出格式:
n行,每行是一个图形的面积。
输入样例:
2
5 5
6 6 6
输出样例:
25
15

参考代码如下:

import java.util.Scanner;
abstract class  Graph{
	abstract double getS();
}
class Triangle extends Graph{
	double a;
	double b;
	double c;
	Triangle(int a,int b,int c){
		this.a=a;
		this.b=b;
		this.c=c;
	}
	public double getS() {
		double p=(a+b+c)/2;
		return Math.sqrt(p*(p-a)*(p-b)*(p-c));
	}
	
}
class Rectangle extends Graph{
	double a;
	double b;
	Rectangle(double a,double b){
		this.a=a;
		this.b=b;
	}
	public double getS() {
		return a*b;
	}
	
}



public class TestS {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		int n=Integer.parseInt(input.nextLine());
		for(int i=0;i<n;i++) {
			String s=input.nextLine().trim();
			String []t=s.split(" ");
			int a=0,b=0,c=0;
			if(t.length==2) {
				a=Integer.parseInt(t[0]);
				b=Integer.parseInt(t[1]);
				Rectangle re=new Rectangle(a,b);
				System.out.println(re.getS());
			}
			else if(t.length==3) {
				a=Integer.parseInt(t[0]);
				b=Integer.parseInt(t[1]);
				c=Integer.parseInt(t[2]);
				Triangle tr=new Triangle(a,b,c);
				System.out.println(tr.getS());
			}
		}
		input.close();
		
		
	}

}

运行结果如下:
在这里插入图片描述

答案仅供参考,
谢谢!

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
A:好的,我可以帮您编写一个Python命令行程序来求解多种平面图形面积与周长。以下是程序的示例代码: ```python import math # 矩形面积计算函数 def rectangle_area(width, height): return width * height # 矩形周长计算函数 def rectangle_perimeter(width, height): return 2 * (width + height) # 梯形面积计算函数 def trapezoid_area(top, bottom, height): return (top + bottom) * height / 2 # 梯形周长计算函数 # 注意:梯形没有固定的周长,此处以底边和斜边之和作为周长 def trapezoid_perimeter(top, bottom, height): slope = math.sqrt(height ** 2 + (bottom - top) ** 2) return top + bottom + 2 * slope # 圆形面积计算函数 def circle_area(radius): return math.pi * radius ** 2 # 圆形周长计算函数 def circle_perimeter(radius): return 2 * math.pi * radius # 三角形面积计算函数 def triangle_area(base, height): return base * height / 2 # 三角形周长计算函数 # 注意:三角形没有固定的周长,此处计算三条边之和作为周长 def triangle_perimeter(side1, side2, side3): return side1 + side2 + side3 # 主程序循环 while True: # 输出提示信息,让用户选择需要计算的图形 print("选择需要计算的图形:") print("1. 矩形 2. 梯形 3. 圆形 4. 三角形") # 读取用户输入,根据用户选择调用相应的计算函数 choice = input("选择图形(输入数字1-4选择,输入q退出程序):") if choice == 'q': # 用户输入q时退出程序 break elif choice == '1': # 计算矩形面积与周长 width = float(input("输入矩形的宽度:")) height = float(input("输入矩形的高度:")) print("矩形面积为:", rectangle_area(width, height)) print("矩形周长为:", rectangle_perimeter(width, height)) elif choice == '2': # 计算梯形面积与周长 top = float(input("输入梯形的上底长度:")) bottom = float(input("输入梯形的下底长度:")) height = float(input("输入梯形的高度:")) print("梯形面积为:", trapezoid_area(top, bottom, height)) print("梯形周长为:", trapezoid_perimeter(top, bottom, height)) elif choice == '3': # 计算圆形面积与周长 radius = float(input("输入圆的半径:")) print("圆的面积为:", circle_area(radius)) print("圆的周长为:", circle_perimeter(radius)) elif choice == '4': # 计算三角形面积与周长 base = float(input("输入三角形的底边长度:")) height = float(input("输入三角形的高度:")) side1 = float(input("输入三角形的第一条边长:")) side2 = float(input("输入三角形的第二条边长:")) print("三角形面积为:", triangle_area(base, height)) print("三角形周长为:", triangle_perimeter(side1, side2, base)) else: print("输入有误,输入!") ``` 您可以将以上代码保存为一个Python文件(例如area_calculator.py),并在命令行中运行该文件,便可以使用该程序进行多种平面图形面积与周长计算。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值