浙江经贸职业技术学院实验报告
一、实验目的
进一步熟悉Visual Studio .NET 2008开发环境;
掌握分支语句和循环语句的使用;
熟悉并掌握TextBox、ComboBox、NumericUpDown等控件的使用;
掌握调试工具的使用。
二、实验内容
使用Visual Studio .NET 2008,完成以下程序:
程序1、完成“使用条件语句”的课堂练习(Conditions)。
程序2、完成“使用循环语句”的课堂练习(Loops);
程序3、从下面的程序中,选择一个来完成:
(1)复印中心收费费用计算;
(2)输入一个整数,计算该整数的阶乘;
(3)求1~99的所有奇数的和;
(4)个人所得税计算器题目(参见课件"3.程序流程控制"幻灯片)
(5)九九乘法表
程序4、简易计算器
三、实验步骤
1、打开“Csharp示例” 文件夹下的“第3课\Conditions\Conditions.sln”。完成其中的任务,并调试运行(程序1):
任务1的代码:
private void button1_Click(object sender, EventArgs e)
{
int v = (int)numericUpDown1.Value;
if (v >= 5000)
OutPut("目标已经达到");
else
MessageBox.Show("目标未达到");
}
2、打开“Csharp示例” 文件夹下的“第3课\Loops\Loop.sln”。完成其中的任务,并调试运行(程序2):
任务1的代码:
private void Output(object anyObject) {
if ( anyObject != null )
{
richTextBox.AppendText( anyObject.ToString());
}
}
private void run_Click(object sender, System.EventArgs e) {
int sum = 0;
for (int i = 1;i <= 1000; i++)
{
sum=sum + i;
if (i < 1000)
Output(i + "+");
else
Output(i);
}
Output( "="+sum);
}
3、程序3的几个题目中,程序中要有比较完备的注释。
(1)
public Form1()
{
InitializeComponent();
textBox1.Text = " 年份\t年终余额";
}
private void button1_Click(object sender, EventArgs e)
{
double bj = 0, ll = 0, dq = 0 ;
string s = "";
try
{
bj = (double)numericUpDown1.Value;
ll = (double)numericUpDown2.Value / 100;
dq = (double)numericUpDown3.Value;
}
catch (System.Exception)
{
MessageBox.Show("输入有误");
}
for (int i = 1; i <= dq; i++)
{
bj += bj * ll;
s = string.Format(" {0}\t{1:C}", i, bj);
OutPut(s);
}
}
private void OutPut(string str)
{
textBox1.Text = textBox1.Text + "\r\n" + str;
}
private void button2_Click(object sender, EventArgs e)
{
numericUpDown1.Value = 5000;
numericUpDown2.Value = 2;
numericUpDown3.Value = 10;
textBox1.Text = " 年份\t年终余额";
}
(2)
private void button1_Click(object sender, EventArgs e)
{
int i=0;
try
{
i = int.Parse(textBox1.Text);
}
catch (System.Exception)
{
MessageBox.Show("请输入一个整数");
}
long s=0,f=1;
for (int j = 1; j <= i; j++)
{
f = f * j;
s += f;
}
textBox2.Text = s.ToString();
label2.Text = i + "!=";
}
(3)
int sum = 0,j = 0;
for (int i = 1; i <= 99; i++)
if (i % 2 != 0)
{
Console.Write(i + " ");
j++;
if (j % 5 == 0)
Console.WriteLine();
sum += i;
}
Console.WriteLine("结果:" + sum);
(4)
private void button1_Click(object sender, EventArgs e)
{
double sr = 0, sx = 0, cze = 3500, yjsk = 0;
try
{
if (!textBox1.Equals(""))
sr = double.Parse(textBox1.Text);
else
{
MessageBox.Show("请输入你的收入!");
throw new Exception();
}
if (sr <= 0)
throw new Exception();
}
catch (System.Exception)
{
MessageBox.Show("你输入的收入不正确!");
}
try
{
if (!textBox2.Equals(""))
sx = double.Parse(textBox2.Text);
else
{
MessageBox.Show("请输入你的三险一金!");
throw new Exception();
}
if (sx < 0)
throw new Exception();
}
catch (System.Exception)
{
MessageBox.Show("你输入的三险一金不正确!");
}
yjsk = sr - sx - cze;
if (yjsk < 1500)
yjsk = yjsk * 0.03 - 0;
else if (yjsk < 4500)
yjsk = 1500 * 0.03 + (yjsk - 1500) * 0.1 - 105;
else if (yjsk < 9000)
yjsk = 1500 * 0.03 + 3000 * 0.1 - 105 + (yjsk - 4500) * 0.2 - 555;
else if (yjsk < 35000)
yjsk = 1500 * 0.03 + 3000 * 0.1 - 105 + 4500 * 0.2 - 555 + (yjsk - 9000) * 0.25 - 1005;
else if (yjsk < 55000)
yjsk = 1500 * 0.03 + 3000 * 0.1 - 105 + 4500 * 0.2 - 555 + 26000 * 0.25 - 1005 + (yjsk - 35000) * 0.3 - 2755;
else if (yjsk < 80000)
yjsk = 1500 * 0.03 + 3000 * 0.1 - 105 + 4500 * 0.2 - 555 + 26000 * 0.25 - 1005 + 20000 * 0.3 - 2755 + (yjsk - 55000) * 0.35 - 5505;
else
yjsk = 1500 * 0.03 + 3000 * 0.1 - 105 + 4500 * 0.2 - 555 + 26000 * 0.25 - 1005 + 20000 * 0.3 - 2755 + 25000 * 0.35 - 5505 + (yjsk - 80000) * 0.45 - 13505;
textBox4.Text = yjsk.ToString();
}
(5)
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= i; j++)
{
Console.Write(j + "*" + i + "=" + (j * i) + " ");
if (i * j < 10)
Console.Write(" ");
}
Console.WriteLine();
}
4、参照第3课课件的内容,独立完成课堂所讲的示例程序(简易计算器)
private void button1_Click(object sender, EventArgs e)
{
textBox3.Text = "";
try
{
a1 = double.Parse(textBox1.Text);
}
catch (System.InvalidCastException)
{
MessageBox.Show("输入的操作数1不是数值");
cls();
}
catch (System.OverflowException)
{
MessageBox.Show("输入的操作数1超出范围");
cls();
}
try
{
a2 = double.Parse(textBox2.Text);
}
catch (System.InvalidCastException)
{
MessageBox.Show("输入的操作数2不是数值");
cls();
}
catch (System.OverflowException)
{
MessageBox.Show("输入的操作数2超出范围");
cls();
}
try
{
st = comboBox1.Text;
switch (st)
{
case "+":
s = a1 + a2;
break;
case "-":
s = a1 - a2;
break;
case "*":
s = a1 * a2;
break;
case "/":
if (a2 == 0)
throw new DivideByZeroException();
s = a1 / a2;
break;
default:
throw new IndexOutOfRangeException();
}
}
catch (System.DivideByZeroException)
{
MessageBox.Show("被除数不能为0");
cls();
}
catch (System.OverflowException)
{
MessageBox.Show("计算结果超出范围");
cls();
}
catch (System.IndexOutOfRangeException)
{
MessageBox.Show("操作符错误");
cls();
}
textBox3.Text = s.ToString();
}
5、
private void button1_Click(object sender, EventArgs e)
{
string tc = "", phone = "";
double zj = 0, bj = 0,hf = 0;
try
{
tc = comboBox1.Text;
if (tc.Equals(""))
throw new Exception();
}
catch (System.Exception)
{
MessageBox.Show("请选择手机套餐!");
}
try
{
phone = textBox1.Text;
if (phone.Equals(""))
throw new Exception();
}
catch (System.Exception)
{
MessageBox.Show("请输入手机号!");
}
try
{
zj = double.Parse(textBox2.Text);
if (zj<0)
throw new Exception();
}
catch (System.Exception)
{
MessageBox.Show("请正确输入主叫时间!");
}
try
{
bj = double.Parse(textBox3.Text);
if (bj < 0)
throw new Exception();
}
catch (System.Exception)
{
MessageBox.Show("请正确输入被叫时间!");
}
switch (tc)
{
case "自由行":
if ((bj + zj) <= 60)
hf = 25;
else
hf = 25 + (bj + zj) * 0.4 - 60 * 0.4;
break;
case "自由行听霸":
hf = 25 + zj * 0.4;
break;
case "江淮行":
hf = 15 + 0.2 * zj;
break;
case "听霸":
hf = 12 + 0.2 * zj;
break;
}
textBox4.Text = hf.ToString();
}
6、
int i, j, k;
for(i=1; i <= 30;i++)
for(j=i;j<=30;j++)
for(k=j;k<=30;k++)
if (i * i + j * j == k * k)
{
Console.WriteLine(i + " " + j + " " + k);
}
Console.ReadLine();
四、程序运行结果截图
程序1的运行结果截图:
程序2的运行结果截图:
程序3的运行结果截图:
(1)
(2)
(3)
(4)
(5)
程序4的运行结果截图:
5/
6、
五、程序源代码(用Winrar压缩后用附件提交,源代码中要有比较完备的注释)
六、收获,体会及问题(写得越详细、越个性化、越真实越好,否则我不知道你做这个实验的心路历程,也就无法充分地判断你是否是独立完成的这个实验、你是否在做这个实验时进行了认真仔细地思考、通过这个实验你是否在实践能力上得到了提高)
通过本实验,我学到了很多。实验的内容都是生活中的,然后一步一步地完成这些实验,是一件很有意思的事情。