Advertisement
axelso

Strukdat fix 02

Mar 19th, 2014
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. //int jumlah=0;
  5.  
  6. void clear()
  7. {
  8. for(int i=0;i<25;i++)
  9. {
  10. printf("\n");
  11. }
  12. }
  13.  
  14. struct data
  15. {
  16. char name[21];
  17. char type[100];
  18. int quantity;
  19. int price;
  20. struct data *next;
  21. }*head=NULL,*tail=NULL,*curr=NULL;
  22.  
  23. void pushTail(char name[],char type[],int quantity,int price)
  24. {
  25. curr=(struct data*)malloc(sizeof(struct data));
  26. strcpy(curr->name,name);
  27. strcpy(curr->type,type);
  28. curr->quantity=quantity;
  29. curr->price=price;
  30. curr->next=NULL;
  31.  
  32. if(head == NULL)
  33. {
  34. head=tail=curr;
  35. }
  36. else
  37. {
  38. tail->next=curr;
  39. tail=curr;
  40. }
  41. tail->next=NULL;
  42. }
  43.  
  44. void itemlist()
  45. {
  46. clear();
  47. int i=1;
  48. printf("==================================================================================\n");
  49. printf("%5s. | %-20s | %-20s | %-10s | %-10s |\n","No.","Name","Type","Quantity","Price");
  50. printf("==================================================================================\n");
  51. curr=head;//posisi curr di head
  52. while(curr != head)
  53. {
  54. printf("%5d. | %-20s | %-20s | %-10d | %-10d |\n",i,curr->name,curr->type,curr->quantity,curr->price);
  55. curr=curr->next;
  56. i++;
  57. }
  58. printf("==================================================================================\n");
  59. getchar();
  60. }
  61.  
  62. void additem()
  63. {
  64. char tempname[21];
  65. char temptype[100];
  66. int tempqty;
  67. int tempprice;
  68. do{
  69. printf("Input name fo the new item[3..20] = ");
  70. scanf("%[^\n]",&tempname);fflush(stdin);
  71. }while(strlen(tempname)<3 || strlen(tempname)>20);
  72.  
  73.  
  74. do{
  75. printf("input type of the new item[processor/graphic card/memory] = ");
  76. scanf("%[^\n]",temptype);fflush(stdin);
  77. }while(strcmp(temptype,"processor")==1 &&strcmp(temptype,"graphic card")==1 && strcmp(temptype,"memory")==1);
  78.  
  79.  
  80. do{
  81. printf("input Quantity of the new item[1..20] = ");
  82. scanf("%[^\n]",&tempqty);fflush(stdin);
  83. }while(tempqty<1 && tempqty>20);
  84.  
  85.  
  86. do{
  87. printf("input Price of the new item[1..$1000] = $");
  88. scanf("%[^\n]",&tempprice);fflush(stdin);
  89. }while(tempprice<1 && tempprice>1000);
  90.  
  91. clear();
  92. printf("--- add new item success ---");
  93.  
  94. pushTail(tempname,temptype,tempqty,tempprice);
  95.  
  96. }
  97.  
  98. void menu()
  99. {
  100. clear();
  101. puts("BLUE COMPUTER ADMINISTRATOR");
  102. puts("+++++++++++++++++++++++++++");
  103. puts("1. Item List");
  104. puts("2. Add (PUSH) New Item");
  105. puts("3. Exit");
  106. printf(">> Input your choice : ");
  107. }
  108.  
  109. int main()
  110. {
  111. int choose;
  112.  
  113. do{
  114. choose=0;
  115. menu();
  116. scanf("%d",&choose);fflush(stdin);
  117. switch(choose)
  118. {
  119. case 1:itemlist();break;
  120. case 2:additem();break;
  121.  
  122. }
  123. }while(choose !=3);
  124.  
  125.  
  126.  
  127.  
  128. getchar();
  129. return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement