Untitled
unknown
c_cpp
a year ago
1.9 kB
7
Indexable
Never
#include <stdio.h> typedef struct { int nnnn; char description[32]; char type; int qty; } Transaction; typedef struct { int nnnn; char description[32]; double price; int in_stock; int back_order; } Product; void manageInventory(Transaction t, int inventory[]) { // ‘D’ Delivery = +1 // ‘R’ Return = +1 // ‘P’ Purchase = +1 if (t.type == 'P') { inventory[t.nnnn] -= t.qty; } else if (t.type == 'R' || t.type == 'D') { inventory[t.nnnn] += t.qty; } } int main() { FILE *fp; Transaction t; int inventory[9999] = {0}; Product product; fp = fopen("transaction.dat", "r"); if (fp == NULL) { printf("File could not be opened."); return 1; } while (fscanf(fp, "%d %31[^\t] %c %d\n", &t.nnnn, t.description, &t.type, &t.qty) == 4) { manageInventory(t, inventory); } fclose(fp); fp = fopen("datafile.dat", "r"); FILE *fp2; fp2 = fopen("output.dat", "w"); if (fp == NULL) { printf("File could not be opened."); return 1; } while (fscanf(fp, "%d %31[^\t] %lf %d %d\n", &product.nnnn, product.description, &product.price, &product.in_stock, &product.back_order) == 5) { int inv = inventory[product.nnnn]; if (inv > 0){ // first do the back order then the in stock if (inv >= product.back_order) { product.in_stock += (inv - product.back_order); product.back_order = 0; } else { product.back_order -= inv; } } else if(inv < 0) { product.back_order += (-1 * inv); } fprintf(fp2, "%d\t%s\t%.2lf\t%d\t%d\n", product.nnnn, product.description, product.price, product.in_stock, product.back_order); } fclose(fp); fclose(fp2); return 0; }