C#实验.docx

上传人:b****3 文档编号:11777486 上传时间:2023-06-02 格式:DOCX 页数:15 大小:35.78KB
下载 相关 举报
C#实验.docx_第1页
第1页 / 共15页
C#实验.docx_第2页
第2页 / 共15页
C#实验.docx_第3页
第3页 / 共15页
C#实验.docx_第4页
第4页 / 共15页
C#实验.docx_第5页
第5页 / 共15页
C#实验.docx_第6页
第6页 / 共15页
C#实验.docx_第7页
第7页 / 共15页
C#实验.docx_第8页
第8页 / 共15页
C#实验.docx_第9页
第9页 / 共15页
C#实验.docx_第10页
第10页 / 共15页
C#实验.docx_第11页
第11页 / 共15页
C#实验.docx_第12页
第12页 / 共15页
C#实验.docx_第13页
第13页 / 共15页
C#实验.docx_第14页
第14页 / 共15页
C#实验.docx_第15页
第15页 / 共15页
亲,该文档总共15页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

C#实验.docx

《C#实验.docx》由会员分享,可在线阅读,更多相关《C#实验.docx(15页珍藏版)》请在冰点文库上搜索。

C#实验.docx

C#实验

 

《C#程序设计及应用》

实验报告

 

班级:

******

姓名:

******

学号:

******

教师:

******

 

信息工程学院计算机系

实验一小型计算器设计

一、实验目的

1、了解并掌握常用控件的基本属性;

2、了解并掌握控件的基本事件程序书写方法;

3、掌握控件基本属性的设置与修改。

二、实验内容

编写代码实现小型计算器,要求具有加减乘除基本功能,可以实现连续的加减乘除运算(自左向右,不需要考虑算符优先级),要求支持小数运算。

以“.”开头的数要转化为以“0.”开头的小数,如果除数为0,要给出提示信息。

三、实验环境

四、实验步骤

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Windows.Forms;

namespacejisuanqi

{

publicpartialclassForm1:

Form

{

intpos=0;

doubletemp1=-1;

doubletemp2=-1;

publicForm1()

{

InitializeComponent();

}

privatevoidForm1_Load(objectsender,EventArgse)

{

}

privatevoidtextBox1_TextChanged(objectsender,EventArgse)

{

}

publicvoidaddNum(intnum)//显示框

{

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=textBox1.Text.Substring

(1);

}

}

//数字输入0到9

privatevoidbutton1_Click(objectsender,EventArgse)

{

addNum

(1);

}

privatevoidbutton2_Click(objectsender,EventArgse)

{

addNum

(2);

}

privatevoidbutton3_Click(objectsender,EventArgse)

{

addNum(3);

}

privatevoidbutton4_Click(objectsender,EventArgse)

{

addNum(4);

}

privatevoidbutton5_Click(objectsender,EventArgse)

{

addNum(5);

}

privatevoidbutton6_Click(objectsender,EventArgse)

{

addNum(6);

}

privatevoidbutton7_Click(objectsender,EventArgse)

{

addNum(7);

}

privatevoidbutton8_Click(objectsender,EventArgse)

{

addNum(8);

}

privatevoidbutton9_Click(objectsender,EventArgse)

{

addNum(9);

}

privatevoidbutton10_Click(objectsender,EventArgse)

{

addNum(0);

}

//添加小数点

privatevoidbutton11_Click(objectsender,EventArgse)

{

if(textBox1.Text=="")

textBox1.Text="0.";

elseif(textBox1.Text.IndexOf(".")>=0)

MessageBox.Show("只能添加一个小数点");

else

textBox1.Text=textBox1.Text+".";

}

//加减乘除运算

privatevoidbutton12_Click(objectsender,EventArgse)//+

{

pos=1;

intindex=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=temp1.ToString()+"+";

}

}

privatevoidbutton13_Click(objectsender,EventArgse)//-

{

pos=2;

intindex=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=temp1.ToString()+"-";

}

}

privatevoidbutton14_Click(objectsender,EventArgse)//*

{

pos=3;

intindex=textBox1.Text.IndexOf("=");

if(index<=-1)//没有“=”号

{

temp1=Convert.ToDouble(textBox1.Text);

textBox1.Text=textBox1.Text+"X";

}

else

{

temp1=Convert.ToDouble(textBox1.Text.Substring(index+1));

textBox1.Text=temp1.ToString()+"X";

}

}

privatevoidbutton15_Click(objectsender,EventArgse)//

{

pos=4;

intindex=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=temp1.ToString()+"÷";

}

}

//等于事件

privatevoidbutton17_Click(objectsender,EventArgse)

{

intindexofA;

switch(pos)

{

case1:

indexofA=textBox1.Text.IndexOf("+");

temp2=Convert.ToDouble(textBox1.Text.Substring(indexofA+1));

textBox1.Text=textBox1.Text+"="+(temp1+temp2).ToString();

break;

case2:

indexofA=textBox1.Text.IndexOf("-");

stringbb=textBox1.Text.Substring(indexofA+1);

temp2=Convert.ToDouble(textBox1.Text.Substring(indexofA+1));

textBox1.Text=textBox1.Text+"="+(temp1-temp2).ToString();

break;

case3:

indexofA=textBox1.Text.IndexOf("X");

temp2=Convert.ToDouble(textBox1.Text.Substring(indexofA+1));

textBox1.Text=textBox1.Text+"="+(temp1*temp2).ToString();

break;

case4:

indexofA=textBox1.Text.IndexOf("÷");

temp2=Convert.ToDouble(textBox1.Text.Substring(indexofA+1));

if(temp2==0)

MessageBox.Show("除数不能为0!

");

else

textBox1.Text=textBox1.Text+"="+(temp1/temp2).ToString();

break;

}

}

//清零

privatevoidbutton16_Click(objectsender,EventArgse)

{

textBox1.Text="0";

temp1=0;

pos=0;

}

 

}

}

5、实验结果

可连加,可显示运算过程

实验二多线程售票程序设计

一、实验目的

1、理解线程的基本概念

2、理解线程同步的基本方法

3、掌握利用Lock实现线程同步的方法

二、实验内容

假设有M(0

三、实验环境

四、实验步骤

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Windows.Forms;

usingSystem.Threading;

namespacegoupiaoxitong

{

publicpartialclassForm1:

Form

{

intM;//票数

intN;//人数

privateObjectlockthis=newObject();//用于上锁

privateListticketlist;

publicForm1()

{

InitializeComponent();

M=Convert.ToInt32(textBox1.Text);//读取控件框的值

N=Convert.ToInt32(textBox2.Text);

CheckForIllegalCrossThreadCalls=false;//重点:

新创建的线程不能访问UI线程创建的窗口控件

}

privatevoidbutton1_Click(objectsender,EventArgse)//按钮

{

ticketlist=newList(M);

for(inti=1;i<=M;i++)

{

ticketlist.Add(i.ToString().PadLeft(3,'0'));//票号3位

}

listBox2.Items.Clear();//清空

Thread[]myThread=newThread[N];//创建多线程

for(inti=0;i

{

Threadt=newThread(sell);

t.Name="线程"+i.ToString();

myThread[i]=t;

}

for(inti=0;i

{

myThread[i].Start();

}

}

 

publicvoidsell()//线程方法

{

for(inti=0;i

{

lock(lockthis)

{

if(ticketlist.Count>0)

{

stringticketNo=ticketlist[0];

stringstr=Thread.CurrentThread.Name+"买了票"+ticketNo;

ticketlist.RemoveAt(0);

str+="...剩余"+ticketlist.Count+"张票";

listBox2.Items.Add(str);

}

}

}

}

privatevoidlistBox2_SelectedIndexChanged(objectsender,EventArgse)

{

}

privatevoidtextBox1_TextChanged(objectsender,EventArgse)

{

}

privatevoidlabel3_Click(objectsender,EventArgse)

{

}

privatevoidForm1_Load(objectsender,EventArgse)

{

}

}

}

五、实验结果

6、结果分析

注意:

控件名是listbox2.

CheckForIllegalCrossThreadCalls=false;//重点:

新创建的线程不能访问UI线程创建的窗口控件

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 工作范文 > 行政公文

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2