当前位置:文档之家› c#实验报告_简易计算器

c#实验报告_简易计算器

重庆邮电大学移通学院C#课程设计一、实验目的:构造一个类似Windows自带的计算器一样的简易计算器,能够连续进行加、减、乘、除四则整数运算,并能随时清除计算结果进行下一次计算。

二、具体实验步骤:1、添加编辑框对应的变量m_Display2、添加运算符类型变量char CompuType; //用于存储运算符类型CString FirstInput; //用于保存运算符前面的输入值三、实验内容设计的基本要求:能正确输入数据,能实现加,减,乘,除等算术运算,能实现简单的三角运算,运行结果能正确显示,可以清除数据等。

1、要点分析:这是一个简单的对话窗体设计,涉及button,editbox,static text,radio button,group box等控件,主要影响事件为BN_CLICKED2、程序源代码:using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace Study{public partial class Form1 : Form{public Form1(){InitializeComponent();}private int opMain = 0; // 运算类型,其中1(加法) 2(减法) 3(乘法) 4(除法)private double mainNum1 = 0; // 存储第一个数private double mainNum2 = 0; // 存储第二个数private bool isSecond = false; // 用来判断输入的是第一个还是第二个数private bool isDone = false; // 用来判断是否按了等于按钮private bool isDecimal = false; // 用来判断是否有小数private bool isNokeydown = false;// 用来判断是否没输入第二个数而按了"="号键private bool isKeyupclear = true;//用来判断是否按了clear键,程序开始前默认按了;public void setText(string textest) //设置文本框的值{if (textest.Equals("clear")){textBox1.Text = "0.";isSecond = false;isDone = false;isDecimal = false;isKeyupclear = true;}else{if (isSecond){textBox1.Text = textest;isSecond = false;isDecimal = false;}else{if (isDone){textBox1.Text = textest;isDone = false;}else{if (isKeyupclear) //对是否按下clear键的判断{textBox1.Text = textest;isKeyupclear = false;}elsetextBox1.Text += textest;}}}btnEqual.Select(); //设置"="号的焦点}public void Calc(double num1, double num2, int op){double answer = 0;switch (op) //判断所进行的运算{case 1:answer = num1 + num2;break;case 2:answer = num1 - num2;break;case 3:answer = num1 * num2;break;case 4:answer = num1 / num2;break;case 5:answer = num1 % num2;break;}setText(answer.ToString()); //显示结果}//执行运算private void doEquals(){if (isNokeydown) //判断已经输入第二个数后按了"="号键{mainNum2 = double.Parse(textBox1.Text);setText("clear");Calc(mainNum1, mainNum2, opMain);isDone = true;isNokeydown = false;}}//切换正负private void changeSign(){double storNum;if (textBox1.Text.Length > 0){storNum = double.Parse(textBox1.Text); storNum *= -1;textBox1.Text = storNum.ToString();}btnEqual.Select(); //设置"="号的焦点}//设置运算类型private void setOperator(int operation){if (textBox1.Text.Length > 0){opMain = operation;mainNum1 = double.Parse(textBox1.Text); isSecond = true;isDone = false;isNokeydown = true;btnEqual.Select(); //设置"="号的焦点}}//设置小数点private void setDecimal(){if (!isDecimal){setText(".");isDecimal = true;}btnEqual.Select(); //设置"="号的焦点}//开平方private void doSquart(){double storNum;storNum = double.Parse(textBox1.Text);if (storNum > 0){storNum = Math.Sqrt(storNum);textBox1.Text = storNum.ToString();}elsetextBox1.Text = "负数不能开平方。

";btnEqual.Select(); //设置"="号的焦点}//求倒数private void doreciprocal(){double storNum;storNum = double.Parse(textBox1.Text);if (storNum != 0){storNum = 1 / storNum;textBox1.Text = storNum.ToString();}elsetextBox1.Text = "除数不能为零。

";btnEqual.Select();}private void btn7_Click(object sender, EventArgs e) {setText("7");}private void btn8_Click(object sender, EventArgs e) {setText("8");}private void btn9_Click(object sender, EventArgs e) {setText("9");}private void btn4_Click(object sender, EventArgs e) {setText("4");}private void btn5_Click(object sender, EventArgs e){setText("5");}private void btn6_Click(object sender, EventArgs e){setText("6");}private void btn1_Click(object sender, EventArgs e){setText("1");}private void btn2_Click(object sender, EventArgs e){setText("2");}private void btn3_Click(object sender, EventArgs e){setText("3");}private void btn0_Click(object sender, EventArgs e){setText("0");}private void btnPoint_Click(object sender, EventArgs e){setDecimal();}private void btnSubtract_Click(object sender, EventArgs e)//正负号交换{changeSign();}private void btnEqual_Click(object sender, EventArgs e){doEquals();}private void btnAdd_Click(object sender, EventArgs e){setOperator(1);}private void btnSub_Click(object sender, EventArgs e){setOperator(2);}private void btnMulti_Click(object sender, EventArgs e){setOperator(3);}private void btnDivide_Click(object sender, EventArgs e){setOperator(4);}private void btnC_Click(object sender, EventArgs e)//清除键{isSecond = false;setText("clear");}private void btnSqrt_Click(object sender, EventArgs e)//开平方 {doSquart();}private void btnPercentage_Click(object sender, EventArgs e)//求模{setOperator(5);}private void btnReciprocal_Click(object sender, EventArgs e)//求倒{doreciprocal();}private void btnCE_Click(object sender, EventArgs e)//CE{isSecond = false;setText("clear");}private void btnBackspace_Click(object sender, EventArgs e)//退格键{string old = textBox1.Text;if (old.Length >1&&old!="0.")textBox1.Text = old.Substring(0, old.Length - 1);elsetextBox1.Text = "0.";}}}3、运行结果四、实验小结此次的设计是1个新的体验、新的挑战,我在此过程中不但应用了所学的知识,而且还不断的学习新的知识、工具,以完成设计的需要,在设计的过程中我深深体会到作为1个学习计算机专业的人士,为了实现1个模块的代码、为了1个设计的实现思想、经常绞尽脑汁来达到设计所要达到的目的,虽然我的基础知识不够扎实,但是我在此期间积累了许多宝贵的经验,这都是我以后走上工作岗位的巨大的财富。

相关主题