/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication25;
import java.util.Scanner;
/**
*
* @author KevinPiazzoli
*/
public class JavaApplication25 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner leer = new Scanner(System.in);
String sC = "";
do{
System.out.println("Ingresa 3 cadenas con numeros y una X en cada cadena, las cadenas seran separadas por un espacio, no pongas mas de 3 cadenas ni menos de 3 cadenas");
sC = leer.nextLine();
}while(ver3cadenas(sC)==3);
System.out.println("Gracias, ahora espera a que se resuelva que operacion con las 3 cadenas que ingresaste");
String cadenas[] = separarCadenas(sC);
System.out.println(ej1(cadenas[0],cadenas[1],cadenas[2]));
}
public static String ej1(CharSequence c1, CharSequence c2, CharSequence c3){
String[] cadena1 = encontrarX(c1);
String[] cadena2 = encontrarX(c2);
String[] cadena3 = encontrarX(c3);
int positionX1 = positionX(cadena1);
int positionX2 = positionX(cadena2);
int positionX3 = positionX(cadena3);
String rSuma = verificarSuma(cadena1,cadena2,cadena3,positionX1,positionX2,positionX3);
if(!rSuma.isEmpty())return rSuma;
else{
String rResta = verificarSuma(cadena1,cadena2,cadena3,positionX1,positionX2,positionX3);
if(!rResta.isEmpty())return rResta;
else{
String rMulti = verificarMultiplicacion(cadena1,cadena2,cadena3,positionX1,positionX2,positionX3);
if(!rMulti.isEmpty())return rMulti;
else{
String rDiv = verificarDivision(cadena1,cadena2,cadena3,positionX1,positionX2,positionX3);
if(!rDiv.isEmpty())return rDiv;
}
}
}
return "No existe solucion";
}
public static String verificarSuma(String[] cadena1,String[] cadena2,String[] cadena3,int positionX1,int positionX2,int positionX3){
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
for(int k=0;k<10;k++){
if((returnNum(cadena1,positionX1,k)
+returnNum(cadena2,positionX2,j))==
returnNum(cadena3,positionX3,i)){
return ""+returnNum(cadena1,positionX1,k)+"+"+
returnNum(cadena2,positionX2,j)+"="+
returnNum(cadena3,positionX3,i);
}
}
}
}
return "";
}
public static String verificarResta(String[] cadena1,String[] cadena2,String[] cadena3,int positionX1,int positionX2,int positionX3){
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
for(int k=0;k<10;k++){
if((returnNum(cadena1,positionX1,k)
-returnNum(cadena2,positionX2,j))==
returnNum(cadena3,positionX3,i)){
return ""+returnNum(cadena1,positionX1,k)+"-"+
returnNum(cadena2,positionX2,j)+"="+
returnNum(cadena3,positionX3,i);
}
}
}
}
return "";
}
public static String verificarMultiplicacion(String[] cadena1,String[] cadena2,String[] cadena3,int positionX1,int positionX2,int positionX3){
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
for(int k=0;k<10;k++){
if((returnNum(cadena1,positionX1,k)
*returnNum(cadena2,positionX2,j))==
returnNum(cadena3,positionX3,i)){
return ""+returnNum(cadena1,positionX1,k)+"*"+
returnNum(cadena2,positionX2,j)+"="+
returnNum(cadena3,positionX3,i);
}
}
}
}
return "";
}
public static String verificarDivision(String[] cadena1,String[] cadena2,String[] cadena3,int positionX1,int positionX2,int positionX3){
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
for(int k=0;k<10;k++){
if((returnNum(cadena1,positionX1,k)
/returnNum(cadena2,positionX2,j))==
returnNum(cadena3,positionX3,i)){
return ""+returnNum(cadena1,positionX1,k)+"/"+
returnNum(cadena2,positionX2,j)+"="+
returnNum(cadena3,positionX3,i);
}
}
}
}
return "";
}
public static int returnNum(String[] cadena1, int positionX,int num){
String cadenaNumerica = "";
for(int i=0;i<cadena1.length;i++){
if(positionX==i-1) cadenaNumerica = cadenaNumerica + num;
else cadenaNumerica = cadenaNumerica + cadena1[i];
}
return Integer.parseInt(cadenaNumerica);
}
public static int positionX(String[] cadena1){
int positionX = 0;
for(int i=0;i<cadena1.length;i++){
if(cadena1[i].compareTo("x")=='x'){
positionX = i;
break;
}
}
return positionX;
}
public static String[] encontrarX(CharSequence cS){
String arrayCadena[] = new String[3];
String cadena = "";
for(int i=0;i<cS.length();i++){
if(cS.charAt(i)=='x' || cS.charAt(i)=='X'){
arrayCadena[0] = cadena;
arrayCadena[1] = "x";
i++;
String cadena3 = "";
for(;i<cS.length();i++){
cadena3 = cadena3+cS.charAt(i);
}
arrayCadena[2] = cadena3;
break;
}else{
cadena = cadena+cS.charAt(i);
}
}
return arrayCadena;
}
public static int ver3cadenas(CharSequence string){
int espacios = 0;
for(int i=0;i<string.length();i++){
if(string.charAt(i)==' ') espacios++;
}
return espacios;
}
public static String[] separarCadenas(CharSequence string){
String arrayCadena[] = new String[3];
String cadena = "";
int position = 0;
for(int i=0;i<string.length();i++){
if(string.charAt(i)==' '){
arrayCadena[position] = cadena;
cadena = "";
position++;
}else{
cadena = cadena+string.charAt(i);
}
}
arrayCadena[2] = cadena;
return arrayCadena;
}
}
Grupo de Facebook
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.