2

我已经实现了我在这个链接上找到的算法来评估 j2me 的算术表达式。但只有当我有单个数字操作数时它才能正常工作,即 2、5、7、3 但不适用于 456、56、34.45 等。我做了一些更改,因为我知道我必须添加找到的数字不是一个一个,但直到我们以数字结尾。然后我也找到了这个链接,但事情仍然没有像预期的那样顺利。首先,我尝试了exp4j库,但我想这在 j2me 上不起作用。代码现在变得非常混乱。我可以就如何解决这个问题获得任何意见吗?对此的任何帮助将不胜感激。

最初仅适用于单个数字操作数的代码:

import java.util.Stack;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

public class SampleInfixToPostfixToResult {

static Stack stack=new Stack();
static StringBuffer p=new StringBuffer();
static Vector v=new Vector();

public static StringBuffer toPostfix(String q)
{
for(int i=0;i<q.length();i++)
{
    if(q.charAt(i)=='0'||q.charAt(i)=='1'||q.charAt(i)=='2'||q.charAt(i)=='3'||q.charAt(i)=='4'||q.charAt(i)=='5'||q.charAt(i)=='6'||q.charAt(i)=='7'||q.charAt(i)=='8'||q.charAt(i)=='9')
    {
        p.append(q.charAt(i));
    }
    else if(q.charAt(i)=='(')
    {
         stack.push(q.charAt(i));
    }
    else if(q.charAt(i)==')')
    {
        for(int j=0;!stack.empty()&&!stack.peek().equals('(');j++)
        {
            p.append(stack.pop());
        }
        stack.pop();
    }
    else if(q.charAt(i)=='+'||q.charAt(i)=='-'||q.charAt(i)=='/'||q.charAt(i)=='*')
    {
        if(stack.empty()||stack.peek().equals('('))
            stack.push(q.charAt(i));
        else
        {
            while(!stack.empty()&&!stack.peek().equals('(')&&isHigh(q.charAt(i), stack.peek()))
            {
                p.append(stack.pop());
            }
            stack.push(q.charAt(i));
        }
    }
}
while(!stack.empty())
{
    p.append(stack.pop());
}
System.out.println(p);
return p;
}


public static boolean isHigh(char op, Object AtStack)
{
char opAtStack=AtStack.toString().charAt(0);
boolean isHigh=false;
if((op==opAtStack))
    isHigh=true;
if((op=='+'||op=='-')&&(opAtStack=='/'||opAtStack=='*'))
{
    isHigh=true;
}
if((op=='+'&&opAtStack=='-')||(op=='-'&&opAtStack=='+')||(op=='/'&&opAtStack=='*')||(op=='*'&&opAtStack=='/'))
{
    isHigh=true;
}
    return isHigh;
}

public static double result(StringBuffer p)
{

for(int i=0;i<p.length();i++)
{
    System.out.println(p.charAt(i));
    if(p.charAt(i)=='0'||p.charAt(i)=='1'||p.charAt(i)=='2'||p.charAt(i)=='3'||p.charAt(i)=='4'||p.charAt(i)=='5'||p.charAt(i)=='6'||p.charAt(i)=='7'||p.charAt(i)=='8'||p.charAt(i)=='9')
    {
        stack.push(p.charAt(i));
    }
    if(p.charAt(i)=='+'||p.charAt(i)=='-'||p.charAt(i)=='/'||p.charAt(i)=='*')
    {
        double a=Double.parseDouble(stack.pop().toString());
        double b=Double.parseDouble(stack.pop().toString());
        if(p.charAt(i)=='+')
        {
            stack.push(b+a);
        }
        else if(p.charAt(i)=='-')
        {
            stack.push(b-a);
        }
        else if(p.charAt(i)=='/')
        {
            stack.push(b/a);
        }
        else if(p.charAt(i)=='*')
        {
            stack.push(b*a);
        }
    }
}
System.out.println(stack);
return 0;
}

public static void main(String[] args) {
    // TODO code application logic here
    String s=new String("65*(76+2)-0/4+(2*5-6*(2*5))");


    SampleInfixToPostfixToResult.result(SampleInfixToPostfixToResult.toPostfix(s));
    System.out.println(66*(76+2)-0/4+(2*5-6*(2*5)));
}
}

更新了我用于评估多位操作数的代码(真的搞砸了)

import java.util.Stack;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

