fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct product {
  5. int pid;
  6. char name[100];
  7. int cost;
  8. } PRODUCT;
  9.  
  10. PRODUCT products[] = {
  11. {100, "せんべい", 90},
  12. {101, "チョコレート", 200},
  13. {102, "ガム", 100},
  14. {105, "缶コーヒー", 80},
  15. {107, "ヨーグルト", 180},
  16. };
  17.  
  18. int num_products = 5;
  19.  
  20. typedef struct stock {
  21. int pid;
  22. int count;
  23. struct stock* next;
  24. } STOCK;
  25.  
  26. STOCK *stocks = NULL;
  27.  
  28. STOCK *create_stock(int pid, int count) {
  29. STOCK* s = malloc(sizeof(STOCK));
  30. s->pid = pid;
  31. s->count = count;
  32. s->next = NULL;
  33. return s;
  34. }
  35.  
  36. PRODUCT *find_product_by_pid(int pid) {
  37. int i;
  38. for (i = 0; i < num_products; i++) {
  39. if (products[i].pid == pid) {
  40. return &products[i];
  41. }
  42. }
  43. return NULL;
  44. }
  45.  
  46. void print_product(int pid) {
  47. for (int i = 0; i < num_products; i++)
  48. if (products[i].pid == pid) {
  49. printf("name: %s, cost: %d\n", products[i].name, products[i].cost);
  50. break;
  51. }
  52. }
  53.  
  54. /* --------- */
  55. void receive_product(int pid, int count) {
  56. STOCK *s = stocks;
  57. while(s != NULL){
  58. if (s->pid == pid) {
  59. s->count += count;
  60. return;
  61. }
  62. s = s->next;
  63. }
  64. s = create_stock(pid, count);
  65. s->next = stocks;
  66. stocks = s;
  67. }
  68.  
  69. int ship_product(int pid, int count) {
  70. STOCK *s = stocks;
  71. while(s != NULL){
  72. if (s->pid == pid) {
  73. if (s->count > count) {
  74. s->count -= count;
  75. return count; /* out of loop */
  76. } else {
  77. int ret = s->count;
  78. s->count = 0;
  79. return ret; /* out of loop */
  80. }
  81. }
  82. s = s->next;
  83. }
  84. return 0;
  85. }
  86.  
  87. int evaluate_stocks() {
  88. int total_evaluate_value = 0;
  89. STOCK *s = stocks;
  90. while (s != 0) {
  91. total_evaluate_value += (s->count * (find_product_by_pid(s->pid)->cost));
  92. s = s->next;
  93. }
  94. return total_evaluate_value;
  95. }
  96.  
  97. void dump(STOCK *root) {
  98. STOCK *s = root;
  99. while (s != 0) {
  100. printf("pid: %d, count: %d\n", s->pid, s->count);
  101. s = s->next;
  102. }
  103. }
  104.  
  105. void release(STOCK *root) {
  106. STOCK *s = root;
  107. while (s != 0) {
  108. STOCK *t = s;
  109. s = s->next;
  110. free(t);
  111. }
  112. }
  113.  
  114. int main(void) {
  115. print_product(100);
  116. print_product(107);
  117. receive_product(products[0].pid, 10);
  118. receive_product(products[1].pid, 20);
  119. receive_product(products[2].pid, 30);
  120.  
  121. printf(":%d\n", ship_product(products[0].pid, 5));
  122. printf(":%d\n", ship_product(products[1].pid, 6));
  123. printf(":%d\n", ship_product(products[2].pid, 7));
  124. printf(":%d\n", ship_product(products[2].pid, 100));
  125. printf(":%d\n", ship_product(products[2].pid, 100));
  126.  
  127. printf("Value: %d\n", evaluate_stocks());
  128. release(stocks);
  129. return 0;
  130. }
  131. /* end */
  132.  
Success #stdin #stdout 0s 4412KB
stdin
Standard input is empty
stdout
name: せんべい, cost: 90
name: ヨーグルト, cost: 180
:5
:6
:7
:23
:0
Value: 3250