Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * @author amine_err
- */
- public class Calc {
- public static boolean isNumber(String str){
- return str.matches("-?(\\d+|\\d+\\.\\d+|\\.\\d+|\\d+\\.)");
- }
- public static double pow(double a, int b){
- double res = 1;
- int i = 0;
- while( i<b ){
- res *= a;
- i++;
- }
- return res;
- }
- public static double round(double n, int a){
- double p = pow(10, a);
- return Math.round(p*n)/p;
- }
- public static boolean check(String exp) throws Exception {
- //if syntax correct
- // not empty
- if ("".equals(exp)) {
- throw new Exception("empty! ");
- }
- // no 2 [*,.,|] in a row
- String ops[] = new String[]{"+", "-", "*", "/", "%", "^"};
- for (String op : ops) {
- for (String op1 : ops) {
- if (exp.contains(op + op1)) {
- //throw ;
- throw new Exception("syntax error! " + op + op1);
- }
- }
- }
- // no op)
- for (String op : ops) {
- if (exp.contains(op + ")")) {
- throw new Exception("syntax error! " + op + ")");
- }
- }
- // no (["*", "/", "%", "^"]
- for (int i = 2; i < ops.length; i++) {
- if (exp.contains("(" + ops[i])) {
- throw new Exception("syntax error! " + "(" + ops[i]);
- }
- }
- // no start with ["*", "/", "%", "^"]
- for (int i = 2; i < ops.length; i++) {
- if (exp.charAt(0) == ops[i].charAt(0)) {
- throw new Exception("syntax error! starting with " + ops[i] );
- }
- }
- //
- int k = 0;
- for (int i = 0; i < exp.length(); i++) {
- if (exp.charAt(i) == '(') {
- k++;
- }
- if (exp.charAt(i) == ')') {
- k--;
- }
- if (k < 0) {
- throw new Exception("syntax error! mismatched parentheses ");
- }
- }
- if (k > 0) {
- throw new Exception("syntax error! mismatched parentheses ");
- }
- return true;
- }
- public static double calc(String str) throws Exception{
- // if str is a simple number
- if ( isNumber(str) ) {
- return Double.parseDouble(str);
- }
- else if (!str.equals("")) {
- // (----) => ----
- if (str.charAt(0) == '(' && str.charAt(str.length() - 1) == ')') {
- return calc(str.substring(1, str.length() - 1));
- }
- int k, i;
- // +, -
- for (i = str.length() - 1; i > -1; i--) {
- if (str.charAt(i) == ')') {
- k = -1;
- while (!(k == 0 && str.charAt(i) == '(')) {
- i--;
- if (str.charAt(i) == ')')
- k--;
- else if (str.charAt(i) == '(')
- k++;
- }
- i--;
- }
- if (i > -1) {
- if (str.charAt(i) == '+') {
- return calc(str.substring(0, i))+calc(str.substring(i + 1, str.length()));
- }
- if (str.charAt(i) == '-') {
- return calc(str.substring(0, i))-calc(str.substring(i + 1, str.length()));
- }
- }
- }
- // *, /, %
- for (i = str.length() - 1; i > -1; i--) {
- if (str.charAt(i) == ')') {
- k = -1;
- while (!(k == 0 && str.charAt(i) == '(')) {
- i--;
- if (str.charAt(i) == ')')
- k--;
- else if (str.charAt(i) == '(')
- k++;
- }
- i--;
- }
- if (i > -1) {
- if (str.charAt(i) == '*') {
- return calc(str.substring(0, i))*calc(str.substring(i + 1, str.length()));
- }
- if (str.charAt(i) == '/') {
- return calc(str.substring(0, i))/calc(str.substring(i + 1, str.length()));
- }
- if (str.charAt(i) == '%') {
- return calc(str.substring(0, i))%calc(str.substring(i + 1, str.length()));
- }
- }
- }
- for (i = 0; i < str.length(); i++) {
- if (str.charAt(i) == '(') {
- k = 1;
- while (!(k == 0 && str.charAt(i) == ')')) {
- i++;
- if (str.charAt(i) == ')')
- k--;
- else if (str.charAt(i) == '(')
- k++;
- }
- i++;
- }
- if (i < str.length()) {
- if (str.charAt(i) == '^') {
- return pow(calc(str.substring(0, i)),(int)calc(str.substring(i + 1, str.length())));
- }
- }
- }
- // look if there is a function
- int fi = str.indexOf("(", 1);
- if( fi!=-1 ){
- String function_name = str.substring(0,fi);
- int epi = str.length()-1;
- String parameter = str.substring(fi+1,epi);
- switch (function_name) {
- case "cos":
- return Math.cos( calc(parameter+"/180*"+Math.PI) );
- case "sin":
- return Math.sin( calc(parameter+"/180*"+Math.PI) );
- case "tan":
- return Math.tan( calc(parameter+"/180*"+Math.PI) );
- default:
- throw new Exception(function_name+": is not defined");
- }
- }
- }
- return 0;
- }
- public static void resultat(String expr){
- expr = expr.replaceAll(" ", "");
- try {
- if( check(expr) )
- System.out.println(round(calc(expr), 15));
- } catch (Exception ex) {
- System.out.println(ex.getMessage());
- }
- }
- public static void main(String[] args) {
- String expr = "1+2*2 +6*(1.2 -0.2)/3 + cos(90)";
- resultat(expr);
- String expr1 = "5+2*10+ccos(60)";
- resultat(expr1);
- String expr2 = "(10*5+10)^2/100";
- resultat(expr2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment