Advertisement
vasylmartyniv

C2S1-Yulia-Coursework

Dec 21st, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 49.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. #define stringlength 255
  6. #define STR2(x) #x
  7. #define STR(X) STR2(X)
  8.  
  9. typedef struct PlantStruct {
  10.     int id;
  11.     char *name;
  12.     char *type;
  13.     char *photo;
  14.     char *desc;
  15.     char *bloom;
  16.     char *temperature;
  17.     char *water;
  18.     char *light;
  19. } Plant;
  20. Plant* Plants = NULL;
  21. int PlantsCounter = 0;
  22.  
  23. typedef struct PlantTypeStruct{
  24.     int id;
  25.     char *name;
  26. } PlantType;
  27. PlantType* PlantTypes = NULL;
  28. int PlantTypesCounter = 0;
  29.  
  30. typedef struct TemperatureTypeStruct{
  31.     int id;
  32.     char *name;
  33. } TemperatureType;
  34. TemperatureType* TemperatureTypes = NULL;
  35. int TemperatureTypesCounter = 0;
  36.  
  37. typedef struct WaterTypeStruct{
  38.     int id;
  39.     char *name;
  40. } WaterType;
  41. WaterType* WaterTypes = NULL;
  42. int WaterTypesCounter = 0;
  43.  
  44. typedef struct LightTypeStruct{
  45.     int id;
  46.     char *name;
  47. } LightType;
  48. LightType* LightTypes = NULL;
  49. int LightTypesCounter = 0;
  50.  
  51.  
  52. int FindType(char* str,int mode) {
  53.     //Modes:
  54.     //1-Plant Types
  55.     //2-Temperature
  56.     //3-Water
  57.     //4-Light
  58.     switch(mode){
  59.         case 1:
  60.             for (int i = 0; i < PlantTypesCounter; i++) {
  61.                 if (strcmp(PlantTypes[i].name, str) == 0) {
  62.                     return i;
  63.                 }
  64.             }
  65.             return 0;
  66.             break;
  67.         case 2:
  68.             for (int i = 0; i < TemperatureTypesCounter; i++) {
  69.                 if (strcmp(TemperatureTypes[i].name, str) == 0) {
  70.                     return i;
  71.                 }
  72.             }
  73.             return 0;
  74.             break;
  75.         case 3:
  76.             for (int i = 0; i < WaterTypesCounter; i++) {
  77.                 if (strcmp(WaterTypes[i].name, str) == 0) {
  78.                     return i;
  79.                 }
  80.             }
  81.             return 0;
  82.             break;
  83.         case 4:
  84.             for (int i = 0; i < LightTypesCounter; i++) {
  85.                 if (strcmp(LightTypes[i].name, str) == 0) {
  86.                     return i;
  87.                 }
  88.             }
  89.             return 0;
  90.             break;
  91.         default:
  92.             return -1;
  93.     }
  94. }
  95.  
  96. void PrintType(int mode) {
  97.     //1-Plant type
  98.     //2-Temperature type
  99.     //3-Water type
  100.     //4-Light type
  101.     switch (mode){
  102.         case 1:
  103.             printf("-----------Plant Types List-----------\n");
  104.             for (int i = 0; i < PlantTypesCounter; i++) {
  105.                 printf("\n%d) %s", PlantTypes[i].id, PlantTypes[i].name);
  106.             }
  107.             break;
  108.         case 2:
  109.             printf("-----------Temperature Types List-----------\n");
  110.             for (int i = 0; i < TemperatureTypesCounter; i++) {
  111.                 printf("\n%d) %s", TemperatureTypes[i].id, TemperatureTypes[i].name);
  112.             }
  113.             break;
  114.         case 3:
  115.             printf("-----------Water Types List-----------\n");
  116.             for (int i = 0; i < WaterTypesCounter; i++) {
  117.                 printf("\n%d) %s", WaterTypes[i].id, WaterTypes[i].name);
  118.             }
  119.             break;
  120.         case 4:
  121.             printf("-----------Light Types List-----------\n");
  122.             for (int i = 0; i < LightTypesCounter; i++) {
  123.                 printf("\n%d) %s", LightTypes[i].id, LightTypes[i].name);
  124.             }
  125.             break;
  126.         default:
  127.             break;
  128.     }
  129. }
  130.  
  131. void AddType(char* str, int mode,int ask) {
  132.     //1-Plant type
  133.     //2-Temperature type
  134.     //3-Water type
  135.     //4-Light type
  136.     if(ask==1){
  137.         str = malloc(stringlength* sizeof(char));
  138.         printf("Enter name for type:");
  139.         scanf("%"STR2(stringlength)"s",str);
  140.     }
  141.     switch(mode){
  142.         case 1:
  143.             PlantTypesCounter++;
  144.             PlantTypes = realloc(PlantTypes, PlantTypesCounter * sizeof(PlantType));
  145.             PlantType* newPlantType = PlantTypes + PlantTypesCounter - 1;
  146.             newPlantType->name = malloc(stringlength * sizeof(char));
  147.             newPlantType->id = PlantTypesCounter;
  148.             strcpy(newPlantType->name, str);
  149.             break;
  150.         case 2:
  151.             TemperatureTypesCounter++;
  152.             TemperatureTypes = realloc(TemperatureTypes, TemperatureTypesCounter * sizeof(TemperatureType));
  153.             TemperatureType* newTemperatureType = TemperatureTypes + TemperatureTypesCounter - 1;
  154.             newTemperatureType->name = malloc(stringlength * sizeof(char));
  155.             newTemperatureType->id = TemperatureTypesCounter;
  156.             strcpy(newTemperatureType->name, str);
  157.             break;
  158.         case 3:
  159.             WaterTypesCounter++;
  160.             WaterTypes = realloc(WaterTypes, WaterTypesCounter * sizeof(WaterType));
  161.             WaterType* newWaterType = WaterTypes + WaterTypesCounter - 1;
  162.             newWaterType->name = malloc(stringlength * sizeof(char));
  163.             newWaterType->id = WaterTypesCounter;
  164.             strcpy(newWaterType->name, str);
  165.             break;
  166.         case 4:
  167.             LightTypesCounter++;
  168.             LightTypes = realloc(LightTypes, LightTypesCounter * sizeof(LightType));
  169.             LightType* newLightType = LightTypes + LightTypesCounter - 1;
  170.             newLightType->name = malloc(stringlength * sizeof(char));
  171.             newLightType->id = LightTypesCounter;
  172.             strcpy(newLightType->name, str);
  173.             break;
  174.         default:
  175.             break;
  176.     }
  177. }
  178.  
  179. void RemoveType(int mode){
  180.     int id;
  181.     PrintType(mode);
  182.     printf("\nEnter id of the type you want to remove:");
  183.     scanf("%d", &id);
  184.     switch(mode){
  185.         case 1:
  186.             for (int i = 0; i < PlantTypesCounter; i++) {
  187.                 if (PlantTypes[i].id == id) {
  188.                     char* old = malloc(stringlength* sizeof(char));
  189.                     int oldid;
  190.                     strcpy(old, PlantTypes[i].name);
  191.                     oldid = PlantTypes[i].id;
  192.                     for (int k = i; k < PlantTypesCounter - 1; k++) {
  193.                         PlantTypes[k].name = PlantTypes[k + 1].name;
  194.                         PlantTypes[k].id = PlantTypes[k + 1].id - 1;
  195.                     }
  196.                     PlantTypesCounter--;
  197.                     PlantTypes = realloc(PlantTypes, PlantTypesCounter * sizeof(PlantType));
  198.                     for (int j = 0; j < PlantsCounter; j++) {
  199.                         if (strcmp(Plants[i].type, old) == 0) {
  200.                             strcpy(Plants[i].type, "#No type");
  201.                         }
  202.                     }
  203.                     break;
  204.                 }
  205.             }
  206.             break;
  207.         case 2:
  208.             for (int i = 0; i < TemperatureTypesCounter; i++) {
  209.                 if (TemperatureTypes[i].id == id) {
  210.                     char* old = malloc(stringlength* sizeof(char));
  211.                     int oldid;
  212.                     strcpy(old, TemperatureTypes[i].name);
  213.                     oldid = TemperatureTypes[i].id;
  214.  
  215.                     for (int k = i; k < TemperatureTypesCounter - 1; k++) {
  216.                         TemperatureTypes[k].name = TemperatureTypes[k + 1].name;
  217.                         TemperatureTypes[k].id = TemperatureTypes[k + 1].id - 1;
  218.                     }
  219.  
  220.                     TemperatureTypesCounter--;
  221.                     TemperatureTypes = realloc(TemperatureTypes, TemperatureTypesCounter * sizeof(TemperatureType));
  222.                     for (int j = 0; j < PlantsCounter; j++) {
  223.                         if (strcmp(Plants[i].temperature, old) == 0) {
  224.                             strcpy(Plants[i].temperature, "#No type");
  225.                         }
  226.                     }
  227.                     break;
  228.                 }
  229.             }
  230.             break;
  231.         case 3:
  232.             for (int i = 0; i < WaterTypesCounter; i++) {
  233.                 if (WaterTypes[i].id == id) {
  234.                     char* old = malloc(stringlength* sizeof(char));
  235.                     int oldid;
  236.                     strcpy(old, WaterTypes[i].name);
  237.                     oldid = WaterTypes[i].id;
  238.  
  239.                     for (int k = i; k < WaterTypesCounter - 1; k++) {
  240.                         WaterTypes[k].name = WaterTypes[k + 1].name;
  241.                         WaterTypes[k].id = WaterTypes[k + 1].id - 1;
  242.                     }
  243.  
  244.                     WaterTypesCounter--;
  245.                     WaterTypes = realloc(WaterTypes, WaterTypesCounter * sizeof(WaterType));
  246.                     for (int j = 0; j < PlantsCounter; j++) {
  247.                         if (strcmp(Plants[i].water, old) == 0) {
  248.                             strcpy(Plants[i].water, "#No type");
  249.                         }
  250.                     }
  251.                     break;
  252.                 }
  253.             }
  254.             break;
  255.         case 4:
  256.             for (int i = 0; i < LightTypesCounter; i++) {
  257.                 if (LightTypes[i].id == id) {
  258.                     char* old = malloc(stringlength* sizeof(char));
  259.                     int oldid;
  260.                     strcpy(old, LightTypes[i].name);
  261.                     oldid = LightTypes[i].id;
  262.  
  263.                     for (int k = i; k < LightTypesCounter - 1; k++) {
  264.                         LightTypes[k].name = LightTypes[k + 1].name;
  265.                         LightTypes[k].id = LightTypes[k + 1].id - 1;
  266.                     }
  267.  
  268.                     LightTypesCounter--;
  269.                     LightTypes = realloc(LightTypes, LightTypesCounter * sizeof(LightType));
  270.                     for (int j = 0; j < PlantsCounter; j++) {
  271.                         if (strcmp(Plants[i].light, old) == 0) {
  272.                             strcpy(Plants[i].light, "#No type");
  273.                         }
  274.                     }
  275.                     break;
  276.                 }
  277.             }
  278.             break;
  279.         default:
  280.             break;
  281.     }
  282. };
  283.  
  284. void AddPlant() {
  285.     PlantsCounter++;
  286.     Plants = realloc(Plants, PlantsCounter * sizeof(Plant));
  287.     Plant* newPlant = Plants + PlantsCounter - 1;
  288.     newPlant->id = PlantsCounter;
  289.     newPlant->name = malloc(sizeof(char) * stringlength);
  290.     newPlant->type = malloc(sizeof(char) * stringlength);
  291.     newPlant->photo = malloc(sizeof(char) * stringlength);
  292.     newPlant->desc = malloc(sizeof(char) * stringlength);
  293.     newPlant->bloom = malloc(sizeof(char) * stringlength);
  294.     newPlant->temperature = malloc(sizeof(char) * stringlength);
  295.     newPlant->water = malloc(sizeof(char) * stringlength);
  296.     newPlant->light = malloc(sizeof(char) * stringlength);
  297.     char* tempname = malloc(sizeof(char) * stringlength);
  298.  
  299.     printf("\n-----------New Bakery-----------");
  300.     printf("\nName:");
  301.     scanf("%" STR2(stringlength)"s", newPlant->name);
  302.  
  303.     printf("Type:");
  304.     scanf("%" STR2(stringlength)"s", tempname);
  305.     int typeexists = FindType(tempname,1);
  306.     if (typeexists > 0) {
  307.         strcpy(newPlant->type, PlantTypes[typeexists - 1].name);
  308.     }
  309.     else {
  310.         AddType(tempname,1,0);
  311.         strcpy(newPlant->type, tempname);
  312.     }
  313.  
  314.     printf("Photo:");
  315.     scanf("%" STR2(stringlength)"s", newPlant->photo);
  316.     printf("Description:");
  317.     scanf("%" STR2(stringlength)"s", newPlant->desc);
  318.     printf("Bloom:");
  319.     scanf("%" STR2(stringlength)"s", newPlant->bloom);
  320.  
  321.     printf("Temperature:");
  322.     scanf("%" STR2(stringlength)"s", tempname);
  323.     typeexists = FindType(tempname,2);
  324.     if (typeexists > 0) {
  325.         strcpy(newPlant->temperature, TemperatureTypes[typeexists - 1].name);
  326.     }
  327.     else {
  328.         AddType(tempname, 2,0);
  329.         strcpy(newPlant->temperature, tempname);
  330.     }
  331.  
  332.     printf("Water:");
  333.     scanf("%" STR2(stringlength)"s", tempname);
  334.     typeexists = FindType(tempname,3);
  335.     if (typeexists > 0) {
  336.         strcpy(newPlant->water, WaterTypes[typeexists - 1].name);
  337.     }
  338.     else {
  339.         AddType(tempname, 3,0);
  340.         strcpy(newPlant->water, tempname);
  341.     }
  342.  
  343.     printf("Light:");
  344.     scanf("%" STR2(stringlength)"s", tempname);
  345.     typeexists = FindType(tempname,4);
  346.     if (typeexists > 0) {
  347.         strcpy(newPlant->light, LightTypes[typeexists - 1].name);
  348.     }
  349.     else {
  350.         AddType(tempname, 4,0);
  351.         strcpy(newPlant->light, tempname);
  352.     }
  353. }
  354.  
  355. void PrintPlant(int id){
  356.     if (id == -1) {
  357.         printf("-----------Plants List-----------\n");
  358.         for (int i = 0; i < PlantsCounter; i++) {
  359.             printf("\n=====--- %d ---=====", Plants[i].id);
  360.             printf("\nName: %s", Plants[i].name);
  361.             printf("\nType: %s", Plants[i].type);
  362.             printf("\nPhoto: %s", Plants[i].photo);
  363.             printf("\nDescription: %s", Plants[i].desc);
  364.             printf("\nBloom: %s", Plants[i].bloom);
  365.             printf("\nTemperature: %s", Plants[i].temperature);
  366.             printf("\nWater: %s", Plants[i].water);
  367.             printf("\nLight: %s", Plants[i].light);
  368.         }
  369.     }
  370.     else {
  371.         for (int i = 0; i < PlantsCounter; i++) {
  372.             if (Plants[i].id == id) {
  373.                 printf("\n=====--- %d ---=====", Plants[i].id);
  374.                 printf("\nName: %s", Plants[i].name);
  375.                 printf("\nType: %s", Plants[i].type);
  376.                 printf("\nPhoto: %s", Plants[i].photo);
  377.                 printf("\nDescription: %s", Plants[i].desc);
  378.                 printf("\nBloom: %s", Plants[i].bloom);
  379.                 printf("\nTemperature: %s", Plants[i].temperature);
  380.                 printf("\nWater: %s", Plants[i].water);
  381.                 printf("\nLight: %s", Plants[i].light);
  382.                 break;
  383.             }
  384.         }
  385.     }
  386. }
  387.  
  388. void RemovePlant(){
  389.     int id;
  390.     PrintPlant(-1);
  391.     printf("\nEnter id of Plant you want to remove:");
  392.     scanf("%d", &id);
  393.     for (int i = 0; i < PlantsCounter; i++) {
  394.         if (Plants[i].id == id) {
  395.             for (int k = i; k < PlantsCounter - 1; k++) {
  396.                 Plants[k] = Plants[k + 1];
  397.                 Plants[k].id--;
  398.             }
  399.             PlantsCounter--;
  400.             Plants = realloc(Plants, PlantsCounter * sizeof(Plant));
  401.             break;
  402.         }
  403.     }
  404. }
  405.  
  406. void FindPlant() {
  407.     int option = 0;
  408.     printf("\nBy which parameter you want to search?");
  409.     printf("\n1) Name");
  410.     printf("\n2) Type");
  411.     printf("\n3) Photo");
  412.     printf("\n4) Description");
  413.     printf("\n5) Bloom");
  414.     printf("\n6) Temperature");
  415.     printf("\n7) Water");
  416.     printf("\n8) Light");
  417.     printf("\nYour choice (Number):");
  418.     scanf("%d", &option);
  419.     int number;
  420.     float division;
  421.     char* str = malloc(10);
  422.     switch (option) {
  423.         case 1:
  424.             printf("Enter name:");
  425.             scanf("%s", str);
  426.             printf("-----------Plants List-----------\n");
  427.             for (int i = 0; i < PlantsCounter; i++) {
  428.                 if (strcmp(Plants[i].name, str) == 0) {
  429.                     PrintPlant(Plants[i].id);
  430.                 }
  431.             }
  432.             break;
  433.         case 2:
  434.             printf("Enter type:");
  435.             scanf("%s", str);
  436.             printf("-----------Plants List-----------\n");
  437.             for (int i = 0; i < PlantsCounter; i++) {
  438.                 if (strcmp(Plants[i].type, str) == 0) {
  439.                     PrintPlant(Plants[i].id);
  440.                 }
  441.             }
  442.             break;
  443.         case 3:
  444.             printf("Enter photo link:");
  445.             scanf("%s", str);
  446.             printf("-----------Plants List-----------\n");
  447.             for (int i = 0; i < PlantsCounter; i++) {
  448.                 if (strcmp(Plants[i].photo, str) == 0) {
  449.                     PrintPlant(Plants[i].id);
  450.                 }
  451.             }
  452.             break;
  453.         case 4:
  454.             printf("Enter description link:");
  455.             scanf("%s", str);
  456.             printf("-----------Plants List-----------\n");
  457.             for (int i = 0; i < PlantsCounter; i++) {
  458.                 if (strcmp(Plants[i].desc, str) == 0) {
  459.                     PrintPlant(Plants[i].id);
  460.                 }
  461.             }
  462.             break;
  463.         case 5:
  464.             printf("Enter bloom:");
  465.             scanf("%s", str);
  466.             printf("-----------Plants List-----------\n");
  467.             for (int i = 0; i < PlantsCounter; i++) {
  468.                 if (strcmp(Plants[i].bloom, str) == 0) {
  469.                     PrintPlant(Plants[i].id);
  470.                 }
  471.             }
  472.             break;
  473.         case 6:
  474.             printf("Enter temperature:");
  475.             scanf("%s", str);
  476.             printf("-----------Plants List-----------\n");
  477.             for (int i = 0; i < PlantsCounter; i++) {
  478.                 if (strcmp(Plants[i].temperature, str) == 0) {
  479.                     PrintPlant(Plants[i].id);
  480.                 }
  481.             }
  482.             break;
  483.         case 7:
  484.             printf("Enter water:");
  485.             scanf("%s", str);
  486.             printf("-----------Plants List-----------\n");
  487.             for (int i = 0; i < PlantsCounter; i++) {
  488.                 if (strcmp(Plants[i].water, str) == 0) {
  489.                     PrintPlant(Plants[i].id);
  490.                 }
  491.             }
  492.             break;
  493.         case 8:
  494.             printf("Enter light:");
  495.             scanf("%s", str);
  496.             printf("-----------Plants List-----------\n");
  497.             for (int i = 0; i < PlantsCounter; i++) {
  498.                 if (strcmp(Plants[i].light, str) == 0) {
  499.                     PrintPlant(Plants[i].id);
  500.                 }
  501.             }
  502.             break;
  503.         default:
  504.             break;
  505.     }
  506. }
  507.  
  508. void OpenFile(){
  509.     int option = 0;
  510.     printf("\n1)Photo");
  511.     printf("\n2)Decription");
  512.     printf("\nWhat do you want to open :");
  513.     scanf("%d",&option);
  514.     if(option==1){
  515.         int id;
  516.         PrintPlant(-1);
  517.         printf("\nWhich plants picture (id):");
  518.         scanf("%d",&id);
  519.         for (int i = 0; i < PlantsCounter; i++) {
  520.             if (Plants[i].id == id) {
  521.                 char command[100]="start ../Photos/";
  522.                 system(strcat(command,Plants[i].photo));
  523.             }
  524.         }
  525.     }
  526.     if(option==2){
  527.         int id;
  528.         PrintPlant(-1);
  529.         printf("\nWhich plants descriiption link (id):");
  530.         scanf("%d",&id);
  531.         for (int i = 0; i < PlantsCounter; i++) {
  532.             if (Plants[i].id == id) {
  533.                 char command[100]="start ../Description/";
  534.                 system(strcat(command,Plants[i].desc));
  535.             }
  536.         }
  537.     }
  538. }
  539.  
  540. int main() {
  541.     setbuf(stdout, 0);
  542.     int cycle = 1;
  543.     while (cycle == 1) {
  544.         int choice = 0;
  545.         printf("\n\n-----------Menu-----------");
  546.         printf("\n1)Plant list");//
  547.         printf("\n2)Plant add");//
  548.         printf("\n3)Plant remove");//
  549.         printf("\n4)Plant find");//
  550.         printf("\n5)Open Picture/Description");//
  551.         printf("\n");
  552.         printf("\n6)Plant type list");//
  553.         printf("\n7)Plant type add");//
  554.         printf("\n8)Plant type remove");//
  555.         printf("\n");
  556.         printf("\n9)Temperature type list");//
  557.         printf("\n10)Temperature type add");//
  558.         printf("\n11)Temperature type remove");//
  559.         printf("\n");
  560.         printf("\n12)Water type list");//
  561.         printf("\n13)Water type add");//
  562.         printf("\n14)Water type remove");//
  563.         printf("\n");
  564.         printf("\n15)Light type list");//
  565.         printf("\n16)Light type add");//
  566.         printf("\n17)Light type remove");//
  567.         printf("\n");
  568.         printf("\n18)Exit");//
  569.         printf("\nEnter your choice:");
  570.         scanf("%d", &choice);
  571.         switch (choice) {
  572.             //Plants
  573.             case 1:
  574.                 PrintPlant(-1);
  575.                 break;
  576.             case 2:
  577.                 AddPlant();
  578.                 break;
  579.             case 3:
  580.                 RemovePlant();
  581.                 break;
  582.             case 4:
  583.                 FindPlant();
  584.                 break;
  585.             case 5:
  586.                 OpenFile();
  587.                 break;
  588.             //Plant Types
  589.             case 6:
  590.                 PrintType(1);
  591.                 break;
  592.             case 7:
  593.                 AddType("",1,1);
  594.                 break;
  595.             case 8:
  596.                 RemoveType(1);
  597.                 break;
  598.             //Temperature types
  599.             case 9:
  600.                 PrintType(2);
  601.                 break;
  602.             case 10:
  603.                 AddType("",2,1);
  604.                 break;
  605.             case 11:
  606.                 RemoveType(2);
  607.                 break;
  608.             //Water types
  609.             case 12:
  610.                 PrintType(3);
  611.                 break;
  612.             case 13:
  613.                 AddType("",3,1);
  614.                 break;
  615.             case 14:
  616.                 RemoveType(3);
  617.                 break;
  618.             //Light types
  619.             case 15:
  620.                 PrintType(4);
  621.                 break;
  622.             case 16:
  623.                 AddType("",4,1);
  624.                 break;
  625.             case 17:
  626.                 RemoveType(4);
  627.                 break;
  628.             //Exit
  629.             case 18:
  630.                 cycle = 0;
  631.                 break;
  632.             default:
  633.                 break;
  634.         }
  635.     }
  636. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement