Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- #define stringlength 255
- #define STR2(x) #x
- #define STR(X) STR2(X)
- typedef struct PlantStruct {
- int id;
- char *name;
- char *type;
- char *photo;
- char *desc;
- char *bloom;
- char *temperature;
- char *water;
- char *light;
- } Plant;
- Plant* Plants = NULL;
- int PlantsCounter = 0;
- typedef struct PlantTypeStruct{
- int id;
- char *name;
- } PlantType;
- PlantType* PlantTypes = NULL;
- int PlantTypesCounter = 0;
- typedef struct TemperatureTypeStruct{
- int id;
- char *name;
- } TemperatureType;
- TemperatureType* TemperatureTypes = NULL;
- int TemperatureTypesCounter = 0;
- typedef struct WaterTypeStruct{
- int id;
- char *name;
- } WaterType;
- WaterType* WaterTypes = NULL;
- int WaterTypesCounter = 0;
- typedef struct LightTypeStruct{
- int id;
- char *name;
- } LightType;
- LightType* LightTypes = NULL;
- int LightTypesCounter = 0;
- int FindType(char* str,int mode) {
- //Modes:
- //1-Plant Types
- //2-Temperature
- //3-Water
- //4-Light
- switch(mode){
- case 1:
- for (int i = 0; i < PlantTypesCounter; i++) {
- if (strcmp(PlantTypes[i].name, str) == 0) {
- return i;
- }
- }
- return 0;
- break;
- case 2:
- for (int i = 0; i < TemperatureTypesCounter; i++) {
- if (strcmp(TemperatureTypes[i].name, str) == 0) {
- return i;
- }
- }
- return 0;
- break;
- case 3:
- for (int i = 0; i < WaterTypesCounter; i++) {
- if (strcmp(WaterTypes[i].name, str) == 0) {
- return i;
- }
- }
- return 0;
- break;
- case 4:
- for (int i = 0; i < LightTypesCounter; i++) {
- if (strcmp(LightTypes[i].name, str) == 0) {
- return i;
- }
- }
- return 0;
- break;
- default:
- return -1;
- }
- }
- void PrintType(int mode) {
- //1-Plant type
- //2-Temperature type
- //3-Water type
- //4-Light type
- switch (mode){
- case 1:
- printf("-----------Plant Types List-----------\n");
- for (int i = 0; i < PlantTypesCounter; i++) {
- printf("\n%d) %s", PlantTypes[i].id, PlantTypes[i].name);
- }
- break;
- case 2:
- printf("-----------Temperature Types List-----------\n");
- for (int i = 0; i < TemperatureTypesCounter; i++) {
- printf("\n%d) %s", TemperatureTypes[i].id, TemperatureTypes[i].name);
- }
- break;
- case 3:
- printf("-----------Water Types List-----------\n");
- for (int i = 0; i < WaterTypesCounter; i++) {
- printf("\n%d) %s", WaterTypes[i].id, WaterTypes[i].name);
- }
- break;
- case 4:
- printf("-----------Light Types List-----------\n");
- for (int i = 0; i < LightTypesCounter; i++) {
- printf("\n%d) %s", LightTypes[i].id, LightTypes[i].name);
- }
- break;
- default:
- break;
- }
- }
- void AddType(char* str, int mode,int ask) {
- //1-Plant type
- //2-Temperature type
- //3-Water type
- //4-Light type
- if(ask==1){
- str = malloc(stringlength* sizeof(char));
- printf("Enter name for type:");
- scanf("%"STR2(stringlength)"s",str);
- }
- switch(mode){
- case 1:
- PlantTypesCounter++;
- PlantTypes = realloc(PlantTypes, PlantTypesCounter * sizeof(PlantType));
- PlantType* newPlantType = PlantTypes + PlantTypesCounter - 1;
- newPlantType->name = malloc(stringlength * sizeof(char));
- newPlantType->id = PlantTypesCounter;
- strcpy(newPlantType->name, str);
- break;
- case 2:
- TemperatureTypesCounter++;
- TemperatureTypes = realloc(TemperatureTypes, TemperatureTypesCounter * sizeof(TemperatureType));
- TemperatureType* newTemperatureType = TemperatureTypes + TemperatureTypesCounter - 1;
- newTemperatureType->name = malloc(stringlength * sizeof(char));
- newTemperatureType->id = TemperatureTypesCounter;
- strcpy(newTemperatureType->name, str);
- break;
- case 3:
- WaterTypesCounter++;
- WaterTypes = realloc(WaterTypes, WaterTypesCounter * sizeof(WaterType));
- WaterType* newWaterType = WaterTypes + WaterTypesCounter - 1;
- newWaterType->name = malloc(stringlength * sizeof(char));
- newWaterType->id = WaterTypesCounter;
- strcpy(newWaterType->name, str);
- break;
- case 4:
- LightTypesCounter++;
- LightTypes = realloc(LightTypes, LightTypesCounter * sizeof(LightType));
- LightType* newLightType = LightTypes + LightTypesCounter - 1;
- newLightType->name = malloc(stringlength * sizeof(char));
- newLightType->id = LightTypesCounter;
- strcpy(newLightType->name, str);
- break;
- default:
- break;
- }
- }
- void RemoveType(int mode){
- int id;
- PrintType(mode);
- printf("\nEnter id of the type you want to remove:");
- scanf("%d", &id);
- switch(mode){
- case 1:
- for (int i = 0; i < PlantTypesCounter; i++) {
- if (PlantTypes[i].id == id) {
- char* old = malloc(stringlength* sizeof(char));
- int oldid;
- strcpy(old, PlantTypes[i].name);
- oldid = PlantTypes[i].id;
- for (int k = i; k < PlantTypesCounter - 1; k++) {
- PlantTypes[k].name = PlantTypes[k + 1].name;
- PlantTypes[k].id = PlantTypes[k + 1].id - 1;
- }
- PlantTypesCounter--;
- PlantTypes = realloc(PlantTypes, PlantTypesCounter * sizeof(PlantType));
- for (int j = 0; j < PlantsCounter; j++) {
- if (strcmp(Plants[i].type, old) == 0) {
- strcpy(Plants[i].type, "#No type");
- }
- }
- break;
- }
- }
- break;
- case 2:
- for (int i = 0; i < TemperatureTypesCounter; i++) {
- if (TemperatureTypes[i].id == id) {
- char* old = malloc(stringlength* sizeof(char));
- int oldid;
- strcpy(old, TemperatureTypes[i].name);
- oldid = TemperatureTypes[i].id;
- for (int k = i; k < TemperatureTypesCounter - 1; k++) {
- TemperatureTypes[k].name = TemperatureTypes[k + 1].name;
- TemperatureTypes[k].id = TemperatureTypes[k + 1].id - 1;
- }
- TemperatureTypesCounter--;
- TemperatureTypes = realloc(TemperatureTypes, TemperatureTypesCounter * sizeof(TemperatureType));
- for (int j = 0; j < PlantsCounter; j++) {
- if (strcmp(Plants[i].temperature, old) == 0) {
- strcpy(Plants[i].temperature, "#No type");
- }
- }
- break;
- }
- }
- break;
- case 3:
- for (int i = 0; i < WaterTypesCounter; i++) {
- if (WaterTypes[i].id == id) {
- char* old = malloc(stringlength* sizeof(char));
- int oldid;
- strcpy(old, WaterTypes[i].name);
- oldid = WaterTypes[i].id;
- for (int k = i; k < WaterTypesCounter - 1; k++) {
- WaterTypes[k].name = WaterTypes[k + 1].name;
- WaterTypes[k].id = WaterTypes[k + 1].id - 1;
- }
- WaterTypesCounter--;
- WaterTypes = realloc(WaterTypes, WaterTypesCounter * sizeof(WaterType));
- for (int j = 0; j < PlantsCounter; j++) {
- if (strcmp(Plants[i].water, old) == 0) {
- strcpy(Plants[i].water, "#No type");
- }
- }
- break;
- }
- }
- break;
- case 4:
- for (int i = 0; i < LightTypesCounter; i++) {
- if (LightTypes[i].id == id) {
- char* old = malloc(stringlength* sizeof(char));
- int oldid;
- strcpy(old, LightTypes[i].name);
- oldid = LightTypes[i].id;
- for (int k = i; k < LightTypesCounter - 1; k++) {
- LightTypes[k].name = LightTypes[k + 1].name;
- LightTypes[k].id = LightTypes[k + 1].id - 1;
- }
- LightTypesCounter--;
- LightTypes = realloc(LightTypes, LightTypesCounter * sizeof(LightType));
- for (int j = 0; j < PlantsCounter; j++) {
- if (strcmp(Plants[i].light, old) == 0) {
- strcpy(Plants[i].light, "#No type");
- }
- }
- break;
- }
- }
- break;
- default:
- break;
- }
- };
- void AddPlant() {
- PlantsCounter++;
- Plants = realloc(Plants, PlantsCounter * sizeof(Plant));
- Plant* newPlant = Plants + PlantsCounter - 1;
- newPlant->id = PlantsCounter;
- newPlant->name = malloc(sizeof(char) * stringlength);
- newPlant->type = malloc(sizeof(char) * stringlength);
- newPlant->photo = malloc(sizeof(char) * stringlength);
- newPlant->desc = malloc(sizeof(char) * stringlength);
- newPlant->bloom = malloc(sizeof(char) * stringlength);
- newPlant->temperature = malloc(sizeof(char) * stringlength);
- newPlant->water = malloc(sizeof(char) * stringlength);
- newPlant->light = malloc(sizeof(char) * stringlength);
- char* tempname = malloc(sizeof(char) * stringlength);
- printf("\n-----------New Bakery-----------");
- printf("\nName:");
- scanf("%" STR2(stringlength)"s", newPlant->name);
- printf("Type:");
- scanf("%" STR2(stringlength)"s", tempname);
- int typeexists = FindType(tempname,1);
- if (typeexists > 0) {
- strcpy(newPlant->type, PlantTypes[typeexists - 1].name);
- }
- else {
- AddType(tempname,1,0);
- strcpy(newPlant->type, tempname);
- }
- printf("Photo:");
- scanf("%" STR2(stringlength)"s", newPlant->photo);
- printf("Description:");
- scanf("%" STR2(stringlength)"s", newPlant->desc);
- printf("Bloom:");
- scanf("%" STR2(stringlength)"s", newPlant->bloom);
- printf("Temperature:");
- scanf("%" STR2(stringlength)"s", tempname);
- typeexists = FindType(tempname,2);
- if (typeexists > 0) {
- strcpy(newPlant->temperature, TemperatureTypes[typeexists - 1].name);
- }
- else {
- AddType(tempname, 2,0);
- strcpy(newPlant->temperature, tempname);
- }
- printf("Water:");
- scanf("%" STR2(stringlength)"s", tempname);
- typeexists = FindType(tempname,3);
- if (typeexists > 0) {
- strcpy(newPlant->water, WaterTypes[typeexists - 1].name);
- }
- else {
- AddType(tempname, 3,0);
- strcpy(newPlant->water, tempname);
- }
- printf("Light:");
- scanf("%" STR2(stringlength)"s", tempname);
- typeexists = FindType(tempname,4);
- if (typeexists > 0) {
- strcpy(newPlant->light, LightTypes[typeexists - 1].name);
- }
- else {
- AddType(tempname, 4,0);
- strcpy(newPlant->light, tempname);
- }
- }
- void PrintPlant(int id){
- if (id == -1) {
- printf("-----------Plants List-----------\n");
- for (int i = 0; i < PlantsCounter; i++) {
- printf("\n=====--- %d ---=====", Plants[i].id);
- printf("\nName: %s", Plants[i].name);
- printf("\nType: %s", Plants[i].type);
- printf("\nPhoto: %s", Plants[i].photo);
- printf("\nDescription: %s", Plants[i].desc);
- printf("\nBloom: %s", Plants[i].bloom);
- printf("\nTemperature: %s", Plants[i].temperature);
- printf("\nWater: %s", Plants[i].water);
- printf("\nLight: %s", Plants[i].light);
- }
- }
- else {
- for (int i = 0; i < PlantsCounter; i++) {
- if (Plants[i].id == id) {
- printf("\n=====--- %d ---=====", Plants[i].id);
- printf("\nName: %s", Plants[i].name);
- printf("\nType: %s", Plants[i].type);
- printf("\nPhoto: %s", Plants[i].photo);
- printf("\nDescription: %s", Plants[i].desc);
- printf("\nBloom: %s", Plants[i].bloom);
- printf("\nTemperature: %s", Plants[i].temperature);
- printf("\nWater: %s", Plants[i].water);
- printf("\nLight: %s", Plants[i].light);
- break;
- }
- }
- }
- }
- void RemovePlant(){
- int id;
- PrintPlant(-1);
- printf("\nEnter id of Plant you want to remove:");
- scanf("%d", &id);
- for (int i = 0; i < PlantsCounter; i++) {
- if (Plants[i].id == id) {
- for (int k = i; k < PlantsCounter - 1; k++) {
- Plants[k] = Plants[k + 1];
- Plants[k].id--;
- }
- PlantsCounter--;
- Plants = realloc(Plants, PlantsCounter * sizeof(Plant));
- break;
- }
- }
- }
- void FindPlant() {
- int option = 0;
- printf("\nBy which parameter you want to search?");
- printf("\n1) Name");
- printf("\n2) Type");
- printf("\n3) Photo");
- printf("\n4) Description");
- printf("\n5) Bloom");
- printf("\n6) Temperature");
- printf("\n7) Water");
- printf("\n8) Light");
- printf("\nYour choice (Number):");
- scanf("%d", &option);
- int number;
- float division;
- char* str = malloc(10);
- switch (option) {
- case 1:
- printf("Enter name:");
- scanf("%s", str);
- printf("-----------Plants List-----------\n");
- for (int i = 0; i < PlantsCounter; i++) {
- if (strcmp(Plants[i].name, str) == 0) {
- PrintPlant(Plants[i].id);
- }
- }
- break;
- case 2:
- printf("Enter type:");
- scanf("%s", str);
- printf("-----------Plants List-----------\n");
- for (int i = 0; i < PlantsCounter; i++) {
- if (strcmp(Plants[i].type, str) == 0) {
- PrintPlant(Plants[i].id);
- }
- }
- break;
- case 3:
- printf("Enter photo link:");
- scanf("%s", str);
- printf("-----------Plants List-----------\n");
- for (int i = 0; i < PlantsCounter; i++) {
- if (strcmp(Plants[i].photo, str) == 0) {
- PrintPlant(Plants[i].id);
- }
- }
- break;
- case 4:
- printf("Enter description link:");
- scanf("%s", str);
- printf("-----------Plants List-----------\n");
- for (int i = 0; i < PlantsCounter; i++) {
- if (strcmp(Plants[i].desc, str) == 0) {
- PrintPlant(Plants[i].id);
- }
- }
- break;
- case 5:
- printf("Enter bloom:");
- scanf("%s", str);
- printf("-----------Plants List-----------\n");
- for (int i = 0; i < PlantsCounter; i++) {
- if (strcmp(Plants[i].bloom, str) == 0) {
- PrintPlant(Plants[i].id);
- }
- }
- break;
- case 6:
- printf("Enter temperature:");
- scanf("%s", str);
- printf("-----------Plants List-----------\n");
- for (int i = 0; i < PlantsCounter; i++) {
- if (strcmp(Plants[i].temperature, str) == 0) {
- PrintPlant(Plants[i].id);
- }
- }
- break;
- case 7:
- printf("Enter water:");
- scanf("%s", str);
- printf("-----------Plants List-----------\n");
- for (int i = 0; i < PlantsCounter; i++) {
- if (strcmp(Plants[i].water, str) == 0) {
- PrintPlant(Plants[i].id);
- }
- }
- break;
- case 8:
- printf("Enter light:");
- scanf("%s", str);
- printf("-----------Plants List-----------\n");
- for (int i = 0; i < PlantsCounter; i++) {
- if (strcmp(Plants[i].light, str) == 0) {
- PrintPlant(Plants[i].id);
- }
- }
- break;
- default:
- break;
- }
- }
- void OpenFile(){
- int option = 0;
- printf("\n1)Photo");
- printf("\n2)Decription");
- printf("\nWhat do you want to open :");
- scanf("%d",&option);
- if(option==1){
- int id;
- PrintPlant(-1);
- printf("\nWhich plants picture (id):");
- scanf("%d",&id);
- for (int i = 0; i < PlantsCounter; i++) {
- if (Plants[i].id == id) {
- char command[100]="start ../Photos/";
- system(strcat(command,Plants[i].photo));
- }
- }
- }
- if(option==2){
- int id;
- PrintPlant(-1);
- printf("\nWhich plants descriiption link (id):");
- scanf("%d",&id);
- for (int i = 0; i < PlantsCounter; i++) {
- if (Plants[i].id == id) {
- char command[100]="start ../Description/";
- system(strcat(command,Plants[i].desc));
- }
- }
- }
- }
- int main() {
- setbuf(stdout, 0);
- int cycle = 1;
- while (cycle == 1) {
- int choice = 0;
- printf("\n\n-----------Menu-----------");
- printf("\n1)Plant list");//
- printf("\n2)Plant add");//
- printf("\n3)Plant remove");//
- printf("\n4)Plant find");//
- printf("\n5)Open Picture/Description");//
- printf("\n");
- printf("\n6)Plant type list");//
- printf("\n7)Plant type add");//
- printf("\n8)Plant type remove");//
- printf("\n");
- printf("\n9)Temperature type list");//
- printf("\n10)Temperature type add");//
- printf("\n11)Temperature type remove");//
- printf("\n");
- printf("\n12)Water type list");//
- printf("\n13)Water type add");//
- printf("\n14)Water type remove");//
- printf("\n");
- printf("\n15)Light type list");//
- printf("\n16)Light type add");//
- printf("\n17)Light type remove");//
- printf("\n");
- printf("\n18)Exit");//
- printf("\nEnter your choice:");
- scanf("%d", &choice);
- switch (choice) {
- //Plants
- case 1:
- PrintPlant(-1);
- break;
- case 2:
- AddPlant();
- break;
- case 3:
- RemovePlant();
- break;
- case 4:
- FindPlant();
- break;
- case 5:
- OpenFile();
- break;
- //Plant Types
- case 6:
- PrintType(1);
- break;
- case 7:
- AddType("",1,1);
- break;
- case 8:
- RemoveType(1);
- break;
- //Temperature types
- case 9:
- PrintType(2);
- break;
- case 10:
- AddType("",2,1);
- break;
- case 11:
- RemoveType(2);
- break;
- //Water types
- case 12:
- PrintType(3);
- break;
- case 13:
- AddType("",3,1);
- break;
- case 14:
- RemoveType(3);
- break;
- //Light types
- case 15:
- PrintType(4);
- break;
- case 16:
- AddType("",4,1);
- break;
- case 17:
- RemoveType(4);
- break;
- //Exit
- case 18:
- cycle = 0;
- break;
- default:
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement