




已阅读5页,还剩63页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C#.NET实验报告 姓名 郭佳 班级 计142 学号 149074043 指导老师 邰伟鹏 实验一 创建简单的.NET应用程序【实验目的】熟悉VS2005开发环境,掌握如何在此开发环境下开发简单的.NET应用程序,以及调试程序的基本操作技巧。【实验内容】分别创建不同类型的.NET应用程序项目,体会基本的设计与编程方法。【实验要求】(1) 通过实验掌握【工具箱】、【属性】窗口、【解决方案资源管理器】等的用法和基本操作技巧。(2) 通过实验观察各种应用程序结构及特点;(3) 通过实验观察生成的可执行文件的存放位置,掌握项目备份与恢复的方法;(4) 通过实验掌握利用断点进行程序调试的方法。【源代码】步骤1using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace SimpleWindowsApplication public partial class FormMain : Form public FormMain() InitializeComponent(); private void buttonExit_Click(object sender, EventArgs e) this.Close(); 步骤2 using System;using System.Collections.Generic;using System.Text;namespace SimpleConsoleApplication class Program static void Main(string args) Console.Write(请输入一个字符串:); string welcomeString=Console.ReadLine(); Console.WriteLine(Welcome:0,welcomeString); Console.ReadLine(); 步骤3using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page protected void Page_Load(object sender, EventArgs e) protected void ButtonOK_Click(object sender, EventArgs e) Response.Write(alert(哈哈哈);); 【运行结果】步骤1步骤2步骤3实验二 C#基本编程方式【实验目的】(1) 练习C#中变量声明和赋值的方法。(2) 练习类型转换的方法。(3) 练习分支语的基本用法。(4) 练习循环语句的用法。【实验内容】 为银行个人存款客户提供一个 “超级存款计算器” , 以简单直观的操作界面为客户提供一个银行存款本息到期金额结算查询程序, 以便客户决定选择那种存款方式。 用户输入存款金额及相应信息后, 单击【计算】按钮,程序能自动在【到期结算总额】中显示到期应得的本金和利息合计总金额。【实验要求】(1) 要求用startAmount表示初始存款金额。(2) 要求用yearRate表示年数。(3) 要求用years表示年数。(4) 要求用calculateFrequency保存用户选择的计算方式,即“按月算息”、“按季度算息”和“按年算息”,当用户在【计算方式】中选择某个计算方式后,程序会根据选择结果对calculateFrequency赋以相应的字符串值,如赋值为“按月算息”。(5) 要求用rate表示按选择的计算方式使用的利率。(6) 要求将计算出的结算总金额赋给total变量,并在只读的textBoxTotal中显示结果。【实验代码】 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace SuperCalculator public partial class FormMain : Form public FormMain() InitializeComponent(); this.StartPosition = FormStartPosition.CenterScreen; / / 将字符串转换为32位整数或64位浮点数 / / 被转换的字符串 / 是否有必须大于零的要求 / 转换后的结果 / private bool ConvertStringToNumber(string str, bool mustGreatThanZero, out int result) bool isValid = false; if (int.TryParse(str, out result) = false) MessageBox.Show(string.Format(无法将0转换为整数, str); else if (result = 0) MessageBox.Show(string.Format(0不是正数, str); else isValid = true; return isValid; / / 将字符串转换为32位整数或64位浮点数 / / 被转换的字符串 / 是否有必须大于零的要求 / 转换后的结果 / private bool ConvertStringToNumber(string str, bool mustGreatThanZero, out double result) bool isValid = false; if (double.TryParse(str, out result) = false) MessageBox.Show(string.Format(无法将0转换为实数, str); else if (result = 0) MessageBox.Show(string.Format(0不是正数, str); else isValid = true; return isValid; private void buttonOk_Click(object sender, EventArgs e) int startAmount; /存款金额 double yearRate; /年利率 int years; /存期 if (ConvertStringToNumber(textBoxStartAmount.Text, true, out startAmount) = false) return; if (startAmount 100) MessageBox.Show(金额不能小于100元); return; if (ConvertStringToNumber(textBoxYearRate.Text, true, out yearRate) = false) return; yearRate /= 100; if (ConvertStringToNumber(textBoxYears.Text, true, out years) = false) return; if (comboBoxCalculateFrequency.SelectedIndex = -1) MessageBox.Show(请选择提供的利息计算方式); return; string calculateFrequency = comboBoxCalculateFrequency.SelectedItem.ToString(); switch (calculateFrequency) case 按月计息: textBoxTotal.Text = string.Format(0:F2元, Caculate(startAmount, yearRate / 12, years * 12); break; case 按季度计息: textBoxTotal.Text = string.Format(0:F2元, Caculate(startAmount, yearRate / 4, years * 4); break; case 按年计息: textBoxTotal.Text = string.Format(0:F2元, Caculate(startAmount, yearRate, years); break; / / 计算到期结算金额 / / 存款金额 / 利率 / 叠加次数 / private double Caculate(double startAmount, double rate, int count) double total = startAmount; for (int i = 1; i = count; i+) total += total * rate; return total; private void FormMain_Shown(object sender, EventArgs e) textBoxStartAmount.Focus(); private void groupBox1_Enter(object sender, EventArgs e) /保证修改任一输入值时,不显示计算结果 textBoxTotal.Clear(); private void FormMain_Load(object sender, EventArgs e) private void label1_Click(object sender, EventArgs e) private void textBoxStartAmount_TextChanged(object sender, EventArgs e) 【运行结果】实验三 面向对象的编程基础【实验目的】(1) 练习如何创建类和对象。(2) 练习如何为定义的类编写相应的方法。(3) 练习如何通过属性访问对象中的数据。(4) 练习如何创建类及其派生类。【实验要求】为个人银行存款账户定义两个类,一个是活期存款账户CheckingCustom类,另一个是定期存款账户FixedCustom类。【实验代码】 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace BankCustoms public partial class FormMain : Form /定义一个枚举类型,表示提供的业务类型 private enum CustomType 活期存款, 定期存款 /保存所有活期存款帐户,用帐户名做键,同一个人只能有一个活期帐号 static SortedList checkingCustomers = new SortedList(); /保存所有定期存款帐户,用帐户名做键,同一个人只能有一个定期帐号 static SortedList fixedCustomers = new SortedList(); public FormMain() InitializeComponent(); /将表示存款类型的所有枚举值添入到下拉列表框中 comboBoxAccountType.Items.AddRange(Enum.GetNames(typeof(CustomType); /设置默认选项为活期存款 comboBoxAccountType.SelectedIndex = 0; / / 活期存款或定期存款开户 / / 存款类型,有两种:活期存款,定期存款 / 帐户名 / 开户金额 private void CreateCustom(CustomType customType, string userName, double initMoney) if (customType = CustomType.活期存款) try CheckingCustom newCustom = new CheckingCustom(userName, initMoney); checkingCustomers.Add(userName, newCustom); AddItemToListBox(string.Format(0开户1元, userName, initMoney), newCustom); catch (Exception err) MessageBox.Show(err.Message); else /定期存款 try FixedCustom newCustom = new FixedCustom(userName, initMoney); fixedCustomers.Add(userName, newCustom); AddItemToListBox(string.Format(0开户1元, userName, initMoney), newCustom); catch (Exception err) MessageBox.Show(err.Message); / / 将操作信息添加到状态信息列表中 / / 提示信息 / 活期存款帐户 private void AddItemToListBox(string prompt, CheckingCustom custom) listBoxStatus.Items.Add(string.Format(0, 帐户信息:帐户名1,账号2:d4,当前余额3:n2元, prompt, custom.AccountName, custom.AccountNumber, custom.AccountBalance); / / 将操作信息添加到状态信息列表中 / / 提示信息 / 定期存款帐户 private void AddItemToListBox(string prompt, FixedCustom custom) listBoxStatus.Items.Add(string.Format(0, 帐户信息:帐户名1,账号2:d4,当前余额3:n2元, prompt, custom.AccountName, custom.AccountNumber, custom.AccountBalance); private void buttonDeposit_Click(object sender, EventArgs e) double money; if (double.TryParse(textBoxMoney.Text, out money) = false) MessageBox.Show(存款金额不正确, 无法存款, MessageBoxButtons.OK, MessageBoxIcon.Question); return; if (comboBoxAccountType.SelectedItem.ToString() = 活期存款) if (checkingCustomers.ContainsKey(textBoxUserName.Text) = false) /如果无此帐户,创建帐户,并将存款作为开户金额 CreateCustom(CustomType.活期存款, textBoxUserName.Text, money); else /如果有此帐户,在现有帐户上追加存款 try CheckingCustom custom = checkingCustomerstextBoxUserName.Text; custom.Diposit(money); AddItemToListBox(string.Format(0存款1元, textBoxUserName.Text, money), custom); catch (Exception err) MessageBox.Show(err.Message); else if (fixedCustomers.ContainsKey(textBoxUserName.Text) = false) CreateCustom(CustomType.定期存款, textBoxUserName.Text, money); else try FixedCustom custom = fixedCustomerstextBoxUserName.Text; custom.Diposit(money); AddItemToListBox(string.Format(0存款1元, textBoxUserName.Text, money), custom); catch (Exception err) MessageBox.Show(err.Message); private void buttonWithdraw_Click(object sender, EventArgs e) double money; if (double.TryParse(textBoxMoney.Text, out money) = false) MessageBox.Show(取款金额不正确, 无法取款, MessageBoxButtons.OK, MessageBoxIcon.Question); return; if (comboBoxAccountType.SelectedItem.ToString() = 活期存款) if (checkingCustomers.ContainsKey(textBoxUserName.Text) = false) MessageBox.Show(无此帐户,无法取款, 取款失败, MessageBoxButtons.OK, MessageBoxIcon.Question); else /如果有此帐户,在现有帐户中取款 try CheckingCustom custom = checkingCustomerstextBoxUserName.Text; /获取结算前余额 double balance = custom.AccountBalance; /取款 custom.Withdraw(money); AddItemToListBox(string.Format(0取款1元,结算利息2:n2元, textBoxUserName.Text, money, custom.AccountBalance + money - balance), custom); catch (Exception err) MessageBox.Show(err.Message); else if (fixedCustomers.ContainsKey(textBoxUserName.Text) = false) MessageBox.Show(无此帐户,无法取款, 取款失败, MessageBoxButtons.OK, MessageBoxIcon.Question); else /如果有此帐户,在现有帐户中取款 try FixedCustom custom = fixedCustomerstextBoxUserName.Text; /获取结算前余额 double balance = custom.AccountBalance; /取款 custom.Withdraw(money); AddItemToListBox(string.Format(0取款1元,结算利息2:n2元, textBoxUserName.Text, money, custom.AccountBalance + money - balance), custom); catch (Exception err) MessageBox.Show(err.Message); private void buttonShowCustoms_Click(object sender, EventArgs e) listBoxStatus.Items.Clear(); for (int i = 0; i checkingCustomers.Count; i+) AddItemToListBox(活期存款, checkingCustomers.Valuesi); for (int i = 0; i fixedCustomers.Count; i+) AddItemToListBox(定期存款, fixedCustomers.Valuesi); 【运行结果】实验四 界面设计与文件存取【实验目的】(1) 练习界面功能的设计方法。(2) 练习文本文件的存取方法。(3) 练习对话框的使用方法。(4) 练习菜单的设计方法。(5) 练习工具条和状态条的设计方法。(6) 练习一个窗体调用另一个窗体以及窗体间参数传递的方法。【实验内容】重新设计和处理与个人存取款相关的业务,假定处理业务仅有活期存款一种,而且业务处理规定与实验三的活期存款业务相同。要求功能界面中包括菜单条、工具条和状态条,并要求在文本文件中保存账户的相关信息。【实验代码】 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace MenuBankCustoms public partial class FormMain : Form SortedList accounts = new SortedList(); Customers customs = new Customers(); public FormMain() InitializeComponent(); / / 将操作信息添加到状态信息列表中 / / 提示信息 / 活期存款帐户 private void AddItemToListBox(string prompt, Account account) listBoxStatus.Items.Add(string.Format(0, 帐户信息:帐户名1,帐号2:d4,当前余额3:n2元, prompt, account.AccountName, account.AccountNumber, account.AccountBalance); private void menuItemOpen_Click(object sender, EventArgs e) accounts = customs.Load(); listBoxStatus.Items.Clear(); for (int i = 0; i accounts.Count; i+) AddItemToListBox(记录+(i+1), accounts.Valuesi); private void menuItemSave_Click(object sender, EventArgs e) if (customs.Save(accounts) = true) MessageBox.Show(保存成功, 恭喜, MessageBoxButtons.OK, MessageBoxIcon.Information); private void buttonDeposit_Click(object sender, EventArgs e) double money; if (double.TryParse(textBoxMoney.Text, out money) = false) MessageBox.Show(存款金额不正确, 无法存款, MessageBoxButtons.OK, MessageBoxIcon.Question); return; if (accounts.Contai
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论