1、C#实验 C#程序设计及应用实 验 报 告班 级: * 姓 名: * 学 号: * 教 师: * 信息工程学院计算机系实验一 小型计算器设计一、实验目的1、了解并掌握常用控件的基本属性;2、了解并掌握控件的基本事件程序书写方法;3、掌握控件基本属性的设置与修改。二、实验内容编写代码实现小型计算器,要求具有加减乘除基本功能,可以实现连续的加减乘除运算(自左向右,不需要考虑算符优先级),要求支持小数运算。以“.”开头的数要转化为以“0.”开头的小数,如果除数为0,要给出提示信息。三、实验环境四、实验步骤using System;using System.Collections.Generic;us
2、ing System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace jisuanqi public partial class Form1 : Form int pos = 0; double temp1 = -1; double temp2 = -1; public Form1() InitializeComponent(); private void Form1_Load(object
3、 sender, EventArgs e) private void textBox1_TextChanged(object sender, EventArgs e) public void addNum(int num) /显示框 textBox1.Text = textBox1.Text + num.ToString(); if (textBox1.Text.Length = 2) /首字为0则去除 if (textBox1 .Text .Substring(0,1)=0 & textBox1 .Text .Substring(1,1) !=.) textBox1 .Text =textB
4、ox1 .Text .Substring(1); /数字输入0到9 private void button1_Click(object sender, EventArgs e) addNum(1); private void button2_Click(object sender, EventArgs e) addNum(2); private void button3_Click(object sender, EventArgs e) addNum(3); private void button4_Click(object sender, EventArgs e) addNum(4); pr
5、ivate void button5_Click(object sender, EventArgs e) addNum(5); private void button6_Click(object sender, EventArgs e) addNum(6); private void button7_Click(object sender, EventArgs e) addNum(7); private void button8_Click(object sender, EventArgs e) addNum(8); private void button9_Click(object send
6、er, EventArgs e) addNum(9); private void button10_Click(object sender, EventArgs e) addNum(0); /添加小数点 private void button11_Click(object sender, EventArgs e) if (textBox1.Text = ) textBox1.Text = 0.; else if (textBox1.Text.IndexOf(.) = 0) MessageBox.Show(只能添加一个小数点); else textBox1.Text = textBox1.Tex
7、t + .; /加减乘除运算 private void button12_Click(object sender, EventArgs e)/+ pos = 1; int index = textBox1.Text.IndexOf(=); if (index = -1) /没有“=”号 temp1 = Convert.ToDouble(textBox1.Text); textBox1.Text = textBox1.Text + +; else temp1 = Convert.ToDouble(textBox1.Text.Substring(index + 1); textBox1.Text
8、= temp1.ToString() + +; private void button13_Click(object sender, EventArgs e)/- pos = 2; int index = textBox1.Text.IndexOf(=); if (index = -1) /没有“=”号 temp1 = Convert.ToDouble(textBox1.Text); textBox1.Text = textBox1.Text + -; else temp1 = Convert.ToDouble(textBox1.Text.Substring(index + 1); textB
9、ox1.Text = temp1.ToString() + -; private void button14_Click(object sender, EventArgs e)/* pos = 3; int index = textBox1.Text.IndexOf(=); if (index = -1) /没有“=”号 temp1 = Convert.ToDouble(textBox1.Text); textBox1.Text = textBox1.Text + X; else temp1 = Convert.ToDouble(textBox1.Text.Substring(index +
10、1); textBox1.Text = temp1.ToString() + X; private void button15_Click(object sender, EventArgs e)/ pos = 4; int index = textBox1.Text.IndexOf(=); if (index = -1) /没有“=”号 temp1 = Convert.ToDouble(textBox1.Text); textBox1.Text = textBox1.Text + ; else temp1 = Convert.ToDouble(textBox1.Text.Substring(i
11、ndex + 1); textBox1.Text = temp1.ToString() + ; /等于 事件 private void button17_Click(object sender, EventArgs e) int indexofA; switch (pos) case 1: indexofA = textBox1.Text.IndexOf(+); temp2 = Convert.ToDouble(textBox1.Text.Substring(indexofA + 1); textBox1.Text = textBox1.Text + = + (temp1 + temp2).T
12、oString(); break ; case 2: indexofA = textBox1.Text.IndexOf(-); string bb = textBox1.Text.Substring(indexofA + 1); temp2 = Convert.ToDouble( textBox1.Text.Substring(indexofA + 1); textBox1.Text = textBox1.Text + = + (temp1 - temp2).ToString(); break ; case 3: indexofA = textBox1.Text.IndexOf(X); tem
13、p2 = Convert.ToDouble( textBox1.Text.Substring(indexofA + 1); textBox1.Text = textBox1.Text + = + (temp1 * temp2).ToString(); break ; case 4: indexofA = textBox1.Text.IndexOf(); temp2 = Convert.ToDouble( textBox1.Text.Substring(indexofA + 1); if(temp2=0) MessageBox .Show (除数不能为0!); else textBox1.Tex
14、t = textBox1.Text + = + (temp1 / temp2).ToString(); break ; /清零 private void button16_Click(object sender, EventArgs e) textBox1.Text = 0; temp1 = 0; pos = 0; 5、实验结果可连加,可显示运算过程实验二 多线程售票程序设计一、实验目的1、理解线程的基本概念2、理解线程同步的基本方法3、掌握利用Lock实现线程同步的方法二、实验内容假设有M(0M300)张票,由N(输入,0N800)个线程来实现一个WinForm桌面应用程序实现售票,每次线程
15、运行时都首先检查是否还有票未售出,如果有就按照票号从小到大的顺序售出,同时输出剩余票数。三、实验环境四、实验步骤using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Threading; namespace goupiaoxitong public partial class
16、 Form1 : Form int M; /票数 int N; /人数 private Object lockthis = new Object(); /用于上锁 private List ticketlist; public Form1() InitializeComponent(); M = Convert.ToInt32(textBox1.Text); /读取控件框的值 N = Convert.ToInt32(textBox2.Text); CheckForIllegalCrossThreadCalls = false; /重点:新创建的线程不能访问UI线程创建的窗口控件 private
17、 void button1_Click(object sender, EventArgs e) /按钮 ticketlist = new List(M); for (int i = 1; i = M; i+) ticketlist.Add(i.ToString().PadLeft(3, 0);/票号3位 listBox2.Items.Clear(); /清空 Thread myThread = new ThreadN;/创建多线程 for (int i = 0; i N; i+) Thread t = new Thread(sell); t.Name = 线程 + i.ToString();
18、myThreadi = t; for (int i = 0; i N; i+) myThreadi.Start(); public void sell() /线程方法 for (int i = 0; i 0) string ticketNo = ticketlist0; string str = Thread.CurrentThread.Name + 买了票 + ticketNo ; ticketlist.RemoveAt (0); str += .剩余 + ticketlist.Count + 张票; listBox2.Items.Add(str); private void listBox
19、2_SelectedIndexChanged(object sender, EventArgs e) private void textBox1_TextChanged(object sender, EventArgs e) private void label3_Click(object sender, EventArgs e) private void Form1_Load(object sender, EventArgs e) 五、实验结果6、结果分析注意:控件名是listbox2. CheckForIllegalCrossThreadCalls = false; /重点:新创建的线程不能访问UI线程创建的窗口控件
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2