• WPF四则运算《《《《《策略模式


    设计思路:

              因为之前没有用过WPF,听说和window窗体语法类似,就想着仿照之前的Window窗体做的,首先用三个textbox存储数据,添加一个comboBox,利用索引选择运算方式,用Count和Right分别计算答题总数和对的数目,结束运算弹出Form1对话框,显示做题结果。

    arictor.cs

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace Counter
     7 {
     8     class arictor
     9     {
    10          public interface Calculator        //声明计算接口
    11         {
    12             double Cal(double x, double y);
    13         }
    14         private double x;                //定义x变量;
    15 
    16         public double X                  //封装字段
    17         {
    18             get { return x; }
    19             set { x = value; }
    20 
    21         }
    22         private double y;               //定义y变量
    23 
    24 
    25         public double Y                  //封装字段
    26         {
    27             get { return y; }
    28             set { y = value; }
    29 
    30         }
    31         public class Add : Calculator            //接口法运算
    32         {
    33             public double Cal(double x, double y)
    34             {
    35                 double result = 0;
    36                 result = x + y;
    37                 return result;
    38             }
    39         }
    40         public class Sub : Calculator 
    41         {
    42             public double Cal(double x, double y)
    43             {
    44                 double result = 0;
    45                 result = x - y;
    46                 return result;
    47             }
    48         }
    49         public class Mul : Calculator 
    50         {
    51             public double Cal(double x, double y)
    52             {
    53                 double result = 0;
    54                 result = x * y;
    55                 return result;
    56             }
    57         }
    58         public class Div : Calculator  
    59         {
    60             public double Cal(double x, double y)
    61             {
    62                 double result = 0;
    63                 result = x / y;
    64                 return result;
    65             }
    66         }
    67         public class Opear                  //定义运算符  
    68         {
    69             private Calculator calculate;
    70             public Opear(Calculator calculate)
    71             {
    72                 this.calculate = calculate;
    73             }
    74             public double Cal(double x, double y, String op)  //返回运算结果
    75             {
    76                 return this.calculate.Cal(x, y);
    77             }
    78         }
    79     }
    80 }

    MianWindow代码实现

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Windows;
     6 using System.Windows.Controls;
     7 using System.Windows.Data;
     8 using System.Windows.Documents;
     9 using System.Windows.Input;
    10 using System.Windows.Media;
    11 using System.Windows.Media.Imaging;
    12 using System.Windows.Navigation;
    13 using System.Windows.Shapes;
    14 
    15 namespace Counter
    16 {
    17     /// <summary>
    18     /// MainWindow.xaml 的交互逻辑
    19     /// </summary>
    20     public partial class MainWindow : Window
    21     {
    22         public MainWindow()
    23         {
    24             InitializeComponent();
    25         }
    26         public static int Count = 0;               //计算所做的题数
    27         public static int Right = 0;               //回答正确的题数
    28         private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    29         {
    30 
    31         }
    32 
    33         private void button1_Click(object sender, RoutedEventArgs e)
    34         {
    35               string op =comboBox1.SelectedItem.ToString();
    36           double x = Convert.ToDouble(textBox1.Text); 
    37             double y = Convert.ToDouble(textBox2.Text); //把方法中定义的x,y赋值给textBox1和textBox2
    38             Counter.arictor.Opear opear =null;
    39           if (comboBox1.SelectedIndex == 0)    //取comboBox1的索引
    40           {
    41               opear = new Counter.arictor.Opear(new Counter.arictor.Add());    
    42           }
    43           else if (comboBox1.SelectedIndex == 1)
    44           {
    45             opear  = new Counter.arictor.Opear(new Counter.arictor.Sub());    
    46           }
    47           else if (comboBox1.SelectedIndex == 2)
    48           {
    49               opear = new Counter.arictor.Opear(new Counter.arictor.Mul());
    50           }
    51           else if (comboBox1.SelectedIndex == 3)
    52           {
    53              opear = new Counter.arictor.Opear(new Counter.arictor.Div());
    54           }
    55           string answer = opear.Cal(x, y, op).ToString(); 
    56          // string result = textBox1.Text + comboBox1.SelectedItem.ToString() + textBox2.Text;
    57           if (textBox3.Text == answer)                                 
    58           {
    59               MessageBox.Show("回答正确!");
    60               Count++;
    61               Right++;
    62           }
    63   
    64           else                                                       
    65           {   
    66               MessageBox.Show("回答错误!");  
    67               Count++;
    68           }
    69           textBox1.Text = "";             
    70           textBox2.Text = "";
    71           textBox3.Text = "";  
    72       }
    73 
    74         private void button2_Click(object sender, RoutedEventArgs e)
    75         {
    76             结果 form = new 结果();
    77             form.ShowDialog();
    78         }
    79      }
    80         }
    81     

    Form1.cs代码

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Windows.Forms;
     9 
    10 namespace Counter
    11 {
    12     public partial class 结果 : Form
    13     {
    14         public 结果()
    15         {
    16             InitializeComponent();
    17         }
    18 
    19         private void 结果_Load(object sender, EventArgs e)
    20         {
    21             textBox1.Text = MainWindow.Count.ToString();
    22             textBox2.Text = MainWindow.Right.ToString();
    23             textBox3.Text = ((MainWindow.Right / (double)(MainWindow.Count)) * 100).ToString() + "%";
    24         }
    25     }
    26 }
     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Windows.Forms;
     9 
    10 namespace Counter
    11 {
    12     public partial class 结果 : Form
    13     {
    14         public 结果()
    15         {
    16             InitializeComponent();
    17         }
    18 
    19         private void 结果_Load(object sender, EventArgs e)
    20         {
    21             textBox1.Text = MainWindow.Count.ToString();
    22             textBox2.Text = MainWindow.Right.ToString();
    23             textBox3.Text = ((MainWindow.Right / (double)(MainWindow.Count)) * 100).ToString() + "%";
    24         }
    25     }
    26 }

    运行结果:

    开始做题:

    回答正确:

    回答错误:

    结束运算:

    总结:

          这次是用WPF实现的运算,感觉语法和Window窗体没有太大区别,在形式上和.NET更为相似。因为之前没有用过WPF,在做的过程中遇到好多困难,还好最后都解决了。从这次作业中可以感受到什么叫做学无止境。

  • 相关阅读:
    Java开发中的23种设计模式详解(转)
    主表和从表
    MyBatis开发中解决返回字段不全的问题
    个人常用配置文件解析
    SpringMVC+MyBatis开发中指定callSettersOnNulls,可解决返回字段不全的问题
    mybatis之sql执行有数据但返回结果为null
    Hadoop window win10 基础环境搭建(2.8.1)
    什么水平算精通C++ Builder?
    Delphi中取得汉字的首字母(十分巧妙)
    全部的Windows消息对应值
  • 原文地址:https://www.cnblogs.com/thinking-star/p/5042710.html
Copyright © 2020-2023  润新知