Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define N 20
- enum lampchoice {table ,chandelier , error };
- enum lampchoice stringToType(char *str)
- {
- if (strcasecmp(str, "table") == 0)
- {
- return table;
- }
- else if (strcasecmp(str, "chandelier") == 0)
- {
- return chandelier;
- }
- else
- {
- return error;
- }
- exit(1);
- };
- typedef struct chandelier_details
- {
- int plafonds;
- } typechandelier_details;
- typedef struct table_details
- {
- char colour[N];
- } typetable_details;
- typedef struct main_properties
- {
- char name[N];
- enum lampchoice kind;
- int price;
- int amount;
- typechandelier_details ch_details;
- typetable_details t_details;
- } typemain_properties;
- typemain_properties *Input (int *p)
- {
- char buffer[N];
- char select[5];
- typemain_properties *item = malloc(sizeof(typemain_properties) * 1);
- int i=-1;
- FILE *fn=fopen("text.bin" , "ab" );
- while (1)
- {
- printf("Do you want to continue?\n");
- scanf("%s" , &select);
- if (strcmp(select, "no") == 0)
- {
- break;
- }
- i++;
- printf ("Enter kind:");
- scanf ("%s", &buffer);
- if (strcmp(buffer, "table") != 0 && strcmp(buffer, "chandelier") != 0)
- {
- i--;
- continue;
- }
- printf("%d" , i);
- item = realloc(item, sizeof(typemain_properties) * (i + 1));
- item[i].kind=stringToType(buffer);
- printf ("Enter the name of item:");
- scanf ("%s", &item[i].name);
- if(item[i].kind==table)
- {
- printf("Enter colour:");
- scanf ("%s", &item[i].t_details.colour);
- }
- if(item[i].kind==chandelier)
- {
- printf ("Enter number of plafonds:");
- scanf ("%d", &item[i].ch_details.plafonds);
- }
- printf ("Enter price:");
- scanf ("%d", &item[i].price);
- printf ("Enter amount:");
- scanf ("%d", &item[i].amount);
- }
- remove("text.bin");
- fwrite (item,sizeof(typemain_properties),i+1,fn);
- fclose(fn);
- *p=i;
- return item;
- }
- void Output(typemain_properties item) {
- if (item.kind == table)
- {
- printf("Kind: %d \nName: %s \nColour: %s \nPrice: %d \nAmount: %d \n",
- item.kind, item.name, item.t_details.colour,
- item.price, item.amount);
- }
- if (item.kind == chandelier)
- {
- printf("Kind: %d \nName: %s \nNumber of plafonds: %d \nPrice: %d \nAmount: %d \n",
- item.kind, item.name, item.ch_details.plafonds,
- item.price, item.amount);
- }
- }
- int main()
- {
- int *p = malloc(4);
- typemain_properties *item = malloc(sizeof(typemain_properties) * 100);
- int i=0,j=0;
- FILE *fn = fopen("text.bin", "rb");
- printf("Add new items\n");
- Input(p);
- fread(item, sizeof(typemain_properties), 100, fn);
- fclose(fn);
- char choice[N];
- char buffer[N];
- while (strcmp(choice, "5") != 0)
- {
- printf("1.Search by name\n");
- printf("2.Search by category\n");
- printf("3.Search by price\n");
- printf("4.Available items\n");
- printf("5.Exit\n");
- scanf("%s", &choice);
- if (strcmp(choice, "1") == 0)
- {
- printf("Enter name:");
- scanf("%s", &buffer);
- for (int j = 0; j < 100; j++)
- {
- if (strcmp(item[j].name, buffer) == 0)
- {
- Output(item[j]);
- }
- }
- /*char y='y';
- while(y=='y')
- {
- if((T=fopen("text.bin","w+b"))!=NULL)
- printf ("Enter kind:");
- scanf ("%s", &buffer);
- target.kind=stringToType(buffer);
- printf ("Enter the name of item:");
- scanf ("%s", &target.name);
- if(target.kind==table)
- {
- printf("Enter colour:");
- scanf ("%s", &target.t_details.colour);
- }
- if(target.kind==chandelier)
- {
- printf ("Enter number of plafonds:");
- scanf ("%d", &target.ch_details.plafonds);
- }
- printf ("Enter price:");
- scanf ("%d", &target.price);
- printf ("Enter amount:");
- scanf ("%d", &target.amount);
- getchar();
- fwrite (&target,sizeof(target),1,T);
- printf("Do you want to continue?\n");
- y=getchar();
- }
- //fclose(T);*/
- }
- if (strcmp(choice, "2") == 0)
- {
- int x;
- printf("Enter category:");
- scanf("%s", &buffer);
- if (strcmp(buffer, "table") == 0) x=0;
- if (strcmp(buffer, "chandelier") == 0) x=1;
- for (int j = 0; j < 100; j++)
- {
- if ((item[j].kind==x) && (item[j].amount!=0))
- Output(item[j]);
- }
- }
- /*T=fopen("text.bin", "a+b");
- char y='y';
- while(y=='y')
- {
- printf ("Enter kind:");
- scanf ("%s", &buffer);
- target.kind=stringToType(buffer);
- printf ("Enter the name of item:");
- scanf ("%s", &target.name);
- if(target.kind==table)
- {
- printf("Enter colour:");
- scanf ("%s", &target.t_details.colour);
- }
- if(target.kind==chandelier)
- {
- printf ("Enter number of plafonds:");
- scanf ("%d", &target.ch_details.plafonds);
- }
- printf ("Enter price:");
- scanf ("%d", &target.price);
- printf ("Enter amount:");
- scanf ("%d", &target.amount);
- getchar();
- fwrite (&target,sizeof(target),1,T);
- printf("Do you want to continue?\n");
- y=getchar();
- }
- fclose(T);
- }*/
- if (strcmp(choice, "3") == 0)
- {
- int lowest_price, highest_price;
- printf("Enter the min and max price to search:\n");
- scanf("%d %d", &lowest_price, &highest_price);
- for (int j = 0; j < 100; j++)
- {
- if((item[j].price>=lowest_price)&&(item[j].price<=highest_price))
- Output(item[j]);
- }
- /*char nam[N];
- printf("Enter the name of item for search: \n");
- scanf("%s",&nam);
- T=fopen("text.bin","r+b");
- while(!feof(T))
- {
- fread (&target, sizeof(target), 1, T);
- if((strcmp(target.name,nam))==0)
- {
- printf ("name:");
- printf ("%s ", target.name);
- printf ("kind:");
- printf ("%d ", target.kind);
- if(target.kind==table)
- {
- printf ("colour:");
- printf ("%s ", target.t_details.colour);
- }
- if(target.kind==chandelier)
- {
- printf ("number of plafonds:");
- printf ("%d ", target.ch_details.plafonds);
- }
- printf("price:");
- printf ("%d ", target.price);
- printf("amount:");
- printf ("%d ", target.amount);
- printf("\n \n");
- }
- }
- fclose(T); */
- }
- if (strcmp(choice, "4") == 0)
- {
- printf("All available items\n:");
- for (int j = 0; j < 100; j++)
- {
- if (item[j].price!=0) Output(item[j]);
- }
- }
- /*
- enum lampchoice naznachenie;
- printf("Enter the kind of item to search\n");
- scanf ("%s", &buffer);
- naznachenie=stringToType(buffer);
- T=fopen("text.bin","r+b");
- while(!feof(T))
- {
- fread (&target, sizeof(target), 1, T);
- if(target.kind==naznachenie)
- {
- printf("name:");
- printf ("%s ", target.name);
- if(target.kind==table)
- {
- printf ("colour:");
- printf ("%s ", target.t_details.colour);
- }
- if(target.kind==chandelier)
- {
- printf ("number of plafonds:");
- printf ("%d ", target.ch_details.plafonds);
- }
- printf("price:");
- printf ("%d ", target.price);
- printf("amount:");
- printf ("%d ", target.amount);
- printf("\n \n");
- }
- }
- fclose(T);
- }
- if (choice==5)
- {
- int lowest_price, highest_price;
- printf("Enter the min and max price to search:\n");
- scanf("%d %d", &lowest_price, &highest_price);
- T=fopen("text.bin","r+b");
- while(!feof(T))
- {
- fread (&target, sizeof(target), 1, T);
- if((target.price>=lowest_price)&&(target.price<=highest_price))
- {
- printf("price:");
- printf ("%d ", target.price);
- printf("kind:");
- printf ("%d ", target.kind);
- printf("name:");
- if(target.kind==table)
- {
- printf ("colour:");
- printf ("%s ", target.t_details.colour);
- }
- if(target.kind==chandelier)
- {
- printf ("number of plafonds:");
- printf ("%d ", target.ch_details.plafonds);
- }
- printf ("%s ", target.name);
- printf("amount:");
- printf ("%d ", target.amount);
- printf("\n \n");
- }
- }
- fclose(T);
- }
- if (choice==6)
- {
- T=fopen("text.bin","r+b");
- while(!feof(T))
- {
- fread (&target, sizeof(target), 1, T);
- printf("kind:");
- printf ("%d ", target.kind);
- printf("name:");
- printf ("%s ", target.name);
- printf("price:");
- if(target.kind==table)
- {
- printf ("colour:");
- printf ("%s ", target.t_details.colour);
- }
- if(target.kind==chandelier)
- {
- printf ("number of plafonds:");
- printf ("%d ", target.ch_details.plafonds);
- }
- printf ("%d ", target.price);
- printf("amount:");
- printf ("%d ", target.amount);
- printf("\n");
- }
- fclose(T);
- } */
- if (strcmp(choice, "5") == 0)
- {
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement