Created
December 17, 2013 17:14
-
-
Save nikoncode/8008693 to your computer and use it in GitHub Desktop.
vovan
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 <string.h> | |
| #include <conio.h> | |
| struct drugstore { | |
| char name[20]; | |
| char manufacturer[20]; | |
| unsigned long cost; | |
| char type[20]; | |
| char indications[50]; | |
| char sell_type[20]; | |
| }; | |
| int append_drug(int n, drugstore *a) { | |
| printf("Enter name: "); | |
| scanf(" %[^\n]", &a[n].name); | |
| printf("Enter manufacturer: "); | |
| scanf(" %[^\n]", &a[n].manufacturer); | |
| printf("Enter cost: "); | |
| scanf("%lu", &a[n].cost); | |
| printf("Enter type: "); | |
| scanf(" %[^\n]", &a[n].type); | |
| printf("Enter indication of use: "); | |
| scanf(" %[^\n]", &a[n].indications); | |
| printf("Enter sell type: "); | |
| scanf(" %[^\n]", &a[n].sell_type); | |
| return n + 1; | |
| } | |
| int delete_drug(int n, drugstore *a, int del_id) { | |
| for (int i=del_id;i<n;++i) | |
| a[i] = a[i+1]; | |
| return n-1; | |
| } | |
| int delete_stype_free(int n, drugstore *a) { | |
| for (int i=0;i<n;++i) | |
| if (strcmp(a[i].sell_type, "free") == 0) { | |
| //printf("Drug with id='?d' has type 'free' and this record will be remove.\n", i); | |
| n = delete_drug(n, a, i); | |
| i--; | |
| } | |
| return n; | |
| } | |
| int inc_cost(int d_id, int added_value, drugstore *a) { | |
| a[d_id].cost += added_value; | |
| return a[d_id].cost; | |
| } | |
| int print_all_with_type(int n, char * type, drugstore * a, drugstore * res) { | |
| int cnt = 0; | |
| for (int i=0;i<n;++i) | |
| if (strcmp(a[i].type, type) == 0) | |
| res[cnt++] = a[i]; | |
| return cnt; | |
| } | |
| void print_drug(int n, drugstore *a) { | |
| if (n==0) { | |
| printf("Db has empty.\n"); | |
| return; | |
| } | |
| printf("-------------------------------------------------------------------------\n"); | |
| printf("| name|manufacturer| cost| type|indications| sell type|\n"); | |
| printf("------------------------------------------------------------------------\n"); | |
| for (int i=0;i<n;++i) { | |
| printf("|%10s| %11s| %10lu| %10s| %10s| %10s|\n", | |
| a[i].name, | |
| a[i].manufacturer, | |
| a[i].cost, | |
| a[i].type, | |
| a[i].indications, | |
| a[i].sell_type | |
| ); | |
| printf("-------------------------------------------------------------------------\n"); | |
| } | |
| } | |
| int menu() { | |
| clrscr(); | |
| char *points[] = { | |
| "Append drug in db", | |
| "Delete drug from db", | |
| "Print all drugs in db", | |
| "Delete all nonprescription drugs", | |
| "Increase drug cost", | |
| "Print all drugs by entered type", | |
| "Exit" | |
| }; | |
| int p_cnt = 7,temp; | |
| for (int i=0;i<p_cnt;++i) | |
| printf("%d: %s\n",i+1,points[i]); | |
| printf("Choose your destiny: "); | |
| scanf("%d", &temp); | |
| printf("Flawless victory (SIC!)\n"); | |
| return temp; | |
| } | |
| int main() { | |
| struct drugstore arr[20], res[20]; | |
| int n=0,id,add,rcnt; | |
| char type[20]; | |
| while (1) { | |
| int choose = menu(); | |
| clrscr(); | |
| switch (choose) { | |
| case 1: | |
| printf("BEFORE: \n"); | |
| print_drug(n, arr); | |
| n = append_drug(n, arr); | |
| printf("AFTER: \n"); | |
| print_drug(n, arr); | |
| break; | |
| case 2: | |
| printf("Enter id: "); | |
| scanf ("%d", &id); | |
| printf("BEFORE: \n"); | |
| print_drug(n, arr); | |
| n = delete_drug(n, arr, id); | |
| printf("AFTER: \n"); | |
| print_drug(n, arr); | |
| break; | |
| case 3: | |
| printf("CURRENT: \n"); | |
| print_drug(n, arr); | |
| break; | |
| case 4: | |
| printf("BEFORE: \n"); | |
| print_drug(n, arr); | |
| n = delete_stype_free(n, arr); | |
| printf("AFTER: \n"); | |
| print_drug(n, arr); | |
| break; | |
| case 5: | |
| printf("Enter id: "); | |
| scanf ("%d", &id); | |
| printf("Enter increase value: "); | |
| scanf ("%d", &add); | |
| printf("BEFORE: \n"); | |
| print_drug(n, arr); | |
| inc_cost(id, add, arr); | |
| printf("AFTER: \n"); | |
| print_drug(n, arr); | |
| break; | |
| case 6: | |
| printf("Enter type: "); | |
| scanf ("%s", &type); | |
| printf("BEFORE: \n"); | |
| print_drug(n, arr); | |
| rcnt = print_all_with_type(n, type, arr, res); | |
| printf("FINDING: \n"); | |
| print_drug(rcnt, res); | |
| break; | |
| case 7: | |
| return 0; | |
| } | |
| getch(); | |
| } | |
| //return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment