| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1716 人关注过本帖
标题:求表达式计算器思路~
只看楼主 加入收藏
QQ395108605
Rank: 1
等 级:新手上路
帖 子:194
专家分:0
注 册:2006-11-29
收藏
 问题点数:0 回复次数:9 
求表达式计算器思路~
表达式计算器怎么做?
搜索更多相关主题的帖子: 计算器 思路 表达 
2008-05-25 20:00
野比
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:24
帖 子:1627
专家分:516
注 册:2007-5-24
收藏
得分:0 
词法分析
用树来做

女侠,约吗?
2008-05-26 01:29
QQ395108605
Rank: 1
等 级:新手上路
帖 子:194
专家分:0
注 册:2006-11-29
收藏
得分:0 
文本框1输入A+B
文本框2输入结果;
  能给贴一下代码吗?  我想看看     谢谢!

学海无崖~!
2008-05-26 18:51
rindybo
Rank: 2
等 级:论坛游民
帖 子:126
专家分:51
注 册:2007-3-8
收藏
得分:0 
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace csharpcounter
{
    class Y
    {
        public string Expresstion;//String类型表达式

        public Y()
        {
        
        }
        
        public Y(string expresstion)
        {
            this.Expresstion = expresstion;
        }

        // 判断字符串是否为数值
        public bool IsNumberExp(string str)
        {
            bool isnumeric = false;
            byte c;
            if (str == null || str.Length == 0)
                return false;
            System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();
            byte[] bytestr = ascii.GetBytes(str);
            for (int i = 0; i < bytestr.Length; i++)
            {
                c = bytestr[i];
                if ((c >= 48 && c <= 57) || c == 46)
                {
                    isnumeric = true; ;
                }
                else
                {
                    if (c == 45 && bytestr .Length  > 1)
                    {
                        isnumeric = true;
                    }
                    else
                    {
                        isnumeric = false;
                        break;
                    }
                }
            }
            return isnumeric;
        }

        // 基本一目计算
        public double account(double n1, double n2, string num_op)
        {
            double aresult = 0;
            switch (num_op)
            {
                case "+":
                    aresult = n1 + n2;
                    break;
                case "-":
                    aresult = n1 - n2;
                    break;
                case "*":
                    aresult = n1 * n2;
                    break;
                case "/":
                    aresult = n1 / n2;
                    break;
            }
            return aresult;
        }

        // 将String类型表达式转为由操作数和运算符组成的ArrayList类型表达式
        public ArrayList Toexp_arraylist(string exp_str)
        {
            string exp_element="",expchar;
            ArrayList exp_arraylist = new ArrayList();
            //遍历表达式
            for (int i = 0; i < exp_str.Length; i++)
            {
                expchar = exp_str.Substring(i, 1);
                //如果该字符为数字,小数字或者负号(非运算符的减号)
                if (char.IsNumber(exp_str, i) || expchar == "." || (expchar == "-" && (i == 0 || exp_str.Substring(i - 1, 1) == "(")))
                {
                    exp_element += expchar;//存为操作数
                }
                else//为运算符
                {
                    //将操作数添加到ArrayList类型表达式
                    if (exp_element != "")
                        exp_arraylist.Add(exp_element);
                    //将运算符添加到ArrayList类型表达式
                    exp_arraylist.Add(expchar);
                    exp_element = "";
                }
            }
            //如果还有操作数未添加到ArrayList类型表达式,则执行添加操作
            if (exp_element != "")
                exp_arraylist.Add(exp_element );
            return exp_arraylist;
        }

        //返回运算符的优先级
        private int Operatororder(string op)
        {
            switch (op)
            {
                case "*":
                    return 3;
                    break;
                case "/":
                    return 4;
                    break;
                case "+":
                    return 1;
                    break;
                case "-":
                    return 2;
                    break;
                default:
                    return 0;
                    break;
            }
        }

        private bool IsPop(string op,Stack operators)
        {
            if (operators.Count == 0)
            {
                return false;
            }
            else
            {
                if (operators.Peek().ToString() == "(" || Operatororder(op) > Operatororder(operators.Peek().ToString()))
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }

        //将ArrayList类型的中缀表达式转为ArrayList类型的后缀表达式
        public ArrayList Toexpback_arraylist(ArrayList exp)
        {
            ArrayList expback_arraylist = new ArrayList();
            Stack operators = new Stack();
            string op;
            //遍历ArrayList类型的中缀表达式
            foreach (string s in exp)
            {
                //若为数字则添加到ArrayList类型的后缀表达式
                if (IsNumberExp(s))
                {
                    expback_arraylist.Add(s);
                }
                else
                {
                    switch (s)
                    {
                        //为运算符
                        case "+":
                        case "-":
                        case "*":
                        case "/":
                            while (IsPop(s,operators ))
                            {
                                expback_arraylist.Add(operators .Pop ().ToString ());
                            }
                            operators.Push(s);
                            break;
                        //为开括号
                        case "(":
                            operators.Push(s);
                            break;
                        //为闭括号
                        case ")":
                            while (operators.Count !=0)
                            {
                                op = operators.Pop().ToString();
                                if (op != "(")
                                {
                                    expback_arraylist.Add(op);
                                }
                                else
                                {
                                    break;
                                }
                            }
                            break;
                    }
                }
            }
            while (operators.Count != 0)
            {
                expback_arraylist.Add(operators .Pop ().ToString ());
            }
            return expback_arraylist;
        }
        
        //计算一个ArrayList类型的后缀表达式的值
        public double ExpValue(ArrayList expback)
        {
            double num1,num2,result=0;
            Stack num = new Stack ();
            foreach (string n in expback)
            {
                if (IsNumberExp(n))
                {
                    num.Push(n);
                }
                else
                {
                    try
                    {
                        num2 = Convert.ToDouble(num.Pop());
                        num1 = Convert.ToDouble(num.Pop());
                        result = account(num1, num2, n);
                        num.Push(result);
                    }
                    catch
                    { }
                }
            }
            return result;
        }

        //返回本类的表达式值
        public double ExpValue()
        {
            ArrayList a1 = new ArrayList();
            ArrayList a2 = new ArrayList();
            a1 = Toexp_arraylist(Expresstion);
            a2 = Toexpback_arraylist(a1);
            return ExpValue(a2);
        }

    }
}

[ffg,#6CCFF7,#FFFFFF] ←★→┠最┨┠愛┨┠伱┨┠了┨←★→ [/ft]
2008-05-27 11:20
QQ395108605
Rank: 1
等 级:新手上路
帖 子:194
专家分:0
注 册:2006-11-29
收藏
得分:0 
谢谢~   我先看看  有什么不懂的地方再请教

学海无崖~!
2008-05-27 13:29
QQ395108605
Rank: 1
等 级:新手上路
帖 子:194
专家分:0
注 册:2006-11-29
收藏
得分:0 
怎么没有 Main() ???

学海无崖~!
2008-05-27 13:38
rindybo
Rank: 2
等 级:论坛游民
帖 子:126
专家分:51
注 册:2007-3-8
收藏
得分:0 
晕哦,这是一个类啊,你实例化下,调用方法就行了

[ffg,#6CCFF7,#FFFFFF] ←★→┠最┨┠愛┨┠伱┨┠了┨←★→ [/ft]
2008-05-27 17:11
QQ395108605
Rank: 1
等 级:新手上路
帖 子:194
专家分:0
注 册:2006-11-29
收藏
得分:0 
啊~   看的有点头大了~   有好多都还弄不懂的呢~  
            问一下!  调用哪个方法?

学海无崖~!
2008-05-27 21:12
rindybo
Rank: 2
等 级:论坛游民
帖 子:126
专家分:51
注 册:2007-3-8
收藏
得分:0 
调用方法一:
double rs=0;
Y y = new Y("你要计算的表达式");
rs=y.ExpValue()

调用方法二:
double rs=0;
Y y = new Y();
y.Expresstion="你要计算的表达式";
rs=y.ExpValue()



ok?

[ffg,#6CCFF7,#FFFFFF] ←★→┠最┨┠愛┨┠伱┨┠了┨←★→ [/ft]
2008-05-28 09:02
QQ395108605
Rank: 1
等 级:新手上路
帖 子:194
专家分:0
注 册:2006-11-29
收藏
得分:0 
OK~   
     行了 ~  谢谢啊!                        
               哪有看不懂的再问你!  呵呵

学海无崖~!
2008-05-28 12:37
快速回复:求表达式计算器思路~
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021674 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved