Advertisement
cd62131

Product

Jul 8th, 2014
445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.98 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define NAME (20)
  5. #define SPEC (50)
  6. typedef struct {
  7.   int id;
  8.   char name[NAME], spec[SPEC];
  9.   int price, items;
  10. } product_t;
  11. typedef struct {
  12.   int id;
  13.   char name[NAME], spec[SPEC];
  14.   int price;
  15. } item_t;
  16. typedef struct {
  17.   int id, items;
  18. } order_t;
  19. static product_t *add_product(int id, char *name, char *spec, int price, int items) {
  20.   product_t *new = (product_t *) malloc(sizeof(product_t));
  21.   new->id = id;
  22.   strcpy(new->name, name);
  23.   strcpy(new->spec, spec);
  24.   new->price = price;
  25.   new->items = items;
  26.   return new;
  27. }
  28. static product_t **read(FILE *in, int *order) {
  29.   char line[BUFSIZ], *tok;
  30.   int c, id, price, items;
  31.   char name[NAME], spec[SPEC];
  32.   product_t **all_product = NULL;
  33.   *order = 0;
  34.   while (fgets(line, BUFSIZ, in)) {
  35.     for (c = 0, tok = strtok(line, ","); tok && *tok; c++, tok = strtok(NULL, ",\n")) {
  36.       if (c == 0) {
  37.         id = atoi(tok);
  38.         continue;
  39.       }
  40.       if (c == 1) {
  41.         strcpy(name, tok);
  42.         continue;
  43.       }
  44.       if (c == 2) {
  45.         strcpy(spec, tok);
  46.         continue;
  47.       }
  48.       if (c == 3) {
  49.         price = atoi(tok);
  50.         continue;
  51.       }
  52.       if (c == 4) {
  53.         items = atoi(tok);
  54.         break;
  55.       }
  56.     }
  57.     (*order)++;
  58.     all_product = (product_t **) realloc(all_product, *order * sizeof(product_t *));
  59.     all_product[*order - 1] = add_product(id, name, spec, price, items);
  60.   }
  61.   return all_product;
  62. }
  63. static order_t **extract_order(product_t **all_product, int order) {
  64.   int i;
  65.   order_t **all_order = (order_t **) malloc(order * sizeof(order_t *));
  66.   for (i = 0; i < order; i++) {
  67.     all_order[i] = (order_t *) malloc(sizeof(order_t));
  68.     all_order[i]->id = all_product[i]->id;
  69.     all_order[i]->items = all_product[i]->items;
  70.   }
  71.   return all_order;
  72. }
  73. static item_t *find_id_from_item(item_t **all_item, int id, int ids) {
  74.   int i;
  75.   for (i = 0; i < ids; i++) {
  76.     if (all_item[i]->id == id) return all_item[i];
  77.   }
  78.   return NULL;
  79. }
  80. static item_t *add_item(int id, char *name, char *spec, int price) {
  81.   item_t *new = (item_t *) malloc(sizeof(item_t));
  82.   new->id = id;
  83.   strcpy(new->name, name);
  84.   strcpy(new->spec, spec);
  85.   new->price = price;
  86.   return new;
  87. }
  88. static item_t **extract_item(product_t **all_product, int order, int *ids) {
  89.   int i;
  90.   product_t *p;
  91.   item_t **all_item = NULL;
  92.   *ids = 0;
  93.   for (i = 0; i < order; i++) {
  94.     p = all_product[i];
  95.     if (!find_id_from_item(all_item, p->id, *ids)) {
  96.       (*ids)++;
  97.       all_item = (item_t **) realloc(all_item, *ids * sizeof(item_t *));
  98.       all_item[*ids - 1] = add_item(p->id, p->name, p->spec, p->price);
  99.     }
  100.   }
  101.   return all_item;
  102. }
  103. static void puts_item_number(FILE *out, int ids) {
  104.   fprintf(out, "===商品数===\n%d\n", ids);
  105. }
  106. static void puts_total_per_item(FILE *out, item_t **all_item, order_t **all_order, int order, int ids, int *all_total) {
  107.   int i, j, total, id;
  108.   *all_total = 0;
  109.   fprintf(out, "\n===商品ごとの合計===\n");
  110.   for (i = 0; i < ids; i++) {
  111.     id = all_item[i]->id;
  112.     for (total = 0, j = 0; j < order; j++)
  113.       if (id == all_order[j]->id) total += all_item[i]->price * all_order[j]->items;
  114.     fprintf(out, "%s,%d\n", all_item[i]->name, total);
  115.     *all_total += total;
  116.   }
  117. }
  118. static void puts_all_total(FILE *out, int all_total) {
  119.   fprintf(out, "\n===全商品の合計===\n%d\n", all_total);
  120. }
  121. int main(int argc, char **argv) {
  122.   FILE *in, *out;
  123.   product_t **all_product = NULL;
  124.   item_t **all_item = NULL;
  125.   order_t **all_order = NULL;
  126.   int order, ids, all_total;
  127.   in = fopen(argv[1], "r");
  128.   all_product = read(in, &order);
  129.   fclose(in);
  130.   all_order = extract_order(all_product, order);
  131.   all_item = extract_item(all_product, order, &ids);
  132.   out = fopen(argv[2], "w");
  133. //  out = stdout;
  134.   puts_item_number(out, ids);
  135.   puts_total_per_item(out, all_item, all_order, order, ids, &all_total);
  136.   puts_all_total(out, all_total);
  137.   fclose(out);
  138.   return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement