Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle order of operations when making a trigonometric calculator

Tags:

java

Update: Unfortunately i was unable to finish this assignment, although the deadline has passed i feel that i have invested too much time on this to just set it aside and i know that I am close to the solution. This is the code that i have produced over the past couple of days

/* 

 "[5*sin(3*t+0.523),4*cos(2*t)]"

*/

import java.util.*;             // 
import javax.swing.*;           // 
import javax.swing.JFrame;      // lets me create the window 
import java.awt.Graphics;       // for drawing 
import java.awt.Point;          // allows the use of points 
import java.awt.Graphics2D;     // for drawing 
import javax.swing.JComponent;  // for drawing 
import javax.swing.JPanel;     // for drawing 
import java.lang.Math;         // for trig 
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;




public class Graph 
{   

//-------------------Queue---String----------------------------------------------------------------//

  public static double[] queue_str(String str, double[] results)
  {
    Queue<Character> token = new LinkedList<Character>();
    Queue<Double> numbers = new LinkedList<Double>();
    double[] t_vals = new double [5];
    int t_vals_len = t_vals.length;
    int numbers_sz = 0;
    int token_sz = 0;
    t_vals[0] = 7.0;
    t_vals[1] = 2.0;
    t_vals[2] = 3.0;
    t_vals[3] = 4.0;
    t_vals[4] = 3.0;
    char temp_token;
    char temp_char; 
    double p1_num = 0, temp_num = 0, p2_num = 0;

    if(results.length == 0){}

    for(int i = 0; i < str.length(); i++)
    {
        if(str.charAt(i) == '*' && str.charAt(i) != 't'  || str.charAt(i) == '+'&& str.charAt(i) != 't' || str.charAt(i) == '-' && str.charAt(i) != 't' || str.charAt(i) == '/' && str.charAt(i) != 't')
        {
            temp_token = str.charAt(i);
            token.add(temp_token);
            token_sz ++;
            String[] parts = str.split("\\" + String.valueOf(temp_token));
            String str1 = parts[0];
            String str2 = parts[1];


            if(str1.matches("-?\\d+(\\.\\d+)?"))
            {
                p1_num = Double.parseDouble(str1);
                numbers.add(p1_num);
                numbers_sz ++;
            }

            if(str2.matches("-?\\d+(\\.\\d+)?"))
            {
                p2_num = Double.parseDouble(str2);
                numbers.add(p2_num);
                numbers_sz ++;
            }
        }
            else if(str.charAt(i) == 't')
            {
                temp_token = str.charAt(i);

                String [] t_char = str.split(String.valueOf(temp_token));

                for(int k = 0; k < t_vals_len; k++)
                {
                    temp_num = t_vals[k];
                    numbers.add(temp_num);
                    numbers_sz ++;
                }
            }

    }
      double[] dbl_numbs = new double [numbers_sz];

      while(numbers.peek()!=null)
      {
           for (int iter = 0; iter < numbers_sz; iter++)
           {
             dbl_numbs [iter] = numbers.poll();
           }  
      }

         while(token.peek()!=null)
          {
               temp_token = token.peek();
               token.poll();

               if(temp_token== '+')
               {

                 eval_inner_add(dbl_numbs, numbers_sz);
                 results = eval_inner_add(dbl_numbs,numbers_sz);

               }
               else if(temp_token == '-')
               {

                eval_inner_add(dbl_numbs, numbers_sz);
               }
               else if(temp_token == '*')
               {
                 results  = eval_inner_multi(dbl_numbs, numbers_sz);

               }
               else if(temp_token == '/')
               {

                eval_inner_div(dbl_numbs, numbers_sz);
               }
          }

      return t_vals;
  }
//-----------------Removing---Brackets-------------------------------------------------------------//  
  public static String [] remove_brackets(String remove_brack)
  {
     String [] substring = remove_brack.split ("[\\[,\\]]");
     String rest_of_str = substring [1];
     String rest_of_str_2 = substring [2];
     return new String [] {rest_of_str,rest_of_str_2};
  } 
//-----------------Removing---Parenthesis-------------------------------------------------------------//    
   public static String [] remove_paren(String remove_paren)
  {
     String [] substring = remove_paren.split ("[\\(,\\)]");
     String rest_of_str = substring [0];
     String rest_of_str_2 = substring [1];
     return new String [] {rest_of_str,rest_of_str_2};
  } 
//---------------Retrieving--Far-Left--Number----------------------------------------------------//  
  public static double get_first_exp (String get_left_exp)  
  {
    double eval_num = 0;

    if(get_left_exp.matches("[-]"))
    {
        String [] check_neg = get_left_exp.split("[\\-]");
        String neg_num = check_neg[0];
        eval_num = Double.parseDouble(neg_num);
        eval_num = eval_num * (-1.0);
        return eval_num;
    }
    else
    {
        String [] parts = get_left_exp.split("[\\/,\\*,\\+]");
        String sub_str_1 = parts [0];
        eval_num = Double.parseDouble(sub_str_1);
        return eval_num;
    }
  }
//-------------Evaluating--equations--containing--sine----------------------------------------//

  public static double eval_sin (String sin_eq)
  {
   double number = 0;
   String [] split = remove_paren(sin_eq);
   String inner_exp = split[1];
   double [] nothing = new double [0];

   number = get_first_exp (sin_eq);
   queue_str(inner_exp, nothing);
   return number;
  }

//-------------Evaluating--equations--containing--cosine----------------------------------------//
  public static double eval_cos (String cos_eq)
  {
   double number = 0;
   double [] nothing = new double [0];
   String [] split = remove_paren(cos_eq);
   String inner_exp = split[1];

   number = get_first_exp (cos_eq);
   queue_str(inner_exp, nothing);
   return number;
  }
//---------------Evalutating--points-------------------------------------------------------------//  
  public static double[] eval_inner_multi (double [] numbers, int numbers_sz)
  {
   String nothing  = "";
   double[] results = new double [numbers_sz];
   double  first_num = numbers[0];
   System.out.println(first_num);

    for(int i = 1; i < numbers_sz-1; i++)
    {
     results[i] = first_num * numbers[i]; 
     System.out.println(results[i]);
    }
      queue_str(nothing,results);
      return results; 
  }

  public static double[] eval_inner_add (double [] numbers, int numbers_sz)
  {
   String nothing  = "";
   double[] results = new double [numbers_sz];
   double  first_num = numbers[0];


    for(int i = 1; i < numbers_sz-1; i++)
    {
     results[i] = first_num + numbers[i]; 
     System.out.println(results[i]);
    }
      queue_str(nothing,results);
      return results; 
  }

  public static double[] eval_inner_sub(double [] numbers, int numbers_sz)
  {
   double[] t_vals = new double [5];
   return t_vals; 
  }

  public static double[] eval_inner_div(double [] numbers,int numbers_sz)
  {
   double[] t_vals = new double [5];
   return t_vals; 
  }
//------------------------------Main------------------------------------------------------------------------//  
  public static void main(String[] args) 
    {
      String left_exp = "";
      String right_exp = "";
      double left_sin_num , right_sin_num, left_cos_num, right_cos_num = 0;

      for(String s: args)  // taking in user input for command line 
      {
           String [] new_str  =  remove_brackets(s);
           left_exp = new_str[0];
           right_exp = new_str[1];
      }

      if(left_exp.contains("sin")) // add SINE, SIN, COSINE, COS
      {
           left_sin_num = eval_sin(left_exp);
      }
      else if (right_exp.contains("sin"))
      {
           right_sin_num = eval_sin(right_exp);
      }
      if (right_exp.contains("cos"))
      {
           right_cos_num = eval_cos(right_exp);
      }
      else if (left_exp.contains("cos"))
      {
           left_cos_num = eval_cos(left_exp);
      }

    }

}

The purpose of this assignment was to create a program that evaluates parametric equations and joins the coordinates for plotting a graph. The problem that i have is when i send my array of doubles in the queue_str() function to get evaluated, the instance where i have two operations present stumps we. I cannot properly send the contents back to the queue_str() to be evaluated by the second operand for example take into account the example function in my top comments i am able to send 3*t where t_vals are test cases to be multiplied by the three. In this case i have to go size of array -1 because i have to take into account the first and last element in the array. This is where im stuck how can I properly evaluate this expression? I would appreciate any help and i apologize for this paragraph, i just know im so close to getting this to work. Thanks.

like image 205
Vega512 Avatar asked Nov 26 '25 16:11

Vega512


1 Answers

You should parse your expression into a tree, instead of evaluating them from left to right.

  sin
   |
   +
  / \
  *  5
 / \
2  t

This is explained in more detail here.

like image 141
Javier Avatar answered Nov 28 '25 07:11

Javier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!