Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define NAME (20)
- #define SPEC (50)
- typedef struct {
- int id;
- char name[NAME], spec[SPEC];
- int price, items;
- } product_t;
- typedef struct {
- int id;
- char name[NAME], spec[SPEC];
- int price;
- } item_t;
- typedef struct {
- int id, items;
- } order_t;
- static product_t *add_product(int id, char *name, char *spec, int price, int items) {
- product_t *new = (product_t *) malloc(sizeof(product_t));
- new->id = id;
- strcpy(new->name, name);
- strcpy(new->spec, spec);
- new->price = price;
- new->items = items;
- return new;
- }
- static product_t **read(FILE *in, int *order) {
- char line[BUFSIZ], *tok;
- int c, id, price, items;
- char name[NAME], spec[SPEC];
- product_t **all_product = NULL;
- *order = 0;
- while (fgets(line, BUFSIZ, in)) {
- for (c = 0, tok = strtok(line, ","); tok && *tok; c++, tok = strtok(NULL, ",\n")) {
- if (c == 0) {
- id = atoi(tok);
- continue;
- }
- if (c == 1) {
- strcpy(name, tok);
- continue;
- }
- if (c == 2) {
- strcpy(spec, tok);
- continue;
- }
- if (c == 3) {
- price = atoi(tok);
- continue;
- }
- if (c == 4) {
- items = atoi(tok);
- break;
- }
- }
- (*order)++;
- all_product = (product_t **) realloc(all_product, *order * sizeof(product_t *));
- all_product[*order - 1] = add_product(id, name, spec, price, items);
- }
- return all_product;
- }
- static order_t **extract_order(product_t **all_product, int order) {
- int i;
- order_t **all_order = (order_t **) malloc(order * sizeof(order_t *));
- for (i = 0; i < order; i++) {
- all_order[i] = (order_t *) malloc(sizeof(order_t));
- all_order[i]->id = all_product[i]->id;
- all_order[i]->items = all_product[i]->items;
- }
- return all_order;
- }
- static item_t *find_id_from_item(item_t **all_item, int id, int ids) {
- int i;
- for (i = 0; i < ids; i++) {
- if (all_item[i]->id == id) return all_item[i];
- }
- return NULL;
- }
- static item_t *add_item(int id, char *name, char *spec, int price) {
- item_t *new = (item_t *) malloc(sizeof(item_t));
- new->id = id;
- strcpy(new->name, name);
- strcpy(new->spec, spec);
- new->price = price;
- return new;
- }
- static item_t **extract_item(product_t **all_product, int order, int *ids) {
- int i;
- product_t *p;
- item_t **all_item = NULL;
- *ids = 0;
- for (i = 0; i < order; i++) {
- p = all_product[i];
- if (!find_id_from_item(all_item, p->id, *ids)) {
- (*ids)++;
- all_item = (item_t **) realloc(all_item, *ids * sizeof(item_t *));
- all_item[*ids - 1] = add_item(p->id, p->name, p->spec, p->price);
- }
- }
- return all_item;
- }
- static void puts_item_number(FILE *out, int ids) {
- fprintf(out, "===商品数===\n%d\n", ids);
- }
- static void puts_total_per_item(FILE *out, item_t **all_item, order_t **all_order, int order, int ids, int *all_total) {
- int i, j, total, id;
- *all_total = 0;
- fprintf(out, "\n===商品ごとの合計===\n");
- for (i = 0; i < ids; i++) {
- id = all_item[i]->id;
- for (total = 0, j = 0; j < order; j++)
- if (id == all_order[j]->id) total += all_item[i]->price * all_order[j]->items;
- fprintf(out, "%s,%d\n", all_item[i]->name, total);
- *all_total += total;
- }
- }
- static void puts_all_total(FILE *out, int all_total) {
- fprintf(out, "\n===全商品の合計===\n%d\n", all_total);
- }
- int main(int argc, char **argv) {
- FILE *in, *out;
- product_t **all_product = NULL;
- item_t **all_item = NULL;
- order_t **all_order = NULL;
- int order, ids, all_total;
- in = fopen(argv[1], "r");
- all_product = read(in, &order);
- fclose(in);
- all_order = extract_order(all_product, order);
- all_item = extract_item(all_product, order, &ids);
- out = fopen(argv[2], "w");
- // out = stdout;
- puts_item_number(out, ids);
- puts_total_per_item(out, all_item, all_order, order, ids, &all_total);
- puts_all_total(out, all_total);
- fclose(out);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement