Last active
March 30, 2018 07:20
-
-
Save peacher5/f357e8bf36f9c1d367653719d04b7885 to your computer and use it in GitHub Desktop.
ComPro Lab (File & Struct)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| typedef struct item { | |
| char name[20]; | |
| float price; | |
| int pcs; | |
| } Item; | |
| typedef struct stock { | |
| int n_item; | |
| Item *items; | |
| } Stock; | |
| void updateStock(Stock stock) { | |
| int i; | |
| FILE *file = fopen("stock.txt", "w"); | |
| fprintf(file, "%d\n", stock.n_item); | |
| for (i = 0; i < stock.n_item; i++) { | |
| fprintf(file, "%s\t%.2f\t%d\n", stock.items[i].name, stock.items[i].price, stock.items[i].pcs); | |
| } | |
| fclose(file); | |
| } | |
| void addStock() { | |
| char name[20]; | |
| float price; | |
| int pcs, size; | |
| printf(" Enter a item name, item price and quantity:\n >> "); | |
| scanf("%s %f %d", name, &price, &pcs); | |
| FILE *file = fopen("stock.txt", "a+"); | |
| fprintf(file, "%s\t%.2f\t%d\n", name, price, pcs); | |
| fseek(file, 0, SEEK_SET); | |
| fscanf(file, "%d", &size); | |
| fclose(file); | |
| file = fopen("stock.txt", "r+"); | |
| fprintf(file, "%d", size + 1); | |
| fclose(file); | |
| } | |
| int main() { | |
| Stock st; | |
| int i, no, pcs, mode; | |
| float sum = 0; | |
| char more; | |
| while (1) { | |
| printf("\nSelect Mode (1 = Customer / 2 = Add item): "); | |
| scanf("%d", &mode); | |
| if (mode == 2) { | |
| printf(" Add Item:\n"); | |
| addStock(); | |
| } else break; | |
| } | |
| printf("\n"); | |
| FILE *file = fopen("stock.txt", "r"); | |
| fscanf(file, "%d", &st.n_item); | |
| st.items = (Item*) malloc(st.n_item * sizeof (Item)); | |
| printf(" No. | Item | Price | Pcs.\n"); | |
| printf("-----|-----------------|----------|--------\n"); | |
| for (i = 0; i < st.n_item; i++) { | |
| fscanf(file, "%s %f %d", st.items[i].name, &st.items[i].price, &st.items[i].pcs); | |
| printf("%4d | %15s | %8.2f | ", i+1, st.items[i].name, st.items[i].price, st.items[i].pcs); | |
| if (st.items[i].pcs <= 0) | |
| printf("%6s\n", "N/A"); | |
| else | |
| printf("%6d\n", st.items[i].pcs); | |
| } | |
| fclose(file); | |
| printf("\n What do you want to buy?\n"); | |
| printf(" Type a item number, then a quantity:\n"); | |
| while (1) { | |
| printf(" >> "); | |
| scanf("%d %d", &no, &pcs); | |
| if (no < 1 || no > st.n_item) { | |
| printf("Invalid item number.\n"); | |
| continue; | |
| } | |
| if (pcs <= 0) { | |
| printf("Invalid item quantity.\n"); | |
| continue; | |
| } | |
| if (st.items[no-1].pcs <= 0) { | |
| printf("%s is out of stock.\n", st.items[no-1].name); | |
| continue; | |
| } | |
| if (pcs > st.items[no-1].pcs) { | |
| printf("Max item of %s = %d.\n", st.items[no-1].name, st.items[no-1].pcs); | |
| continue; | |
| } | |
| st.items[no-1].pcs -= pcs; | |
| sum += st.items[no-1].price * pcs; | |
| printf(" Add more? (y/n): "); | |
| scanf(" %c", &more); | |
| if (more == 'N' || more == 'n') | |
| break; | |
| } | |
| printf("\nTotal amount is %.2f. Thanks. <3\n", sum); | |
| updateStock(st); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 5 | |
| Lays 30.00 5 | |
| KitKat 25.50 8 | |
| Jelly_Bean 42.50 7 | |
| Oreo 30.00 0 | |
| Eclair 25.00 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment