Foro de elhacker.net

Programación => Java => Mensaje iniciado por: .mokk. en 8 Abril 2011, 19:58 pm



Título: MEF SUmatoria de Bases
Publicado por: .mokk. en 8 Abril 2011, 19:58 pm
Bueno esto es un pequeño projecto que dejaron de tarea en la escuela, que trata sobre una Sumatoria de bases en Maquina de Estado Finito(MEF), al principio fue solamente en base 2, pero luego nos pidio en base 3, entonces lo que haciamos era la matriz manualmente, entonces fue que pense mejor en que el usuario meta la base que decee y automaticamente cree la matriz.

Bueno aqui dejo una imagen sobre lo que vendria siendo una Maquina de Estado Finito
(http://3.bp.blogspot.com/_js6wgtUcfdQ/R37rPP0aUlI/AAAAAAAABk4/07WVB4rFTXY/s400/maquina_de_estado_finito.png)

Y ya de ahi se crean las Tablas F & G, F son las entradas y a donde nos enviaran y G, las salidas que obtendriamos
Bueno ya sin mas aqui les dejo mi code, por si les interesa n_n, o algo que sea de utilidad.

CODIGO

Clase Consola:
Código
  1. package MEF;
  2. /**
  3.  * @Author Victor
  4.  */
  5.  
  6. public class Consola
  7. {
  8.  
  9. public static void main(String[] args)
  10. {
  11.  
  12. BASE Obj = new BASE();
  13. //Obj.convertirbybase(0);
  14. int base = 0;
  15. String a="0",b="0";
  16. do{
  17. base = read.dataInt("Teclee Base 2 - 9: ") ;
  18. }while(base < 2);
  19.  
  20. String [][] f = Obj.Llenar(base, 0);
  21. //String [][] g = Obj.Llenar(base, 0);
  22. String [][] g = Obj.Llenar(base, 1);
  23. Obj.SOP(Obj.imprimir(base, f, g));
  24.  
  25. Obj.SOP("\n\n");
  26.  
  27. a = read.data("Teclee Primer valor: ");
  28. b = read.data("Teclee Segundo valor: ");
  29.  
  30. String suma = Obj.suma(a, b, f, g);
  31. Obj.SOP("\nLa Suma de " + a + " y " + b + " es igual a: " + suma);
  32.  
  33. }
  34.  
  35. }
  36.  

Clase BASE
Código
  1. package MEF;
  2. /**
  3.  *
  4.  * @author Victor
  5.  */
  6. public class BASE
  7. {
  8.    int pos = 1;
  9. public String suma(String a, String b, String [][] f, String [][] g)
  10. {
  11. String n,nn;
  12. String r = "";
  13. while(a.length() > b.length())
  14. {
  15. b = "0" + b;
  16. }
  17. while(a.length() < b.length())
  18. {
  19. a = "0" + a;
  20. }
  21.  
  22. for(int x = (a.length()-1); x>=0; x--)
  23. {
  24. n = String.valueOf(a.charAt(x)) + "" + String.valueOf(b.charAt(x));
  25. for(int t = 0; t<f[0].length; t++)
  26. {
  27. nn = (f[0][t]);
  28. if(n.equals(nn))
  29. {
  30. r = valor(t,f,g) + r;
  31. }
  32. }
  33. }
  34.  
  35. if(pos == 2)
  36. r = "1"+r;
  37.  
  38. return r;
  39.  
  40. }
  41.  
  42. private String valor(int columna,String [][] f,String [][] g)
  43. {
  44. String m = "";
  45. if (pos == 1)
  46. {
  47. if(f[1][columna] != "SA")
  48. {
  49. pos = 2;
  50. }
  51. m = g[1][columna];
  52. }else{
  53. if(f[2][columna] == "SA")
  54. {
  55. pos = 1;
  56. }
  57. m = g[2][columna];
  58. }
  59. return m;
  60. }
  61.  
  62. public String imprimir(int base,String [][] f, String [][] g)
  63. {
  64. String k = "";
  65. k = k + ("\nTABLA F - BASE:" + base+"\n");
  66.  
  67. for(int x = 0; x<3; x++)
  68. {
  69. if(x == 1)
  70. k = k + ("SA|");
  71. else if(x == 2)
  72. k = k + ("CA|");
  73. else
  74. k = k + ("EI|");
  75. for(int xx = 0; xx<(base*base); xx++)
  76. {
  77. k = k + (f[x][xx] + "|");
  78. }
  79.  
  80. k = k + ("\n");
  81. }
  82.  
  83. k = k + ("\nTABLA G - BASE:" + base+"\n");
  84. for(int x = 0; x<3; x++)
  85. {
  86. if(x == 1)
  87. k = k + ("SA|");
  88. else if(x == 2)
  89. k = k + ("CA|");
  90. else
  91. k = k + ("EI|");
  92. for(int xx = 0; xx<(base*base); xx++)
  93. {
  94. while(g[0][xx].length() > g[x][xx].length())
  95. {
  96. g[x][xx] = " " + g[x][xx];
  97. }
  98. k = k + (g[x][xx] + "|");
  99. }
  100. k = k + ("\n");
  101. }
  102.  
  103. return k;
  104. }
  105.  
  106.  
  107.  
  108. //Llenando la matriz
  109. public String [][] Llenar(int base, int tipo)
  110. {
  111. //Creamos una matriz la cual luego llenaremos
  112. String [][] m = new String[2][base*base];
  113. int n=0,xx = 0;
  114. //Vemos que matriz es la que llenaremos f=0 o g=1
  115. if(tipo == 0)
  116. {
  117. String [][] f = new String[3][base*base];
  118.  
  119. for(int x = 0; x<3; x++)
  120. {
  121. xx = 0;
  122. for(int i = 0; i<base; i++)
  123. {
  124. for(int ii = 0; ii<base; ii++)
  125. {
  126. if(x == 1)
  127. {
  128. if((i+ii) < base)
  129. f[x][xx] = "SA";
  130. else
  131. f[x][xx] = "CA";
  132. xx++;;
  133. }else if(x == 2){
  134. if(((i+ii)+1) < base)
  135. f[x][xx] = "SA";
  136. else
  137. f[x][xx] = "CA";
  138. xx++;;
  139. }else{
  140. f[x][n] = convertirnumero(i) + "" + convertirnumero(ii);
  141. n++;
  142. }
  143. }
  144. }
  145. }
  146. m = f;
  147. }else{
  148. String [][] g = new String[3][base*base];
  149.  
  150. for(int x = 0; x<3; x++)
  151. {
  152. xx = 0;
  153. for(int i = 0; i<base; i++)
  154. {
  155. for(int ii = 0; ii<base; ii++)
  156. {
  157. if(x == 1)
  158. {
  159. if((i+ii) < base)
  160. g[x][xx] = convertirnumero(i+ii);
  161. else
  162. g[x][xx] = convertirnumero(g(base,i,ii));
  163. xx++;;
  164. }else if(x == 2){
  165. if(((i+ii)+1) < base)
  166. g[x][xx] = convertirnumero((i+ii+1));
  167. else
  168. g[x][xx] = convertirnumero(g1(base,i,ii));
  169. xx++;;
  170. }else{
  171. g[x][n] = convertirnumero(i) + "" + convertirnumero(ii);
  172. n++;
  173. }
  174. }
  175. }
  176. }
  177. m = g;
  178. }
  179. return m;
  180. }
  181.  
  182. public void SOP(String msg)
  183. {
  184. System.out.print(msg);
  185. }
  186.  
  187. private static int g(int base, int i, int ii)
  188. {
  189. return (int)((i+ii)-base);
  190. }
  191.  
  192. private static int g1(int base, int i, int ii)
  193. {
  194. return (int)((i+ii+1)-base);
  195. }
  196.  
  197. public static String convertirnumero(int i)
  198. {
  199. //System.out.println("\n" + i);
  200. String [] letras = {"A", "B", "C" , "D", "E", "F", "G", "H"};
  201. for(int n = 0; n<(n+letras.length); n++)
  202. {
  203. if(i == n)
  204. {
  205. if(n >= 10)
  206. return letras[n-10];
  207. else
  208. return String.valueOf(n);
  209. }
  210. }
  211. return "0";
  212. }
  213.  
  214. }
  215.  

Clase read
Código
  1. package MEF;
  2. /**
  3.  *
  4.  * @author cryptt3r
  5.  */
  6.  
  7. import java.io.*;
  8. public class read
  9. {
  10. public static boolean error;
  11. public static String data(String message) {
  12.    String sdato = "";
  13.    System.out.print(message);
  14.    try {
  15.      // Definir un flujo de caracteres de entrada: flujoE
  16.      BufferedReader flujoE = new BufferedReader(isr);
  17.      // Leer. La entrada finaliza al pulsar la tecla Entrar
  18.      sdato = flujoE.readLine();
  19.    }
  20.    catch(IOException e) {
  21.      System.err.println("Error: " + e.getMessage());
  22.    }
  23.    return sdato; // devolver el dato tecleado
  24.  }
  25. public static int dataInt(String message){
  26.    try {
  27.     error=false;
  28.     int val=Integer.parseInt(data(message));
  29.     if (val<-32768||val>32767)
  30.     error=true;
  31.      return val;
  32.    }
  33.    catch(NumberFormatException e) {
  34.      return Integer.MIN_VALUE; // valor m&#225;s peque&#241;o
  35.    }
  36. }
  37.  
  38. public static short datoShort(String message){
  39.   try {
  40. return Short.parseShort(data(message));
  41. }
  42. return Short.MIN_VALUE;
  43. }
  44. }
  45.  
  46.  
  47.  
  48. public static long dataLong(String message){
  49. try {
  50. return Long.parseLong(data(message));
  51. }
  52. return Long.MIN_VALUE;
  53. }
  54. }
  55.  
  56. public static float dataFloat(String message){
  57. try {
  58. Float f = new Float(data(message));
  59. return f.floatValue();
  60. }
  61. return Float.NaN;
  62. }
  63. }
  64.  
  65. public static double dataDouble(String message){
  66. try {
  67. Double d = new Double(data(message));
  68. return d.doubleValue();
  69. }
  70. return Double.NaN;
  71. }
  72. }
  73. /*public static char dataChar(String message){
  74. try {
  75. Char d = new Char(data(message));
  76. return d.charValue();
  77. }
  78. catch (NumberFormatException e){
  79. return Char.NaN;
  80. }
  81. }*/
  82.  
  83. }
  84.  

Solamente me falto, hacer la comprobacion de que los numeros que ingresemos ninguno sea mayor a la base, sorry se me paso eso, pero lo pueden hacer ahi mientras leen.