Advertisement
Guest User

Untitled

a guest
Nov 25th, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1. // intento2.cpp: define el punto de entrada de la aplicación de consola.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <cstdlib>
  6. #include <stdlib.h>
  7. #include <iostream>
  8.  
  9. using namespace std;
  10.  
  11.  
  12.  
  13. typedef struct tnodo *pnodo;
  14. typedef struct tnodo
  15. {
  16. int dato;
  17. pnodo izquierdo;
  18. pnodo derecho;
  19. };
  20.  
  21. int mostrarMenu();
  22. void iniciar(pnodo &x);
  23. void recorrer(pnodo a);
  24. pnodo crearNodo(pnodo nuevo);
  25. void agregar(pnodo &a, pnodo nuevo);
  26. void unirLosConjunto(pnodo a, pnodo b);
  27. void posorden(pnodo a);
  28. pnodo hacerInterseccion(pnodo x, pnodo y);
  29. bool verificarPertenencia(pnodo x, int valor);
  30. pnodo hacerDiferencia(pnodo x, pnodo y);
  31.  
  32.  
  33. int _tmain(int argc, _TCHAR* argv[])
  34. {
  35. pnodo A, B, nuevo, dif, inter;
  36. nuevo = NULL;
  37. dif = NULL;
  38. inter = NULL;
  39. bool bandera,bandera2, salir, per;
  40. salir = true;
  41. bandera = false;
  42. bandera2 = false;
  43. int opc, numero;
  44. char r, con;
  45.  
  46. while(salir == true){
  47. opc = mostrarMenu();
  48. if(opc == 1){
  49. iniciar(A);
  50. iniciar(B);
  51. bandera = true;
  52. cout << "CONJUNTOS INICIALIZADOS" << endl;
  53. }
  54. if((opc == 2)&&(bandera == true)){
  55. cout << "conjunto A" << endl;
  56. cout << "Continua? (S/N)" << endl;
  57. cin >> r;
  58. while((r == 's')||(r == 'S')){
  59. agregar(A, nuevo);
  60. bandera2 = true;
  61. cout << "Continua? (S/N)" << endl;
  62. cin >> r;
  63. }
  64. cout << "conjunto B" << endl;
  65. cout << "Continua? (S/N)" << endl;
  66. cin >> r;
  67. while((r == 's')||(r == 'S')){
  68. agregar(B, nuevo);
  69. bandera2 = true;
  70. cout << "Continua? (S/N)" << endl;
  71. cin >> r;
  72. }
  73. }
  74. if((opc == 3)&&(bandera2 == true)){
  75. recorrer(A);
  76. }
  77. if((opc == 4)&&(bandera2 == true)){
  78. unirLosConjunto(A, B);
  79. }
  80. if((opc == 5)&&(bandera2 == true)){
  81. inter = hacerInterseccion(A, B);
  82. recorrer(inter);
  83. }
  84. if((opc == 6)&&(bandera2 == true)){
  85. cout << "Ingrese el conjunto que quiere verificar la pertenencia de un valor" << endl;
  86. cin >> con;
  87. if((con == 'A') || (con == 'a')){
  88. cout << "ingrese el numero" << endl;
  89. cin >> numero;
  90. per = verificarPertenencia(A, numero);
  91. if(per == false)
  92. cout << "El dato no existe en l conjunto A" << endl;
  93. else
  94. cout << "El dato si existe en el conjunto A" << endl;
  95. }else{
  96. if((con == 'B') || (con == 'b')){
  97. cout << "ingrese el numero" << endl;
  98. cin >> numero;
  99. per = verificarPertenencia(B, numero);
  100. if(per == false)
  101. cout << "El dato no existe en l conjunto B" << endl;
  102. else
  103. cout << "El dato si existe en el conjunto B" << endl;
  104. }
  105. }
  106. }
  107. if((opc == 7)&&(bandera2 == true)){
  108. dif = hacerDiferencia(A, B);
  109. recorrer(dif);
  110. }
  111. if(opc == 8){
  112. cout << " Autor: Marina Soledad, Gonzalez Torres " << endl;
  113. cout << " Fecha: 26 de Noviembre de 2014 " << endl;
  114. salir = false;
  115. }
  116. }
  117.  
  118. return 0;
  119. }
  120.  
  121. int mostrarMenu(){
  122. int opc;
  123. cout << "************* MENU PRINCIPAL *************" << endl;
  124. cout << "1) Iniciar los conjuntos A y B" << endl;
  125. cout << "2) Asignar elementos a A y B" << endl;
  126. cout << "3) Mostrar los conjuntos A y B" << endl;
  127. cout << "4) Union de A y B" << endl;
  128. cout << "5) Interseccion de A y B" << endl;
  129. cout << "6) Pertenencia (conjunto A)" << endl;
  130. cout << "7) Diferencia entre A y B" << endl;
  131. cout << "8) Salir" << endl;
  132. cout << "************* MENU PRINCIPAL *************" << endl;
  133. cout << "ELIJA UNA OPCION" << endl;
  134. cin >> opc;
  135. return opc;
  136. }
  137.  
  138. void recorrer(pnodo a){
  139. if(a != NULL){
  140. cout << a->dato;
  141. recorrer(a->izquierdo);
  142. recorrer(a->derecho);
  143. }
  144. }
  145.  
  146. void iniciar(pnodo &x){
  147. x = NULL;
  148. }
  149.  
  150. void agregar(pnodo &a, pnodo nuevo){
  151. crearNodo(nuevo);
  152. if (a==NULL){
  153. a=nuevo;
  154. }
  155. else{
  156. if (nuevo->dato < a->dato)
  157. agregar(a->izquierdo, nuevo);
  158. else
  159. agregar(a->derecho, nuevo);
  160. }
  161. }
  162.  
  163. pnodo crearNodo(pnodo nuevo){
  164. nuevo = new tnodo;
  165. if(nuevo != NULL){
  166. cout << "Ingrese valor" << endl;
  167. cin >> nuevo->dato;
  168. nuevo->izquierdo = NULL;
  169. nuevo->derecho = NULL;
  170. }
  171. return nuevo;
  172. }
  173.  
  174. void unirLosConjunto(pnodo a, pnodo b){
  175. pnodo c;
  176. if((a != NULL)&&(b != NULL)){
  177. if(a->dato != b->dato){
  178. if(a->dato < b->dato){
  179. agregar(c, a);
  180. unirLosConjunto(a, b);
  181. }else{
  182. agregar(c, b);
  183. unirLosConjunto(a, b);
  184. }
  185. }
  186. }
  187. }
  188.  
  189. void posorden(pnodo a){
  190. if (a!=NULL){
  191. posorden(a->izquierdo);
  192. posorden(a->derecho);
  193. cout << a->dato;
  194. }
  195. }
  196.  
  197. pnodo hacerInterseccion(pnodo x, pnodo y){
  198. pnodo z;
  199. if((x != NULL)&&(y != NULL)){
  200. if(x->dato == y->dato){
  201. agregar(z, x);
  202. hacerInterseccion(x->izquierdo, y->izquierdo);
  203. hacerInterseccion(x->derecho, y->derecho);
  204. }else{
  205. hacerInterseccion(x->izquierdo, y->izquierdo);
  206. hacerInterseccion(x->derecho, y->derecho);
  207. }
  208. }
  209. return z;
  210. }
  211.  
  212. bool verificarPertenencia(pnodo x, int valor){
  213. bool pertenece = false;
  214. if(x->dato == valor)
  215. pertenece = true;
  216. else{
  217. recorrer(x);
  218. verificarPertenencia(x, valor);
  219. }
  220. return pertenece;
  221. }
  222.  
  223. pnodo hacerDiferencia(pnodo x, pnodo y){
  224. pnodo d;
  225. if(x != NULL){
  226. if(x->dato != y->dato){
  227. agregar(d, x);
  228. hacerDiferencia(x->izquierdo, y);
  229. hacerDiferencia(x->derecho, y);
  230. }else{
  231. hacerDiferencia(x->izquierdo, y);
  232. hacerDiferencia(x->derecho, y);
  233. }
  234. }
  235.  
  236. return d;
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement