Witam. Mam taki program:
import java.util.*;
import static java.lang.Math.*;
import javax.swing.JOptionPane;
import java.text.NumberFormat;
class Main{
public static void main(String[] args) {
Funkcje temp = new Funkcje();
String s="x^2+2*x+1";
String x="sin((15+30/2)*PI/180)";
if (x.length()>0) {
System.out.println("Zmienna x = "+x);
}
System.out.println("Wynik dla dzialania "+s+" = "+temp.uprosc(s,x));
}
}
class Funkcje {
public Funkcje() {
}
public String uprosc(String a,String x) {
String wynik=a.replace(" ","");
wynik=wynik.replace("PI",Math.PI+"");
if (x!="") { wynik=wynik.replace("x",this.uprosc(x,"")); }
String delta="";
int koniec=a.length()-1;
int poczatek=0;
boolean zmiana=true;
while (zmiana==true && wynik!=null) {
a=wynik;
koniec=a.length()-1;
poczatek=0;
for (int i=0;i<a.length();i++) {
if (a.substring(i,i+1).equals(")")) {
koniec=i-1;
i=a.length()+1;
}
}
for (int i=koniec-1;i>=0;i--) {
System.out.println(i);
if (a.substring(i,i+1).equals("(")) {
poczatek=i+1;
i=-1;
}
}
for (int i=poczatek-1;i<a.length();i++) {
if (a.substring(i,i+1).equals(")")) {
koniec=i-1;
}
}
delta=a.substring(poczatek,koniec+1);
delta=policz(delta);
if (delta==null) {
wynik=null;
} else {
if (poczatek>=4) {
if (a.toUpperCase().substring(poczatek-4,poczatek-1).equals("SIN")) {
delta=""+sin(Double.parseDouble(delta));
poczatek=poczatek-3;
} else if (a.toUpperCase().substring(poczatek-4,poczatek-1).equals("COS")) {
delta=""+cos(Double.parseDouble(delta));
poczatek=poczatek-3;
} else if (a.toUpperCase().substring(poczatek-4,poczatek-1).equals("CTG")) {
if (sin(Double.parseDouble(delta))!=0) {
delta=""+cos(Double.parseDouble(delta))/sin(Double.parseDouble(delta));
poczatek=poczatek-3;
} else { wynik=null; }
} else if (a.toUpperCase().substring(poczatek-4,poczatek-1).equals("EXP")) {
delta=""+exp(Double.parseDouble(delta));
poczatek=poczatek-3;
}
} else if (poczatek>=3) {
if (a.toUpperCase().substring(poczatek-3,poczatek-1).equals("LN")) {
delta=""+log(Double.parseDouble(delta));
poczatek=poczatek-2;
} else if (a.toUpperCase().substring(poczatek-3,poczatek-1).equals("TG")) {
if (cos(Double.parseDouble(delta))!=0) {
delta=""+sin(Double.parseDouble(delta))/cos(Double.parseDouble(delta));
poczatek=poczatek-2;
} else { wynik=null; }
}
}
if (wynik!=null) {
if (poczatek<1) poczatek=1;
if (koniec>a.length()-2) koniec=a.length()-2;
wynik=a.replace(a.substring(poczatek-1,koniec+2),delta);
if (wynik.equals(a)) {
zmiana=false;
}
}
}
System.out.println(wynik);
}
if (wynik==null) {
wynik="Blad dzielenia przez 0!";
}
return wynik;
}
public String policz(String a) {
String wynik=a;
if (wynik!="") {
int koniec=a.length()-1;
int poczatek=0;
int srodek=0;
double t1=0;
double t2=0;
String operator="";
String podwynik="";
boolean zmiana=true;
if (a.substring(a.length()-1,a.length()).equals("-") || a.substring(a.length()-1,a.length()).equals("/") || a.substring(a.length()-1,a.length()).equals("+") || a.substring(a.length()-1,a.length()).equals("^") || a.substring(a.length()-1,a.length()).equals("*")) {
wynik=null;
}
while (zmiana==true && wynik!=null) {
a=wynik;
koniec=a.length();
srodek=0;
poczatek=0;
zmiana=false;
for (int i=a.length()-2;i>=0;i--) {
if (a.substring(i,i+1).equals("^")) {
srodek=i;
i=-1;
zmiana=true;
operator="^";
}
}
if (zmiana==false) {
for (int i=a.length()-2;i>=0;i--) {
if (a.substring(i,i+1).equals("*")) {
srodek=i;
i=-1;
zmiana=true;
operator="*";
}
}
if (zmiana==false) {
for (int i=a.length()-2;i>=0;i--) {
if (a.substring(i,i+1).equals("/")) {
srodek=i;
i=-1;
zmiana=true;
operator="/";
}
}
if (zmiana==false) {
for (int i=a.length()-2;i>=0;i--) {
if (a.substring(i,i+1).equals("+")) {
srodek=i;
i=-1;
zmiana=true;
operator="+";
}
}
if (zmiana==false) {
for (int i=a.length()-2;i>=0;i--) {
if (a.substring(i,i+1).equals("-")) {
if (i!=0) {
srodek=i;
i=-1;
zmiana=true;
} else {
zmiana=false;
}
operator="-";
}
}
}
}
}
}
if (zmiana==true) {
for (int i=srodek+2;i<a.length();i++) {
if (a.substring(i,i+1).equals("-") || a.substring(i,i+1).equals("+") || a.substring(i,i+1).equals("^") || a.substring(i,i+1).equals("*") || a.substring(i,i+1).equals("/")) {
koniec=i;
i=a.length()+1;
}
}
for (int i=srodek-1;i>=0;i--) {
if (i>0) {
if (a.substring(i,i+1).equals("-") && (a.substring(i-1,i).equals("+") || a.substring(i-1,i).equals("-") || a.substring(i-1,i).equals("^") || a.substring(i-1,i).equals("*") || a.substring(i-1,i).equals("/"))) {
i=i-1;
}
}
if (a.substring(i,i+1).equals("+") || a.substring(i,i+1).equals("^") || a.substring(i,i+1).equals("*") || a.substring(i,i+1).equals("/")) {
poczatek=i+1;
i=-1;
}
}
t1=Double.parseDouble(a.substring(poczatek,srodek));
t2=Double.parseDouble((a+" ").substring(srodek+1,koniec));
if (operator.equals("+")) {
podwynik=(t1+t2)+"";
}
if (operator.equals("*")) {
podwynik=(t1*t2)+"";
}
if (operator.equals("/")) {
if (t2==0) { podwynik=null;} else {podwynik=(t1/t2)+"";}
}
if (operator.equals("^")) {
podwynik=(Math.pow(t1,t2))+"";
}
if (operator.equals("-")) {
podwynik=(t1-t2)+"";
}
if (podwynik==null) {wynik=null;} else {
wynik=a.substring(0,poczatek)+podwynik;
if (a.length()>koniec) {wynik=wynik+a.substring(koniec,a.length());}
}
}
}
}
wynik="1";
return wynik;
}
public String[] addString(String a[],String napis) {
String wynik[]=new String[a.length+1];
for (int i=0;i<a.length;i++) {
wynik[i]=a[i];
}
wynik[a.length]=napis;
return wynik;
}
public String howMany(String a[]) {
Arrays.sort(a);
int j=0;
String wynik="";
String oldString=a[0];
for (int i=0;i<a.length;i++) {
if (!oldString.equals(a[i])) {
wynik=wynik+oldString+" -> "+(j)+"\n";
oldString=a[i];
j=0;
}
if (i==a.length-1 && oldString.equals(a[i])) {
wynik=wynik+oldString+" -> "+(j+1)+"\n";
}
j=j+1;
}
return wynik;
}
public static boolean isNum(String s) {
try {
Double.parseDouble(s);
}
catch (NumberFormatException nfe) {
return false;
}
return true;
}
public static boolean isString(String s,int minimum) {
s=s.replace("0","");
s=s.replace("1","");
s=s.replace("2","");
s=s.replace("3","");
s=s.replace("4","");
s=s.replace("5","");
s=s.replace("6","");
s=s.replace("7","");
s=s.replace("8","");
s=s.replace("9","");
if (s.length()>minimum && s!=null) { return true; }
else {
return false;
}
}
public static boolean isToken(String s,int minimum) {
int j=1;
for (int i=0; i<s.length(); i++) {
if (s.charAt(i)==' ') {
j=j+1;
}
}
if (j>=minimum) { return true; }
else {
return false;
}
}
}
Kompilacja przebiegła w porządku, nie ma żadnych errorów i tak dalej.
Chciałem sprawdzić czy dobrze działa ale pojawia się problem. Na konsoli wyświetla się błęd, którego nigdy wcześniej nie widziałem (mimo pisania podobnych programów):
Error: Main method not found in class Funkcje, please define the main method as:
public static void main(String[] args)
Nie bardzo rozumiem dlaczego coś takiego się stało, skoro klasa main wygląda zazwyczaj podobnie.
W internecie radzą coś takiego:
public static void main(String[] args) {
new Main();
}
Tylko dlaczego?
Jakby mógł ktoś wyjaśnić... Będę wdzięczny.