Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #define STACKSIZE 20
  5.  
  6. typedef int info_t;
  7.  
  8. typedef struct s {
  9. info_t S[STACKSIZE];
  10. int top;
  11. } stack;
  12.  
  13. void Init(stack * m) {
  14. m->top = -1;
  15. }
  16.  
  17. void StackOverflow(void) {
  18. fprintf(stderr, "ERROR: StackOverflow\n");
  19. exit(-1);
  20. }
  21.  
  22. void StackUnderflow(void) {
  23. fprintf(stderr, "ERROR: StackUnderflow\n");
  24. exit(-1);
  25. }
  26.  
  27. void Push(stack *m, info_t x) {
  28. if (m->top >= STACKSIZE - 1)
  29. StackOverflow();
  30. m->S[++(m->top)] = x;
  31. }
  32.  
  33. info_t Peek(stack * m) {
  34. if (m->top<0)
  35. StackUnderflow();
  36. return (m->S[(m->top)]);
  37. }
  38.  
  39. info_t Pop(stack *m) {
  40. if (m->top<0)
  41. StackUnderflow();
  42. return(m->S[(m->top)--]);
  43. }
  44.  
  45. int isEmpty(stack *m) {
  46. return((m->top)<0);
  47. }
  48.  
  49. void dek_vobin(int n,stack *m)
  50. {
  51. Init(m);
  52. while(n!=0)
  53. {
  54. Push(m,n%2);
  55. n=n/2;
  56. }
  57. while(!isEmpty(m))
  58. {
  59. printf("%d",Pop(m));
  60. }
  61. }
  62.  
  63. int main()
  64. {
  65. stack *m;
  66. Init(m);
  67. int n;
  68. scanf("%d",&n);
  69. dek_vobin(n,m);
  70. return(0);
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement