Calculation

定义一个Strategy接口,其中定义一个方法,用于计算

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Calculation
{
    interface Interface1
    {
        int calculate(int a, int b);
    }
}

定义具体的算法类,实现Strategy接口,算法类中的算法各自不同:加减乘等

1,加法类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Calculation
{
    class Add:Interface1
    {
        public int calculate(int a, int b)
        {
            return  a + b;
        }

    }
}

2,减法类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Calculation
{
    class Subtract : Interface1
    {
        public int calculate(int a, int b)
        {
            return  a - b;
        }
    }
}

3,乘法类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Calculation
{
    class Multiply:Interface1
    {
        public int calculate(int a, int b)
        {

             return a * b;
        }

    }
}

4,除法类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Calculation
{
    class Except:Interface1
    {
        public int calculate(int a, int b)
        {
            return  a / b ;

        }
    }
}

定义具体的环境角色

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Calculation
{
    class Environment
    {
        private Interface1 inter;
        public Environment(Interface1 face)
        {
            inter = face;
        }
        public Interface1 gewrt()
        {
            return inter;
        }
        public void setwrt(Interface1 face)
        {
            inter=face;
        }
        public int calculate(int a, int b)
        {
            return inter.calculate(a, b);
        }

    }
}

form1的代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Calculation
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            displays();
        }
        private void displays()
        {
            Random random = new Random();
            int a, b;
            a = random.Next(0, 100);
            b = random.Next(0, 100);
            fasttext.Text = a.ToString();
            lasttext.Text = b.ToString();
            string[] operjact = new string[] { "+", "-", "*", "/" };
            string f = operjact[new Random().Next(0, 4)];
            operja.Text = f;

        }

        private void textBox3_KeyDown(object sender, KeyEventArgs e)
        {
            int a = int.Parse(fasttext.Text);
            int b = int.Parse(lasttext.Text);

            if (e.KeyCode == Keys.Enter)
            {

                int answer = int.Parse(textBox3.Text);

                string OPter=operja.Text;
                switch (OPter)
                {
                    case "+":
                        Add addss = new Add();
                        Environment environment = new Environment(addss);
                        environment.calculate(a, b);
                        if (answer == environment.calculate(a, b))
                        {
                            MessageBox.Show("回答正确!");
                        }
                        else
                        {
                            MessageBox.Show("回答错误!");
                        }
                        break;
                    case "-":
                         Subtract subtrss = new Subtract();
                         Environment environment1 = new Environment(subtrss);
                         environment1.calculate(a,b);
                         if (answer == environment1.calculate(a, b))
                         {
                             MessageBox.Show("回答正确!");
                         }
                         else
                         {
                             MessageBox.Show("回答错误!");
                         }
                       break;
                    case "*":
                       Multiply muli = new Multiply();
                       Environment environment2 = new Environment(muli);
                       environment2.calculate(a, b);
                       if (answer == environment2.calculate(a, b))
                       {
                           MessageBox.Show("回答正确!");
                       }
                       else
                       {
                           MessageBox.Show("回答错误!");
                       }
                       break;
                    case "/":
                       Except except = new Except();
                       Environment enviroment3 = new Environment(except);
                       enviroment3.calculate(a, b);
                        if(answer==enviroment3.calculate(a,b))
                        {
                            MessageBox.Show("回答正确!");
                        }
                        else
                        {
                            MessageBox.Show("回答错误!");
                        }
                       break;

                }
                textBox3.Clear();
                displays();

            }

        }
    }
}

总结:

两个数是让它随机产生的,运算符是随机产生的,无法保证除以零的情况。总的来说这些代码套来套去的有点发晕。不知道自己写的怎么样。

其中有一部分代码不太明白感觉没什么用?

   public Interface1 gewrt()
        {
            return inter;
        }
        public void setwrt(Interface1 face)
        {
            inter=face;
        }

就把他们删了试着运行了一下,还是可以正常运行。

