《实验三C和ASP4.doc》由会员分享,可在线阅读,更多相关《实验三C和ASP4.doc(12页珍藏版)》请在课桌文档上搜索。
1、实验三 C#和ASP.NET 4.5 一、实验名称:C#和ASP.NET 4.5二、实验容:1.转换输入的成绩到相应的等级1控件设置:2单击事件:protectedvoid btnSubmit_Click(object sender, EventArgs e) float fGrade = float.Parse(t*tInput.Te*t);int iGrade = (int)(fGrade / 10);switch (iGrade) case 10:case 9: lblDisplay.Te*t = 优秀;break;case 8: lblDisplay.Te*t = 良好;break;c
2、ase 7: lblDisplay.Te*t = 中等;break;case 6: lblDisplay.Te*t = 及格;break;default: lblDisplay.Te*t = 不及格;break; 2.在Web窗体中输出九九乘法表1登录事件protectedvoid Page_Load(object sender, EventArgs e) for (int i = 1; i = 9; i+) for (int j = 1; j = i; j+) Response.Write(i.ToString() + * + j.ToString() + = + (i * j).ToStr
3、ing(); Response.Write(  ); Response.Write(); 3.输入一组以空格间隔的共10个以的整数,输出该组整数的降序排列1控件设置2单击事件protectedvoid btnSubmit_Click(object sender, EventArgs e) string sInput = t*tInput.Te*t.Trim() + ;int j = 0;int aInput = newint10;string temp = 0;for (int i = 0; i = sInput.Length - 1; i+) if (sInput.Subs
4、tring(i, 1) != ) temp += sInput.Substring(i, 1); else aInputj = int.Parse(temp); j+; temp = 0; Array.Sort(aInput);Array.Reverse(aInput);foreach (int i in aInput) if (i != 0) Response.Write(i+ ); 4.计算两个数的商1控件设置2单击事件protectedvoid btnSubmit_Click(object sender, EventArgs e) try float divsor
5、= float.Parse(t*tDivsor.Te*t);float dividend = float.Parse(t*tDividend.Te*t); Response.Write(商为: + divsor / dividend); catch (E*ception ee) Response.Write(请输入正确的数字!); 5.设计并实现一个用户信息类UserInfopublicclassUserInfoprivatestring _Name;privateDateTime _Birthday;publicstring Name get return _Name;set _Name =
6、 value; publicDateTime Birthday get return _Birthday; set _Birthday = value; public UserInfo(string name,DateTime birthday)this._Name = name;this._Birthday = birthday;publicstring DecideAge() if (DateTime.Now.Year - _Birthday.Year 18) returnthis._Name + ,您还没长大呢?; else returnthis._Name + ,您是成人了!; 6.在
7、Web窗体中应用UserInfo类1控件设置2单击事件protectedvoid btnSubmit_Click(object sender, EventArgs e) string name = t*tName.Te*t;string brithday = t*tBrithday.Te*t;UserInfo userInfo = newUserInfo(name ,DateTime.ParseE*act(brithday,yyyyMMdd,null); Response.Write(userInfo.DecideAge(); 7.调试九九乘法表三、习题:1.扩大成及转换程序。要求增加对输入成
8、绩的合法性判断。protectedvoid btnSubmit_Click(object sender, EventArgs e) try float fGrade = float.Parse(t*tInput.Te*t);if (fGrade = 0 & fGrade = 100) int iGrade = (int)(fGrade / 10);switch (iGrade) case 10:case 9: lblDisplay.Te*t = 优秀;break;case 8: lblDisplay.Te*t = 良好;break;case 7: lblDisplay.Te*t = 中等;br
9、eak;case 6: lblDisplay.Te*t = 及格;break;default: lblDisplay.Te*t = 不及格;break; else lblDisplay.Te*t = 请输入正确的成绩!; catch (E*ception ee) lblDisplay.Te*t = 不合法!; 2.将九九乘法表改成如图3-13所时的浏览效果。protectedvoid Page_Load(object sender, EventArgs e) for (int i = 1; i = 9; i+) for (int j = i; j = 9; j+) Response.Write
10、(i.ToString() + * + j.ToString() + = + (i * j).ToString(); Response.Write(  ); Response.Write(); 3.完善实验容3的程序,要求能完成包含0和负数的排序protectedvoid btnSubmit_Click(object sender, EventArgs e) string sInput = t*tInput.Te*t.Trim() + ;int j = 0;int a=0;for (int * = 0; * = sInput.Length - 1; *+) if ( sInp
11、ut.Substring(*, 1) = ) a+; int aInput = newinta;string temp = ;for (int i = 0; i = sInput.Length - 1; i+) if (sInput.Substring(i, 1) = -) while (sInput.Substring(i, 1) != ) temp += sInput.Substring(i, 1); i+; aInputj =int.Parse(temp); j+; temp = ; else if (sInput.Substring(i, 1) != ) temp += sInput.
12、Substring(i, 1); else aInputj = int.Parse(temp); j+; temp = ; Array.Sort(aInput);Array.Reverse(aInput);foreach (int i in aInput) if (i != 0) Response.Write(i + ); else if (i = 0) Response.Write(0 + ); 4.使用ArrayList类实现降序排列一组整数的功能ArrayList类:publicclassArrayListpublic ArrayList(
13、string sInput)/ TODO: 在此处添加构造函数逻辑/this._sInput = sInput; privatestring _sInput;privateDateTime _Birthday;publicstring sInput get return _sInput;set _sInput = value; publicstring J*() int j = 0; int a = 0;for (int * = 0; * = _sInput.Length - 1; *+) if (sInput.Substring(*, 1) = ) a+; int aInput = newi
14、nta;string temp = ;for (int i = 0; i = sInput.Length - 1; i+) if (sInput.Substring(i, 1) = -) while (sInput.Substring(i, 1) != ) temp += sInput.Substring(i, 1); i+; aInputj = int.Parse(temp); j+; temp = ; else if (sInput.Substring(i, 1) != ) temp += sInput.Substring(i, 1); else aInputj = int.Parse(t
15、emp); j+; temp = ; Array.Sort(aInput);Array.Reverse(aInput);string s = ;foreach (int i in aInput) if (i != 0) s+=i + else if (i = 0) s+=0 + return s; 设计页面using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI
16、.WebControls;publicpartialclassE*3_ArrayDescending : System.Web.UI.Pageprotectedvoid Page_Load(object sender, EventArgs e) protectedvoid btnSubmit_Click(object sender, EventArgs e) string sInput = t*tInput.Te*t.Trim() + ;ArrayList arraylist = newArrayList(sInput); Response.Write(arraylist.J*(); 6改写U
17、serInfo类,要求如下1增加一个事件ValidateBrithday。2改写DecideAge方法,当输入的生日值大于当前日期或小于1900-1-1时触发事件ValidateBrithday3设计页面并应用修改后的UserInfo类。改写的UserInfo类:publicclassUserInfoprivatestring _Name;privateDateTime _Birthday;publicstring Name get return _Name;set _Name = value; publicDateTime Birthday get return _Birthday; set
18、 _Birthday = value; public UserInfo(string name,DateTime birthday)this._Name = name;this._Birthday = birthday;publicdelegatevoidEventHandler(object sender, EventArgs e);/声明事件所需的代理publiceventEventHandler ValidateBrithday;publicvoid OnValidateBrithday(object sender, EventArgs e) if (ValidateBrithday !
19、= null) ValidateBrithday(this, e); publicstring DecideAge() if (DateTime.Now.Year- _Birthday.Year0| _Birthday.Year1900) OnValidateBrithday(this, EventArgs.Empty);return ; else if (DateTime.Now.Year - _Birthday.Year 18) returnthis._Name + ,您还没长大呢?; else returnthis._Name + ,您是成人了!; 设计页面using System;us
20、ing System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;publicpartialclassE*3_UserInfoPage : System.Web.UI.Pageprotectedvoid Page_Load(object sender, EventArgs e) privatevoid V(object sender, EventArgs e) Response.Write(请输入合法的生日!); protec
21、tedvoid btnSubmit_Click(object sender, EventArgs e) string name = t*tName.Te*t;string brithday = t*tBrithday.Te*t;UserInfo v = newUserInfo(name, DateTime.ParseE*act(brithday, yyyyMMdd, null); v.ValidateBrithday += newUserInfo.EventHandler(V); v.DecideAge();UserInfo userInfo = newUserInfo(name,DateTime.ParseE*act(brithday,yyyyMMdd,null); Response.Write(userInfo.DecideAge();