c#实验报告简易计算器Word文件下载.docx
- 文档编号:7728287
- 上传时间:2023-05-09
- 格式:DOCX
- 页数:14
- 大小:89.20KB
c#实验报告简易计算器Word文件下载.docx
《c#实验报告简易计算器Word文件下载.docx》由会员分享,可在线阅读,更多相关《c#实验报告简易计算器Word文件下载.docx(14页珍藏版)》请在冰点文库上搜索。
2、程序源代码:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
namespaceStudy
{
publicpartialclassForm1:
Form
{
publicForm1()
InitializeComponent();
}
privateintopMain=0;
//运算类型,其中1(加法)2(减法)3(乘法)4(除法)
privatedoublemainNum1=0;
//存储第一个数
privatedoublemainNum2=0;
//存储第二个数
privateboolisSecond=false;
//用来判断输入的是第一个还是第二个数
privateboolisDone=false;
//用来判断是否按了等于按钮
privateboolisDecimal=false;
//用来判断是否有小数
privateboolisNokeydown=false;
//用来判断是否没输入第二个数而按了"
="
号键
privateboolisKeyupclear=true;
//用来判断是否按了clear键,程序开始前默认按了;
publicvoidsetText(stringtextest)//设置文本框的值
if(textest.Equals("
clear"
))
textBox1.Text="
0."
;
isSecond=false;
isDone=false;
isDecimal=false;
isKeyupclear=true;
else
if(isSecond)
textBox1.Text=textest;
if(isDone)
if(isKeyupclear)//对是否按下clear键的判断
isKeyupclear=false;
textBox1.Text+=textest;
btnEqual.Select();
//设置"
号的焦点
publicvoidCalc(doublenum1,doublenum2,intop)
doubleanswer=0;
switch(op)//判断所进行的运算
case1:
answer=num1+num2;
break;
case2:
answer=num1-num2;
case3:
answer=num1*num2;
case4:
answer=num1/num2;
case5:
answer=num1%num2;
setText(answer.ToString());
//显示结果
//执行运算
privatevoiddoEquals()
if(isNokeydown)//判断已经输入第二个数后按了"
mainNum2=double.Parse(textBox1.Text);
setText("
);
Calc(mainNum1,mainNum2,opMain);
isDone=true;
isNokeydown=false;
//切换正负
privatevoidchangeSign()
doublestorNum;
if(textBox1.Text.Length>
0)
storNum=double.Parse(textBox1.Text);
storNum*=-1;
textBox1.Text=storNum.ToString();
//设置运算类型
privatevoidsetOperator(intoperation)
opMain=operation;
mainNum1=double.Parse(textBox1.Text);
isSecond=true;
isNokeydown=true;
//设置小数点
privatevoidsetDecimal()
if(!
isDecimal)
."
isDecimal=true;
}
//开平方
privatevoiddoSquart()
if(storNum>
storNum=Math.Sqrt(storNum);
负数不能开平方。
"
//求倒数
privatevoiddoreciprocal()
if(storNum!
=0)
storNum=1/storNum;
除数不能为零。
privatevoidbtn7_Click(objectsender,EventArgse)
7"
privatevoidbtn8_Click(objectsender,EventArgse)
8"
privatevoidbtn9_Click(objectsender,EventArgse)
9"
privatevoidbtn4_Click(objectsender,EventArgse)
4"
privatevoidbtn5_Click(objectsender,EventArgse)
5"
privatevoidbtn6_Click(objectsender,EventArgse)
6"
privatevoidbtn1_Click(objectsender,EventArgse)
1"
privatevoidbtn2_Click(objectsender,EventArgse)
2"
privatevoidbtn3_Click(objectsender,EventArgse)
3"
privatevoidbtn0_Click(objectsender,EventArgse)
0"
privatevoidbtnPoint_Click(objectsender,EventArgse)
setDecimal();
privatevoidbtnSubtract_Click(objectsender,EventArgse)//正负号交换
changeSign();
privatevoidbtnEqual_Click(objectsender,EventArgse)
doEquals();
privatevoidbtnAdd_Click(objectsender,EventArgse)
setOperator
(1);
privatevoidbtnSub_Click(objectsender,EventArgse)
setOperator
(2);
privatevoidbtnMulti_Click(objectsender,EventArgse)
setOperator(3);
privatevoidbtnDivide_Click(objectsender,EventArgse)
setOperator(4);
privatevoidbtnC_Click(objectsender,EventArgse)//清除键
privatevoidbtnSqrt_Click(objectsender,EventArgse)//开平方
doSquart();
privatevoidbtnPercentage_Click(objectsender,EventArgse)//求模
setOperator(5);
privatevoidbtnReciprocal_Click(objectsender,EventArgse)//求倒
doreciprocal();
privatevoidbtnCE_Click(objectsender,EventArgse)//CE
privatevoidbtnBackspace_Click(objectsender,EventArgse)//退格键
stringold=textBox1.Text;
if(old.Length>
1&
&
old!
)
textBox1.Text=old.Substring(0,old.Length-1);
}
3、运行结果
四、实验小结
此次的设计是1个新的体验、新的挑战,我在此过程中不但应用了所学的知识,而且还不断的学习新的知识、工具,以完成设计的需要,在设计的过程中我深深体会到作为1个学习计算机专业的人士,为了实现1个模块的代码、为了1个设计的实现思想、经常绞尽脑汁来达到设计所要达到的目的,虽然我的基础知识不够扎实,但是我在此期间积累了许多宝贵的经验,这都是我以后走上工作岗位的巨大的财富。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- c# 实验 报告 简易 计算器