gpt4 book ai didi

java - 制作三角函数计算器时如何处理运算顺序

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:50:47 27 4
gpt4 key购买 nike

更新:不幸的是,我无法完成这项任务,尽管截止日期已经过去,但我觉得我在这上面投入了太多时间,无法将其搁置一旁,而且我知道我已经接近解决方案了。这是我在过去几天生成的代码

/* 

"[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);
}

}

这项作业的目的是创建一个程序来计算参数方程并连接坐标以绘制图形。我遇到的问题是,当我在 queue_str() 函数中发送我的 double 组以进行评估时,我有两个操作存在的实例让我们感到困惑。我无法将内容正确发送回 queue_str() 以由第二个操作数进行评估,例如,考虑到我的顶级评论中的示例函数,我能够发送 3*t,其中 t_vals 是要乘以三个的测试用例.在这种情况下,我必须将数组的大小设置为 -1,因为我必须考虑数组中的第一个和最后一个元素。这就是我卡住的地方,我该如何正确评估这个表达式?我将不胜感激任何帮助,我为这一段道歉,我只知道我非常接近让这个工作。谢谢。

最佳答案

你应该将你的表达式解析成一棵树,而不是从左到右评估它们。

  sin
|
+
/ \
* 5
/ \
2 t

对此有更详细的解释 here .

关于java - 制作三角函数计算器时如何处理运算顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15467697/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com