jaOjaa

stack program

Mar 5th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. void clrscr() { system("@cls||clear");}
  7.  
  8. struct Node{
  9. int stock;
  10. char genre[8];
  11. struct Node *next;
  12. };
  13.  
  14. void printList(struct Node *n) {
  15. if(n!=NULL&&n->stock>0){
  16. printf("[ %-14s | %-3d song(s) ] --> [top]\n",n->genre,n->stock);
  17. n=n->next;
  18. }
  19. while(n != NULL){
  20. if(n->stock==0){
  21. n=n->next;
  22. }else{
  23. printf("[ %-14s | %-3d song(s) ]\n",n->genre,n->stock);
  24. n=n->next;
  25. }
  26. }
  27. }
  28. int main(){
  29. struct Node* head= NULL;
  30. head=(struct Node*)malloc(sizeof(struct Node));
  31. head->next=NULL;
  32. head->stock=0;
  33. int ulang=1;
  34. int counter=0;
  35. do{
  36. clrscr();
  37. printf("JACKIE CD STOCK\n");
  38. printf("_______________\n\n");
  39. printf(" CD Stock <STACK>\n");
  40. printList(head);
  41. printf("\n\nOption :\n");
  42. printf("1. Stock of CD\n");
  43. printf("2. Sell a CD\n");
  44. printf("3. Exit\n");
  45. int choice;
  46. do{
  47. printf("\n>>> Input choice : ");
  48. scanf("%d",&choice);
  49. if(choice<1||choice>3){
  50. printf("\t\t\t[!] Menu Unavaiable!");
  51. }
  52. }while(choice<1||choice>3);
  53. clrscr();
  54. switch(choice){
  55. case 1:{
  56. if(counter==0){
  57. char cdgenre[8];
  58. int rep=0;
  59. do{
  60. printf("Input CD Genre [rock/jazz/blues] : ");
  61. scanf("%s",cdgenre);
  62. if(strcmp("rock",cdgenre)==0||strcmp("jazz",cdgenre)==0||strcmp("blues",cdgenre)==0){
  63. rep=1;
  64. }
  65. }while(rep==0);
  66. int song_number;
  67. rep=0;
  68. do{
  69. printf("\nInput the number of songs on the CD [5...12 song<s>] : ");
  70. scanf("%d",&song_number);
  71. if(song_number>4&&song_number<13){
  72. rep=1;
  73. }
  74. }while(rep==0);
  75. if(strcmp("rock",cdgenre)==0){
  76. strcpy(head->genre,"rock CD");
  77. }
  78. if(strcmp("jazz",cdgenre)==0){
  79. strcpy(head->genre,"jazz CD");
  80. }
  81. if(strcmp("blues",cdgenre)==0){
  82. strcpy(head->genre,"blues CD");
  83. }
  84. head->stock=song_number;
  85. counter++;
  86. printf("\n--- Add CD Success ---");
  87. getch();
  88. }else{
  89. if(counter>0&&counter<10){
  90. struct Node* top= NULL;
  91. top=(struct Node*)malloc(sizeof(struct Node));
  92. top->next=head;
  93. head=top;
  94. char cdgenre[8];
  95. int rep=0;
  96. do{
  97. printf("Input CD Genre [rock/jazz/blues] : ");
  98. scanf("%s",cdgenre);
  99. if(strcmp("rock",cdgenre)==0||strcmp("jazz",cdgenre)==0||strcmp("blues",cdgenre)==0){
  100. rep=1;
  101. }
  102. }while(rep==0);
  103. int song_number;
  104. rep=0;
  105. do{
  106. printf("\nInput the number of songs on the CD [5...12 song<s>] : ");
  107. scanf("%d",&song_number);
  108. if(song_number>4&&song_number<13){
  109. rep=1;
  110. }
  111. }while(rep==0);
  112. if(strcmp("rock",cdgenre)==0){
  113. strcpy(head->genre,"rock CD");
  114. }
  115. if(strcmp("jazz",cdgenre)==0){
  116. strcpy(head->genre,"jazz CD");
  117. }
  118. if(strcmp("blues",cdgenre)==0){
  119. strcpy(head->genre,"blues CD");
  120. }
  121. head->stock=song_number;
  122. counter++;
  123. printf("\n--- Add CD Success ---");
  124. getch();
  125. }else{
  126. printf("Stack Overflow!!");
  127. getch();
  128. }
  129. }
  130. break;
  131. }
  132. case 2:{
  133. if(counter==0){
  134. printf("--- The CD Storage is Empty ---");
  135. getch();
  136. }else{
  137. head=head->next;
  138. printf("--- Sell CD Success ---");
  139. counter--;
  140. getch();
  141. }
  142. break;
  143. }
  144. case 3:{
  145. ulang=0;
  146. break;
  147. }
  148. }
  149. }while(ulang==1);
  150. return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment