面向对象中,绝大多数都是以类做为统计数据PCB的基本上基层单位,它将统计数据和操作方式统计数据的方式紧密结合成两个类。结构设计类时不期望ED55类中的统计数据,而要期望透过方式来读取统计数据,这种就能达至PCB统计数据的目地,方便快捷之后的保护升级换代,也能在操作方式统计数据时福兰县推论。除此之外,PCB还能化解统计数据读取的职权难题,能采用PCB将统计数据逐步形成两个半封闭的外部空间,接着增设统计数据在外部空间外部和外部的采用职权。

可执行程序:

自动草稿

计算机程序介面

计算机程序:

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceWindowsFormsApplication1{publicpartialclassCalculator:Form{publicCalculator(){
InitializeComponent();
}publicclassSZ{privatedoublesz1 =0;privatedoublesz2=0;publicdoubleSZ1
{get{returnsz1; }set{ sz1 =value; }
}publicdoubleSZ2
{get{returnsz2; }set{ sz2 =value; }
}//将类中的成员定义成虚拟的,表示这些成员将会在继承后重写其中的内容。publicvirtualdoubleGetResult(){doubleresult =0;returnresult;
}
}//添加四个加减乘除类继承SZ类,重写父类中的GetResult方式。//加法类publicclassOperationAdd:SZ{publicoverridedoubleGetResult(){doubleresult =0;
result = SZ1 SZ2;returnresult;
}
}//减法类publicclassOperationSub:SZ{publicoverridedoubleGetResult(){doubleresult =0;
result = SZ1 – SZ2;returnresult;
}
}//乘法类publicclassOperationMul:SZ{publicoverridedoubleGetResult(){doubleresult =0;
result = SZ1 * SZ2;returnresult;
}
}//除法类publicclassOperationDiv:SZ{publicoverridedoubleGetResult(){doubleresult =0;if(SZ2 ==0)
{thrownewException(“除数不能为0”);
}
result = SZ1 *1.0/ SZ2;returnresult;
}
}// 添加两个运算符枚举类,代表四则运算符号publicenumEnumOperator
{
None =0,Add =1,Subtraction =2,Multiply =3,Divide =4}publicclassOperatorInput{//只需输入运算符就能实例化出合适的对象publicstaticSZcreateOperate(EnumOperator eo){
SZ oper =null;switch(eo)
{caseEnumOperator.Add:
oper =newOperationAdd();break;caseEnumOperator.Subtraction:
oper =newOperationSub();break;caseEnumOperator.Multiply:
oper =newOperationMul();break;caseEnumOperator.Divide:
oper =newOperationDiv();break;default:break;
}returnoper;
}
}doublecurrentValue =0;//第1个数doublenumSum =0;//第2个数privateEnumOperator currentOperator;//当前操作方式符privatevoidClickNumber(stringstrNumber){
tex_result.Text = tex_result.Text strNumber;
currentValue = Convert.ToDouble(tex_result.Text);
tex_display.Text = strNumber;
}privatevoidbut7_Click(objectsender, EventArgs e){
ClickNumber(but7.Text);
}privatevoidbut8_Click(objectsender, EventArgs e){
ClickNumber(but8.Text);
}privatevoidbut9_Click(objectsender, EventArgs e){
ClickNumber(but9.Text);
}privatevoidbut4_Click(objectsender, EventArgs e){
ClickNumber(but4.Text);
}privatevoidbut5_Click(objectsender, EventArgs e){
ClickNumber(but5.Text);
}privatevoidbut6_Click(objectsender, EventArgs e){
ClickNumber(but6.Text);
}privatevoidbut1_Click(objectsender, EventArgs e){
ClickNumber(but1.Text);
}privatevoidbut2_Click(objectsender, EventArgs e){
ClickNumber(but2.Text);
}privatevoidbut3_Click(objectsender, EventArgs e){
ClickNumber(but3.Text);
}privatevoidbut0_Click(objectsender, EventArgs e){
ClickNumber(but0.Text);
}//定义两个枚举运算符参数的方式OperatorClick(EnumOperator eo),里面调用DisplayOperator(EnumOperator eo)方式privatevoidOperatorClick(EnumOperator eo){if(currentOperator != EnumOperator.None)
{
Calculate();//计算}else{double.TryParse(tex_result.Text,outnumSum);
}
DisplayOperator(eo);
tex_result.Text =“”;
currentOperator = eo;
}//四则运算符按钮的点击事件中调用代表运算符号的枚举// privatevoidbutton12_Click(objectsender, EventArgs e){
OperatorClick(EnumOperator.Add);
}//-privatevoidbutton13_Click(objectsender, EventArgs e){
OperatorClick(EnumOperator.Subtraction);
}//Xprivatevoidbutton14_Click(objectsender, EventArgs e){
OperatorClick(EnumOperator.Multiply);
}//÷privatevoidbutton15_Click(objectsender, EventArgs e){
OperatorClick(EnumOperator.Divide);
}//根据运算符号来执行四则运算显示的内容 函数 DisplayOperator(EnumOperator op)privatevoidDisplayOperator(EnumOperator op){switch(op)
{caseEnumOperator.None:
tex_display.Text = tex_result.Text;
tex_display.Text =“”;break;caseEnumOperator.Add:if(tex_result.Text !=“”)
{
tex_display.Text = tex_result.Text;
}
tex_display.Text =” “;break;caseEnumOperator.Subtraction:if(tex_result.Text !=“”)
{
tex_display.Text = tex_result.Text;
}
tex_display.Text =“-“;break;caseEnumOperator.Multiply:
tex_display.Text = tex_result.Text;
tex_display.Text =“x”;break;caseEnumOperator.Divide:
tex_display.Text = tex_result.Text;
tex_display.Text =“/”;break;default:break;
}
}//根据运算符和两个数字的计算结果 调用单纯工厂模式生成的类 Evaluate()privatevoidCalculate(){
SZ oper;//根据不同的对象生成不同的类,多态!switch(currentOperator)
{caseEnumOperator.None:break;caseEnumOperator.Add:
oper = OperatorInput.createOperate(EnumOperator.Add);
oper.SZ1 = numSum;
oper.SZ2 = currentValue;
numSum = oper.GetResult();break;caseEnumOperator.Subtraction:
oper = OperatorInput.createOperate(EnumOperator.Subtraction);
oper.SZ1 = numSum;
oper.SZ2 = currentValue;
numSum = oper.GetResult();break;caseEnumOperator.Multiply:
oper = OperatorInput.createOperate(EnumOperator.Multiply);
oper.SZ1 = numSum;
oper.SZ2 = currentValue;
numSum = oper.GetResult();break;caseEnumOperator.Divide:if(currentValue !=0)
{
oper = OperatorInput.createOperate(EnumOperator.Divide);
oper.SZ1 = numSum;
oper.SZ2 = currentValue;
numSum = oper.GetResult();
}else{
MessageBox.Show(“除数不能为零”,“请重新输入”);
}break;
}
currentValue =0;
currentOperator = EnumOperator.None;
}privatevoidbutEqual_Click(objectsender, EventArgs e){
Calculate();
tex_result.Text = numSum.ToString();//防止重复按 =if(tex_result.Text.Length

1.本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2.分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3.不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4.本站提供的源码、模板、插件等其他资源,都不包含技术服务请大家谅解!
5.如有链接无法下载或失效,请联系管理员处理!
6.本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!