计算器的实现.docx

上传人:b****7 文档编号:24007073 上传时间:2023-05-23 格式:DOCX 页数:38 大小:137.66KB
下载 相关 举报
计算器的实现.docx_第1页
第1页 / 共38页
计算器的实现.docx_第2页
第2页 / 共38页
计算器的实现.docx_第3页
第3页 / 共38页
计算器的实现.docx_第4页
第4页 / 共38页
计算器的实现.docx_第5页
第5页 / 共38页
点击查看更多>>
下载资源
资源描述

计算器的实现.docx

《计算器的实现.docx》由会员分享,可在线阅读,更多相关《计算器的实现.docx(38页珍藏版)》请在冰豆网上搜索。

计算器的实现.docx

计算器的实现

计算器的实现

03计算机一班梁伟明2003374116

一、实验目的

熟悉可视化程序设计的编程方法,练习基本控件、基本语句的使用

二、实验内容

模拟计算器的基本功能,可以实现对输入的数进行加、减、乘、除、开方等的运算并得出结果。

三、程序功能描述

1)、该程序包含四个类:

1、Form1

实现计算器的主界面,包括23个按键和一个菜单,实现了用户与计算器的交互,还实现了用户的键盘输入功能。

2、Form2

实现了计算器帮助功能的界面,包括功能说明和使用说明

3、Converts

实现了由用户输入的数据格式转换为类Count所要求输入的数据格式,并把count函数返回的数据格式转换成计算器输出界面所要求的数据格式。

通过调节输入,通过调用count函数的加减乘除来实现计算器的其他功能。

该类还实现了输入的数据计算的优先级判断及一些连续输入操作的实现。

此类为Form1与count的接口,使用了此类能使Form1与count具有各自的独立性,不需考虑输入对计算的影响,即使输入格式或顺序改变也不会影响count类。

4、Count

实现了大浮点数的加减乘除并返回结果。

计算的精度为168个字符,返回长度为168个字符的字符串

2)、计算器的输入要求与精度要求

该计算器最多能输入42个数字字符,精度为42位的整数和42位小数。

3)、计算器的输出

该计算器最多输出42个数字字符,可以输出科学表示的大整数或小数,例如当结果大于42位整数时,结果输入为x.xxxxxe+n。

如结果过少,会输出x.xxxxxxe-n。

4)、计算器的功能

加法:

1、单击‘+’按钮执行加法运算。

2、按下键盘‘+’键也能进行加法运算。

";

减法:

1、单击‘-’按钮执行减法运算。

2、按下键盘‘-’键也能进行减法运算。

";

乘法:

1、单击‘*’按钮执行乘法运算。

2、按下键盘‘*’键也能进行乘法运算。

";

除法:

1、单击‘/’按钮执行除法运算。

2、按下键盘‘/’键也能进行除法运算。

若输入为0将会出错,系统会有提示!

";

取反:

1、单击‘+/-’按钮执行取反运算。

";

取倒数:

1、单击‘1/x’按钮执行取倒数运算。

";

开方:

1、单击‘sqr’按钮执行开方运算。

\n\n若输入为负数将会出错,系统会有提示!

取百分数:

1、单击‘%’按钮执行取百分数运算。

";

清除操作数:

1、单击‘CE’按钮清除输入的操作数。

";

清除所有操作:

1、单击‘CE’按钮清除以前输入的操作数和操作码,计算器重新开始计算。

清除最后一位操作数:

1、单击‘backspace’按钮清除操作数的最后一位数字。

\n\n2、按下键盘‘backspace’键也能清除操作数最后一位。

";

复制:

把计算器的输入值或者结果复制到系统剪贴板

粘贴:

把系统剪贴板里的数字粘贴到计算器作为操作数的输入,要是剪贴板上的不是数字则忽略此操作,要是剪贴板上前半部分是数字,则只把前半部分转换为浮点型粘贴到计算器。

帮助主题:

包括功能说明与使用说明两个菜单,里面有简单的使用说明。

功能附加说明

该计算器能够实现连续的输入操作,优先级跟输入的顺序一样。

一下举了几个例子说明输入与优先级的关系:

1、输入顺序9+8*7/6-2+4sqrt%(1/x)等价于1/(((9+8)*7/6-2+sqrt(4))%)

2、输入顺序9+8=+7=sqtr等价于sqtr((9+8)+7)

3、输入顺序9+8=5+6%等价于9+8、(5+6)%两个式子。

四、用户操作界面

五、主要源代码

Form1:

publicForm1()

