• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

JavaCC解析加减乘除正余弦/切,反正余弦/切

JAVA相关 水墨上仙 3115次浏览

javaCC实现double型数据的加减乘除法,正弦、余弦、正切、余切、反正弦、反余弦、反正切、反余

/**
 * JavaCC file
 */
 
options {
  JDK_VERSION = "1.5";
}
PARSER_BEGIN(computer)
package com.zju.edu;
import java.math.*;
public class computer {
  public static void main(String args[]) throws ParseException {
    computer parser = new computer(System.in);
    while (true) {
      System.out.println("Reading from standard input...");
      System.out.print("Enter an expression like \"1+(2+3)*4/5+sin(1)*cos(2)*tan(3)/cot(4)+arcsin(0.4)*arccos(0.5)*arctan(5)/arccot(3)=\" :");
      try {
        switch (computer.one_line()) {
        case 0:
          System.out.println("OK.");
          break;
        case 1:
          System.out.println("Goodbye.");
          break;
        default:
          break;
        }
      } catch (Exception e) {
        System.out.println("NOK.");
        System.out.println(e.getMessage());
        computer.ReInit(System.in);
      } catch (Error e) {
        System.out.println("Oops.");
        System.out.println(e.getMessage());
        break;
      }
    }
  }
}
PARSER_END(computer)
SKIP :
{
 	" "
|	"\r"
|	"\t"
|	"\n"
}
TOKEN : /* OPERATORS */
{
	< PLUS: "+" >
|	< MINUS: "-" >
|	< MULTIPLY: "*" >
|	< DIVIDE: "/" >
}
TOKEN :  
{
    < CONSTANT: ( <DIGIT> )+ ("."(<DIGIT>)+)? >
|   < #DIGIT: ["0" - "9"] >
}
TOKEN:{
   < SIN:"sin" >
|  < COS:"cos" >
|  < TAN:"tan" >	
|  < COT:"cot" >
|  < ARCSIN:"arcsin" >
|  < ARCCOS:"arccos" >
|  < ARCTAN:"arctan" >
|  < ARCCOT:"arccot" >
}
int one_line() : {
   double a;
}
{
   a = sum() "=" {System.out.println(a); return 0; }
| "="	    { System.out.println("没有任何输入"); return 1; }
}
double sum() : {
  double a;
  double b;
}
{
  a = term() 
  (
   <PLUS> b=term()  { a += b;} 
  | <MINUS>  b=term() { a-=b;}
  )*  { return a;}
}
double term() : {
  double a;
  double b;
}
{
  a = unary() 
  (
  <MULTIPLY> b = unary()  { a *= b;}
  | <DIVIDE> b = unary()  { a /= b;}
  )*  {return a;}
}
double unary() : {
   double a ;
}
{
  <MINUS> a =  element()  { return -a;}
 | a = element()  { return a;}
}
double element() : {
   Token t;
   double a ;
}
{
  t = <CONSTANT>  { return Double.parseDouble(t.toString());   }
| <SIN> "(" t = <CONSTANT> ")" { return Math.sin(Double.parseDouble(t.toString()));}
| <COS> "(" t = <CONSTANT> ")" { return Math.cos(Double.parseDouble(t.toString()));}
| <TAN> "(" t = <CONSTANT> ")" { return Math.tan(Double.parseDouble(t.toString()));}
| <COT> "(" t = <CONSTANT> ")" { return 1/Math.tan(Double.parseDouble(t.toString()));}
| <ARCSIN> "(" t = <CONSTANT> ")" { return Math.asin(Double.parseDouble(t.toString()));}
| <ARCCOS> "(" t = <CONSTANT> ")" { return Math.acos(Double.parseDouble(t.toString()));}
| <ARCTAN> "(" t = <CONSTANT> ")" { return Math.atan(Double.parseDouble(t.toString()));}
| <ARCCOT> "(" t = <CONSTANT> ")" { return ((Math.PI / 2) - Math.atan(Double.parseDouble(t.toString())));}
| "(" a = sum() ")" { return a;}
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明JavaCC解析加减乘除正余弦/切,反正余弦/切
喜欢 (0)
加载中……