Advertisement
Guest User

Untitled

a guest
May 27th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define N 20
  5.  
  6. enum lampchoice {table ,chandelier , error };
  7.  
  8. enum lampchoice stringToType(char *str)
  9. {
  10.     if (strcasecmp(str, "table") == 0)
  11.     {
  12.         return table;
  13.     }
  14.     else if (strcasecmp(str, "chandelier") == 0)
  15.     {
  16.         return chandelier;
  17.     }
  18.     else
  19.     {
  20.         return error;
  21.     }
  22.     exit(1);
  23. };
  24.  
  25. typedef struct chandelier_details
  26. {
  27.     int plafonds;
  28. } typechandelier_details;
  29.  
  30. typedef struct table_details
  31. {
  32.     char colour[N];
  33. } typetable_details;
  34.  
  35. typedef struct main_properties
  36. {
  37.     char name[N];
  38.     enum lampchoice kind;
  39.     int price;
  40.     int amount;
  41.     typechandelier_details ch_details;
  42.     typetable_details t_details;
  43. } typemain_properties;
  44.  
  45. typemain_properties *Input (int *p)
  46. {
  47.     char buffer[N];
  48.     char select[5];
  49.     typemain_properties *item = malloc(sizeof(typemain_properties) * 1);
  50.     int i=-1;
  51.     FILE *fn=fopen("text.bin" , "ab" );
  52.  
  53.     while (1)
  54.     {
  55.        printf("Do you want to continue?\n");
  56.        scanf("%s" , &select);
  57.        if (strcmp(select, "no") == 0)
  58.         {
  59.             break;
  60.         }
  61.         i++;
  62.        printf ("Enter kind:");
  63.             scanf ("%s", &buffer);
  64.             if (strcmp(buffer, "table") != 0 && strcmp(buffer, "chandelier") != 0)
  65.         {
  66.             i--;
  67.             continue;
  68.         }
  69.         printf("%d" , i);
  70.             item = realloc(item, sizeof(typemain_properties) * (i + 1));
  71.             item[i].kind=stringToType(buffer);
  72.        printf  ("Enter the name of item:");
  73.             scanf ("%s", &item[i].name);
  74.  
  75.        if(item[i].kind==table)
  76.                 {
  77.                     printf("Enter colour:");
  78.                     scanf ("%s", &item[i].t_details.colour);
  79.                 }
  80.  
  81.         if(item[i].kind==chandelier)
  82.                 {
  83.                     printf ("Enter number of plafonds:");
  84.                     scanf ("%d", &item[i].ch_details.plafonds);
  85.                 }
  86.        printf ("Enter price:");
  87.             scanf ("%d", &item[i].price);
  88.        printf ("Enter amount:");
  89.             scanf ("%d", &item[i].amount);
  90.     }
  91.                 remove("text.bin");
  92.                 fwrite (item,sizeof(typemain_properties),i+1,fn);
  93.                 fclose(fn);
  94.                 *p=i;
  95.                 return item;
  96.  
  97. }
  98.  
  99. void Output(typemain_properties item) {
  100.     if (item.kind == table)
  101.     {
  102.         printf("Kind: %d \nName: %s \nColour: %s \nPrice: %d \nAmount: %d \n",
  103.                item.kind, item.name, item.t_details.colour,
  104.                item.price, item.amount);
  105.     }
  106.     if (item.kind == chandelier)
  107.     {
  108.         printf("Kind: %d \nName: %s \nNumber of plafonds: %d \nPrice: %d \nAmount: %d \n",
  109.                item.kind, item.name, item.ch_details.plafonds,
  110.                item.price, item.amount);
  111.     }
  112. }
  113.  
  114. int main()
  115. {
  116.     int *p = malloc(4);
  117.     typemain_properties *item = malloc(sizeof(typemain_properties) * 100);
  118.     int i=0,j=0;
  119.     FILE *fn = fopen("text.bin", "rb");
  120.     printf("Add new items\n");
  121.     Input(p);
  122.  
  123.     fread(item, sizeof(typemain_properties), 100, fn);
  124.     fclose(fn);
  125.  
  126.     char choice[N];
  127.     char buffer[N];
  128.  
  129.  
  130.     while (strcmp(choice, "5") != 0)
  131.     {
  132.         printf("1.Search by name\n");
  133.         printf("2.Search by category\n");
  134.         printf("3.Search by price\n");
  135.         printf("4.Available items\n");
  136.         printf("5.Exit\n");
  137.  
  138.         scanf("%s", &choice);
  139.  
  140.         if (strcmp(choice, "1") == 0)
  141.         {
  142.             printf("Enter name:");
  143.             scanf("%s", &buffer);
  144.  
  145.  
  146.  
  147.             for (int j = 0; j < 100; j++)
  148.             {
  149.             if (strcmp(item[j].name, buffer) == 0)
  150.             {
  151.                 Output(item[j]);
  152.             }
  153.             }
  154.  
  155.             /*char y='y';
  156.             while(y=='y')
  157.             {
  158.                 if((T=fopen("text.bin","w+b"))!=NULL)
  159.                     printf ("Enter kind:");
  160.                 scanf ("%s", &buffer);
  161.                 target.kind=stringToType(buffer);
  162.  
  163.                 printf  ("Enter the name of item:");
  164.                 scanf ("%s", &target.name);
  165.  
  166.                 if(target.kind==table)
  167.                 {
  168.                     printf("Enter colour:");
  169.                     scanf ("%s", &target.t_details.colour);
  170.                 }
  171.  
  172.                 if(target.kind==chandelier)
  173.                 {
  174.                     printf ("Enter number of plafonds:");
  175.                     scanf ("%d", &target.ch_details.plafonds);
  176.                 }
  177.                 printf ("Enter price:");
  178.                 scanf ("%d", &target.price);
  179.                 printf ("Enter amount:");
  180.                 scanf ("%d", &target.amount);
  181.                 getchar();
  182.                 fwrite (&target,sizeof(target),1,T);
  183.                 printf("Do you want to continue?\n");
  184.                 y=getchar();
  185.             }
  186.             //fclose(T);*/
  187.         }
  188.  
  189.         if (strcmp(choice, "2") == 0)
  190.         {
  191.             int x;
  192.             printf("Enter category:");
  193.             scanf("%s", &buffer);
  194.  
  195.             if (strcmp(buffer, "table") == 0) x=0;
  196.             if (strcmp(buffer, "chandelier") == 0) x=1;
  197.  
  198.             for (int j = 0; j < 100; j++)
  199.             {
  200.                 if ((item[j].kind==x) && (item[j].amount!=0))
  201.                 Output(item[j]);
  202.             }
  203.         }
  204.             /*T=fopen("text.bin", "a+b");
  205.             char y='y';
  206.             while(y=='y')
  207.             {
  208.                 printf ("Enter kind:");
  209.                 scanf ("%s", &buffer);
  210.                 target.kind=stringToType(buffer);
  211.  
  212.                 printf ("Enter the name of item:");
  213.                 scanf ("%s", &target.name);
  214.  
  215.                 if(target.kind==table)
  216.                 {
  217.                     printf("Enter colour:");
  218.                     scanf ("%s", &target.t_details.colour);
  219.                 }
  220.  
  221.                 if(target.kind==chandelier)
  222.                 {
  223.                     printf ("Enter number of plafonds:");
  224.                     scanf ("%d", &target.ch_details.plafonds);
  225.                 }
  226.                 printf ("Enter price:");
  227.                 scanf ("%d", &target.price);
  228.                 printf ("Enter amount:");
  229.                 scanf ("%d", &target.amount);
  230.                 getchar();
  231.  
  232.                 fwrite (&target,sizeof(target),1,T);
  233.  
  234.                 printf("Do you want to continue?\n");
  235.                 y=getchar();
  236.             }
  237.             fclose(T);
  238.         }*/
  239.         if (strcmp(choice, "3") == 0)
  240.         {
  241.             int lowest_price, highest_price;
  242.             printf("Enter the min and max price to search:\n");
  243.             scanf("%d %d", &lowest_price, &highest_price);
  244.  
  245.             for (int j = 0; j < 100; j++)
  246.             {
  247.             if((item[j].price>=lowest_price)&&(item[j].price<=highest_price))
  248.             Output(item[j]);
  249.             }
  250.  
  251.             /*char nam[N];
  252.             printf("Enter the name of item for search: \n");
  253.             scanf("%s",&nam);
  254.             T=fopen("text.bin","r+b");
  255.  
  256.             while(!feof(T))
  257.             {
  258.                 fread (&target, sizeof(target), 1, T);
  259.                 if((strcmp(target.name,nam))==0)
  260.                 {
  261.                     printf ("name:");
  262.                     printf ("%s ", target.name);
  263.                     printf ("kind:");
  264.                     printf ("%d ", target.kind);
  265.  
  266.                     if(target.kind==table)
  267.                     {
  268.                         printf ("colour:");
  269.                         printf ("%s ", target.t_details.colour);
  270.                     }
  271.                     if(target.kind==chandelier)
  272.                     {
  273.                         printf ("number of plafonds:");
  274.                         printf ("%d ", target.ch_details.plafonds);
  275.                     }
  276.                     printf("price:");
  277.                     printf ("%d ", target.price);
  278.                     printf("amount:");
  279.                     printf ("%d ", target.amount);
  280.                     printf("\n \n");
  281.                 }
  282.             }
  283.             fclose(T); */
  284.         }
  285.  
  286.         if (strcmp(choice, "4") == 0)
  287.         {
  288.  
  289.             printf("All available items\n:");
  290.  
  291.             for (int j = 0; j < 100; j++)
  292.             {
  293.                 if (item[j].price!=0) Output(item[j]);
  294.             }
  295.         }
  296.             /*
  297.             enum lampchoice naznachenie;
  298.             printf("Enter the kind of item to search\n");
  299.             scanf ("%s", &buffer);
  300.             naznachenie=stringToType(buffer);
  301.             T=fopen("text.bin","r+b");
  302.             while(!feof(T))
  303.             {
  304.                 fread (&target, sizeof(target), 1, T);
  305.                 if(target.kind==naznachenie)
  306.                 {
  307.                     printf("name:");
  308.                     printf ("%s ", target.name);
  309.  
  310.                     if(target.kind==table)
  311.                     {
  312.                         printf ("colour:");
  313.                         printf ("%s ", target.t_details.colour);
  314.                     }
  315.                     if(target.kind==chandelier)
  316.                     {
  317.                         printf ("number of plafonds:");
  318.                         printf ("%d ", target.ch_details.plafonds);
  319.                     }
  320.  
  321.                     printf("price:");
  322.                     printf ("%d ", target.price);
  323.                     printf("amount:");
  324.                     printf ("%d ", target.amount);
  325.                     printf("\n \n");
  326.                 }
  327.             }
  328.             fclose(T);
  329.         }
  330.         if (choice==5)
  331.         {
  332.             int lowest_price, highest_price;
  333.             printf("Enter the min and max price to search:\n");
  334.             scanf("%d %d", &lowest_price, &highest_price);
  335.             T=fopen("text.bin","r+b");
  336.  
  337.             while(!feof(T))
  338.             {
  339.                 fread (&target, sizeof(target), 1, T);
  340.                 if((target.price>=lowest_price)&&(target.price<=highest_price))
  341.                 {
  342.                     printf("price:");
  343.                     printf ("%d ", target.price);
  344.                     printf("kind:");
  345.                     printf ("%d ", target.kind);
  346.                     printf("name:");
  347.                     if(target.kind==table)
  348.                     {
  349.                         printf ("colour:");
  350.                         printf ("%s ", target.t_details.colour);
  351.                     }
  352.                     if(target.kind==chandelier)
  353.                     {
  354.                         printf ("number of plafonds:");
  355.                         printf ("%d ", target.ch_details.plafonds);
  356.                     }
  357.                     printf ("%s ", target.name);
  358.                     printf("amount:");
  359.                     printf ("%d ", target.amount);
  360.                     printf("\n \n");
  361.                 }
  362.             }
  363.  
  364.             fclose(T);
  365.         }
  366.         if (choice==6)
  367.         {
  368.  
  369.  
  370.  
  371.             T=fopen("text.bin","r+b");
  372.             while(!feof(T))
  373.             {
  374.                 fread (&target, sizeof(target), 1, T);
  375.                 printf("kind:");
  376.                 printf ("%d ", target.kind);
  377.                 printf("name:");
  378.                 printf ("%s ", target.name);
  379.                 printf("price:");
  380.                 if(target.kind==table)
  381.                 {
  382.                     printf ("colour:");
  383.                     printf ("%s ", target.t_details.colour);
  384.                 }
  385.                 if(target.kind==chandelier)
  386.                 {
  387.                     printf ("number of plafonds:");
  388.                     printf ("%d ", target.ch_details.plafonds);
  389.                 }
  390.                 printf ("%d ", target.price);
  391.                 printf("amount:");
  392.                 printf ("%d ", target.amount);
  393.                 printf("\n");
  394.             }
  395.             fclose(T);
  396.         } */
  397.         if (strcmp(choice, "5") == 0)
  398.         {
  399.             break;
  400.         }
  401.  
  402.     }
  403.     return 0;
  404. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement