C#实验报告-简单计算器设计

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

第1页共5页华北水利水电大学C#实验报告2014~2015学年第一学期2012级计算机科学与技术专业实验二一、实验目的:熟悉windows窗体编程。二、实验内容:编写一个可以实现基本功能的计算器(可以选择尝试复杂功能的实现)。三、程序设计代码流程图及源代码:程序代码:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceWindowsFormsApplication2{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidbtnNum1_Click(objectsender,EventArgse){Buttonbtn=(Button)sender;textReadout.Text+=btnNum1.Text;}privatevoidbtnNum2_Click(objectsender,EventArgse){Buttonbtn=(Button)sender;textReadout.Text+=btnNum2.Text;}privatevoidbtnNum3_Click(objectsender,EventArgse)第2页共5页{Buttonbtn=(Button)sender;textReadout.Text+=btnNum3.Text;}privatevoidbtnNum4_Click(objectsender,EventArgse){Buttonbtn=(Button)sender;textReadout.Text+=btnNum4.Text;}privatevoidbtnNum5_Click(objectsender,EventArgse){Buttonbtn=(Button)sender;textReadout.Text+=btnNum5.Text;}privatevoidbtnNum6_Click(objectsender,EventArgse){Buttonbtn=(Button)sender;textReadout.Text+=btnNum6.Text;}privatevoidbtnNum7_Click(objectsender,EventArgse){Buttonbtn=(Button)sender;textReadout.Text+=btnNum7.Text;}privatevoidbtnNum8_Click(objectsender,EventArgse){Buttonbtn=(Button)sender;textReadout.Text+=btnNum8.Text;}privatevoidbtnNum9_Click(objectsender,EventArgse){Buttonbtn=(Button)sender;textReadout.Text+=btnNum9.Text;}privatevoidbtnNum0_Click(objectsender,EventArgse){Buttonbtn=(Button)sender;textReadout.Text+=btnNum0.Text;第3页共5页}privatevoidbtnDot_Click(objectsender,EventArgse){Buttonbtn=(Button)sender;textReadout.Text+=btnDot.Text;}privatevoidbtnC_Click(objectsender,EventArgse){textReadout.Text=;}privatevoidbtnAdd_Click(objectsender,EventArgse){Buttonbtn=(Button)sender;textReadout.Text=textReadout.Text++btnAdd.Text+;}privatevoidbtnSub_Click(objectsender,EventArgse){Buttonbtn=(Button)sender;textReadout.Text=textReadout.Text++btnSub.Text+;}privatevoidbtnMul_Click(objectsender,EventArgse){Buttonbtn=(Button)sender;textReadout.Text=textReadout.Text++btnMul.Text+;}privatevoidbtnDiv_Click(objectsender,EventArgse){Buttonbtn=(Button)sender;textReadout.Text=textReadout.Text++btnDiv.Text+;}privatevoidbtnAmount_Click(objectsender,EventArgse){textReadout.Text=textReadout.Text++btnAmount.Text+;Single[]arg={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};Singles1;inti=0,j=0;char[]op={'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a'第4页共5页,'a','a'};charop1='a';stringt=textReadout.Text;intspace=t.IndexOf('');while(true){s1=Convert.ToSingle(t.Substring(0,space));op1=Convert.ToChar(t.Substring(space+1,1));arg[i]=s1;op[i]=op1;i++;if(op1=='=')break;t=t.Substring(space+3);space=t.IndexOf('');}i=j=0;while(true){if(op[i]=='*'){arg[i+1]=arg[i]*arg[i+1];}if(op[i]=='/')if(arg[i+1]==0){thrownewApplicationException();}else{arg[i+1]=arg[i]/arg[i+1];}i++;if(op[i]=='=')break;}i=j;while(true){if(op[i]=='+'){j=i+1;while(op[j]=='*'||op[j]=='/'){j++;第5页共5页}arg[j]=arg[i]+arg[j];}if(op[i]=='-'){j=i+1;while(op[j]=='*'||op[j]=='/'){j++;}arg[j]=arg[i]-arg[j];}i++;if(op[i]=='=')break;}textReadout.Text=arg[i].ToString();}}}四、测试结果(程序运行结果采用截图的方式打印):五、小结(包括收获、心得体会、存在的问题及解决问题的方法、建议等)编出来的感觉好开心。只要努力去编,程序总会好的!选择合适的算法很重要,算法要结合语言,结合自己能干的事。这个程序学到了substring,IndexOf以及数组的一种用法char[]数组名={……}。

1 / 5
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功