时间: 2024-08-10 15:10:23

Calculation的相关文章

HDU 4965 Fast Matrix Calculation 【矩阵】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4965 题目大意:给你一个N*K的矩阵A以及一个K*N的矩阵B (4 <= N <= 1000)以及 (2 <=K <= 6),然后接下来四步: 算一个新的矩阵C=A*B 算M=C^ (N*N) 对于M中的每个元素%6 将M中每个元素加起来,算出和. 也就是求出A*B * A*B * A*B * A*B * A*B *--* A*B   但是A*B形成的矩阵是N*N,而N大小有可能是10

Calculation of COGM and COGS

Use You can use the Product Cost Planning functions to calculate the cost of goods manufactured (COGM) and cost of goods sold (COGS) for products such as materials and services. The costs may then be analyzed and business decisions (such as "make or

Singular value encountered in calculation for ROI

在ENVI中对一幅TM影像进行监督分类,在进行compute ROI separability时提示Singular value encountered in calculation for ROI,且对应的类别相关性均为0 是ROI的问题,具体问题不清楚,估计是选择的ROI过大或过小,导致计算时出错.删除重新采集一般都不会再出现此问题.

HDOJ 4965 Fast Matrix Calculation

(AB)^n=A*(BA)^(n-1)^B Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 576    Accepted Submission(s): 297 Problem Description One day, Alice and Bob felt bored again, B

hdu 3501 Calculation 2 (欧拉函数)

题目 题意:求小于n并且 和n不互质的数的总和. 思路:求小于n并且与n互质的数的和为:n*phi[n]/2 . 若a和n互质,n-a必定也和n互质(a<n).也就是说num必定为偶数.其中互质的数成对存在.其和为n. 公式证明: 反证法:如果存在K!=1使gcd(n,n-i)=k,那么(n-i)%k==0而n%k=0那么必须保证i%k=0k是n的因子,如果i%k=0那么gcd(n,i)=k,矛盾出现; 所以先求出1--n-1 的和, 再用这个和 减去 上面公式求出来的值. 欧拉函数phi(m)

hdoj 3501 -Calculation 2 (欧拉函数)

Calculation 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3305    Accepted Submission(s): 1367 Problem Description Given a positive integer N, your task is to calculate the sum of the positi

ACM学习历程—HDU 4726 Kia&#39;s Calculation( 贪心&amp;&amp;计数排序)

DescriptionDoctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so careless and alway forget to carry a number when the sum of two digits exceeds 9. For example, when she calculates 4567+5789, she will get 9246, and for 12

贪心 HDOJ 4726 Kia&#39;s Calculation

题目传送门 1 /* 2 这题交给队友做,做了一个多小时,全排列,RE数组越界,赛后发现读题读错了,囧! 3 贪心:先确定最高位的数字,然后用贪心的方法,越高位数字越大 4 5 注意:1. Both A and B will have same number of digits 两个数字位数相同 6 2. which is no larger than 10 6 不是大小,而是长度不超过1e6 7 */ 8 #include <cstdio> 9 #include <iostream&g

OpenCASCADE Curve Length Calculation

OpenCASCADE Curve Length Calculation [email protected] Abstract. The natural parametric equations of a curve are parametric equations that represent the curve in terms of a coordinate-independent parameter, generally arc length s, instead of an arbit

HDU多校赛第9场 HDU 4965Fast Matrix Calculation【矩阵运算+数学小知识】

难度上,,,确实,,,不算难 问题是有个矩阵运算的优化 题目是说给个N*K的矩阵A给个K*N的矩阵B(1<=N<=1000 && 1=<K<=6),先把他们乘起来乘为C矩阵,然后算C^(N*N) 相当于 ABABABABABABAB...=(AB)^(N*N) 不如 A(BA)^(N*N-1)B 因为BA乘得K*K的矩阵,K是比较小的 #include <cstdio> #include <cstdlib> #include <cstr