problem in the pile

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2008
Posts: 1
Reputation: barbaruiva is an unknown quantity at this point 
Solved Threads: 0
barbaruiva barbaruiva is offline Offline
Newbie Poster

problem in the pile

 
0
  #1
Nov 19th, 2008
friends before any thing apologizes for English I am using translator because only of the brasil and I don't know how to write.
my problem is the following I have to create a program that reads a text and it prints the same text to the I thwart oh this that I did
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4.  
  5. #define max 10
  6.  
  7. typedef int TipoChave;
  8. typedef struct {
  9. char texto;
  10.  
  11. } TipoItem;
  12.  
  13. typedef struct Celula_str *Apontador;
  14.  
  15. typedef struct Celula_str {
  16. TipoItem Item;
  17. Apontador Prox;
  18. } Celula;
  19.  
  20. typedef struct {
  21. Apontador Fundo, Topo;
  22. int Tamanho;
  23. } TipoPilha;
  24.  
  25. void FPVazia(TipoPilha *Pilha)
  26. {
  27. Pilha->Topo = (Apontador) malloc(sizeof(Celula));
  28. Pilha->Fundo = Pilha->Topo;
  29. Pilha->Topo->Prox = NULL;
  30. Pilha->Tamanho = 0;
  31. } /* FPVazia */
  32.  
  33. int Vazia(TipoPilha Pilha)
  34. {
  35. return (Pilha.Topo == Pilha.Fundo);
  36. } /* Vazia */
  37.  
  38. void Empilha(TipoItem x, TipoPilha *Pilha)
  39. {
  40. Apontador Aux;
  41.  
  42. Aux = (Apontador) malloc(sizeof(Celula));
  43. Pilha->Topo->Item= x;
  44. Aux->Prox = Pilha->Topo;
  45. Pilha->Topo = Aux;
  46. Pilha->Tamanho++;
  47. } /* Empilha */
  48.  
  49.  
  50. void Desempilha(TipoPilha *Pilha, TipoItem *Item)
  51. {
  52. Apontador q;
  53.  
  54. if (Vazia(*Pilha))
  55. { printf(" Erro lista vazia\n");
  56. return;
  57. }
  58. q = Pilha->Topo;
  59. Pilha->Topo = q->Prox;
  60. *Item = q->Item;
  61. free(q);
  62. Pilha->Tamanho--;
  63. } /* Desempilha */
  64.  
  65.  
  66. int Tamanho(TipoPilha Pilha)
  67. {
  68. return (Pilha.Tamanho);
  69. } /* Tamanho */
  70. int main()
  71. {
  72. TipoPilha pilha;
  73. TipoItem item;
  74. Apontador p;
  75. FPVazia(&pilha);
  76. printf(" digite um texto");
  77. int i=0;
  78. char palavra[30];
  79. gets(palavra);
  80. while (palavra[i]!= '0'){for(i=0;palavra[i]!=(' 'and '0');i++){
  81. item.texto=palavra[i];
  82. Empilha(item, &pilha);}}
  83. while(!Vazia(pilha)){
  84.  
  85. printf(" %c \n", item.texto);
  86. Desempilha(&pilha,&item);
  87. if(palavra[i]!='0'){
  88. i++;}
  89. }
  90. getch();}
Last edited by Narue; Nov 19th, 2008 at 9:41 am. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,485
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: problem in the pile

 
0
  #2
Nov 20th, 2008
1) learn to properly format your code to make it easier to read and understand.

2) The for loop on line 80 is not formed correcly
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #pragma warning(disable: 4996)
  5.  
  6. #define max 10
  7.  
  8. typedef int TipoChave;
  9. typedef struct {
  10. char texto;
  11.  
  12. } TipoItem;
  13.  
  14. typedef struct Celula_str *Apontador;
  15.  
  16. typedef struct Celula_str {
  17. TipoItem Item;
  18. Apontador Prox;
  19. } Celula;
  20.  
  21. typedef struct {
  22. Apontador Fundo, Topo;
  23. int Tamanho;
  24. } TipoPilha;
  25.  
  26. void FPVazia(TipoPilha *Pilha)
  27. {
  28. Pilha->Topo = (Apontador) malloc(sizeof(Celula));
  29. Pilha->Fundo = Pilha->Topo;
  30. Pilha->Topo->Prox = NULL;
  31. Pilha->Tamanho = 0;
  32. } /* FPVazia */
  33.  
  34. int Vazia(TipoPilha Pilha)
  35. {
  36. return (Pilha.Topo == Pilha.Fundo);
  37. } /* Vazia */
  38.  
  39. void Empilha(TipoItem x, TipoPilha *Pilha)
  40. {
  41. Apontador Aux;
  42.  
  43. Aux = (Apontador) malloc(sizeof(Celula));
  44. Pilha->Topo->Item= x;
  45. Aux->Prox = Pilha->Topo;
  46. Pilha->Topo = Aux;
  47. Pilha->Tamanho++;
  48. } /* Empilha */
  49.  
  50.  
  51. void Desempilha(TipoPilha *Pilha, TipoItem *Item)
  52. {
  53. Apontador q;
  54.  
  55. if (Vazia(*Pilha))
  56. { printf(" Erro lista vazia\n");
  57. return;
  58. }
  59. q = Pilha->Topo;
  60. Pilha->Topo = q->Prox;
  61. *Item = q->Item;
  62. free(q);
  63. Pilha->Tamanho--;
  64. } /* Desempilha */
  65.  
  66.  
  67. int Tamanho(TipoPilha Pilha)
  68. {
  69. return (Pilha.Tamanho);
  70. } /* Tamanho */
  71. int main()
  72. {
  73. TipoPilha pilha;
  74. TipoItem item;
  75. // Apontador p;
  76. FPVazia(&pilha);
  77. printf(" digite um texto");
  78. int i=0;
  79. char palavra[30];
  80. fgets(palavra, sizeof(palavra), stdin);
  81. while (palavra[i]!= '0')
  82. {
  83. for(i=0; palavra[i]!=' ' && palavra[i]!='0';i++)
  84. {
  85. item.texto=palavra[i];
  86. Empilha(item, &pilha);}}
  87. while(!Vazia(pilha))
  88. {
  89. printf(" %c \n", item.texto);
  90. Desempilha(&pilha,&item);
  91. if(palavra[i]!='0')
  92. {
  93. i++;
  94. }
  95. }
  96. getch();
  97. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC