I can not make this code resolve the expressions contained in the database. In the bank, the expressions are like this:
All the expressions registered in the bank follow the pattern described below:
$expressao = "$x$op1$y$op2$z$op3$w$op4$k";
Where:
$operadores = array('+', '*', '/', '-');
$op1 = $operadores[rand(0,3)];
$op2 = $operadores[rand(0,3)];
$op3 = $operadores[rand(0,3)];
$op4 = $operadores[rand(0,3)];
$x = rand(1111111111,9999999999).rand(1111111111,9999999999).rand(1111111111,9999999999);
$y = rand(1111111111,9999999999).rand(1111111111,9999999999).rand(1111111111,9999999999);
$z = rand(1111111111,9999999999).rand(1111111111,9999999999).rand(1111111111,9999999999);
$w = rand(1111111111,9999999999).rand(1111111111,9999999999).rand(1111111111,9999999999);
$k = rand(1111111111,9999999999).rand(1111111111,9999999999).rand(1111111111,9999999999);
Example:
136745704413540771491283768999*132288955513943880221144001435+131540870513
718749541202397273/136652154113069665481264184389/126364152611910743531217
601923
public class SD {
static double res;
static int id ;
public SD() {
}
public static Connection con = null;
public static void Conectar() {
System.out.println("Conectando ao banco...");
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1/basedados", "root", "pass");
System.out.println("Conectado.");
} catch (ClassNotFoundException ex) {
System.out.println("Classe não encontrada, adicione o driver nas bibliotecas.");
Logger.getLogger(SD.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException erro) {
System.out.println("Erro: " + erro);
throw new RuntimeException(erro);
}
}
public String Retirar() {
Conectar();
String nome = "Fillipe";
String expressao = null;
List<String> x = new ArrayList<>();
try {
PreparedStatement ps = con.prepareStatement("{call retira_exp(?)}");
ps.setString(1, nome);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
x.add(rs.getString(2));
id = Integer.parseInt(rs.getString(1));
}
// for (String exp : x) {
// }
} catch (SQLException e) {
System.out.println("Erro: " + e);
}
expressao = x.get(0);
return expressao;
}
public static double Calcular() {
SD obj = new SD();
String expr = obj.Retirar();
System.out.println("Expressao: " + expr);
// double a = Double.parseDouble(expr);
String [] valores = expr.split("\+|\-|\*/|\-");
String [] op = expr.split("\d+");
// op[1] = primeiro operador
if(op[1].equals("+")){
//resultado = valor[i] + valor[i+1]
}
return res;
}
public void EntregaResultado() {
Double result = Calcular();
System.out.println("Resultado: " + result);
try {
PreparedStatement pstm = con.prepareStatement("{call entrega_res(?,?)}");
pstm.setInt(1, id);
pstm.setDouble(2, result);
pstm.executeUpdate();
} catch (SQLException e) {
System.out.println("Erro: " + e);
}
}
public static void main(String[] args) {
// SD.Conectar();
SD sd = new SD();
sd.EntregaResultado();
}
}