Boa noite, quem puder ajudar por favor, não consigo colocar um laço para continuar a resolução da equação, usando os servidores de calculo.
Quem puder ajudar, sou grato.
import java.net.URL;
import java.util.;
import org.apache.xmlrpc.client.;
public class ClientCalculadoraRPC {
public static void main(String[] args) throws Exception {
int contador = 0;
String exp = "";
String c = "((321+42)-(25*5)/123)";
System.out.println(c);
ClientCalculadoraRPC calc = new ClientCalculadoraRPC();
String operacao = "";
String novaExpressao="";
int numero1 = 0;
int numero2 = 0;
// int numero3 = 0;
int j = 0;
String textoNumero = "0";
// calc.encontraParenteses(c);
int[] auxiliar = new int[c.length()];
int v = 0;
System.out.println("( )");
for (int i = 0; i < c.length(); i++) {
if (c.charAt(i) == '(') {
auxiliar[v] = i; //salva a posicao do parenteses de abertura no array auxiliar. v controla as posições neste array
v++;
}
if (c.charAt(i) == ')') {
//System.out.println(auxiliar[v-1] + "\t" + i);
exp = c.substring(auxiliar[v - 1] + 1, i);
v--;
System.out.println(exp);
while (calc.ehNumero(exp.charAt(j))) {
textoNumero += exp.charAt(j);
j++;
}
numero1 = Integer.parseInt(textoNumero);
if ((exp.charAt(j) == '+')
|| (exp.charAt(j) == '-')
|| (exp.charAt(j) == '*')
|| (exp.charAt(j) == '/')) {
operacao = exp.charAt(j) + "";
}
textoNumero = "";
j++;
while ((j < exp.length())
&& (calc.ehNumero(exp.charAt(j)))) {
textoNumero += exp.charAt(j);
j++;
}
numero2 = Integer.parseInt(textoNumero);
System.out.println("numero1:" + numero1);
System.out.println("numero2:" + numero2);
System.out.println("operacao:" + operacao);
XmlRpcClient server = new XmlRpcClient();
XmlRpcClientConfigImpl configuracaoCliente = new XmlRpcClientConfigImpl();
String metodoServidor = "";
switch (operacao.charAt(0)) {
case '+':
URL servidorSoma = new URL("http://localhost:9000");
configuracaoCliente.setServerURL(servidorSoma);
metodoServidor = "Somador.soma";
break;
case '-':
URL servidorSubtracao = new URL("http://localhost:51549");
configuracaoCliente.setServerURL(servidorSubtracao);
metodoServidor = "Subtrador.subtracao";
break;
case '*':
URL servidorMultiplicacao = new URL("http://localhost:3400");
configuracaoCliente.setServerURL(servidorMultiplicacao);
metodoServidor = "Multiplicador.multiplicacao";
break;
case '/':
URL servidorDivisao = new URL("http://localhost:8000");
configuracaoCliente.setServerURL(servidorDivisao);
metodoServidor = "Divisor.divisao";
break;
}
server.setConfig(configuracaoCliente);
Vector params = new Vector();
params.addElement(new Integer(numero1));
params.addElement(new Integer(numero2));
Object result = server.execute(metodoServidor, params);
System.out.println("O resultado é: " + (int) result);
System.out.println("resultado final:");
novaExpressao = c.replace("(" + exp + ")", result + "");
System.out.println(novaExpressao);
j--;
break;
//java unboxing e boxing
}
}
c = novaExpressao;
}
// String expressao ="232*13";
private boolean ehNumero(char valor) {
if ((valor == '0') || (valor == '1') || (valor == '2')
|| (valor == '3') || (valor == '4') || (valor == '5')
|| (valor == '6') || (valor == '7') || (valor == '8')
|| (valor == '9')) {
return true;
}
return false;
}
}
import org.apache.xmlrpc.server.PropertyHandlerMapping;
import org.apache.xmlrpc.server.XmlRpcServer;
import org.apache.xmlrpc.webserver.*;
public class ServerDivisaoRPC {
public ServerDivisaoRPC() {
}
public Integer divisao(int x, int y) {
return new Integer(x / y);
}
public static void main(String[] args) {
try {
System.out.println("Tentando iniciar o Servidor XML-RPC...");
WebServer server = new WebServer(8000);
XmlRpcServer servidor = server.getXmlRpcServer(); // Pega o servidor XmlRpc
PropertyHandlerMapping phm = new PropertyHandlerMapping();
phm.addHandler("Divisor", ServerDivisaoRPC.class); // Adiciona um novo "handler" ao PHM
servidor.setHandlerMapping(phm); // Define o handler no servidor
server.start(); // inicia o servidor.
System.out.println("Iniciado com Sucesso.");
System.out.println("Aceitando requisições. (Finalize o programa para parar.)");
} catch (Exception exception) {
System.err.println("JavaServer: " + exception);
}
}
}
import org.apache.xmlrpc.server.PropertyHandlerMapping;
import org.apache.xmlrpc.server.XmlRpcServer;
import org.apache.xmlrpc.webserver.*;
public class ServerMultiplicacaoRPC {
public ServerMultiplicacaoRPC() {
}
public Integer multiplicacao(int x, int y) {
return new Integer(x * y);
}
public static void main(String[] args) {
try {
System.out.println("Tentando iniciar o Servidor XML-RPC...");
WebServer server = new WebServer(3400);
XmlRpcServer servidor = server.getXmlRpcServer(); // Pega o servidor XmlRpc
PropertyHandlerMapping phm = new PropertyHandlerMapping();
phm.addHandler("Multiplicador", ServerMultiplicacaoRPC.class); // Adiciona um novo "handler" ao PHM
servidor.setHandlerMapping(phm); // Define o handler no servidor
server.start(); // inicia o servidor.
System.out.println("Iniciado com Sucesso.");
System.out.println("Aceitando requisições. (Finalize o programa para parar.)");
} catch (Exception exception) {
System.err.println("JavaServer: " + exception);
}
}
}
import org.apache.xmlrpc.server.PropertyHandlerMapping;
import org.apache.xmlrpc.server.XmlRpcServer;
import org.apache.xmlrpc.webserver.*;
public class ServerSomaRPC {
public ServerSomaRPC() {
}
public Integer soma(int x, int y) {
return new Integer(x + y);
}
public static void main(String[] args) {
try {
System.out.println("Tentando iniciar o Servidor XML-RPC...");
WebServer server = new WebServer(9000);
XmlRpcServer servidor = server.getXmlRpcServer(); // Pega o servidor XmlRpc
PropertyHandlerMapping phm = new PropertyHandlerMapping();
phm.addHandler("Somador", ServerSomaRPC.class); // Adiciona um novo "handler" ao PHM
servidor.setHandlerMapping(phm); // Define o handler no servidor
server.start(); // inicia o servidor.
System.out.println("Iniciado com Sucesso.");
System.out.println("Aceitando requisições. (Finalize o programa para parar.)");
} catch (Exception exception) {
System.err.println("JavaServer: " + exception);
}
}
}
import org.apache.xmlrpc.server.PropertyHandlerMapping;
import org.apache.xmlrpc.server.XmlRpcServer;
import org.apache.xmlrpc.webserver.*;
public class ServerSubtracaoRPC {
public ServerSubtracaoRPC() {
}
public Integer subtracao(int x, int y) {
return new Integer(x - y);
}
public static void main(String[] args) {
try {
System.out.println("Tentando iniciar o Servidor XML-RPC...");
WebServer server = new WebServer(51549);
XmlRpcServer servidor = server.getXmlRpcServer(); // Pega o servidor XmlRpc
PropertyHandlerMapping phm = new PropertyHandlerMapping();
phm.addHandler("Subtrador", ServerSubtracaoRPC.class); // Adiciona um novo "handler" ao PHM
servidor.setHandlerMapping(phm); // Define o handler no servidor
server.start(); // inicia o servidor.
System.out.println("Iniciado com Sucesso.");
System.out.println("Aceitando requisições. (Finalize o programa para parar.)");
} catch (Exception exception) {
System.err.println("JavaServer: " + exception);
}
}
}