探索C#对象模型(一个C#风格的C++程序)

发表于:2012-9-20 09:22

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:Justme0    来源:51Testing软件测试网采编

  写C#程序就是在设计一个类!

  先看一个C#程序(表达式求值):

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

namespace ConsoleApplicationCal
{
    class Program
    {
        private static char[,] Precede_Matrix = new char[7, 7]
        {
         {'>', '>', '<', '<', '<', '>', '>',},
         {'>', '>', '<', '<', '<', '>', '>',},
         {'>', '>', '>', '>', '<', '>', '>',},
         {'>', '>', '>', '>', '<', '>', '>',},
         {'<', '<', '<', '<', '<', '=', '0',},
         {'>', '>', '>', '>', '0', '>', '>',},
         {'<', '<', '<', '<', '<', '0', '=',}
        };

        public static char Precede(char a, char b)
        {
            int i = 0;
            int j = 0;
            switch (a)
            {
                case '+': i = 0; break;
                case '-': i = 1; break;
                case '*': i = 2; break;
                case '/': i = 3; break;
                case '(': i = 4; break;
                case ')': i = 5; break;
                case '#': i = 6; break;
                default: break;
            }
            switch (b)
            {
                case '+': j = 0; break;
                case '-': j = 1; break;
                case '*': j = 2; break;
                case '/': j = 3; break;
                case '(': j = 4; break;
                case ')': j = 5; break;
                case '#': j = 6; break;
                default: break;
            }

            return (Precede_Matrix[i, j]);
        }

        public static double Operate(double a, char oper, double b)
        {
            switch (oper)
            {
                case '+': return a + b;
                case '-': return a - b;
                case '*': return a * b;
                case '/': return a / b;
                default: return -1;
            }
        }

        public static bool IsOperand(char c)
        {
            if (('0' <= c && c <= '9') || c == '.') // c是数字或小数点
                return true;
            else
                return false;
        }

        static void Main(string[] args)
        {
            string str;
            while ((str = Console.ReadLine()) != null)
            {
                str += "#";      //  最后是#(结束标志)

                double a;
                double b;
                char x;
                char theta;

                Stack<char> OPTR = new Stack<char>();
                OPTR.Push('#');
                Stack<double> OPND = new Stack<double>();

                int i = 0;
                char c = str[i++];
                double operand = 0;
                while (!(c == '#' && OPTR.Peek() == '#'))
                {
                    if (IsOperand(c)) // c是数字或小数点(这里一定是数字),小数点已在下面转换掉了
                    {
                        int startIndex = i - 1;
                        int length = 1; // c是数字,故初始一定是1
                        while (IsOperand(str[i]))
                        {
                            i++;
                            length++;
                        }

                        string doubleString = str.Substring(startIndex, length);
                        //     operand = atof(&str[i - 1]); // 把从c开头的数转化成double
                        OPND.Push(double.Parse(doubleString));


                        c = str[i++];
                    }
                    else                            // c is operator or delimiter
                    {
                        switch (Precede(OPTR.Peek(), c))
                        {

                            case '<':
                                OPTR.Push(c);
                                c = str[i++];
                                break;

                            case '=':
                                x = OPTR.Pop();

                                c = str[i++];
                                break;

                            case '>':
                                theta = OPTR.Pop();
                                b = OPND.Pop();
                                a = OPND.Pop();
                                OPND.Push(Operate(a, theta, b));
                                break;

                            default:
                                break;
                        }
                    }
                }
                //  OPTR栈的栈顶元素和当前读入的字符均为“#”
                //  即“#”=“#”时整个表达式求值完毕
                Console.WriteLine(OPND.Peek());
            }    
        }
    }
}

21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号