public class SampleInfixToPostfixToResult {

static Stack stack=new Stack();
static StringBuffer p=new StringBuffer();
static Vector v=new Vector();

public static Vector toPostfix(String q) 
{
for(int i=0;i<q.length();i++)
{
    if(q.charAt(i)=='0'||q.charAt(i)=='1'||q.charAt(i)=='2'||q.charAt(i)=='3'||q.charAt(i)=='4'||q.charAt(i)=='5'||q.charAt(i)=='6'||q.charAt(i)=='7'||q.charAt(i)=='8'||q.charAt(i)=='9')
    {
        StringBuffer tn=new StringBuffer();
        while(i<q.length()&&(q.charAt(i)=='0'||q.charAt(i)=='1'||q.charAt(i)=='2'||q.charAt(i)=='3'||q.charAt(i)=='4'||q.charAt(i)=='5'||q.charAt(i)=='6'||q.charAt(i)=='7'||q.charAt(i)=='8'||q.charAt(i)=='9'))
        {
             tn.append(q.charAt(i++));
        }

        v.add(tn);

    }
    else if(q.charAt(i)=='(')
    {
         stack.push(q.charAt(i));
    }
    else if(q.charAt(i)==')')
    {
        for(int j=0;!stack.empty()&&!stack.peek().equals('(');j++)
        {
            v.add(stack.pop());
        }
        stack.pop();
    }
    else if(q.charAt(i)=='+'||q.charAt(i)=='-'||q.charAt(i)=='/'||q.charAt(i)=='*')
    {
        if(stack.empty()||stack.peek().equals('('))
            stack.push(q.charAt(i));
        else
        {
            while(!stack.empty()&&!stack.peek().equals('(')&&isHigh(q.charAt(i), stack.peek()))
            {
                v.add(stack.pop());
            }
            stack.push(q.charAt(i));
        }
    }
}
while(!stack.empty())
{
    v.add(stack.pop());
}

return v;
}

public static boolean isHigh(char op, Object AtStack)
{
char opAtStack=AtStack.toString().charAt(0);
boolean isHigh=false;
if((op==opAtStack))
    isHigh=true;
if((op=='+'||op=='-')&&(opAtStack=='/'||opAtStack=='*'))
{
    isHigh=true;
}
if((op=='+'&&opAtStack=='-')||(op=='-'&&opAtStack=='+')||(op=='/'&&opAtStack=='*')||(op=='*'&&opAtStack=='/'))
{
    isHigh=true;
}
    return isHigh;
}

public static double result(Vector p)
{

for(int i=0;i<p.size();i++)
{
    System.out.println(p.get(i));
    if(p.get(i)=="0"||p.get(i)=="1"||p.get(i)=="2"||p.get(i)=="3"||p.get(i)=="4"||p.get(i)=="5"||p.get(i)=="6"||p.get(i)=="7"||p.get(i)=="8"||p.get(i)=="9")
    {
         StringBuffer tn=new StringBuffer();
        while(i<p.size()&&(p.get(i)=="0"||p.get(i)=="1"||p.get(i)=="2"||p.get(i)=="3"||p.get(i)=="4"||p.get(i)=="5"||p.get(i)=="6"||p.get(i)=="7"||p.get(i)=="8"||p.get(i)=="9"))
        {
             tn.append(p.get(i++));
        }
        System.out.println("This is the full number :"+tn);
        stack.push(tn);
    }
    if(p.get(i)=="+"||p.get(i)=="-"||p.get(i)=="/"||p.get(i)=="*")
    {
        double a=Double.parseDouble(stack.pop().toString());
        double b=Double.parseDouble(stack.pop().toString());
        if(p.get(i)=="+")
        {
            stack.push(b+a);
        }
        else if(p.get(i)=="-")
        {
            stack.push(b-a);
        }
        else if(p.get(i)=="/")
        {
            stack.push(b/a);
        }
        else if(p.get(i)=="*")
        {
            stack.push(b*a);
        }
    }
}
System.out.println("Final value: "+stack);
return 0;
}

public static void main(String[] args) {
    // TODO code application logic here
    String s=new String("65*(79+22)-0/4+(29*5-6*(2*5))");

    SampleInfixToPostfixToResult.result(SampleInfixToPostfixToResult.toPostfix(s));
    //System.out.println(65*(79+22)-0/4+(29*5-6*(2*5)));
}
}
4

1 回答 1

1

解决方案:

1.函数 toPostfix() 现在返回堆栈而不是向量,因为现在后缀表达式被添加到堆栈中,例如 [34,57.+] 等。

2.编辑了通过遍历字符串直到我们有一个数字来存储所有数字到堆栈的代码。(见代码直到第29行)

import java.util.Stack;
import java.util.Vector;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Sandeep
 */
public class Main {
    static Stack stack=new Stack();
    static Stack stackT=new Stack();
    //static Vector v=new Vector();
    //static StringBuffer p=new StringBuffer();

    public static Stack toPostfix(String q)
{

    for(int i=0;i<q.length();i++)
    {   
        if(q.charAt(i)=='0'||q.charAt(i)=='1'||q.charAt(i)=='2'||q.charAt(i)=='3'||q.charAt(i)=='4'||q.charAt(i)=='5'||q.charAt(i)=='6'||q.charAt(i)=='7'||q.charAt(i)=='8'||q.charAt(i)=='9')
        {
            StringBuffer p=new StringBuffer();
            while(q.charAt(i)=='0'||q.charAt(i)=='1'||q.charAt(i)=='2'||q.charAt(i)=='3'||q.charAt(i)=='4'||q.charAt(i)=='5'||q.charAt(i)=='6'||q.charAt(i)=='7'||q.charAt(i)=='8'||q.charAt(i)=='9')
                    p.append(q.charAt(i++));
            stackT.push(p);
            i--;
        }
        else if(q.charAt(i)=='(')
        {
             stack.push(q.charAt(i));
        }
        else if(q.charAt(i)==')')
        {
            for(int j=0;!stack.empty()&&!stack.peek().equals('(');j++)
            {
                stackT.push(stack.pop().toString());
            }
            stack.pop();
        }
        else if(q.charAt(i)=='+'||q.charAt(i)=='-'||q.charAt(i)=='/'||q.charAt(i)=='*')
        {
            if(stack.empty()||stack.peek().equals('('))
                stack.push(q.charAt(i));
            else
            {
                while(!stack.empty()&&!stack.peek().equals('(')&&isHigh(q.charAt(i), stack.peek()))
                {
                    stackT.push(stack.pop().toString());
                }
                stack.push(q.charAt(i));
            }
        }
    }
   while(!stack.empty())
    {
        stackT.push(stack.pop().toString());
    }

       System.out.println(stackT);
    return stackT;
}

//problem lies here
public static boolean isHigh(char op, Object AtStack)
{
    char opAtStack=AtStack.toString().charAt(0);
    boolean isHigh=false;
    if((op==opAtStack))
        isHigh=true;
    if((op=='+'||op=='-')&&(opAtStack=='/'||opAtStack=='*'))
    {
        isHigh=true;
    }
    if((op=='+'&&opAtStack=='-')||(op=='-'&&opAtStack=='+')||(op=='/'&&opAtStack=='*')||(op=='*'&&opAtStack=='/'))
    {
        isHigh=true;
    }
        return isHigh;
}

public static double result(Stack p)
{

    for(int i=0;i<p.size();i++)
    {   
        if(p.get(i).toString().startsWith("0")||p.get(i).toString().startsWith("1")||p.get(i).toString().startsWith("2")||p.get(i).toString().startsWith("3")||p.get(i).toString().startsWith("4")||p.get(i).toString().startsWith("5")||p.get(i).toString().startsWith("6")||p.get(i).toString().startsWith("7")||p.get(i).toString().startsWith("8")||p.get(i).toString().startsWith("9"))
        {
            stack.push(p.get(i));
        }
        if(p.get(i).toString().equals("+")||p.get(i).toString().equals("-")||p.get(i).toString().equals("/")||p.get(i).toString().equals("*"))
        {
            double a=Double.parseDouble(stack.pop().toString());
            double b=Double.parseDouble(stack.pop().toString());
            if(p.get(i).equals("+"))
            {
                stack.push(b+a);
            }
            else if(p.get(i).equals("-"))
            {
                stack.push(b-a);
            }
            else if(p.get(i).equals("/"))
            {
                stack.push(b/a);
            }
            else if(p.get(i).equals("*"))
            {
                stack.push(b*a);
            }
        }
    }
    System.out.println(stack);
    return 0;
}

public static void main(String[] args) {
        // TODO code application logic here
        String s=new String("159*(79+23)-10/4+(22*54-67*(25*58))");
        Main.result(Main.toPostfix(s));
        System.out.println(159*(79+23)-10/4+(22*54-67*(25*58)));
    }
}
于 2013-08-16T13:18:12.040 回答