WP7下实现计算器(1)

-------------2010/10/17/16:10-----------------------------------------

今年刚开始学C#和设计模式又对WP7非常有爱,于是自己尝试自己在Wp7下实现一个计算器,因为太菜困难重重……

无论如何都要坚持,故写此笔记,一是督促自己不要放弃,二是寻找高手多多指点    :)

我想完全仿照windows自带计算器样式制作。

界面:

 
 
个人感觉整个窗体设计过程和Windows窗体设计没有区别……(其实我想问右边那些火星文是怎么出来的?)
 
 
因为刚刚学过工厂设计模式,所以想现学现卖一下
代码:
---------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 numberA; }   
  13.             set { numberB = value; }   
  14.         }   
  15.         public virtual double GetResult()   
  16.         {   
  17.             return 0;   
  18.         }   
  19.     }  
----------allOperations.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.    }  
-----IFacotry.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---------------------------------
是在不会处理运算符的输入,所以在窗体添加了一个影藏的textbox-operatorbox用来在当用户点击运算符按钮时将相应运算符存入其中(脑子蛮乱的)
 
设置了两个临时变量double型NumA,NumB用来读取用户输入的数字然后转给NumberA和NumberB
于是问题便华丽的出现了,在实际计算中NumA的值永远都等于NumB的值
当我输入1+1时候很高兴,因为结果显示2
但是当我输入1+2时 结果却是4……
也就是说现在NumberA=NumberB=2;
这个彻底把握搅晕了…… 
不仅如此,程序还有很多bug
比如说在没有输入任何数字的情况下按运算符号就会出现这样的错误,因为我没有输入任何数……
 
 
 2010/10/17/16:10
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 (isNumASet == true)   
  37.             {   
  38.                 numA = Convert.ToDouble(textBox1.Text);   
  39.                 isNumASet = false;   
  40.                 textBox1.Text = "";   
  41.             }   
  42.             else  
  43.             {   
  44.                 numA = Convert.ToDouble(textBox1.Text);   
  45.                 textBox1.Text = "";   
  46.             }   
  47.         }   
  48.   
  49.         //"+" button clicked   
  50.         private void button5_Click(object sender, RoutedEventArgs e)   
  51.         {   
  52.             operatorbox.Text = "+";   
  53.             setNum();   
  54.         }   
  55.   
  56.         //"-"button clicked   
  57.         private void button6_Click(object sender, RoutedEventArgs e)   
  58.         {   
  59.             operatorbox.Text = "-";   
  60.             setNum();   
  61.         }   
  62.   
  63.   
  64.   
  65.         //"="button clicked   
  66.         private void button7_Click(object sender, RoutedEventArgs e)   
  67.         {   
  68.             setNum();   
  69.             //numB = Convert.ToDouble(textBox1.Text);   
  70.             getFactory(operatorbox.Text);   
  71.             oper = operFactory.createOperator();   
  72.             oper.NumberA = numA;   
  73.             oper.NumberB = numB;   
  74.             textBox1.Text=Convert.ToString( oper.GetResult());   
  75.   
  76.         }   
  77.   
  78.         private void getFactory(string p)   
  79.         {   
  80.             switch (p)   
  81.             {   
  82.                 case "+" :   
  83.                     operFactory = new addFactory();   
  84.                     break;   
  85.                 case "-" :   
  86.                     operFactory = new subFactory();   
  87.                     break;   
  88.             }   
  89.         }   
  90.   
  91.     }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值