{

//Windows窗体设计器支持所必需的

InitializeComponent();

//获取textBox2的焦点

this.Controls.Add(this.textBox2);

this.textBox2.Focus();

this.textBox2.KeyPress+=newKeyPressEventHandler(keypressed);

}

//处理键盘输入

privatevoidkeypressed(Objecto,KeyPressEventArgse)

{

if(e.KeyChar=='+')

compute(this.numbers,'+');

elseif(e.KeyChar=='-')

compute(this.numbers,'-');

elseif(e.KeyChar=='*')

compute(this.numbers,'*');

elseif(e.KeyChar=='=')

compute(this.numbers,'=');

elseif(e.KeyChar=='/')

compute(this.numbers,'/');

elseif(e.KeyChar==8)

{

if(this.numbers!

=null&&this.numbers.Length!

=0)

{

this.numbers=this.numbers.Substring(0,this.numbers.Length-1);

this.textBox1.Text=this.sign+this.numbers+this.douhao;

}

if(this.numbers==null||this.numbers.Length==0)

this.textBox1.Text="0.";

}

else

{

if(e.KeyChar=='0')

{

if(this.numbers!

=null)

{

this.numbers+="0";

this.textBox1.Text=this.sign+this.numbers+this.douhao;

this.textBox2.Focus();

}

elsez="0";

}

elseif(e.KeyChar=='.')

{

if(this.numbers==null||this.numbers.Length==0)

this.numbers+="0";

if(this.numbers.IndexOf(".",0,this.numbers.Length)==-1)

{

this.numbers+=".";

this.douhao=null;

}

this.textBox1.Text=this.sign+this.numbers+this.douhao;

}

elseif(e.KeyChar>='1'&&e.KeyChar<='9')

{

this.numbers+=e.KeyChar;

this.textBox1.Text=this.sign+this.numbers+this.douhao;

}

}

}

protectedoverridevoidDispose(booldisposing)

{

if(disposing)

{

if(components!

=null)

{

components.Dispose();

}

}

base.Dispose(disposing);

}

#regionWindows窗体设计器生成的代码

Windows窗体设计器生成的代码///应用程序的主入口点。

[STAThread]

staticvoidMain()

{

Application.Run(newForm1());

}

//处理鼠标点击按钮事件

privatevoidone_Click(objectsender,System.EventArgse)

{

this.numbers+="1";

this.textBox1.Text=this.sign+this.numbers+this.douhao;

this.textBox2.Focus();

}

//一下数字按钮程序原理同one_Click

privatevoidtwo_Click(objectsender,System.EventArgse)

privatevoidthree_Click(objectsender,System.EventArgse)

privatevoidfore_Click(objectsender,System.EventArgse)

privatevoidfive_Click(objectsender,System.EventArgse)

privatevoidsix_Click(objectsender,System.EventArgse)

privatevoidseven_Click(objectsender,System.EventArgse)

privatevoideight_Click(objectsender,System.EventArgse)

privatevoidnine_Click(objectsender,System.EventArgse)

privatevoidzore_Click(objectsender,System.EventArgse)

{

if(this.numbers!

=null)

{

this.numbers+="0";

this.textBox1.Text=this.sign+this.numbers+this.douhao;

this.textBox2.Focus();

}

elsez="0";

}

privatevoiddou_Click(objectsender,System.EventArgse)

{

if(this.numbers==null||this.numbers.Length==0)

this.numbers+="0";

if(this.numbers.IndexOf(".",0,this.numbers.Length)==-1)

{

this.numbers+=".";

this.douhao=null;

}

this.textBox1.Text=this.sign+this.numbers+this.douhao;

this.textBox2.Focus();

}

privatevoidBackspace_Click(objectsender,System.EventArgse)

{

if(this.numbers!

=null&&this.numbers.Length!

=0)

{

this.numbers=this.numbers.Substring(0,this.numbers.Length-1);

this.textBox1.Text=this.sign+this.numbers+this.douhao;

}

if(this.numbers==null||this.numbers.Length==0)

this.textBox1.Text="0.";

this.textBox2.Focus();

}

privatevoidCE_Click(objectsender,System.EventArgse)

{

this.numbers=null;

this.sign=null;

this.douhao=".";

this.textBox1.Text="0.";

this.textBox2.Focus();

}

privatevoidC_Click(objectsender,System.EventArgse)

{

this.numbers=null;

this.sign=null;

this.douhao=".";

this.textBox1.Text="0.";

converts.setnull();

this.textBox2.Focus();

}

privatevoidadd_Click(objectsender,System.EventArgse)

{

compute(this.numbers,'+');

}

privatevoidsub_Click(objectsender,System.EventArgse)

{

compute(this.numbers,'-');

}

privatevoidor_Click(objectsender,System.EventArgse)

