中易网

求一个用简单工厂模式编写的C#计算器,只有加减乘除和等于就好~谢谢~

答案:3  悬赏:60  
解决时间 2021-03-02 00:11
求一个用简单工厂模式编写的C#计算器,只有加减乘除和等于就好~谢谢~
最佳答案
//这个程序只是实现两个数的加减乘除,其它的功能由于时间原因没写。有兴趣加群113572029
//五个类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{
abstract class Count
{
private double one;
public double One
{
get { return one; }
set { one = value; }
}
private double two;
public double Two
{
get { return two; }
set { two = value; }
}
public Count(){}
public Count(double one, double two)
{
this.One = one;
this.Two = two;
}
public abstract double count();
}
//加
class Add : Count
{
public Add()
:base(){}
public Add(double one, double two)
:base(one,two)
{
this.One = one;
this.Two = two;
}
public override double count()
{
return this.One + this.Two;
}
}
//减
class Cut : Count
{
public Cut()
: base() { }
public Cut(double one, double two)
: base(one, two)
{
this.One = one;
this.Two = two;
}
public override double count()
{
return this.One - this.Two;
}
}
//乘
class Ride : Count
{
public Ride()
: base() { }
public Ride(double one, double two)
: base(one, two)
{
this.One = one;
this.Two = two;
}
public override double count()
{
return this.One * this.Two;
}
}
//除
class Divide : Count
{
public Divide()
: base() { }
public Divide(double one, double two)
: base(one, two)
{
this.One = one;
this.Two = two;
}
public override double count()
{
return this.One / this.Two;
}
}
}
//简单的工厂模式
private void button1_Click(object sender, EventArgs e)
{
try
{
Count count = null;
switch (comboBox1.Text)
{
case "+":
count = new Add(double.Parse(txtOne.Text), double.Parse(txtTwo.Text));
break;
case "-":
count = new Cut(double.Parse(txtOne.Text), double.Parse(txtTwo.Text));
break;
case "*":
count = new Ride(double.Parse(txtOne.Text), double.Parse(txtTwo.Text));
break;
case "/":
count = new Divide(double.Parse(txtOne.Text), double.Parse(txtTwo.Text));
break;

}
if (count != null)
txtCount.Text = count.count().ToString();
else
MessageBox.Show("系统错误!");
}
catch
{
MessageBox.Show("请输入合法的操作数!");
}
}
全部回答
这是我的个人理解: 简单工厂模式的创建步骤: 1、定义申明一个基类 2、建立多个继承同一个的基类的子类。 3、再把子类捆绑到同一个对外接口上 我写的是控制台程序总共有6个类:其中Operate类为基类,其他的4个类为Operate的子类(Add,Sub,Multiply,Div),factory就是那个对外接口。没有实现 等于 功能,其实factory类就相当于等于功能。 Operate类 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _4 { class Operate { int number_L; protected int Number_L { get { return number_L; } } int number_R; protected int Number_R { get { return number_R; } } public Operate(int value_L,int value_R) { this.number_L = value_L; this.number_R = value_R; } public virtual int Result() { return 0; } } } 四个子类只是重写了父类中的 Result方法 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _4 { class Add:Operate //加法实现 { public Add(int value_L, int value_R) : base(value_L, value_R) { } public override int Result() { return this.Number_L + this.Number_R; } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _4 { class Sub:Operate //减法实现 { public Sub(int value_L, int value_R) : base(value_L, value_R) { } public override int Result() { return this.Number_L - this.Number_R; } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _4 { class Mutiply:Operate //乘法实现 { public Mutiply(int value_L, int value_R) : base(value_L, value_R) { } public override int Result() { return this.Number_L * this.Number_R; } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _4 { class Div:Operate //除法实现 { public Div(int value_L, int value_R) : base(value_L, value_R) { } public override int Result() { if (Number_R == 0) return int.MaxValue; return Number_L / Number_R; } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _4 { class Factory //工厂实现,此处可以与 = 相对应 { public Operate Choice(int value_L, int value_R, char value_op) { Operate op = null; switch (value_op) { case '+': op=new Add(value_L,value_R); break; case '-': op=new Sub(value_L, value_R); break; case '*': op= new Mutiply(value_L,value_R); break; case '/': op= new Div(value_L, value_R); break; default: break; } return op; } } } 主程序的实现 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _4 { class Program { static void Main(string[] args) { Factory factory = new Factory(); Console.WriteLine("请输入第一个数字:"); int value_L = int.Parse(Console.ReadLine()); Console.WriteLine("请输入第二个数字:"); int value_R = int.Parse(Console.ReadLine()); Console.WriteLine("请输入运算符号('+'、'-'、'*'、'/'):"); char value_Op=Convert.ToChar(Console.ReadLine()); Console.WriteLine("运算结果:"); int result = factory.Choice(value_L, value_R, value_Op).Result(); Console.WriteLine(result); Console.Read(); } } }
基本和c++一样,区别在于界面设计~当然语法也略有差别。 具体步骤 1、新建窗体,添加数字,运算符号按钮,文本框(近用于显示) 2、为各个按钮编写onclick事件响应函数,主要是记录用户输入和在文本框中显示 3、按下=时计算结果,并将结果显示在文本框中
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
今年冬季流行什么颜色的女帽子
关于家用监控摄像头问题,具体有哪些功能呢?
什么叫受让?
事业单位编哪些岗位好
开封包子我想知道这个在什么地方
请问姓李女孩第二个字八画第三个字十六画取什
绝味鸭脖重庆花卉园店地址在什么地方,想过去
甘肃违章照片怎么查询
帕坦汀车蜡打上去用干毛巾还是湿毛巾擦好
超过退货时间客户要求退货
盘古砂岩雕塑艺术地址有知道的么?有点事想过
警字五行属什么
批处理 bat 如何打开 lockdir.exe 程序后,自
如何在课堂教学中培养学生的数感
新包青天,为什么VCD版本有61集,而DVD版本只
推荐资讯
蒸湘区衡阳诚信家电维修部在什么地方啊,我要
中国移动预存话费送手机办理88套餐一年赠送60
谁能给我一个苹果四s的解锁密码账号
我老婆和我弟媳性格不合,现在大家提出分家,
楼邦强力瓷砖粘贴剂专卖怎么去啊,有知道地址
有没有专门卖洗发水的微商?
请问这是河田玉吗?
求最终进化无错精校版
奋斗ni'of'n'd'co'j'
家用迷你路由器恢复出厂设置后怎么重新设置
同方药业楼山后店在哪里啊,我有事要去这个地
我的电脑无线网用不起来,直接连接宽带却可以
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?