当前位置:编程学习 > C#/ASP.NET >>

【学习】控制台程序模拟计算器功能,希望通过代码大家能指导我进步。

代码:
StringFactory类

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

namespace MiniFoxOperation
{
    class StringFactory
    {

        #region
        //主要字符串
        private string str;

        //由字符串得出的数组
        private double[] dous;

        //由字符串得出的运算符数组
        private Operator[] opers;

        public string Str
        {
            get
            {
                return str;
            }
            set
            {
                str = value;
            }
        }

        public double[] Dous
        {
            get
            {
                return dous;
            }
            set
            {
                dous = value;
            }
        }

        public Operator[] Opers
        {
            get
            {
                return opers;
            }
            set
            {
                opers = value;
            }
        }
        #endregion

        /// <summary>
        /// 根据用户输入的字符串得到其中含有的数字数组
        /// </summary>
        /// <returns></returns>
        private double[] GetNumbers(string ConsoleStr)
        {
            str = ConsoleStr;
            char[] cs = new char[5] { '-', '+', '*', '/', '=' };
            str = str.Trim();
            string[] StrNumbers = str.Split(cs);
            double[] ds = new double[StrNumbers.Length];
            for (int i = 0; i < ds.Length - 1; i++)
            {
                ds[i] = Convert.ToDouble(StrNumbers[i]);
            }
            dous = ds;
            return ds;
        }

        /// <summary>
        /// 根据用户输入的字符串得到其中含有的运算符数组
        /// </summary>
        /// <param name="ConsoleStr"></param>
        /// <returns></returns>
        private Operator[] GetOperators(string ConsoleStr)
        {
            List<Operator> ListOperator = new List<Operator>();
            str = ConsoleStr;
            char[] cs = str.ToCharArray();
            for (int i = 0; i < cs.Length - 1; i++)
            {
                if (cs[i] == '+' || cs[i] == '=' || cs[i] == '/' || cs[i] == '*' || cs[i] == '-')
                {
                    OperationFactory MyOF = new OperationFactory();
                    Operator o = MyOF.GetOperator(cs[i].ToString());
                    ListOperator.Add(o);
                }
            }
            Operator[] os = ListOperator.ToArray();
            Opers = os;
            return os;
        }

        /// <summary>
        /// 根据数组和运算符数组进行运算
        /// </summary>
        /// <param name="ConsoleStr"></param>
        /// <returns></returns>
        public double StringOperation(string ConsoleStr)
        {
            double Resualt = 0;
            dous = GetNumbers(ConsoleStr);
            opers = GetOperators(ConsoleStr);
            OperationFactory OF = new OperationFactory();
            for (int i = 0; i < dous.Length - 1; i++)
            {
                if (i != 0)
                {
                    double r = OF.Operation(dous[i], opers[i - 1]);
                    Console.WriteLine("The " + i.ToString() + "th Operation's Resualt is : " + r.ToString());
                }
                else
                {
                    OF.Operation(dous[i], Operator.plus);
                }
            }
            Resualt = OF.BaseNumber;
            return Resualt;
        }
    }
}

--------------------编程问答-------------------- Operator枚举

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

namespace MiniFoxOperation
{
    /// <summary>
    /// 运算符枚举
    /// </summary>
    public enum Operator
    {
        plus = 1, Reduction = 2, Multiplication = 3, Division = 4, Equal = 5, none = 6
    }
}

--------------------编程问答-------------------- OperationFactory类

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

namespace MiniFoxOperation
{
    class OperationFactory
    {

        #region 字段及属性

        //运算前的基数 
        private double basenumber = 0;

        //新到来的数
        private double newnumber = 0;

        //运算符
        private Operator myoperation;

        /// <summary>
        /// 运算前的基数 
        /// </summary>
        public double BaseNumber 
        {
            get
            {
                return basenumber;
            }
            set
            {
                basenumber = value;
            }
        }

        /// <summary>
        /// 新到来的数
        /// </summary>
        public double NewNumber
        {
            get
            {
                return newnumber;
            }
            set
            {
                newnumber = value;
            }
        }
        
        /// <summary>
        /// 运算符
        /// </summary>
        public Operator MyOperation
        {
            get
            {
                return myoperation;
            }
            set
            {
                myoperation = value;
            }
        }

        #endregion

        #region 计算方法
        /// <summary>
        /// 运算方法,运算结束后并且给BaseNumber赋新值
        /// </summary>
        /// <param name="y"></param>
        /// <param name="o"></param>
        /// <returns></returns>
        public double Operation(double y, Operator o)
        { 
            double Resualt;
            if (o != Operator.Equal)
            {
                newnumber = y;
                myoperation = o;     
                Resualt = InsideOperation(basenumber, y, o);
                basenumber = Resualt;
            }
            else
            {
                Resualt = basenumber;
                basenumber = 0;
                return Resualt;           
            }
            return Resualt;
        }

        /// <summary>
        /// 内部运算方法,将参数1和参数2进行o运算
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="o"></param>
        /// <returns></returns>
        private double InsideOperation(double x, double y, Operator o)
        {
            double Resualt;
            switch (o)
            {
                case Operator.plus:
                    Resualt = x + y;
                    break;
                case Operator.Reduction:
                    Resualt = x - y;
                    break;
                case Operator.Multiplication:
                    Resualt = x * y;
                    break;
                case Operator.Division:
                    Resualt = x / y;
                    break;
                case Operator.Equal:
                    Resualt = x;
                    break;
                default:
                    Resualt = x;
                    break;
            }
            return Resualt;
        }

        /// <summary>
        /// 根据用户输入的运算符枚举判断出运算符的字符串
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public string GetStrOperator(Operator o)
        {
            string str = "";
            switch (o)
            {
                case Operator.plus:
                    str = "+";
                    break;
                case Operator.Reduction:
                    str = "-";
                    break;
                case Operator.Multiplication:
                    str = "*";
                    break;
                case Operator.Division:
                    str = "/";
                    break;
                case Operator.Equal:
                    str = "=";
                    break;
                default:
                    str = "";
                    break;
            }
            return str;
        }

        /// <summary>
        /// 根据用户输入的运算符判断出运算类型属于何种枚举
        /// </summary>
        /// <param name="StrOperator"></param>
        /// <returns></returns>
        public Operator GetOperator(string StrOperator)
        {
            Operator o = Operator.none;
            switch (StrOperator)
            {
                case "+":
                    o = Operator.plus;
                    break;
                case "-":
                    o = Operator.Reduction;
                    break;
                case "*":
                    o = Operator.Multiplication;
                    break;
                case "/":
                    o = Operator.Division;
                    break;
                case "=":
                    o = Operator.Equal;
                    break;
                default:
                    o = Operator.none;
                    break;
            }
            return o;
        }
        #endregion
    }
}

--------------------编程问答-------------------- 主函数

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

namespace MiniFoxOperation
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                try
                {
                    Console.WriteLine("Please input the correct operation test and end with '=' :");
                    Console.WriteLine("---------------------------------------------------------");
                    string str = Console.ReadLine();
                    Console.WriteLine("---------------------------------------------------------");
                    StringFactory SF = new StringFactory();
                    double x = SF.StringOperation(str);
                    Console.WriteLine("The final result is " + x.ToString());
                    Console.WriteLine("---------------------------------------------------------");
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
    }
}

补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,