{

if(this.sign==null)

this.sign="-";

elseif(this.sign=="-")

this.sign=null;

this.textBox1.Text=this.sign+this.numbers+this.douhao;

this.textBox2.Focus();

}

privatevoidmul_Click(objectsender,System.EventArgse)

{

compute(this.numbers,'*');

}

//一下程序原理同mul_Click

privatevoiddiv_Click(objectsender,System.EventArgse)

privatevoidsqrt_Click(objectsender,System.EventArgse)

privatevoiddaoshu_Click(objectsender,System.EventArgse)

privatevoidbaifeng_Click(objectsender,System.EventArgse)

privatevoidequel_Click(objectsender,System.EventArgse)

privatevoidcompute(stringnumber,charsign)

{

if(number!

=null&&this.douhao==".")

number+=".";

if(this.sign==null)this.sign="+";

if(number==null&&z=="0")number="0";

this.numbers=converts.startcount(this.sign+number,sign);

if(this.numbers==null)

{

MessageBox.Show("Waning:

被除数不能为0!

","Error!

",MessageBoxButtons.OK,MessageBoxIcon.Error);

this.numbers=null;

this.sign=null;

this.douhao=".";

this.textBox1.Text="0.";

this.z=null;

this.textBox2.Focus();

}

elseif(this.numbers=="-")

{

MessageBox.Show("Waning:

被开方数不能为负数!

","Error!

",MessageBoxButtons.OK,MessageBoxIcon.Error);

this.numbers=null;

this.sign=null;

this.douhao=".";

this.textBox1.Text="0.";

this.z=null;

this.textBox2.Focus();

}

 

else

{

if(this.numbers.Length>42)

this.numbers=this.numbers.Substring(0,42);

this.textBox1.Text=this.numbers;

this.numbers=null;

this.sign=null;

this.douhao=".";

this.z=null;

this.textBox2.Focus();

}

}

privatevoidtextBox1_TextChanged(objectsender,System.EventArgse)

{

if(textBox1.TextLength==43)

{

this.numbers=this.numbers.Substring(0,this.numbers.Length-1);

this.textBox1.Text=this.sign+this.numbers+this.douhao;

MessageBox.Show("Waning:

最多只能输入42个字符!

","Waning!

",MessageBoxButtons.OK,MessageBoxIcon.Warning);

}

}

privatevoidcopy_Click(objectsender,System.EventArgse)

{

stringtext=this.textBox1.Text;

Clipboard.SetDataObject(text);

}

privatevoidmenuItem3_Click(objectsender,System.EventArgse)

{

stringnum=null;

stringsign=null;

IDataObjectdata=Clipboard.GetDataObject();

if(data.GetDataPresent(typeof(string)))

{

stringdatas=(string)data.GetData(typeof(string));

Console.WriteLine(datas);

for(inti=0;i

{

if(i==0&&datas[i]=='-')sign="-";

elseif((datas[i]>='0'&&datas[i]<='9')||(datas[i]=='.'&&i!

=0))num+=datas[i];

elsebreak;

}

if(num!

=null&&num.IndexOf(".",0,num.Length)==-1)this.douhao=".";

elsethis.douhao=null;

if(num!

=null)

{

this.sign=sign;

this.numbers=num;

this.textBox1.Text=this.sign+this.numbers+this.douhao;

}

}

}

privatevoidtextBox1_GotFocus(objectsender,System.EventArgse)

{

Console.WriteLine("hello");

this.textBox2.Focus();

}

privatevoidmenuItem4_Click(objectsender,System.EventArgse)

{

Form2childform=newForm2();

childform.Show();

}

}

}

Converts:

publicConverts()

{

//

//TODO:

在此处添加构造函数逻辑

//

this.number1="0";

this.number2="0";

this.numbers=null;

this.sign1="+";

this.sign2="+";

this.sign='+';

this.number1=this.number1.PadRight(168,'0');

this.number2=this.number2.PadRight(168,'0');

}

publicvoidsetnull()

{

this.number1="0";

this.number2="0";

this.numbers=null;

this.sign1="+";

this.sign2="+";

this.sign='+';

this.number1=this.number1.PadRight(168,'0');

this.number2=this.number2.PadRight(168,'0');

}

privatestringconverting(stringnum)

{

stringnums;

intposition;

nums=num;

position=nums.IndexOf(".",0,nums.Length);

nums=nums.PadRight(84+1+position,'0');

nums=nums.PadLeft(168,'0');

nums=nums.Remove(83,1);

nums='0'+nums;

returnnums;

}

privatestringre

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

当前位置:首页 > 经管营销 > 经济市场

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1