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 ConsoleApp1
{
public class Human
{
public Human(string name, int age)
{
this.Name = name;
this.Age = age;
}
public string Name { get; set; }
public int Age { get; set; }
}
public class Student : Human
{
public Student(string name, int age, string classroom, int score) : base(name, age)
{
this.classroom = classroom;
this.score = score;
}
public string classroom { get; set; }//班级
public int score { get; set; }//总分
public bool IsQualified//判断条件
{
get
{
bool qualified = false;
if (Age >= 16 && Age <= 20)
{
qualified = this.score >= 480;
}
else if (Age >= 21 && Age <= 30)
{
qualified = this.score >= 450;
}
return qualified;
}
}
}
class Program
{
static void Main(string[] args)
{
Student st = new Student("小明", 17, "1", 480);
Console.WriteLine("{0}的年龄为{1}岁,在{2}班,总分为{3}",st.Name,st.Age,st.classroom,st.score);
if (st.IsQualified)
{
Console.WriteLine("{0}的成绩合格",st.Name);
}
else
{
Console.WriteLine("{0}的成绩不合格",st.Name);
}
Console.ReadLine();
}
}
}
运行结果:
二、
(1)定义一个“四边形”类,包含4个顶点坐标,以及定义判断这4个顶点是否构成四边形的函数,并计算其面积。
//判断四边形
class SBX
{
public int x1, x2, x3, x4, y1, y2, y3, y4;//顶点坐标
public bool IsSBX()//判断是否为四边形
{
if ((y3 - y4) * (x3 - x1) == (y3 - y1) * (x3 - x4) ||
(y3 - y2) * (x3 - x4) == (y3 - y4) * (x3 - x2) ||
(y4 - y2) * (x4 - x1) == (y4 - y1) * (x4 - x2) ||
(y3 - y2) * (x3 - x1) == (y3 - y1) * (x3 - x2))//任意三个顶点成直线,非四边形
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(x2*y3+x3*y4+x4*y2-y2*x3-y3*x4-y4*x2)/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((x4-x1)*(x4-x1)+(y4-y1)*(y4-y1),0.5);
d = Math.Pow((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2),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 == A * A + C * C) || (E == B * B + D * D))
return true;
else
return false;
}
}
主方法:
class Program
{
static void Main(string[] args)
{
JX jx = new JX();
Console.Write("请输入第1个点X:"); jx.x1 = Convert.ToInt16(Console.ReadLine());
Console.Write("请输入第1个点Y:"); jx.y1 = Convert.ToInt16(Console.ReadLine());
Console.Write("请输入第2个点X:"); jx.x2 = Convert.ToInt16(Console.ReadLine());
Console.Write("请输入第2个点Y:"); jx.y2 = Convert.ToInt16(Console.ReadLine());
Console.Write("请输入第3个点X:"); jx.x3 = Convert.ToInt16(Console.ReadLine());
Console.Write("请输入第3个点Y:"); jx.y3 = Convert.ToInt16(Console.ReadLine());
Console.Write("请输入第4个点X:"); jx.x4 = Convert.ToInt16(Console.ReadLine());
Console.Write("请输入第4个点Y:"); jx.y4 = Convert.ToInt16(Console.ReadLine());
if (jx.IsSBX())
{
Console.WriteLine("该四边形面积={0}",jx.Mj());
//Console.ReadLine();
}
else
{
Console.WriteLine("该点不能构成四边形!");
//Console.ReadLine();
}
if (jx.IsPXSBX())
{
Console.WriteLine("该四边形是平行四边形!");
//Console.ReadLine();
}
else
{
Console.WriteLine("该四边形不是平行四边形!");
//Console.ReadLine();
}
if (jx.IsJX())
{
Console.WriteLine("该四边形是矩形!");
Console.ReadLine();
}
else
{
Console.WriteLine("该四边形不是矩形!");
Console.ReadLine();
}
}
}
运行结果:
三、设计一个银行账号类,其中包括: Private数据域表示账户信息,包括账号、姓名、开户时间、身份证号码; Public方法,包括开户、存款、取款、查询余额。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace bank
{
public class Account
{
private int accountNumber;//银行账号
private string name;//姓名
private string openTime;//开户时间
private string cardNumber;//身份证号码
private double money;//余额
//开户
public Account()
{
Random rnum = new Random();
accountNumber = rnum.Next(100000,500000);//产生一个100000~500000的随机数
money = 0.0;//账号默认为0
}
//存款
public void addMoney()
{
double money;
Console.Write("请输入您存款的数目:");
money = Convert.ToDouble(Console.ReadLine());
if (money>0)
{
this.money = this.money + money;
Console.WriteLine("存款成功!");
}
else
{
Console.WriteLine("存款金额无效!存款失败!");
}
}
//取款
public void subMoney()
{
double money;
Console.Write("请输入您要取款的数目:");
money = Convert.ToDouble(Console.ReadLine());
if (money<=this.money)
{
this.money = this.money - money;
Console.WriteLine("取款成功!");
}
else
{
Console.WriteLine("余额不足!取款失败!");
}
}
//查询余额
public void showMoney()
{
Console.WriteLine("余额为:{0}",this.money);
}
public void shuru()
{
Console.Write("请输入姓名:");
this.name = Console.ReadLine();
Console.Write("请输入身份证号:");
this.cardNumber = Console.ReadLine();
}
//显示账户信息
public void show()
{
this.openTime = DateTime.Now.ToString();
Console.WriteLine("您的账户信息为:\n银行账号\t姓名\t开户时间\t\t身份证号\n{0}\t\t{1}\t{2}\t{3}",this.accountNumber,this.name,this.openTime,this.cardNumber);
}
}
class Program
{
static void Main(string[] args)
{
Account account = new Account();
Console.Write("请选择操作:0 退出,1 开户,2 存款,3 取款,4 查询余额, 5 查询用户信息\n");
while (true)
{
int i = Convert.ToInt16(Console.ReadLine());
switch (i)
{
case 0:
Console.WriteLine("欢迎下次光临!");
System.Environment.Exit(0);
break;
case 1:
account.shuru();
break;
case 2:
account.addMoney();
break;
case 3:
account.subMoney();
break;
case 4:
account.showMoney();
break;
case 5:
account.show();
break;
}
}
}
}
}
运行结果: