C# 实验
一、实验目的:
熟悉C#的面向对象程序设计方法,掌握类、方法、属性和对象的定义与实现方法
二、实验内容:
创建控制台应用程序,根据要求使用面向对象程序设计方法,编译和运行程序。
一、 定义一个“学生”类,继承“人”类,并增加班级、总分数据域,以及判断是否合格的函数,合格的标准是:年龄在16-20岁之间,总分大于480分,或者年龄在21-30之间,总分大于450分。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary1
{
public class Class1
{
static void Main(string[] args)
{
//测试学生小明
student xiaoming = new student();
xiaoming.Name = "小明";
xiaoming.Age = 18;
xiaoming.Classroom = 1;
xiaoming.Sum = 430;
xiaoming.message();
Console.WriteLine(",在{0}班,总分为{1}",xiaoming.Classroom,xiaoming.Sum
);
xiaoming.quiz();
Console.ReadLine();
}
}
class people
{
string name;
int age;
public void message()
{
Console.Write(Name + "的年龄为" + Age + "岁");
}
public string Name { get => name; set => name = value; }
public int Age { get => age; set => age = value; }
}
class student:people
{
int classroom;
float sum;
public void quiz()
{
if ((Age >= 16 && Age <= 20 && Sum > 480) || (Age >= 21 && Age <= 30 && Sum > 450))
Console.WriteLine("合格");
else
Console.WriteLine("不合格");
}
public int Classroom { get => classroom; set => classroom = value; }
public float Sum { get => sum; set => sum = value; }
}
}
二、
(1)定义一个“四边形”类,包含4个顶点坐标,以及定义判断这4个顶点是否构成四边形的函数,并计算其面积。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp13
{
class Program
{
static void Main(string[] args)
{
SBX sbx = new SBX();
Console.Write("请输入第1个点x:");
sbx.x1 = Convert.ToInt16(Console.ReadLine());
Console.Write("请输入第1个点y:"); sbx.y1 = Convert.ToInt16(Console.ReadLine());
Console.Write("请输入第2个点x:"); sbx.x2 = Convert.ToInt16(Console.ReadLine());
Console.Write("请输入第2个点y:"); sbx.y2 = Convert.ToInt16(Console.ReadLine());
Console.Write("请输入第3个点x:"); sbx.x3 = Convert.ToInt16(Console.ReadLine());
Console.Write("请输入第3个点y:"); sbx.y3 = Convert.ToInt16(Console.ReadLine());
Console.Write("请输入第4个点x:"); sbx.x4 = Convert.ToInt16(Console.ReadLine());
Console.Write("请输入第4个点y:"); sbx.y4 = Convert.ToInt16(Console.ReadLine());
if (sbx.IsSBX())
{
Console.WriteLine("该四边形的面积是{0}", sbx.MJ());
Console.ReadLine();
}
else
{
Console.WriteLine("不能构成四边形!");
Console.ReadLine();
}
}
}
class SBX
{
public double x1, x2, x3, x4, y1, y2, y3, y4;
public bool IsSBX()
{
if (((y3 - y4) * (x3 - x1) == (x3 - x4) * (y3 - y1)) ||
((y3 - y2) * (x3 - x4) == (x3 - x2) * (y3 - y4)) ||
((y4 - y2) * (x4 - x1) == (x4 - x2) * (y4 - y1)) ||
((y3 - y2) * (x3 - x1) == (x3 - x2) * (y3 - y1)))
return false;//任意三个顶点成直线,非四边形
else
return true;
}
public double MJ()
{
return Math.Abs(x1 * y2 + x2 * y3 + x3 * y1 - y1 * x2 - y2 * x3 - y3 * x1) / 2.0 +
Math.Abs(x4 * y2 + x2 * y3 + x3 * y4 - y4 * x2 - y4 * x3 - y3 * x4) / 2.0;
}
}
}
(2)继承“四边形”类,定义“平行四边形”类,增加判断是否为平行四边形的函数。
class PXSBX:SBX
{
public bool ISPXSBX()
{
double a, b, c, d;
a = Math.Pow((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1), 0.5);
b = Math.Pow((x4 - x3) * (x4 - x3) + (y4 - y3) * (y4 - y3), 0.5);
c = Math.Pow((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2), 0.5);
a = Math.Pow((x4 - x1) * (x4 - x1) + (y4 - y1) * (y4 - y1), 0.5);
if ((a == b) || (c == d))
return true;
else
return false;
}
}
(3) “平行四边形”类,定义“矩形”类,增加判断是否为矩形的函数。
class JX : PXSBX
{
public bool ISJX()
{
double A, B, C, D, E;
A = Math.Pow((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1), 0.5);
B = Math.Pow((x4 - x3) * (x4 - x3) + (y4 - y3) * (y4 - y3), 0.5);
C = Math.Pow((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2), 0.5);
D = Math.Pow((x4 - x1) * (x4 - x1) + (y4 - y1) * (y4 - y1), 0.5);
E = (x3 - x1) * (x3 - x1) + (y3 - y1) * (y3 - y1);
if ((E == C*C+ A * A) || (E == D*D + B*B))
return true;
else
return false;
}
}
结果图:
以上。