WP7下实现计算器(2)

 -----------------------2010/10/18/22:32-----------------------------------------------------------
首先我必须感谢Green,没有他的帮助不可能这么快找到错误……
 
如果你看过第一篇笔记,你会发现一个正常人不会犯的错误
在Operation类里,NumberB属性的get居然被我写成了return numberA……
 
好吧,这就是我一直纠结的问题,郁闷啊
 
此外,计算器的另外一些问题在Green的帮助下也解决了,具体还是看代码吧……
 
首先这是Operation类,嗯,改正的
---------Operation.cs------------------
Code:
  1. public class operation   
  2.    {   
  3.        private double numberA = 0;   
  4.        private double numberB = 0;   
  5.        public double NumberA   
  6.        {   
  7.            get { return numberA; }   
  8.            set { numberA = value; }   
  9.        }   
  10.        public double NumberB   
  11.        {   
  12.            get { return numberB; }   
  13.            set { numberB = value; }   
  14.        }   
  15.        public virtual double GetResult()   
  16.        {   
  17.            return 0;   
  18.        }   
  19.    }  
具体的运算类没有改变
--------allOperation.cs---------------------
Code:
  1. class operationAdd : operation   
  2. {   
  3.     public override double GetResult()   
  4.     {   
  5.         double result = 0;   
  6.         result = NumberA + NumberB;   
  7.         return result;   
  8.     }   
  9. }   
  10. class operationSub : operation   
  11. {   
  12.     public override double GetResult()   
  13.     {   
  14.         double result = 0;   
  15.         result = NumberA - NumberB;   
  16.         return result;   
  17.     }   
  18. }  
 
------------IFactory.cs-------------------------
Code:
  1. interface Ifactory   
  2.     {   
  3.          operation createOperator();   
  4.     }  

都没变

----------allFactories.cs----------------------------

Code:
  1. class addFactory : Ifactory   
  2.     {   
  3.         public  operation createOperator()   
  4.         {   
  5.             operation oper = new operationAdd();   
  6.             return oper;   
  7.         }   
  8.     }   
  9.     class subFactory : Ifactory   
  10.     {   
  11.         public  operation createOperator()   
  12.         {   
  13.             operation oper = new operationSub();   
  14.             return oper;   
  15.         }   
  16.     }  

下面是客户端,改得最严重的地方

--------------client---------------------------------

Code:
  1. public partial class MainPage : PhoneApplicationPage   
  2.    {   
  3.        operation oper;   
  4.        Ifactory operFactory;   
  5.        bool isNumASet = true;  //是否设置NumA   
  6.        double numA = 0;   
  7.        double numB = 0;   
  8.        public MainPage()   
  9.        {   
  10.            InitializeComponent();   
  11.        }   
  12.   
  13.        private void button1_Click(object sender, RoutedEventArgs e)   
  14.        {   
  15.               
  16.           textBox1.Text += "1";   
  17.        }   
  18.   
  19.        private void button2_Click(object sender, RoutedEventArgs e)   
  20.        {   
  21.            textBox1.Text += "2";   
  22.        }   
  23.   
  24.        private void button3_Click(object sender, RoutedEventArgs e)   
  25.        {   
  26.            textBox1.Text += "3";   
  27.        }   
  28.   
  29.        private void button4_Click(object sender, RoutedEventArgs e)   
  30.        {   
  31.            textBox1.Text += "4";   
  32.        }   
  33.   
  34.        private void setNum()   
  35.        {   
  36.            if (textBox1.Text.Length == 0)   
  37.            {   
  38.                numB = numA;   
  39.                return;   
  40.            }   
  41.            if (isNumASet == true)   
  42.            {   
  43.                numA = Convert.ToDouble(textBox1.Text);   
  44.                isNumASet = false;   
  45.                textBox1.Text = "";   
  46.            }   
  47.            else  
  48.            {   
  49.                numB = Convert.ToDouble(textBox1.Text);   
  50.                textBox1.Text = "";   
  51.            }   
  52.        }   
  53.   
  54.        //"+" button clicked   
  55.        private void button5_Click(object sender, RoutedEventArgs e)   
  56.        {   
  57.            operatorbox.Text = "+";   
  58.            setNum();   
  59.        }   
  60.   
  61.        //"-"button clicked   
  62.        private void button6_Click(object sender, RoutedEventArgs e)   
  63.        {   
  64.            operatorbox.Text = "-";   
  65.            setNum();   
  66.        }   
  67.   
  68.   
  69.   
  70.        //"="button clicked   
  71.        private void button7_Click(object sender, RoutedEventArgs e)   
  72.        {   
  73.            setNum();   
  74.            getFactory(operatorbox.Text);   
  75.            oper = operFactory.createOperator();   
  76.            oper.NumberA = numA;   
  77.            oper.NumberB = numB;   
  78.            textBox1.Text=Convert.ToString( oper.GetResult());   
  79.            isNumASet = true;   
  80.   
  81.        }   
  82.   
  83.        private void getFactory(string p)   
  84.        {   
  85.            switch (p)   
  86.            {   
  87.                case "+" :   
  88.                    operFactory = new addFactory();   
  89.                    break;   
  90.                case "-" :   
  91.                    operFactory = new subFactory();   
  92.                    break;   
  93.            }   
  94.        }   
  95.   
  96.    }  

36行加入的判断语句很棒(自我感觉良好)

79行的语句也很重要,其次还有另一种实现方法(Green提供)就是将NumA=oper.GetResult();

似乎同样可以达到效果。

至此,简单的加减功能实现,无论你怎么虐待界面都不会跳出错误,而且输入“+=”也能正确得出结果。

嗯 似乎 so far so good

由于使用了工厂模式的设计模式,所以我想扩展其他运算功能似乎不是问题。

现在的问题是“点号”的加入和“清零”的加入,看看明天能实现不  lol

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值