elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Usando Git para manipular el directorio de trabajo, el índice y commits (segunda parte)


  Mostrar Mensajes
Páginas: [1]
1  Programación / Programación C/C++ / Árbol en: 14 Diciembre 2015, 23:45 pm
Hola, quisiera saber como se podría agregar una cadena, que acepte cadena y entero, los dos; por ejemplo:
-Ingrese nombre:
pedro
-Ingrese edad:
10
y que se visualice como: pedro-10

Les dejo el código por si tienen alguna idea o la solución :P
Gracias :D

Código
  1. #include <iostream>
  2. #include<cctype>
  3. #include <stdlib.h>
  4. #include <conio.h>
  5.  
  6. using namespace std;
  7.  
  8. struct node
  9. {
  10. int element;
  11. node *left;
  12. node *right;
  13. int altura;
  14. };
  15. typedef struct node *nodeptr;
  16. class bsarbol
  17. {
  18. public:
  19. void insert(int,nodeptr &);
  20. void del(int, nodeptr &);
  21. int deletemin(nodeptr &);
  22. void find(int,nodeptr &);
  23. nodeptr findmin(nodeptr);
  24. nodeptr findmax(nodeptr);
  25. void makeempty(nodeptr &);
  26. void copy(nodeptr &,nodeptr &);
  27. nodeptr nodecopy(nodeptr &);
  28. void preorder(nodeptr);
  29. void inorder(nodeptr);
  30. void postorder(nodeptr);
  31. int bsheight(nodeptr);
  32. nodeptr srl(nodeptr &);
  33. nodeptr drl(nodeptr &);
  34. nodeptr srr(nodeptr &);
  35. nodeptr drr(nodeptr &);
  36. int max(int,int);
  37. int nonodes(nodeptr);
  38. };
  39. // Inserting a node
  40. void bsarbol::insert(int x,nodeptr &p)
  41. {
  42. if (p == NULL)
  43. {
  44. p = new node;
  45. p->element = x;
  46. p->left=NULL;
  47. p->right = NULL;
  48. p->altura=0;
  49. if (p==NULL)
  50. {
  51. cout<<"Fuera de rango\n"<<endl;
  52. }
  53. }
  54. else
  55. {
  56. if (x<p->element)
  57. {
  58. insert(x,p->left);
  59. if ((bsheight(p->left) - bsheight(p->right))==2)
  60. {
  61. if (x < p->left->element)
  62. {
  63. p=srl(p);
  64. }
  65. else
  66. {
  67. p = drl(p);
  68. }
  69. }
  70. }
  71. else if (x>p->element)
  72. {
  73. insert(x,p->right);
  74. if ((bsheight(p->right) - bsheight(p->left))==2)
  75. {
  76. if (x > p->right->element)
  77. {
  78. p=srr(p);
  79. }
  80. else
  81. {
  82. p = drr(p);
  83. }
  84. }
  85. }
  86. else
  87. {
  88. cout<<"El elemento existe\n"<<endl;
  89. }
  90. }
  91. int m,n,d;
  92. m=bsheight(p->left);
  93. n=bsheight(p->right);
  94. d=max(m,n);
  95. p->altura = d + 1;
  96. }
  97. // Finding the Smallest
  98. nodeptr bsarbol::findmin(nodeptr p)
  99. {
  100. if (p==NULL)
  101. {
  102. cout<<"El arbol esta vacio\n"<<endl;
  103. return p;
  104. }
  105. else
  106. {
  107. while(p->left !=NULL)
  108. {
  109. p=p->left;
  110. //return p;
  111. }
  112. return p;
  113. }
  114. }
  115. // Finding the Largest node
  116. nodeptr bsarbol::findmax(nodeptr p)
  117. {
  118. if (p==NULL)
  119. {
  120. cout<<"El arbol esta vacio\n"<<endl;
  121. return p;
  122. }
  123. else
  124. {
  125. while(p->right !=NULL)
  126. {
  127. p=p->right;
  128. //return p;
  129. }
  130. return p;
  131. }
  132. }
  133. // Finding an element
  134. void bsarbol::find(int x,nodeptr &p)
  135. {
  136. if (p==NULL)
  137. {
  138. cout<<"Elemento no encontrado \n"<<endl;
  139. }
  140. else
  141. {
  142. if (x < p->element)
  143. {
  144. find(x,p->left);
  145. }
  146. else
  147. {
  148. if (x>p->element)
  149. {
  150. find(x,p->right);
  151. }
  152. else
  153. {
  154. cout<<"Elemento encontrado\n"<<endl;
  155. }
  156. }
  157. }
  158. }
  159. // Copy a tree
  160. void bsarbol::copy(nodeptr &p,nodeptr &p1)
  161. {
  162. makeempty(p1);
  163. p1 = nodecopy(p);
  164. }
  165. // Make a tree empty
  166. void bsarbol::makeempty(nodeptr &p)
  167. {
  168. nodeptr d;
  169. if (p != NULL)
  170. {
  171. makeempty(p->left);
  172. makeempty(p->right);
  173. d=p;
  174. free(d);
  175. p=NULL;
  176. }
  177. }
  178. // Copy the nodes
  179. nodeptr bsarbol::nodecopy(nodeptr &p)
  180. {
  181. nodeptr temp;
  182. if (p==NULL)
  183. {
  184. return p;
  185. }
  186. else
  187. {
  188. temp = new node;
  189. temp->element = p->element;
  190. temp->left = nodecopy(p->left);
  191. temp->right = nodecopy(p->right);
  192. return temp;
  193. }
  194. }
  195.  
  196. // Deleting a node
  197. void bsarbol::del(int x,nodeptr &p)
  198. {
  199. nodeptr d;
  200. if (p==NULL)
  201. {
  202. cout<<"Elemnto no encontrado\n"<<endl;
  203. }
  204. else if ( x < p->element)
  205. {
  206. del(x,p->left);
  207. }
  208. else if (x > p->element)
  209. {
  210. del(x,p->right);
  211. }
  212. else if ((p->left == NULL) && (p->right == NULL))
  213. {
  214. d=p;
  215. free(d);
  216. p=NULL;
  217. cout<<"Elemento borrado\n"<<endl;
  218. }
  219. else if (p->left == NULL)
  220. {
  221. d=p;
  222. free(d);
  223. p=p->right;
  224. cout<<"Elemento borrado\n"<<endl;
  225. }
  226. else if (p->right == NULL)
  227. {
  228. d=p;
  229. p=p->left;
  230. free(d);
  231. cout<<"Elemento borrado\n"<<endl;
  232. }
  233. else
  234. {
  235. p->element = deletemin(p->right);
  236. }
  237. }
  238.  
  239. int bsarbol::deletemin(nodeptr &p)
  240. {
  241. int c;
  242. cout<<"inside deltemin\n"<<endl;
  243. if (p->left == NULL)
  244. {
  245. c=p->element;
  246. p=p->right;
  247. return c;
  248. }
  249. else
  250. {
  251. c=deletemin(p->left);
  252. return c;
  253. }
  254. }
  255. void bsarbol::preorder(nodeptr p)
  256. {
  257. if (p!=NULL)
  258. {
  259. cout<<p->element<<"\t";
  260. preorder(p->left);
  261. preorder(p->right);
  262. }
  263. }
  264.  
  265. // Inorder Printing
  266. void bsarbol::inorder(nodeptr p)
  267. {
  268. if (p!=NULL)
  269. {
  270. inorder(p->left);
  271. cout<<p->element<<"\t";
  272. inorder(p->right);
  273. }
  274. }
  275.  
  276. // PostOrder Printing
  277. void bsarbol::postorder(nodeptr p)
  278. {
  279. if (p!=NULL)
  280. {
  281. postorder(p->left);
  282. postorder(p->right);
  283. cout<<p->element<<"\t";
  284. }
  285. }
  286.  
  287. int bsarbol::max(int value1, int value2)
  288. {
  289. return ((value1 > value2) ? value1 : value2);
  290. }
  291. int bsarbol::bsheight(nodeptr p)
  292. {
  293. int t;
  294. if (p == NULL)
  295. {
  296. return -1;
  297. }
  298. else
  299. {
  300. t = p->altura;
  301. return t;
  302. }
  303. }
  304.  
  305. nodeptr bsarbol:: srl(nodeptr &p1)
  306. {
  307. nodeptr p2;
  308. p2 = p1->left;
  309. p1->left = p2->right;
  310. p2->right = p1;
  311. p1->altura = max(bsheight(p1->left),bsheight(p1->right)) + 1;
  312. p2->altura = max(bsheight(p2->left),p1->altura) + 1;
  313. return p2;
  314. }
  315. nodeptr bsarbol:: srr(nodeptr &p1)
  316. {
  317. nodeptr p2;
  318. p2 = p1->right;
  319. p1->right = p2->left;
  320. p2->left = p1;
  321. p1->altura = max(bsheight(p1->left),bsheight(p1->right)) + 1;
  322. p2->altura = max(p1->altura,bsheight(p2->right)) + 1;
  323. return p2;
  324. }
  325. nodeptr bsarbol:: drl(nodeptr &p1)
  326. {
  327. p1->left=srr(p1->left);
  328. return srl(p1);
  329. }
  330. nodeptr bsarbol::drr(nodeptr &p1)
  331. {
  332. p1->right = srl(p1->right);
  333. return srr(p1);
  334. }
  335.  
  336. int bsarbol::nonodes(nodeptr p)
  337. {
  338. int count=0;
  339. if (p!=NULL)
  340. {
  341. nonodes(p->left);
  342. nonodes(p->right);
  343. count++;
  344. }
  345. return count;
  346. }
  347.  
  348. int main()
  349. {
  350. //clrscr();
  351. nodeptr root,root1,min,max;//,flag;
  352. int a,op,findele,delele;
  353. char ch='y';
  354. bsarbol bst;
  355.  
  356. //system("clear");
  357. root = NULL;
  358. root1=NULL;
  359. cout<<"\n\t\t\t\tBIENVENIDO A AVL "<<endl;
  360. cout<<"\t\t\t\t:::::::::::::::::::\n"<<endl;
  361.  
  362. do
  363. {
  364. cout<<"\t\t::::::::::::::::::::::::::::::::::::::::::::::::"<<endl;
  365. cout<<"\t\t::::Ingrese 1 para nuevo nodo::::::::::::::::"<<endl;
  366. cout<<"\t\t::::Ingrese 2 para encontrar el minimo valor:::::::::::"<<endl;
  367. cout<<"\t\t::::Ingrese 3 para encontrar maximo valor:::::::::::::::"<<endl;
  368. cout<<"\t\t::::Ingrese 4 para buscar un valor:::::::::::::::::::"<<endl;
  369. cout<<"\t\t::::Ingrese 5 para borrar un valor:::::::::::::::::::"<<endl;
  370. cout<<"\t\t::::Ingrese 6 para mostrar Preorden:::::::::::::::::"<<endl;
  371. cout<<"\t\t::::Ingrese 7 para mostrar Inorden::::::::::::::::::"<<endl;
  372. cout<<"\t\t::::Ingrese 8 para mostara Postorden::::::::::::::::"<<endl;
  373. cout<<"\t\t::::Ingrese 9 para mostrar la altura del arbol:::"<<endl;
  374. cout<<"\t\t::::Ingrese 0 para salir:::::::::::::::::::::::::::::"<<endl;
  375. cout<<"\t\t::::::::::::::::::::::::::::::::::::::::::::::::\n"<<endl;
  376.  
  377. cout<<"\nIngrese una opcion: ";
  378. cin>>op;
  379.  
  380. switch(op)
  381. {
  382. case 1:
  383. cout<<"\n\t\tAGREGANDO NUEVO NODO"<<endl;
  384. cout<<"\t\t:::::::::::::\n"<<endl;
  385. cout<<"Ingrese un valor: ";
  386. cin>>a;
  387. bst.insert(a,root);
  388. cout<<"\nEl nuevo valor se ha agregado\n"<<endl;
  389. break;
  390. case 2:
  391. if (root !=NULL)
  392. {
  393. min=bst.findmin(root);
  394. cout<<"\nEl minimo elemento del arbol es: "<<min->element<<endl;
  395. }
  396. break;
  397. case 3:
  398. if (root !=NULL)
  399. {
  400. max=bst.findmax(root);
  401. cout<<"\nEl maximo elemento del arbol es: "<<max->element<<endl;
  402. }
  403. break;
  404. case 4:
  405. cout<<"\nNodo a buscar: ";
  406. cin>>findele;
  407. if (root != NULL)
  408. {
  409. bst.find(findele,root);
  410. }
  411. break;
  412. case 5:
  413. cout<<"\nNodo a borrar: ";
  414. cin>>delele;
  415. bst.del(delele,root);
  416. bst.inorder(root);
  417. cout<<endl;
  418. break;
  419. case 6:
  420. cout<<"\n\t\tPRE-ORDER "<<endl;
  421. bst.preorder(root);
  422. cout<<endl;
  423. break;
  424. case 7:
  425. cout<<"\n\t\tIN-ORDER "<<endl;
  426. bst.inorder(root);
  427. cout<<endl;
  428. break;
  429. case 8:
  430. cout<<"\n\t\tPOST ORDER "<<endl;
  431. bst.postorder(root);
  432. cout<<endl;
  433. break;
  434. case 9:
  435. cout<<"\n\t\ALTURA\n"<<endl;
  436. cout<<"La altura del arbol es: "<<bst.bsheight(root)<<endl;
  437.  
  438. break;
  439. case 0:
  440. cout<<"\n\tAdios\n"<<endl;
  441. break;
  442. default:
  443. cout<<"Intente otra vez\n"<<endl;
  444. break;
  445. }
  446. system("pause");
  447. system("cls");
  448. }while(op != 0);
  449. return 0;
  450. }
  451.  


Mod: Los códigos deben ir en etiquetas GeSHi
2  Programación / Programación C/C++ / Arreglo de apuntadores en: 1 Octubre 2015, 01:19 am
Hola, Cómo se podría realizar este programa con apuntadores, soy noob

 #include <iostream>
#include<string.h>
using namespace std;

int validar_letra(char cadena[50]);
main()
{
 char letra[80];
 int validar;
 cout<<"INGRESE UNA LETRA:";
 cin>>letra;
 validar=validar_letra(letra);
 if(validar==0)
 {
  cout<<"ES UNA LETRA VALIDA";
 }
 else
 {
  cout<<"Letra Incorrecta";
 }
 
}
int validar_letra(char cadena[50])
{
 int i=0,validar=0,j;
 j=strlen(cadena);
 while(i<j && validar==0)
 {
  if(isalpha(cadena)!=0)
  {
   i++;
  }
  else
  {
   validar=1;
  }
 }
  return validar;
}
3  Programación / Programación C/C++ / Re: ayuda con calculadora c++ en: 2 Septiembre 2015, 01:52 am
lo que necesito es que guarde un resultado y despues pregunte si se quiere utilizar ese resultado o no. Gracias

por ejemplo:

Elija una operación (solo las básicas)
5+3=8
¿Desea utilizar el resultado?
si
elija operación
resta
8 - otro numero y así
4  Programación / Programación C/C++ / ayuda con calculadora c++ en: 2 Septiembre 2015, 00:27 am
Hola quisiera saber como se podría hacer la función ANS de la calculadora.
El programa que me dejaron fue hacer una calculadora con las operaciones básicas y añadir si se desea utilizar el valor, que se dio de los cálculos anteriores, para realizar nuevos cálculos.

Gracias.
5  Programación / Programación C/C++ / Ayuda con un simple programa en: 23 Agosto 2015, 03:54 am
Hola, estoy aprendiendo c++ y tengo duda con este ejercicio.

escribir un programa que acepte un  numero de tres digitos escrito en palabra y los visualice como un valor de tipo entero. la entrada se termina con un punto, utilizando if o switch

ejem:

doscientos veinticinco.

salida

225


Gracias, no pido el programa ya hecho solo que me guíen para hacerlo, gracias otra vez
Páginas: [1]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines