-------------2010/10/17/16:10-----------------------------------------
今年刚开始学C#和设计模式又对WP7非常有爱,于是自己尝试自己在Wp7下实现一个计算器,因为太菜困难重重……
无论如何都要坚持,故写此笔记,一是督促自己不要放弃,二是寻找高手多多指点 :)
我想完全仿照windows自带计算器样式制作。
界面:
个人感觉整个窗体设计过程和Windows窗体设计没有区别……(其实我想问右边那些火星文是怎么出来的?)
因为刚刚学过工厂设计模式,所以想现学现卖一下
代码:
---------Operation.cs--------------------------
- public class operation
- {
- private double numberA = 0;
- private double numberB = 0;
- public double NumberA
- {
- get { return numberA; }
- set { numberA = value; }
- }
- public double NumberB
- {
- get { return numberA; }
- set { numberB = value; }
- }
- public virtual double GetResult()
- {
- return 0;
- }
- }
----------allOperations.cs-------------------------------
- class operationAdd : operation
- {
- public override double GetResult()
- {
- double result = 0;
- result = NumberA + NumberB;
- return result;
- }
- }
- class operationSub : operation
- {
- public override double GetResult()
- {
- double result = 0;
- result = NumberA - NumberB;
- return result;
- }
- }
-----IFacotry.cs-----------------------------------
- interface Ifactory
- {
- operation createOperator();
- }
------allFactories.cs--------------------------------
- class addFactory : Ifactory
- {
- public operation createOperator()
- {
- operation oper = new operationAdd();
- return oper;
- }
- }
- class subFactory : Ifactory
- {
- public operation createOperator()
- {
- operation oper = new operationSub();
- return oper;
- }
- }
----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
- public partial class MainPage : PhoneApplicationPage
- {
- operation oper;
- Ifactory operFactory;
- bool isNumASet = true; //是否设置NumA
- double numA = 0;
- double numB = 0;
- public MainPage()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, RoutedEventArgs e)
- {
- textBox1.Text += "1";
- }
- private void button2_Click(object sender, RoutedEventArgs e)
- {
- textBox1.Text += "2";
- }
- private void button3_Click(object sender, RoutedEventArgs e)
- {
- textBox1.Text += "3";
- }
- private void button4_Click(object sender, RoutedEventArgs e)
- {
- textBox1.Text += "4";
- }
- private void setNum()
- {
- if (isNumASet == true)
- {
- numA = Convert.ToDouble(textBox1.Text);
- isNumASet = false;
- textBox1.Text = "";
- }
- else
- {
- numA = Convert.ToDouble(textBox1.Text);
- textBox1.Text = "";
- }
- }
- //"+" button clicked
- private void button5_Click(object sender, RoutedEventArgs e)
- {
- operatorbox.Text = "+";
- setNum();
- }
- //"-"button clicked
- private void button6_Click(object sender, RoutedEventArgs e)
- {
- operatorbox.Text = "-";
- setNum();
- }
- //"="button clicked
- private void button7_Click(object sender, RoutedEventArgs e)
- {
- setNum();
- //numB = Convert.ToDouble(textBox1.Text);
- getFactory(operatorbox.Text);
- oper = operFactory.createOperator();
- oper.NumberA = numA;
- oper.NumberB = numB;
- textBox1.Text=Convert.ToString( oper.GetResult());
- }
- private void getFactory(string p)
- {
- switch (p)
- {
- case "+" :
- operFactory = new addFactory();
- break;
- case "-" :
- operFactory = new subFactory();
- break;
- }
- }
- }