jaOjaa

tulung saya pusink

Mar 26th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4. #include <conio.h>
  5. #include <string.h>
  6.  
  7. void clrscr(){
  8. system("@cls||clear");
  9. }
  10. struct node{
  11. char name[20];
  12. int qty;
  13. int price;
  14. struct node *next;
  15. };
  16.  
  17. struct node *start=NULL;
  18. struct node *print(struct node*);
  19. struct node *add_node(struct node*);
  20. struct node *pop(struct node*);
  21.  
  22. int count=0,i;
  23.  
  24. int main(){
  25. int loop=1;
  26. do{
  27. clrscr();
  28. printf("BLUE MOTORCYCLE PARTS\n");
  29. printf(".....................\n\n");
  30. printf("1. View order list\n");
  31. printf("2. Add new order\n");
  32. printf("3. Take order\n");
  33. printf("4. Exit\n\n");
  34. int choice;
  35. do{
  36. printf("> Please choose menu : ");
  37. scanf("%d",&choice);
  38. }while(choice<1||choice>4);
  39. switch(choice){
  40. case 1:{
  41. clrscr();
  42. start=print(start);
  43. getch();
  44. break;
  45. }
  46. case 2:{
  47. clrscr();
  48. start=add_node(start);
  49. getch();
  50. break;
  51. }
  52. case 3:{
  53. clrscr();
  54. start=print(start);
  55. start=pop(start);
  56. getch();
  57. break;
  58. }
  59. case 4:{
  60. for(;count>0;count--){
  61. struct node *ptr;
  62. ptr=start;
  63. while(ptr->next!=start){
  64. ptr=ptr->next;
  65. }
  66. ptr->next=start->next;
  67. free(start);
  68. start=ptr->next;
  69. }
  70. loop=0;
  71. break;
  72. }
  73. }
  74. }while(loop==1);
  75. printf("Thank You");
  76. return 0;
  77. }
  78.  
  79. struct node *print(struct node *n){
  80. printf(" --- ITEM LIST---\n\n");
  81. printf("------+----------------------+--------------+----------------\n");
  82. printf("| No. | Name of Parts | Quantity | Price |\n");
  83. printf("------+----------------------+--------------+----------------\n");
  84. if(count>0){
  85. int i=1;
  86. struct node *ptr;
  87. ptr=n;
  88. if(count>1){
  89. printf("| %2d. | %20s | %12d | %13d | << Head\n",i,ptr->name,ptr->qty,ptr->price);
  90. ptr=ptr->next;
  91. i++;
  92. }
  93. while(ptr->next!=n){
  94. printf("| %2d. | %20s | %12d | %13d |\n",i,ptr->name,ptr->qty,ptr->price);
  95. ptr=ptr->next;
  96. i++;
  97. }
  98. if(count==1){
  99. printf("| %2d. | %20s | %12d | %13d | << Head Tail\n",i,ptr->name,ptr->qty,ptr->price);
  100. }else{
  101. printf("| %2d. | %20s | %12d | %13d | << Tail\n",i,ptr->name,ptr->qty,ptr->price);
  102. }
  103. }
  104. printf("------+----------------------+--------------+----------------\n");
  105. return n;
  106. }
  107.  
  108. struct node *add_node(struct node *start){
  109. struct node *new_node,*ptr;
  110. int pprice, qqty;
  111. char nname[31];
  112. do{
  113. printf("Input name of motorcycle's part [3..20]: ");
  114. fflush(stdin);
  115. scanf("%s",nname);
  116. }
  117. }while(strlen(nname)<3||strlen(nname)>30);
  118.  
  119. do{
  120. printf("Input quantity of the motorcycle's Part [1..20]: ");
  121. scanf("%d",&qqty);
  122. }while(qqty<1||qqty>20);
  123.  
  124. do{
  125. printf("Input price of the motorcycle's part : ");
  126. scanf("%d",&pprice);
  127. }while(pprice<1);
  128.  
  129. new_node=(struct node*)malloc(sizeof(struct node));
  130. strcpy(new_node->name,nname);
  131. new_node->qty=qqty;
  132. new_node->price=pprice;
  133.  
  134. if(start==NULL){
  135. start=new_node;
  136. start->next=start;
  137. }else{
  138. ptr=start;
  139. while(ptr->next!=start){
  140. ptr=ptr->next;
  141. }
  142. ptr->next=new_node;
  143. new_node->next=start;
  144. }
  145.  
  146. count++;
  147. printf("--- Add new node success ---");
  148. return start;
  149.  
  150. }
  151.  
  152. struct node *pop(struct node *start){
  153. struct node *ptr;
  154. if(pil==0){
  155. printf("There are no node available.");
  156. }
  157. else{
  158. struct node *ptr,*temp;
  159. ptr=start;
  160. int input;
  161. do{
  162. printf("Input number of the order [1..%d] : ",pil);
  163. scanf("%d",&input);
  164. }while(input<1||input>pil);
  165. if(input>1){
  166. for(i=1;i<(input-1);i++){
  167. ptr=ptr->next;
  168. }
  169. temp=ptr->next;
  170. ptr->next=temp->next;
  171. free(temp);
  172. }
  173. else{
  174. while(ptr->next!=start){
  175. ptr=ptr->next;
  176. }
  177. ptr->next=start->next;
  178. temp=start;
  179. start=ptr->next;
  180. free(temp);
  181. }
  182. printf("--- Take order success ---\n");
  183. pil--;
  184. }
  185. return start;
  186. }
Advertisement
Add Comment
Please, Sign In to add comment