Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. /*
  2. initializeaza stiva
  3. urcare=1
  4. atata timp cat stiva nu e vida executa
  5. {
  6. - extrage valoarea 'some_value' din varful stivei
  7. - daca urcare=1 atunci - inalta stiva
  8. - la nou varf depune
  9. some_value-1
  10. - daca some_value-1=1 at
  11. urcare=0
  12. altfel
  13. - coboara stiva
  14. - la nou varf depune
  15. produsul valorilor de la varf curent
  16. cu cel anterior
  17. }
  18. afiseaza ultima valoare aflata pe stiva inainte de a
  19. deveni vida
  20. */
  21. #include<iostream>
  22. using namespace std;
  23. int stiva[100];
  24. int varf;
  25.  
  26. int push(int content[],int *top,int some)
  27. {
  28. if ((*top)+1>100) return 0;
  29. *top=*top+1;
  30. content[*top]=some;
  31. return 1;
  32. }
  33.  
  34. int pop(int content[],int *top,int *old)
  35. {
  36. if (*top>=1) {
  37. *old=content[*top];
  38. *top=*top-1;
  39. return 1;
  40. }
  41. return 0;
  42. }
  43.  
  44. int main()
  45. {
  46. int n;
  47. int urcare=1;
  48. cout<<" Input Amount =" ;
  49. cin>>n;
  50. // init stack
  51. varf=1;
  52. stiva[varf]=n;
  53. while (varf>=1)
  54. {
  55. cout<<endl<<varf;
  56. int some_value;
  57. pop(stiva,&varf,&some_value);
  58. push(stiva,&varf,some_value);
  59. if (urcare==1) {
  60. if (push(stiva,&varf,some_value-1))
  61. {
  62. if (some_value-1==1)
  63. urcare=0;
  64. }
  65. else return 1;
  66. }
  67. else {
  68. int some_top1;
  69. int some_top2;
  70. if (pop(stiva,&varf,&some_top1))
  71. {
  72. if (pop(stiva,&varf,&some_top2))
  73. {push(stiva,&varf,some_top1*some_top2);}
  74. }
  75. }
  76. } // atata timp cat nu e vida
  77. cout<<endl<<stiva[1];
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement