• Miércoles 8 de Mayo de 2024, 03:14

Autor Tema:  PROBLEMA AL REALIZAR UN MENÚ EN C++  (Leído 1088 veces)

diavolo616

  • Nuevo Miembro
  • *
  • Mensajes: 1
    • Ver Perfil
PROBLEMA AL REALIZAR UN MENÚ EN C++
« en: Lunes 21 de Marzo de 2011, 19:16 »
0
Hola soy novato en programación y tengo una duda en este código, no se si me puedan ayudar, el problema es que no puedo hacerle un menú donde me de la opción de ingresar una matriz y otra donde me de la opción de salir del programa. Espero y me puedan ayudar!!!.

 
Este es el código... EL MENÚ QUE HICE ESTA AL FINAL PERO NO ME CORRE, ESPERO ME PUEDAN DECIR CUAL ES MI ERROR.... GRACIAS!!!!

Código: C++
  1. #include <iostream.h>
  2. #include <conio.h>
  3.  
  4. class matriz {
  5.  
  6. double **tabla,**tabla2;
  7. int dim;
  8. public:
  9. enum tipo_tabla{coef,ampliada,inversa};
  10. matriz(int);
  11. ~matriz();
  12. void llenar();
  13. void imprimir(int);
  14. bool invertir(int,bool);
  15. };
  16.  
  17. matriz::matriz(int n) {
  18. int i;
  19. dim=n;
  20. tabla=new double *[dim];
  21. for (i=0;i<dim;i++) *(tabla+i)=new double[dim*2];
  22. }
  23.  
  24. matriz::~matriz() {
  25. int i;
  26. for (i=0;i<dim;i++) delete [] *(tabla+i);
  27. delete [] tabla;
  28. for (i=0;i<dim;i++) delete [] *(tabla2+i);
  29. delete [] tabla2;
  30. }
  31.  
  32. void matriz::llenar() {
  33. int i,j;
  34. double num;
  35. for (i=0;i<dim;i++) {
  36. for (j=0;j<dim;j++) {
  37. cout<<"Ingresa un numero en la posicion ["<<i+1<<","<<j+1<<"]: ";
  38. cin>>num;
  39. *(*(tabla+i)+j)=num;
  40. }
  41. for (j=dim;j<dim*2;j++) *(*(tabla+i)+j)=(i==(j-dim))?(double)1:(double)0;
  42. }
  43. tabla2=new double *[dim];
  44. for (i=0;i<dim;i++) *(tabla2+i)=new double[dim*2];
  45. for (i=0;i<dim;i++)
  46. for (j=0;j<dim*2;j++) *(*(tabla2+i)+j)=*(*(tabla+i)+j);
  47. }
  48.  
  49. void matriz::imprimir(int t_tabla) {
  50. int i,j;
  51. if (t_tabla==coef) {
  52. for (i=0;i<dim;i++,cout<<endl)
  53. for (j=0;j<dim;j++) cout<<*(*(tabla+i)+j)<<"t";
  54. }
  55. else if (t_tabla==ampliada) {
  56. for (i=0;i<dim;i++,cout<<endl)
  57. for (j=0;j<dim*2;j++) cout<<*(*(tabla+i)+j)<<"t";
  58. }
  59. else {
  60. for (i=0;i<dim;i++,cout<<endl)
  61. for (j=dim;j<dim*2;j++) cout<<*(*(tabla2+i)+j)<<"t";
  62. }
  63. }
  64.  
  65. bool matriz::invertir(int pivote=0,bool ida=true) {
  66.  
  67. int i,j;
  68. double k;
  69. if (!*(*(tabla2+pivote)+pivote)) return false;
  70. else if (ida) {
  71. if (pivote==dim-1) return invertir(pivote,false);
  72. else {
  73. for (i=pivote;i<dim-1;i++) {
  74. k=*(*(tabla2+i+1)+pivote)/ *(*(tabla2+pivote)+pivote);
  75. for (j=0;j<dim*2;j++)
  76. *(*(tabla2+i+1)+j)=*(*(tabla2+i+1)+j)-*(*(tabla2+pivote)+j)*k;
  77. }
  78. return invertir(++pivote);
  79. }
  80. }
  81. else {
  82. if (pivote==0) { //Se llega al elemento a[1,1]
  83. k=1/ *(*(tabla2+pivote)+pivote);
  84. for (j=0;j<dim*2;j++)
  85. *(*(tabla2+pivote)+j)*=k;
  86. return true;
  87. }
  88. else {
  89. for (i=pivote;i>0;i--) {
  90. k=*(*(tabla2+i-1)+pivote)/ *(*(tabla2+pivote)+pivote);
  91. for (j=0;j<dim*2;j++)
  92. *(*(tabla2+i-1)+j)=*(*(tabla2+i-1)+j)-*(*(tabla2+pivote)+j)*k;
  93. k=1/ *(*(tabla2+pivote)+pivote);
  94. for (j=0;j<dim*2;j++)
  95. *(*(tabla2+pivote)+j)*=k;
  96.  
  97. }
  98. return invertir(--pivote,false);
  99. }
  100. }
  101. }
  102.  
  103. int main() {
  104. int dim, cont;
  105. matriz *tabla;
  106. cout<<"tttPROGRAMA QUE GENERA LA MATRIZ INVERSAnn";
  107. while (cont!=1){
  108. cout<<"1.- Ingresar Matrizn";
  109. cout<<"2.-Salirn";
  110. cout<<"Ingrese la opcion deseada:n ";
  111. cin>>cont;
  112. switch (cont){
  113. case 1:
  114.  
  115. void matriz::llenar();
  116. cout<<"Ingrese la dimension de la matriz: ";
  117. cin>>dim;
  118. tabla=new matriz(dim);
  119. tabla->llenar();
  120. cout<<endl<<"La matriz ingresada es:"<<endl;
  121. tabla->imprimir(matriz::coef);
  122. cout<<endl<<"La matriz ampliada es:"<<endl;
  123. tabla->imprimir(matriz::ampliada);
  124. if (tabla->invertir()) {
  125. // Si la matriz se pudo invertir la muestra
  126. cout<<endl<<"La matriz Inversa es:"<<endl;
  127. tabla->imprimir(matriz::inversa);
  128. }
  129. else cout<<endl<<"DISCULPA, ***La matriz no tiene inversa***"<<endl;
  130. getch();
  131. }
  132. }
  133. }
  134.  

Epa

  • Miembro MUY activo
  • ***
  • Mensajes: 242
  • Nacionalidad: ar
    • Ver Perfil
Re: PROBLEMA AL REALIZAR UN MENÚ EN C++
« Respuesta #1 en: Miércoles 23 de Marzo de 2011, 20:53 »
0
Buenas.

Para empezar al condicion del while esta mal, ya que la idea es que siga mientras que no se elija la opcion de salir, la que es 2.
Ademas tendrias que inicializar la variable cont.

Ademas no tendrias que usar 1 y 2, sino '1' y '2'

Y el switch lo podrias reemplazar por un if

Citar
int cont = 1;
...
while (cont != '2'){
...
if(cont == '1'){
...

Saludos
The sweet smell of a great sorrow lies over the land.