Advertisement
s00rk

Sumatoria de Bases (MEF)

Sep 24th, 2011
656
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.23 KB | None | 0 0
  1. Bueno pues anteriormente trabaje esto pero en Java, cuando veia sobre "Maquina de Estado Finito", lo que hice fue una clase llamada Base que con ella me daba las bases que ocupara desde la base 2 a la 16
  2. Mas sobre [url=http://"http://es.wikipedia.org/wiki/M%C3%A1quina_de_estados"]MEF[/url]
  3.  
  4. Bueno para no decir mucho aqui te lo dejo talvez te sirva para que veas:
  5.  
  6. [code=java]
  7. package MEF;
  8.  
  9. /**
  10. * @author Victor
  11. */
  12. public class BASE
  13.  
  14. {
  15.  
  16. int pos = 1;
  17.  
  18. public String suma(String a, String b, String [][] f, String [][] g)
  19.  
  20. {
  21.  
  22. String n,nn;
  23.  
  24. String r = "";
  25.  
  26. while(a.length() > b.length())
  27.  
  28. {
  29.  
  30. b = "0" + b;
  31.  
  32. }
  33.  
  34. while(a.length() < b.length())
  35.  
  36. {
  37.  
  38. a = "0" + a;
  39.  
  40. }
  41.  
  42.  
  43.  
  44. for(int x = (a.length()-1); x>=0; x--)
  45.  
  46. {
  47.  
  48. n = String.valueOf(a.charAt(x)) + "" + String.valueOf(b.charAt(x));
  49.  
  50. for(int t = 0; t<f[0].length; t++)
  51.  
  52. {
  53.  
  54. nn = (f[0][t]);
  55.  
  56. if(n.equals(nn))
  57.  
  58. {
  59.  
  60. r = valor(t,f,g) + r;
  61.  
  62. }
  63.  
  64. }
  65.  
  66. }
  67.  
  68.  
  69.  
  70. if(pos == 2)
  71.  
  72. r = "1"+r;
  73.  
  74.  
  75.  
  76. return r;
  77.  
  78.  
  79.  
  80. }
  81.  
  82.  
  83.  
  84. private String valor(int columna,String [][] f,String [][] g)
  85.  
  86. {
  87.  
  88. String m = "";
  89.  
  90. if (pos == 1)
  91.  
  92. {
  93.  
  94. if(f[1][columna] != "SA")
  95.  
  96. {
  97.  
  98. pos = 2;
  99.  
  100. }
  101.  
  102. m = g[1][columna];
  103.  
  104. }else{
  105.  
  106. if(f[2][columna] == "SA")
  107.  
  108. {
  109.  
  110. pos = 1;
  111.  
  112. }
  113.  
  114. m = g[2][columna];
  115.  
  116. }
  117.  
  118. return m;
  119.  
  120. }
  121.  
  122.  
  123.  
  124. public String imprimir(int base,String [][] f, String [][] g)
  125.  
  126. {
  127.  
  128. String k = "";
  129.  
  130. k = k + ("\nTABLA F - BASE:" + base+"\n");
  131.  
  132.  
  133.  
  134. for(int x = 0; x<3; x++)
  135.  
  136. {
  137.  
  138. if(x == 1)
  139.  
  140. k = k + ("SA|");
  141.  
  142. else if(x == 2)
  143.  
  144. k = k + ("CA|");
  145.  
  146. else
  147.  
  148. k = k + ("EI|");
  149.  
  150. for(int xx = 0; xx<(base*base); xx++)
  151.  
  152. {
  153.  
  154. k = k + (f[x][xx] + "|");
  155.  
  156. }
  157.  
  158.  
  159.  
  160. k = k + ("\n");
  161.  
  162. }
  163.  
  164.  
  165.  
  166. k = k + ("\nTABLA G - BASE:" + base+"\n");
  167.  
  168. for(int x = 0; x<3; x++)
  169.  
  170. {
  171.  
  172. if(x == 1)
  173.  
  174. k = k + ("SA|");
  175.  
  176. else if(x == 2)
  177.  
  178. k = k + ("CA|");
  179.  
  180. else
  181.  
  182. k = k + ("EI|");
  183.  
  184. for(int xx = 0; xx<(base*base); xx++)
  185.  
  186. {
  187.  
  188. while(g[0][xx].length() > g[x][xx].length())
  189.  
  190. {
  191.  
  192. g[x][xx] = " " + g[x][xx];
  193.  
  194. }
  195.  
  196. k = k + (g[x][xx] + "|");
  197.  
  198. }
  199.  
  200. k = k + ("\n");
  201.  
  202. }
  203.  
  204.  
  205.  
  206. return k;
  207.  
  208. }
  209.  
  210.  
  211.  
  212.  
  213.  
  214. //Llenando la matriz
  215.  
  216. public String [][] Llenar(int base, int tipo)
  217.  
  218. {
  219.  
  220. //Creamos una matriz la cual luego llenaremos
  221.  
  222. String [][] m = new String[2][base*base];
  223.  
  224. int n=0,xx = 0;
  225.  
  226. //Vemos que matriz es la que llenaremos f=0 o g=1
  227.  
  228. if(tipo == 0)
  229.  
  230. {
  231.  
  232. String [][] f = new String[3][base*base];
  233.  
  234.  
  235.  
  236. for(int x = 0; x<3; x++)
  237.  
  238. {
  239.  
  240. xx = 0;
  241.  
  242. for(int i = 0; i<base; i++)
  243.  
  244. {
  245.  
  246. for(int ii = 0; ii<base; ii++)
  247.  
  248. {
  249.  
  250. if(x == 1)
  251.  
  252. {
  253.  
  254. if((i+ii) < base)
  255.  
  256. f[x][xx] = "SA";
  257.  
  258. else
  259.  
  260. f[x][xx] = "CA";
  261.  
  262. xx++;;
  263.  
  264. }else if(x == 2){
  265.  
  266. if(((i+ii)+1) < base)
  267.  
  268. f[x][xx] = "SA";
  269.  
  270. else
  271.  
  272. f[x][xx] = "CA";
  273.  
  274. xx++;;
  275.  
  276. }else{
  277.  
  278. f[x][n] = convertirnumero(i) + "" + convertirnumero(ii);
  279.  
  280. n++;
  281.  
  282. }
  283.  
  284. }
  285.  
  286. }
  287.  
  288. }
  289.  
  290. m = f;
  291.  
  292. }else{
  293.  
  294. String [][] g = new String[3][base*base];
  295.  
  296.  
  297.  
  298. for(int x = 0; x<3; x++)
  299.  
  300. {
  301.  
  302. xx = 0;
  303.  
  304. for(int i = 0; i<base; i++)
  305.  
  306. {
  307.  
  308. for(int ii = 0; ii<base; ii++)
  309.  
  310. {
  311.  
  312. if(x == 1)
  313.  
  314. {
  315.  
  316. if((i+ii) < base)
  317.  
  318. g[x][xx] = convertirnumero(i+ii);
  319.  
  320. else
  321.  
  322. g[x][xx] = convertirnumero(g(base,i,ii));
  323.  
  324. xx++;;
  325.  
  326. }else if(x == 2){
  327.  
  328. if(((i+ii)+1) < base)
  329.  
  330. g[x][xx] = convertirnumero((i+ii+1));
  331.  
  332. else
  333.  
  334. g[x][xx] = convertirnumero(g1(base,i,ii));
  335.  
  336. xx++;;
  337.  
  338. }else{
  339.  
  340. g[x][n] = convertirnumero(i) + "" + convertirnumero(ii);
  341.  
  342. n++;
  343.  
  344. }
  345.  
  346. }
  347.  
  348. }
  349.  
  350. }
  351.  
  352. m = g;
  353.  
  354. }
  355.  
  356. return m;
  357.  
  358. }
  359.  
  360.  
  361.  
  362. public void SOP(String msg)
  363.  
  364. {
  365.  
  366. System.out.print(msg);
  367.  
  368. }
  369.  
  370.  
  371.  
  372. private static int g(int base, int i, int ii)
  373.  
  374. {
  375.  
  376. return (int)((i+ii)-base);
  377.  
  378. }
  379.  
  380.  
  381.  
  382. private static int g1(int base, int i, int ii)
  383.  
  384. {
  385.  
  386. return (int)((i+ii+1)-base);
  387.  
  388. }
  389.  
  390.  
  391.  
  392. public static String convertirnumero(int i)
  393.  
  394. {
  395.  
  396. //System.out.println("\n" + i);
  397.  
  398. String [] letras = {"A", "B", "C" , "D", "E", "F", "G", "H"};
  399.  
  400. for(int n = 0; n<(n+letras.length); n++)
  401.  
  402. {
  403.  
  404. if(i == n)
  405.  
  406. {
  407.  
  408. if(n >= 10)
  409.  
  410. return letras[n-10];
  411.  
  412. else
  413.  
  414. return String.valueOf(n);
  415.  
  416. }
  417.  
  418. }
  419.  
  420. return "0";
  421.  
  422. }
  423.  
  424.  
  425.  
  426. }
  427. [/code]
  428.  
  429. Ahora desde el main lo utilizamos asi:
  430. [code=java]
  431. package MEF;
  432.  
  433. /**
  434. * @author Victor
  435. */
  436. public class MEF
  437. {
  438.  
  439. public static void main(String[] args)
  440. {
  441.  
  442. BASE Obj = new BASE();
  443. int base = 0;
  444. String a="0",b="0";
  445. //do{
  446. base = read.dataInt("Teclee Base 2 - 9: ") ;
  447. //}while(base < 2 || base > 9);
  448.  
  449. String [][] f = Obj.Llenar(base, 0);
  450. String [][] g = Obj.Llenar(base, 1);
  451.  
  452. //Esto solo para imprimir las tablas de F y G
  453. System.out.println(Obj.imprimir(base, f, g));
  454.  
  455. Obj.SOP("\n\n");
  456.  
  457. a = read.data("Teclee Primer valor: ");
  458. b = read.data("Teclee Segundo valor: ");
  459.  
  460. String suma = Obj.suma(a, b, f, g);
  461. Obj.SOP("\nLa Suma de " + a + " y " + b + " es igual a: " + suma);
  462. }
  463.  
  464. }
  465.  
  466. [/code]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement