Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: Beginner Web en 14 Octubre 2018, 22:40 pm



Título: ayuda para invertir un numero
Publicado por: Beginner Web en 14 Octubre 2018, 22:40 pm
Hola tengo que invertir un numero utilizando colas el problema es que cuando ingreso numeros con mas de 2 cigras me devuelve datos erroneos , si ingreso un 23 devuelve 32, si ingreso 321 me devuelve 1203 y asi, no se si mi algoritmo esta mal planteado o que, el "algoritmo" esta en el programa principal, desde ya gracias
Ya lo solucione  ;-)

Código
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. using namespace std;
  6.  
  7. const int MAX=20;
  8. typedef int contenedor[MAX];
  9. typedef struct tcola{
  10. contenedor datos;
  11. int final, frente;
  12. };
  13.  
  14. void init_queue(tcola &q);
  15. void push_queue(tcola &q, int nuevo);
  16. bool full_queue(tcola q);
  17. bool empty_queue(tcola q);
  18. int pop_queue(tcola &q);
  19. int top_queue(tcola q);
  20. int bottom_queue(tcola q);
  21. int next(int indice);
  22.  
  23. int main()
  24. {
  25. int numero, inverso=0, i=-1;
  26. tcola q;
  27. init_queue(q);
  28. cout<<"Ingrese numero: ";
  29. cin>>numero;
  30. while(numero>0){
  31. push_queue(q,numero%10);
  32. numero/=10;
  33. i++;
  34. }
  35. while(empty_queue(q)==false){
  36. inverso+=pop_queue(q)*pow(10.0.i);
  37. i--;
  38. }
  39. cout<<"Inverso: "<<inverso<<endl;
  40. system("pause");
  41. }
  42.  
  43. void init_queue(tcola &q)
  44. {
  45. q.final=MAX-1;
  46. q.frente=MAX-1;
  47. }
  48.  
  49. void push_queue(tcola &q, int nuevo)
  50. {
  51. if(full_queue(q)==true)
  52. cout<<"COLA LLENA"<<endl;
  53. else{
  54. q.final=next(q.final);
  55. q.datos[q.final]=nuevo;
  56. }
  57. }
  58.  
  59. bool full_queue(tcola q)
  60. {
  61. return next(q.final)==q.frente;
  62. }
  63.  
  64. bool empty_queue(tcola q)
  65. {
  66. return q.frente==q.final;
  67. }
  68.  
  69. int pop_queue(tcola &q)
  70. {
  71. int aux;
  72. if(empty_queue(q)==true)
  73. aux=-1;
  74. else{
  75. q.frente=next(q.frente);
  76. aux=q.datos[q.frente];
  77. }
  78. return aux;
  79. }
  80.  
  81. int top_queue(tcola q)
  82. {
  83. int aux;
  84. if(empty_queue(q)==true)
  85. aux=-1;
  86. else{
  87. aux=q.datos[next(q.frente)];
  88. }
  89. return aux;
  90. }
  91.  
  92. int bottom_queue(tcola q)
  93. {
  94. int aux;
  95. if(empty_queue(q)==true)
  96. aux=-1;
  97. else{
  98. aux=q.datos[q.final];
  99. }
  100. return aux;
  101. }
  102.  
  103. int next(int indice)
  104. {
  105. if(indice==MAX-1)
  106. indice=0;
  107. else
  108. indice++;
  109. return indice;
  110. }
  111.