Skip to content

Instantly share code, notes, and snippets.

@tandat2209
Last active May 10, 2016 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tandat2209/d7970e1678fe24e6c9060956b8c3ebe5 to your computer and use it in GitHub Desktop.
Save tandat2209/d7970e1678fe24e6c9060956b8c3ebe5 to your computer and use it in GitHub Desktop.
Tính gần đúng phương trình
package giaipt;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import bai1_2.KyPhapBaLan;
public class BieuThuc {
private String bieuthuc = "";
private int[] heso;
private int bac;
public BieuThuc(String bt) {
this.bieuthuc = bt;
}
public BieuThuc(File f) {
Scanner sc;
try {
sc = new Scanner(f);
if(sc.hasNext()){
this.bieuthuc = sc.nextLine();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// theo phuong phap horner
public double calc(double x){
if(heso != null){
double sum = heso[bac] * x;
for (int i = bac - 1 ; i >= 1; i--)
sum = (sum + heso[i]) * x;
sum += heso[0];
return sum;
}
else{
return KyPhapBaLan.calculate(bieuthuc.replace("x", "("+x+")"));
}
}
public String toString(){
if(heso != null){
String s = "";
for(int i=bac; i>=0; i--){
s += heso[i] +"x^" + i + " ";
}
return s;
}
return bieuthuc;
}
public static void main(String[] args) {
// BieuThuc dt = new BieuThuc(new File(BieuThuc.class.getResource("dathuc.txt").getPath()));
// DaThuc dt = new DaThuc("2^x+x-4");
BieuThuc dt = new BieuThuc("x^3+x-5");
System.out.println(dt);
System.out.println(GiaiGanDung.ppchiadoi(dt, 1, 2, 0.0001));
System.out.println(GiaiGanDung.ppdaycung(dt, 1, 2, 0.0001));
}
}
package giaipt;
public class GiaiGanDung {
public static double ppchiadoi(BieuThuc dt, double a, double b, double epxilon){
double c = (b+a)/2.0, E = Math.abs(b-a);
while(E>epxilon){
// System.out.println(dt.calc(c));
if(dt.calc(c)>=0){
b = c;
} else{
a = c;
}
E = Math.abs(b-a );
c = (b+a)/2.0;
}
return Math.round(a/epxilon)*epxilon;
}
public static double ppdaycung(BieuThuc bt, double a, double b, double epxilon){
double x = a - (b-a)*bt.calc(a)/(bt.calc(b)-bt.calc(a));
double temp = a;
while(Math.abs(x -temp)> epxilon){
if(bt.calc(x)*bt.calc(a)<0){
b = x;
} else{
a = x;
}
temp = x;
x = a - (b-a)*bt.calc(a)/(bt.calc(b)-bt.calc(a));
}
return Math.round(x/epxilon)*epxilon;
}
}
package bai1_2;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class KyPhapBaLan {
public static void main(String[] args) {
String bieuthuc = "sin((-2.0))";
// String bieuthuc = "2*(-2.0)+sin((-2))-4";
ArrayList<String> postfix = InfixToPostfix(bieuthuc);
System.out.println(postfix);
float result = calculate(postfix);
System.out.println(result);
}
public static float calculate(String bieuthuc){
ArrayList<String> postfix = InfixToPostfix(bieuthuc);
return calculate(postfix);
}
public static float calculate(ArrayList<String> postfix){
Stack<Float> stack = new Stack<Float>();
for(int i=0; i<postfix.size(); i++){
String s = postfix.get(i);
if(!isOperator(s) && !isTrigonomicsFunc(s)){
stack.push(Float.valueOf(s));
}
else if(isTrigonomicsFunc(s)){
float o = stack.pop();
float re = 0;
switch (s) {
case "sin":
re = (float) Math.sin(o);
break;
case "cos":
re = (float) Math.cos(o);
break;
case "tan":
re = (float) Math.tan(o);
break;
default:
break;
}
stack.push(re);
}
else{
float o1 = stack.pop();
float o2 = stack.pop();
float o3 = 0;
switch (s) {
case "+":
o3 = o2+o1;
break;
case "-":
o3 = o2-o1;
break;
case "*":
o3 = o2*o1;
break;
case "/":
o3 = o2/o1;
break;
case "%":
o3 = o2%o1;
break;
case "^":
o3 = (float)Math.pow(o2, o1);
break;
}
stack.push(o3);
}
}
return stack.pop();
}
public static int getPriority(String op){
if("sin".equals(op) || "cos".equals(op) || "tan".equals(op))
return 4;
if(op.equals("^"))
return 3;
if ("*".equals(op) || "/".equals(op) || "%".equals(op))
return 2;
if ("+".equals(op) || "-".equals(op))
return 1;
return 0;
}
public static boolean isOperator(String str){ // kiem tra xem co phai toan tu
String operator[] = { "+", "-", "*", "/", ")", "(" , "^"};
Arrays.sort(operator);
if (Arrays.binarySearch(operator, str) > -1)
return true;
return false;
}
public static boolean isTrigonomicsFunc(String str){
return "sin".equals(str) || "cos".equals(str) || "tan".equals(str);
}
public static ArrayList<String> InfixToPostfix(String infix){
// match 2 | 2.0
Pattern number = Pattern.compile("^(\\d+\\.\\d+|\\d+)");
// match (-1) | (-2.3)
Pattern negative = Pattern.compile("^([(][-](\\d+\\.\\d+|\\d+)[)])");
Pattern trigfunc = Pattern.compile("^(sin|cos|tan)");
Stack<String> stack = new Stack();
ArrayList<String > postfix = new ArrayList<>();
for(int i=0; i<infix.length(); i++){
// System.out.println(stack);
// System.out.println(postfix);
String s = infix.charAt(i) + "";
if(!isOperator(s) && s.matches("\\d")){
Matcher m = number.matcher(infix.substring(i));
// System.out.println(infix.substring(i));
if(m.find()) {
String num = m.group(0);
i+= num.length()-1;
postfix.add(num);
}
} else if(!isOperator(s)){
Matcher m = trigfunc.matcher(infix.substring(i));
if(m.find()){
String trig = m.group(0);
i+= trig.length()-1;
stack.add(trig);
}
} else if(s.equals("(")){
Matcher m = negative.matcher(infix.substring(i));
if(m.find()){
// m.group(0) == (-2.0) --need get rid of two ()
String negNum = m.group(0);
i+= negNum.length()-1;
negNum = negNum.substring(1, m.group(0).length()-1);
postfix.add(negNum);
}
else {
// System.out.println(s);
stack.push(s);
}
} else if(s.equals(")")){
String x = stack.pop();
while(!x.equals("(")){
postfix.add(x);
x = stack.pop();
}
} else {
while(!stack.isEmpty() && getPriority(s) <= getPriority(stack.peek()) && !stack.peek().equals("(")){
postfix.add(stack.pop());
}
stack.push(s);
}
}
while(!stack.isEmpty()){
postfix.add(stack.pop());
}
return postfix;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment