• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#表达式计算器----数据结构

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

using System;

using System.Collections.Generic;

using System.Linq; using System.Text;

using System.Collections;

namespace ConsoleApplication7

{   

  class Calculator

    {       

  static void Main(string[] args)

        {            

//string exp = "345*2^(4+2*2-1*3)-5";     

        for (; ; )     

        {         

        Console.WriteLine("input a values:");        

         string exp = Console.ReadLine();       

          Console.WriteLine("output reslut:");    

             Console.WriteLine(Calculate(exp));       

          Console.WriteLine("END");             }     

       // string exp = "130+20";              

          //Console.ReadKey();         }      

   public static double  Calculate(string _exp)    

     {             Stack operandStack = new Stack();//操作数   

          Stack operatorStack = new Stack();//运算符        

     operatorStack.Push('#');       

      char[] exp = (_exp + "#").ToCharArray();

StringBuilder sb = new StringBuilder();     

        for (int i = 0; i < exp.Length; i++)    

         {        

         if (IsPerand(exp[i]))          

       {              

       sb.Append(exp[i]);          

       }          

       else        

         {             

        string sbstr = sb.ToString();         

            if (sbstr!="")

               {                

         operandStack.Push(sb);        

             }               

      sb = new StringBuilder();             

        //优先级大于当前栈顶运算符的优先级  

        if (PriorityBeforeIn(exp[i]) > priorityAferIn((char)operatorStack.Peek()))

               {               

          operatorStack.Push(exp[i]);//#*      

               }              

       else if (PriorityBeforeIn(exp[i]) < priorityAferIn((char)operatorStack.Peek()))         

            {                                   

              object sbd =operandStack.Pop();             

            string str = sbd.ToString();                  

       object sbd1 =operandStack.Pop();             

            string str1 = sbd1.ToString();              

           double dou= Convert.ToDouble(str);          

               double dou1= Convert.ToDouble(str1);

                 operandStack.Push(Count((char)operatorStack.Pop(),dou,dou1));  

                       i--;           

          }                

     else if (PriorityBeforeIn(exp[i]) == priorityAferIn((char)operatorStack.Peek()))       

              {                   

      if (exp[i] == ')')              

           {                        

     while (true)                  

           {                          

       char c = (char)operatorStack.Peek();          

                       if (c == '(')                    

             {                            

         operatorStack.Pop();                

                     break;                   

              }                            

                           object sbd = operandStack.Pop();                       

          string str = sbd.ToString();          

                       object sbd1 = operandStack.Pop();           

                      string str1 = sbd1.ToString();               

                  double dou = Convert.ToDouble(str);                

                 double dou1 = Convert.ToDouble(str1); 

                    operandStack.Push(Count((char)operatorStack.Pop(), dou, dou1));        

                     }                         }             

            else if (exp[i] == '#')            

             {                       

      while (true)                      

       {                              

   char c = (char)operatorStack.Peek();            

                     if (c == '#')                     

            {                               

      operatorStack.Pop();                     

                break;                        

         }                               

object sbd = operandStack.Pop();                

                 string str = sbd.ToString();              

                   object sbd1 = operandStack.Pop();            

                     string str1 = sbd1.ToString();                      

           double dou = Convert.ToDouble(str);                     

            double dou1 = Convert.ToDouble(str1);//出错                  

               operandStack.Push(Count((char)operatorStack.Pop(), dou, dou1));          

                   }                         }                     }                 }

            }         

    return (double)operandStack.Pop();    

     }     

    //判断字符是否为操作数    

     private static bool IsPerand(char c)     

    {             bool flag = false;//不执行    

         switch (c)        

     {            

     case'.':       

          case '0':      

           case '1':       

          case '2':        

         case '3':          

       case '4':           

      case '5':         

        case '6':       

          case '7':        

         case '8':          

       case '9':         

            flag = true;     

                break;         

    }          

   return flag;      

   }        

//获取操作符入栈前的优先级  

       private static int PriorityBeforeIn(char _opertor)    

     {           

  int priority = -1;        

     switch (_opertor)        

     {             

    case '#':        

             priority = 0;           

          break;           

      case '(':           

          priority = 8;           

          break;          

       case '^':              

       priority = 6;           

          break;         

        case '*':        

         case '/':        

         case '%':        

             priority = 4;       

              break;           

      case '+':           

      case '-':           

          priority = 2;     

                break;         

        case ')':          

           priority = 1;     

                break;          

       default:           

          break;         

    }           

  return priority;    

     }       

  //获取操作符入站后的优先级

        private static int priorityAferIn(char _operator)     

    {          

   int priority = -1;     

        switch (_operator)   

          {              

   case '#':          

           priority = 0;       

              break;          

       case '(':                

     priority = 1;           

          break;             

    case '^':               

      priority = 7;             

        break;            

     case '*':            

     case '/':            

     case '%':         

            priority = 5;       

              break;           

      case '+':             

    case '-':           

          priority = 3;        

             break;            

     case ')':                

     priority = 8;          

           break;           

      default:              

       break;         

    }         

    return priority;     

    }      

   //按照指定操作对两个数字进行相应的计算

        private static double  Count(char _operator, double  a, double b)     

    {          

   double result = 0;      

       switch (_operator)       

      {             

    case '^':         

            result = (double)Math.Pow(b, a);    

                 break;         

        case '*':             

        result = b * a;            

         break;           

      case '/':            

         result = b / a;     

                break;         

        case '%':              

       result = b % a;       

              break;          

       case '+':             

        result = b + a;        

             break;           

      case '-':                

     result = b - a;          

           break;            

     default:             

        break;          

   }             return result;   

  }    

  }

}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
asp.netC#打开word文件时命令失败发布时间:2022-07-13
下一篇:
C#中Abstract和Virtual,c#继承。多态,重写,C#中Abstract和Virtual发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap