运行效果预览:
先看实践应用要求:
1.编写一个函数,用于计算1!+2!+3!+4!+5!,在控制台或页面输出运行结果。
2.在控制台或页面输出九九乘法表。
3.输入10个以内的整数,输出该组整数的降序排列,要求采用数组实现。
4.计算两个数的商,在控制台或页面输出结果,要求包含异常处理。
5.定义一个汽车类,该类具有重量和速度属性;再定义一个跑车类,该类继承汽车类的属性,并拥有自己的颜色属性;然后声明一个汽车类的对象和一个跑车类的对象,并把它们的属性输出到控制台上。
6.假设某动物园管理员每天需要给他所负责饲养的狮子、猴子和鸽子喂食。请用一个程序来模拟他喂食的过程。
要求:
(1)饲养员喂食时,不同动物执行不同的吃的功能,例如狮子吃肉、猴子吃香蕉、鸽子吃大米等。
(2)饲养员喂动物时,不能使用判断语句判断动物类型。
(3)使用虚方法或抽象方法实现喂养不同动物的多态,不能使用方法重载。
提示:需要建一个动物类,动物类有一个虚的或抽象的吃方法,动物类下面有几个子类,不同的子类重写父类的吃方法。饲养员类提供喂食方法。然后,在Main方法中一一调用吃的方法。
前端practice.aspx源码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="practice.aspx.cs" Inherits="practice" enableEventValidation="false"%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>实验二 C#程序设计练习</title>
<style type="text/css">
button{
width:120px;
height:30px;
background:#9933FA;
color:#fff;
}
button:hover{
background:#9A32CD;
}
div{
text-align:center;
}
h2{
text-align:center;
color:red;
}
span{
color:gray;
}
textarea{
width:900px;
height:400px;
font-size:20px;
}
</style>
</head>
<body id="mybody">
<form id="form1" runat="server">
<br /><br />
<div id="div_btn" runat="server">
<button id="Button1" onserverclick="factorial" runat="server">factorial</button>
<button id="Button2" onserverclick="multiplication" runat="server">multiplication</button>
<button id="Button3" onserverclick="bubbleSort" runat="server">bubbleSort</button>
<button id="Button4" onserverclick="division" runat="server">division</button>
<button id="Button5" onserverclick="car" runat="server">car</button>
<button id="Button6" onserverclick="zoo" runat="server">zoo</button>
</div>
<br />
<div id="div_bubble" runat="server" EnableViewState="false" visible="false" >
输入参数:<asp:TextBox id="TextNumber" runat="server" OnTextChanged="bubbleBtnClick" AutoPostBack ="true"></asp:TextBox> <asp:Label ID="eMesg_bubble" runat="server">10个以内整数,空格分隔,Enter/Tap键执行排序</asp:Label><br /><br />
</div>
<div id="div_division" runat ="server" EnableViewState="false" visible="false" >
输入被除数:<asp:TextBox id="textA" runat="server"></asp:TextBox>
输入除数:<asp:TextBox id="textB" runat="server" OnTextChanged="divisionBtnClick" AutoPostBack ="true"></asp:TextBox>
<asp:Label ID="eMesg_division" runat="server">Enter/Tap键执行计算</asp:Label><br /><br />
</div>
<div id="div_view" runat="server" EnableViewState="false">
</div>
<div>
<textarea id="displayArea" runat="server" rows="1" cols="1">输出结果将在此处显示.......</textarea>
</div>
</form>
</body>
</html>
后端practice.aspx.cs源码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class practice : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("<h2>单击按钮分别显示各个练习题代码运行结果</h2>");
div_division.Visible = false;
div_bubble.Visible = false;
}
protected void factorial(object sender, EventArgs e)
{
if (IsPostBack)
{
div_bubble.Visible = false;
div_division.Visible = false;
div_view.InnerHtml = "Follow will calculate the union of \"1!+2!+3!+4!+5!\":";
}
factorial calculate = new factorial();
displayArea.InnerHtml = "1!+2!+3!+4!+5!=" + calculate.add().ToString();
}
protected void multiplication(object sender, EventArgs e)
{
if (IsPostBack)
{
div_bubble.Visible = false; div_division.Visible = false;
}
int i, j;
displayArea.InnerHtml = ""; //擦除前一次对textarea的写入记录
for (i = 1; i < 10; i++)
{
for (j = 1; j <= i; j++)
{
displayArea.InnerHtml+=j+"*"+i+"="+(i * j)+"\t";
}
displayArea.InnerHtml += "\n";
}
}
protected void bubbleSort(object sender, EventArgs e)
{
if (IsPostBack)
{
div_bubble.Visible = false;
div_division.Visible = false;
}
div_bubble.Visible = true;
displayArea.InnerHtml = "bubbleSort";
}
protected void bubbleBtnClick(object sender, EventArgs e)
{
if (IsPostBack)
{
div_bubble.Visible = false;
div_division.Visible = false;
}
div_bubble.Visible = true;
String str=TextNumber.Text;
//String[] a=str.Split();//稍候再写,未完
/* int i = 0, j=1; int[] number = new int[12]; while (i < str.Length) { if (str[i].Equals(' ')) { i++; } else { number[j] = int.Parse (str[i].ToString ()); } } */
displayArea.InnerHtml = ""; //擦除前一次textarea输出记录
displayArea.InnerHtml =str;
}
protected void division(object sender, EventArgs e)
{
if (IsPostBack)
{
div_bubble.Visible = false;
div_division.Visible = false;
}
div_division.Visible = true;
displayArea.InnerHtml = "两数求商,输入被除数与除数后,按 Enter / Tap 键执行运算";
}
protected void divisionBtnClick(object sender,EventArgs e)
{
if (IsPostBack)
{
div_bubble.Visible = false;
div_division.Visible = false;
}
float a = float.Parse(textA.Text);
float b = float.Parse(textB.Text);
float result;
div_division.Visible = true;
try
{
if (b == 0)
throw new Exception("Error:DivideByZeroException[除数为零]");
result = a / b;
displayArea.InnerHtml = a + "/" + b + "=";
displayArea.InnerHtml += a / b;
}
catch (Exception error)
{
eMesg_division.Text = error.Message;
}
}
protected void car(object sender, EventArgs e)
{
if (IsPostBack)
{
div_bubble.Visible = false;
div_division.Visible = false;
}
car cardemo = new car();
supercar supercardemo = new supercar();
cardemo.set(2500, 180);
displayArea.InnerHtml = "汽车类的质量:" + cardemo.weight + "Kg,汽车类的速度:" + cardemo.speed + "Km/h\n";
supercardemo.color = "red";
cardemo.set(2500, 180);
supercardemo.set(2000, 250);
displayArea.InnerHtml +="超跑类的重量:"+supercardemo.weight+"Kg,超跑类的速度:"+supercardemo.speed+"Km/h,超跑的颜色:"+supercardemo.color; }
protected void zoo(object sender, EventArgs e)
{
if (IsPostBack)
{
div_bubble.Visible = false;
div_division.Visible = false;
}
feeder feederdemo = new feeder();
animals animalsdemo = new animals();
String food = "null";
String foodmeat,foodbanana,foodrice,meat,banana,rice;
food = "meat";
foodmeat=feederdemo.feed(food);
animalsdemo = new lion();
meat=animalsdemo.eat();
displayArea.InnerHtml = foodmeat + "\n" + meat + "\n";
food = "banana";
foodbanana=feederdemo.feed(food);
animalsdemo = new monkey();
banana=animalsdemo.eat();
displayArea.InnerHtml += foodbanana + "\n" + banana + "\n";
food = "rice";
foodrice=feederdemo.feed(food);
animalsdemo = new pigeon();
rice=animalsdemo.eat();
displayArea.InnerHtml += foodrice + "\n" + rice + "\n";
//displayArea.InnerHtml = foodmeat + "\n" + meat + "\n" + foodbanana + "\n" + banana + "\n" + foodrice + "\n" + rice;
}
}
//1!+2!+3!+4!+5!
class factorial {
public int add()
{
int i = 1, temp = 1, sum = 0;
for (; i < 6; i++)
{
temp = i * temp;
sum = sum + temp;
}
return sum;
}
}
//bubbleSort
/* class bubbleSort {
public void sort(int n)
{
int i, j;
for (i = 1; i < n; i++)
{
for (j = i; j < n; j++)
{
if (a[i] > a[j])
{
a[0] = a[i]; a[i] = a[j]; a[j] = a[0];
}
}
}
}
} */
//car class
class car
{
public float weight;
public float speed;
public void set(float weight, float speed)
{
this.weight = weight;
this.speed = speed;
}
}
class supercar : car {
public String color;
}
//zoo
class feeder
{
public String feed(String food)
{
return (food);
}
}
class animals
{
public virtual String eat()
{
return "feed animals with food!";
}
}
class lion : animals
{
public override String eat()
{
return "\'狮子\'吃的是 meat ......";
}
}
class monkey : animals
{
public override String eat()
{
return "啾~,俺老孙来也,\'猴子\'就爱吃 banana ......";
}
}
class pigeon : animals
{
public override String eat()
{
return "听~,是\'鸽子\'在啄食 rice ......";
}
}
网页初次打开运行效果如图:
单击按钮分别显示各个练习题代码运行结果
multiplication
bubbleSort
division
car
zoo