Advertisement
Guest User

ProgramaPrincipal

a guest
May 26th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.36 KB | None | 0 0
  1. package ProgramaPrincipal;
  2.  
  3. import java.util.Scanner;
  4. import TDAArbolBinario.*;
  5. import TDAArbolBinario.BoundaryViolationException;
  6. import TDAArbolBinario.InvalidPositionException;
  7. import TDAArbolBinario.Position;
  8. import TDACola.*;
  9. import TDALista.*;
  10. import TDAMapeo.*;
  11. import TDAPila.*;
  12.  
  13. public class ProgramaPrincipal {
  14.  
  15. protected static TablaHashAbierta<String, Integer> variables = new TablaHashAbierta<String, Integer>();
  16. protected static ArbolBinario<String> expresion = null;
  17. public static void main(String[] args) {
  18.  
  19.  
  20.  
  21.  
  22. Scanner sc = new Scanner(System.in);
  23. int eleccion = -1;
  24.  
  25. while (eleccion != 0) {
  26.  
  27. System.out.println("Elija una opcion: ");
  28. System.out.println("1) Definir una nueva variable y asignarle un valor.");
  29. System.out.println("2) Mostrar la variables definidas junto a su valor.");
  30. System.out.println("3) Introducir una expresion matematica.");
  31. System.out.println("4) Visualizar la expresion en notacion prefija.");
  32. System.out.println("5) Visualizar la expresion en notacion infija.");
  33. System.out.println("6) Visualizar la expresion en notacion postfijate.");
  34. System.out.println("7) Evaluar la expresion y mostrar el resultado.");
  35. System.out.println("8) Reemplazar termino.");
  36. System.out.println("9) Mostrar el arbol binario.");
  37.  
  38. eleccion = sc.nextInt();
  39.  
  40.  
  41.  
  42.  
  43. if (eleccion == 2) {
  44. programa2(variables);
  45. }
  46.  
  47.  
  48.  
  49. if (eleccion == 6) {
  50. try {
  51. programa6(expresion, expresion.root());
  52. System.out.println();
  53. } catch (EmptyTreeException e) {
  54. // TODO Auto-generated catch block
  55. e.printStackTrace();
  56. }
  57. }
  58. if (eleccion == 7) {
  59. try {
  60. System.out.println(evaluar(expresion,expresion.root(),variables));
  61. } catch (EmptyTreeException e) {
  62. // TODO Auto-generated catch block
  63. e.printStackTrace();
  64. }
  65. }
  66.  
  67. }
  68.  
  69. }
  70. public boolean AgregarMapeo(String s,int n){
  71. return programa1(variables,s,n);
  72.  
  73. }
  74. static boolean programa1(TablaHashAbierta<String, Integer> variables,String s,int n) {
  75.  
  76. int valor=n;
  77. String variable=s;
  78. boolean correcto=true;
  79.  
  80.  
  81. try {
  82. Integer aux = variables.put(variable, valor);
  83. if (aux != null) {
  84. variables.put(variable, aux);
  85. correcto=false;
  86. System.out.println("Ya existe esa variable.");
  87. }
  88. } catch (InvalidKeyException e) {
  89. System.out.println("Variable nula.");
  90. e.printStackTrace();
  91. }
  92. return correcto;
  93. }
  94. public String mostrarMapeo(){
  95. return (programa2(variables));
  96. }
  97. static String programa2(Map<String, Integer> variables) {
  98. String s="";
  99. Iterable<Entry<String, Integer>> entradas = variables.entries();
  100. for (Entry<String, Integer> entrada : entradas) {
  101. s=s+entrada.getKey() +" = "+ entrada.getValue()+", ";
  102. }
  103. return s;
  104. }
  105.  
  106.  
  107. /** Recibe una expresion aritmetica por medio de un string y la transforma a un arbol binario
  108. * @param cad
  109. * @return expresion
  110. *
  111. * **/
  112. static ArbolBinario<String> programa3(String cad) {
  113.  
  114.  
  115. char[] arr = cad.toCharArray();
  116.  
  117. NodeQueue<String> cola = new NodeQueue<String>();
  118. PilaEnlazada<Character> pila = new PilaEnlazada<Character>();
  119. ArbolBinario<String> expresion = new ArbolBinario<String>();
  120. try {
  121. char c;
  122. int i = 0;
  123. while (i < arr.length) {
  124. c = arr[i];
  125.  
  126. if (c == '*' || c == '/' || c == '+' || c == '-' || c == '^' || c == '(') {
  127. pila.push(c);
  128. i++;
  129. } else {
  130. if (c == ')') {
  131. while (!pila.isEmpty() && pila.top() != '(') {
  132. cola.enqueue("" + pila.pop());
  133. }
  134. pila.pop();
  135. i++;
  136. } else {
  137. String var = "";
  138. while (!(c == '*' || c == '/' || c == '+' || c == '-' || c == '^' || c == '(' || c == ')')) {
  139. var = var + c;
  140. i++;
  141. c = arr[i];
  142. }
  143. cola.enqueue(var);
  144. }
  145.  
  146. }
  147.  
  148. }
  149. System.out.println(cola);
  150. PilaEnlazada<ArbolBinario<String>> elePend = new PilaEnlazada<ArbolBinario<String>>();
  151. while (!cola.isEmpty()) {
  152.  
  153. String s = cola.dequeue();
  154. if (!(s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/") || s.equals("^"))) {
  155.  
  156. ArbolBinario<String> arb = new ArbolBinario<String>();
  157. arb.createRoot(s);
  158. elePend.push(arb);
  159.  
  160. } else {
  161.  
  162. ArbolBinario<String> arb = new ArbolBinario<String>();
  163. arb.createRoot(s);
  164. ArbolBinario<String> der = elePend.pop();
  165. ArbolBinario<String> izq = elePend.pop();
  166. // arb.Attach(arb.root(), izq, der);
  167. arb.Attach(arb.root(), izq, der);
  168. elePend.push(arb);
  169.  
  170. }
  171.  
  172. }
  173. expresion = elePend.pop();
  174.  
  175.  
  176. } catch (EmptyStackException | EmptyQueueException | InvalidOperationException | InvalidPositionException
  177. | EmptyTreeException e) {
  178. // TODO Auto-generated catch block
  179. e.printStackTrace();
  180. }
  181.  
  182.  
  183. return expresion;
  184. }
  185.  
  186. /**
  187. *
  188. * @param expresion
  189. * @param r
  190. * @return expresion en prefijo
  191. */
  192. public String programa4(ArbolBinario<String> expresion, Position<String> r) {
  193. String toReturn= r.element();
  194. try {
  195.  
  196.  
  197. if (expresion.hasLeft(r)) {
  198. toReturn=toReturn+programa4(expresion, expresion.left(r));
  199. }
  200. if (expresion.hasRight(r)) {
  201. toReturn=toReturn+programa4(expresion, expresion.right(r));
  202. }
  203.  
  204. } catch (InvalidPositionException | BoundaryViolationException e) {
  205. // TODO Auto-generated catch block
  206. e.printStackTrace();
  207. }
  208. return toReturn;
  209.  
  210. }
  211.  
  212. public String programa5(ArbolBinario<String> expresion, Position<String> r) {
  213. String toReturn=r.element();
  214. try {
  215. if (expresion.hasLeft(r)) {
  216. if (!(expresion.left(r).element().equals("+") || expresion.left(r).element().equals("-")
  217. || expresion.left(r).element().equals("*") || expresion.left(r).element().equals("/")
  218. || expresion.left(r).element().equals("^"))) {
  219. //System.out.print("(");
  220. toReturn = "("+programa5(expresion, expresion.left(r))+toReturn;
  221.  
  222.  
  223. }
  224. else{
  225. toReturn=programa5(expresion, expresion.left(r))+toReturn;
  226. }
  227. }
  228. //System.out.print(r.element());
  229.  
  230. if (expresion.hasRight(r)) {
  231. toReturn=toReturn+programa5(expresion, expresion.right(r));
  232. if (!(expresion.right(r).element().equals("+") || expresion.right(r).element().equals("-")
  233. || expresion.right(r).element().equals("*") || expresion.right(r).element().equals("/")
  234. || expresion.right(r).element().equals("^"))) {
  235. //System.out.print(")");
  236. toReturn = toReturn+")";
  237.  
  238. }
  239. }
  240. } catch (InvalidPositionException | BoundaryViolationException e) {
  241. // TODO Auto-generated catch block
  242. e.printStackTrace();
  243. }
  244.  
  245. return toReturn;
  246.  
  247. }
  248.  
  249. public static String programa6(ArbolBinario<String> expresion, Position<String> r) {
  250. String toReturn=r.element();
  251. try {
  252. if (expresion.hasRight(r)) {
  253. toReturn=programa6(expresion, expresion.right(r))+toReturn;
  254. }
  255. if (expresion.hasLeft(r)) {
  256. toReturn=programa6(expresion, expresion.left(r))+toReturn;
  257. }
  258.  
  259.  
  260. //System.out.print(r.element());
  261. } catch (InvalidPositionException | BoundaryViolationException e) {
  262. // TODO Auto-generated catch block
  263. e.printStackTrace();
  264. }
  265. return toReturn;
  266. }
  267.  
  268. public static String programa7(ArbolBinario<String> expresion, TablaHashAbierta<String, Integer> variables) {
  269. String resultado = "";
  270. try {
  271. resultado = resultado + evaluar(expresion, expresion.root(), variables);
  272. } catch (EmptyTreeException e) {
  273. // TODO Auto-generated catch block
  274. e.printStackTrace();
  275. }
  276. return resultado;
  277. }
  278.  
  279.  
  280. public String calcular(ArbolBinario<String>e, Position<String> r){
  281. return(programa7(e,variables));
  282. }
  283.  
  284. public static int evaluar(ArbolBinario<String> expresion, Position<String> r,TablaHashAbierta<String,Integer> variables){
  285. int toReturn = 0;
  286. try{
  287. if(!(r.element().equals("+") || r.element().equals("-") ||r.element().equals("*") ||r.element().equals("/") ||r.element().equals("^"))){
  288. toReturn = variables.get(r.element());
  289. }
  290. else{
  291. if(r.element().equals("+")){
  292. toReturn = evaluar(expresion, expresion.left(r), variables)+evaluar(expresion, expresion.right(r), variables);
  293. }
  294. if(r.element().equals("-")){
  295. toReturn = evaluar(expresion, expresion.left(r), variables)-evaluar(expresion, expresion.right(r), variables);
  296. }
  297. if(r.element().equals("/")){
  298. toReturn = evaluar(expresion, expresion.left(r), variables)/evaluar(expresion, expresion.right(r), variables);
  299. }
  300. if(r.element().equals("*")){
  301. toReturn = evaluar(expresion, expresion.left(r), variables)*evaluar(expresion, expresion.right(r), variables);
  302. }
  303. if(r.element().equals("^")){
  304. toReturn = evaluar(expresion, expresion.left(r), variables)^evaluar(expresion, expresion.right(r), variables);
  305. }
  306. }
  307. } catch (InvalidKeyException | InvalidPositionException | BoundaryViolationException e) {
  308. // TODO Auto-generated catch block
  309. e.printStackTrace();
  310. }
  311. return toReturn;
  312. }
  313.  
  314. public boolean buscar(String s){
  315. boolean aux=false;
  316.  
  317. Iterable<Entry<String, Integer>> entradas = variables.entries();
  318. for(Entry<String,Integer> nombres:entradas){
  319. if(nombres.getKey()==s)
  320. aux=true;
  321. else
  322. aux=false;
  323. }
  324. return aux;
  325. }
  326. public void llamarPrograma8(ArbolBinario<String> expresion,Queue<String> l){
  327.  
  328.  
  329.  
  330. programa8(expresion,variables,l);
  331. System.out.println("Se reemplazo correctamente");
  332. }
  333. public void programa8(ArbolBinario<String> expresion, TablaHashAbierta<String,Integer> variables, Queue<String> l){
  334. try {
  335. if(expresion.altura()>1){
  336. reemplazar(expresion, expresion.root(),variables,l);
  337. }
  338. } catch (EmptyTreeException e) {
  339. // TODO Auto-generated catch block
  340. e.printStackTrace();
  341. }
  342.  
  343. }
  344.  
  345.  
  346.  
  347. protected void reemplazar(ArbolBinario<String> expresion,Position<String> r, TablaHashAbierta<String,Integer> variables,Queue<String>l ){
  348.  
  349. try {
  350. if(expresion.isExternal(expresion.left(r))){
  351. String var=l.dequeue();
  352. int val = evaluar(expresion, r, variables);
  353. expresion.remove(expresion.left(r));
  354. expresion.remove(expresion.right(r));
  355. expresion.replace(r, var);
  356. variables.put(var, val);
  357.  
  358. }
  359. else{
  360. reemplazar(expresion, expresion.left(r), variables,l);
  361. reemplazar(expresion, expresion.right(r), variables,l);
  362. }
  363. } catch (InvalidPositionException |EmptyQueueException| BoundaryViolationException | InvalidOperationException | InvalidKeyException e) {
  364. // TODO Auto-generated catch block
  365. e.printStackTrace();
  366. }
  367.  
  368.  
  369. }
  370.  
  371. public int altura(ArbolBinario<String> t){
  372. int aux=-1;
  373. try {
  374. aux= (t.altura());
  375. } catch (EmptyTreeException e) {
  376. // TODO Auto-generated catch block
  377. e.printStackTrace();
  378. }
  379. return aux;
  380. }
  381. public int cantNodos(ArbolBinario<String> e ){
  382. return e.size();
  383. }
  384.  
  385. public int cantHojas(ArbolBinario<String> e){
  386.  
  387. return (e.cantHojas());
  388. }
  389.  
  390. public int cantInternos(ArbolBinario<String> e){
  391. return e.cantInternos();
  392. }
  393.  
  394.  
  395. public boolean esPropio(ArbolBinario<String> e){
  396. return e.esPropio();
  397. }
  398. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement