如何在我的简单计算器(没有GUI)中输入和解决像sin(60)* 50/4这样的表达式?

时间:2015-07-10 18:28:57

标签: java math calculator

我只能进行4次操作, 我对如何输入像sin(60)* 50/4这样的表达式有疑问 没有GUI并仅使用扫描仪。

很抱歉,我只是编程的新手,还在学习基础知识。 (第一次在编程主题XD中)

这是我目前的代码。我很难在计算器中添加sin,cos,tan,square,mod和exponent。

import java.util.*;

public class Calculator {
public static void main(String[] args) 
{
   Scanner sc = new Scanner(System.in);
   int check=0;
    while (check==0)
 {
   String sInput, sReal, sToken, sMToken, sMULTd;
   int t, M, Md, term, a, b, sb, sbb;

   System.out.print("Enter Expression: ");
    sInput=sc.nextLine();
   if (sInput.charAt(0)== '-')
   {
    sReal = sInput.substring(0,1) + minusTracker(sInput.substring(1));
    System.out.print(sReal);
   }
   else
   {
    sReal = minusTracker(sInput);
    System.out.print(sReal);
   }

    StringTokenizer sADD = new StringTokenizer(sReal, "+");
        t = sADD.countTokens();
        double iTerm[] = new double [t];
        while(sADD.hasMoreTokens())
        {
            sToken = sADD.nextToken();

            for(a=0; a<=(sToken.length()-1); a++)
            {
                b=a+1;

                if( ((sToken.substring(a,b)).equals("*")) || ((sToken.substring(a,b)).equals("/")) )
                {   
                    StringTokenizer sMULT = new StringTokenizer(sToken, "*");
                    M = sMULT.countTokens();
                    double iMTerm[] = new double [M];
                    while(sMULT.hasMoreTokens())
                    {
                        sMToken = sMULT.nextToken();
                        for(sb=0; sb<=(sMToken.length()-1); sb++)
                        {
                            sbb= sb+1;
                            if((sMToken.substring(sb,sbb)).equals("/"))
                            {
                                StringTokenizer sMULTdiv = new StringTokenizer(sMToken, "/");
                                Md = sMULTdiv.countTokens();
                                double iMdTerm[] = new double [Md];
                                while(sMULTdiv.hasMoreTokens())
                                {
                                    sMULTd = sMULTdiv.nextToken();
                                    iMdTerm[--Md] = Double.parseDouble(sMULTd);
                                }
                                double MdTotal = getMdQuotient(iMdTerm);
                                sMToken = Double.toString(MdTotal);
                            }
                            }
                        iMTerm[--M] = Double.parseDouble(sMToken);
                        double mProduct = getMProduct(iMTerm);
                        sToken = Double.toString(mProduct);
                    }
            }
        }
            iTerm[--t]= Double.parseDouble(sToken);
            double finalAnswer = getSum(iTerm);
        if(sADD.hasMoreTokens()==false)
            System.out.println(" = " + finalAnswer );
        }
    }

}
public static String minusTracker(String sInput)
{
    if(sInput.isEmpty())
    {
        return "";
    }
    else if(sInput.charAt(0)== '*' || sInput.charAt(0)=='/' || sInput.charAt(0)=='+' )
    {
            return sInput.substring(0,2) + minusTracker(sInput.substring(2));
    }
    else if( sInput.charAt(0)== '-')
    {
        if(sInput.charAt(1)== '-')
        {
            sInput = sInput.replaceFirst("--", "+");
            return sInput.substring(0,2) + minusTracker(sInput.substring(2));   
        }
        else    
        {
            sInput = sInput.replaceFirst("-", "+-");
            return sInput.substring(0,2) + minusTracker(sInput.substring(2));
        }
    }
    else
    {
        return sInput.substring(0,1) + minusTracker(sInput.substring(1));
    }
}

public static double getMdQuotient(double iMdTerm[])
{
    double quotient= iMdTerm[(iMdTerm.length)-1];
    for(int y=(iMdTerm.length)-2; y>=0; y--)
    {
        quotient = quotient / iMdTerm[y];
    }
    return quotient;
}

public static double getMProduct(double iMTerm[])
{
    double product= 1;
    for(int z=(iMTerm.length)-1; z>=0; z--)
    {
        product = product * iMTerm[z];
    }
    return product;
}

public static double getSum(double iTerm[])
{
    double sum= 0;
    for(int z=(iTerm.length)-1; z>=0; z--)
    {
        sum = sum + iTerm[z];
    }
    return sum;


}
}

0 个答案:

没有答案