VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > c#编程 >
  • C#四则运算的实现

看大话数据结构 利用栈实现四则运算这一块儿正好是只有讲解没有代码实现的 于是照着书上的原理自己写了个四则运算的C#代码 以后可以考虑到作为自己的类库中的组件

主要涉及的是逆波兰式 程序主要部分为逆波兰式的产生 以及通过逆波兰式产生最终的四则运算结果

使用的时候只需输入四则运算的算是即可 如2*(1+2)+3*(4+5) 没有中括号大括号 程序会分三个步骤来 首先分割算式 将运算符与数字之间插入空格 方便之后的出入栈操作 如

2 * ( 1 + 2 ) +  3  * ( 4 + 5 )

之后将原式子转变为逆波兰式 最后通过逆波兰式生成最终的算术结果


  1.  
    using System;
  2.  
    using System.Collections.Generic;
  3.  
    using System.Text;
  4.  
    using System.Text.RegularExpressions;
  5.  
     
  6.  
    namespace ConsoleApplication1
  7.  
    {
  8.  
    class Class1
  9.  
    {
  10.  
    private static Dictionary<string, int> _operatorLevel;
  11.  
     
  12.  
    public static void Main(string[] arg)
  13.  
    {
  14.  
    Console.WriteLine("Type in the source expr");
  15.  
    string sourceExpression = Console.ReadLine();
  16.  
    Console.WriteLine(InsertBlank(sourceExpression));
  17.  
    string rpnExperssion = ConvertToRPN(InsertBlank(sourceExpression));
  18.  
    Console.WriteLine(rpnExperssion);
  19.  
    Console.WriteLine(GetResult(rpnExperssion));
  20.  
    Console.ReadLine();
  21.  
    }
  22.  
     
  23.  
    public static double GetValue(double left, double right, char _operator)
  24.  
    {
  25.  
    switch (_operator)
  26.  
    {
  27.  
    case '+':
  28.  
    return left+right;
  29.  
    case '-':
  30.  
    return left-right;
  31.  
    case '*':
  32.  
    return left*right;
  33.  
    case '/':
  34.  
    return left/right;
  35.  
    }
  36.  
    return 0;
  37.  
    }
  38.  
     
  39.  
    public static double GetResult(string source)
  40.  
    {
  41.  
    Stack<string> stack = new Stack<string>();
  42.  
    var list = source.Split(' ');
  43.  
    for (int i = 0; i < list.Length; i++)
  44.  
    {
  45.  
    string current = list[i];
  46.  
    if (Regex.IsMatch(current, "^([0-9]{1,}){1}quot;))
  47.  
    {
  48.  
    stack.Push(current);
  49.  
    }
  50.  
    else if (OperatorLevel.ContainsKey(current))
  51.  
    {
  52.  
    double right = double.Parse(stack.Pop());
  53.  
    double left = double.Parse(stack.Pop());
  54.  
    stack.Push(GetValue(left, right, current[0]).ToString());
  55.  
    }
  56.  
    }
  57.  
    return double.Parse(stack.Pop());
  58.  
    }
  59.  
     
  60.  
    public static string ConvertToRPN(string source)
  61.  
    {
  62.  
    StringBuilder result = new StringBuilder();
  63.  
    Stack<string> stack = new Stack<string>();
  64.  
    string[] list = source.Split(' ');
  65.  
    for (int i = 0; i < list.Length ; i++)
  66.  
    {
  67.  
    string current = list[i];
  68.  
    if (Regex.IsMatch(current, "^([0-9]{1,}){1}quot;))
  69.  
    {
  70.  
    result.Append(current + " ");
  71.  
    }
  72.  
    else if (OperatorLevel.ContainsKey(current))
  73.  
    {
  74.  
    if (stack.Count > 0)
  75.  
    {
  76.  
    var prev = stack.Peek();
  77.  
    if (prev == "(")
  78.  
    {
  79.  
    stack.Push(current);
  80.  
    continue;
  81.  
    }
  82.  
    if (current == "(")
  83.  
    {
  84.  
    stack.Push(current);
  85.  
    continue;
  86.  
    }
  87.  
    if (current == ")")
  88.  
    {
  89.  
    while (stack.Count > 0 && stack.Peek() != "(")
  90.  
    {
  91.  
    result.Append(stack.Pop() + " ");
  92.  
    }
  93.  
    //Pop the "("
  94.  
    stack.Pop();
  95.  
    continue;
  96.  
    }
  97.  
    if (OperatorLevel[current] < OperatorLevel[prev])
  98.  
    {
  99.  
    while (stack.Count > 0)
  100.  
    {
  101.  
    var top = stack.Pop();
  102.  
    if (top != "(" &&
  103.  
    top != ")")
  104.  
    {
  105.  
    result.Append(top + " ");
  106.  
    }
  107.  
    else
  108.  
    {
  109.  
    break;
  110.  
    }
  111.  
    }
  112.  
    stack.Push(current);
  113.  
    }
  114.  
    else
  115.  
    {
  116.  
    stack.Push(current);
  117.  
    }
  118.  
    }
  119.  
    else
  120.  
    {
  121.  
    stack.Push(current);
  122.  
    }
  123.  
    }
  124.  
    }
  125.  
    if (stack.Count > 0)
  126.  
    {
  127.  
    while (stack.Count > 0)
  128.  
    {
  129.  
    var top = stack.Pop();
  130.  
    if (top != "(" && top != ")")
  131.  
    {
  132.  
    result.Append(top + " ");
  133.  
    }
  134.  
    }
  135.  
    }
  136.  
    return result.ToString();
  137.  
    }
  138.  
     
  139.  
    public static string InsertBlank(string source)
  140.  
    {
  141.  
    StringBuilder sb = new StringBuilder();
  142.  
    var list = source.ToCharArray();
  143.  
    foreach (var temp in list)
  144.  
    {
  145.  
    if (OperatorLevel.ContainsKey(temp.ToString()))
  146.  
    {
  147.  
    sb.Append(" ");
  148.  
    sb.Append(temp.ToString());
  149.  
    sb.Append(" ");
  150.  
    }
  151.  
    else
  152.  
    {
  153.  
    sb.Append(temp);
  154.  
    }
  155.  
    }
  156.  
    return sb.ToString();
  157.  
    }
  158.  
     
  159.  
    //运算符字典 方便查询运算符优先级

  1.  
    public static Dictionary<string, int> OperatorLevel
  2.  
    {
  3.  
    get
  4.  
    {
  5.  
    if(_operatorLevel==null)
  6.  
    {
  7.  
    _operatorLevel = new Dictionary<string, int>();
  8.  
    _operatorLevel.Add("+", 0);
  9.  
    _operatorLevel.Add("-", 0);
  10.  
    _operatorLevel.Add("(", 1);
  11.  
    _operatorLevel.Add("*", 1);
  12.  
    _operatorLevel.Add("/", 1);
  13.  
    _operatorLevel.Add(")", 0);
  14.  
    }
  15.  
    return _operatorLevel;
  16.  
    }
  17.  
    }
  18.  
    }
  19.  

相关教程