C#简单实现
编写一个控制台应用程序,输入三角形或者长方形边长,计算其周长和面积并输出
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace C_Lab1
{
class Triangle {
double side1 = double.Parse(Console.ReadLine());
double side2 = double.Parse(Console.ReadLine());
double side3 = double.Parse(Console.ReadLine());
public void area()
{
double area;
double Cosc = (side1 * side1 + side2 * side2 - side3 * side3) / (2 * side1 * side2);
area = 0.5*side1*side2*System.Math.Sqrt(1-Cosc*Cosc);
Console.WriteLine("a为{0}", side1);
Console.WriteLine("b为{0}", side2);
Console.WriteLine("c为{0}", side2);
Console.WriteLine("三角形的面积为{0}", area);
}
public void input() {
Console.WriteLine("请输入三角形的三边长度");
}
}
class rectangle
{
double side1 = double.Parse(Console.ReadLine());
double side2 = double.Parse(Console.ReadLine());
public void area()
{
double area;
area = side1 * side2;
Console.WriteLine("长方形长为{0}", side1);
Console.WriteLine("长方形宽为{0}", side2);
Console.WriteLine("长方形的面积为{0}", area);
}
public void input()
{
Console.WriteLine("请输入长方形的长和宽");
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请选择要计算面积的图形 1 三角形 2 长方形");
//choice = Console.Read();
//Console.WriteLine(choice);
int choice = int.Parse(Console.ReadLine());
if (choice == 1) {
Console.WriteLine("请输入三角形的三边长:输入一个数据回车一次");
Triangle tri = new Triangle();
tri.area();
}
else if (choice == 2)
{
Console.WriteLine("请输入长方形的长和宽:输入一个数据回车一次");
rectangle rec = new rectangle();
rec.area();
}
//Console.WriteLine("Hello World!");//这个一句输出语句!
//Console.ReadKey();//这个语句为了防止输出窗口一跳而过!
}
}
}