Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdio.h"
- #include "stdlib.h"
- //task
- //alg + prog: input struct
- //control output source data
- //form new list out of source elements, which satisfy conditions
- //output results
- //minimalize method count, struct contains 4-5 fields
- typedef struct MY_DOG
- {
- char* name;
- float weight;
- int height;
- float dexterity;
- }Dog;
- #define MAXBUFFER 32
- #define MAXSTRING 64
- #define MAXARRAY 64
- int getInt();
- char* getString(int);
- float getFloat();
- void InitDog(Dog*);
- void PrintMenu(int hasSource, int canOutput);
- void Help();
- Dog* ReadDogs(int, int*);
- void PrintDog(Dog*);
- void PrintDogs(Dog*, int);
- Dog* FilterDogs(Dog*, int, int*);
- char *CopyString(const char*, int);
- int main()
- {
- Dog* dogs = NULL, *dogResult = NULL;
- int hasSource, canOutput, currentOp, exit, dogCount, dogResultCount;
- exit = hasSource = canOutput = dogCount = dogResultCount = 0;
- do {
- PrintMenu(hasSource, canOutput);
- printf("Input operation number\n");
- currentOp = getInt();
- switch (currentOp)
- {
- case 1: //input source
- dogs = ReadDogs(MAXARRAY, &dogCount);
- if (dogs != NULL)
- hasSource = 1;
- else
- printf("Error occured when trying to get memory for array of dogs! Please retry.\n");
- break;
- case 2: //output source
- if (hasSource)
- PrintDogs(dogs, dogCount);
- else
- printf("No input to print!\n");
- break;
- case 3: //process
- if (hasSource)
- {
- dogResult = FilterDogs(dogs, dogCount, &dogResultCount);
- if(dogResult!=NULL)
- canOutput = 1;
- else
- printf("Memory err!\n");
- }
- else
- printf("No input to process!\n");
- break;
- case 4: //output result
- if (canOutput)
- PrintDogs(dogResult, dogResultCount);
- else
- printf("No processed data to output!\n");
- break;
- case 5: //help
- Help();
- break;
- case 6: //exit
- exit = 1;
- break;
- default:
- printf("Operation not found!\n");
- break;
- }
- system("pause");
- system("cls");
- } while (!exit);
- for (exit = 0; exit < dogCount; exit++)
- free((dogs + exit)->name);
- for (exit = 0; exit < dogResultCount; exit++)
- free((dogResult + exit)->name);
- free(dogs);
- free(dogResult);
- return 0;
- }
- int getInt()
- {
- char line[MAXBUFFER]; //buffer
- char curChar = ' ';
- int temp, result;
- do {
- line[MAXBUFFER - 1] = '\n';
- fgets(line, MAXBUFFER - 1, stdin);
- temp = sscanf(line, "%d%c", &result, &curChar);
- temp = !temp || temp < 0 || (curChar != '\n' && curChar != ' ');
- if (temp)
- printf("Error reading number. Please, try again.\n");
- } while (temp); //not a number actually
- if (line[MAXBUFFER - 1] != '\n') //clear all iput
- while ((curChar = getchar()) != '\n' && curChar != EOF);
- return result;
- }
- char* getString(int maxLength)
- {
- char* line = NULL;
- int i;
- char curChar = ' ';
- line = malloc(maxLength * sizeof(char));
- if (line != NULL)
- {
- i = 0;
- while ((curChar = getchar()) != '\n')
- line[i++] = curChar;
- line[i] = '\0';
- }
- return line;
- }
- float getFloat()
- {
- char line[MAXBUFFER]; //buffer
- char curChar = ' ';
- int temp;
- float result;
- do {
- line[MAXBUFFER - 1] = '\n';
- fgets(line, MAXBUFFER - 1, stdin);
- temp = sscanf(line, "%f%c", &result, &curChar);
- temp = !temp || temp < 0 || (curChar != '\n' && curChar != ' ');
- if (temp)
- printf("Error reading number. Please, try again.\n");
- } while (temp); //not a number actually
- if (line[MAXBUFFER - 1] != '\n') //clear all iput
- while ((curChar = getchar()) != '\n' && curChar != EOF);
- return result;
- }
- void InitDog(Dog* dog)
- {
- printf("Enter dog's name:\n");
- dog->name = getString(MAXSTRING);
- printf("Enter %s's weight (float):\n", dog->name);
- dog->weight = getFloat();
- printf("Enter %s's dexterity (float):\n", dog->name);
- dog->dexterity = getFloat();
- printf("Enter %s's height (integer):\n", dog->name);
- dog->height = getInt();
- }
- Dog* ReadDogs(int maxCount, int* count)
- {
- int responce;
- Dog *dogs=NULL, *reserve=NULL;
- void* mem;
- *count = 1;
- responce = 0;
- system("cls");
- dogs = (Dog*)malloc(sizeof(Dog));
- do {
- if (dogs == NULL) {
- responce = 0;
- free(reserve);
- }
- else {
- InitDog(dogs + (*count)-1);
- printf("Continue? (1 = yes, 0 = no)\n");
- if (responce = getInt()) {
- if (mem = realloc(dogs, (++*count * sizeof(*dogs))))
- dogs = (Dog*)mem;
- else
- free(dogs);
- system("cls");
- }
- }
- } while (responce);
- return dogs;
- }
- void PrintDog(Dog* dog)
- {
- printf("Dog name: %s\n", dog->name);
- printf("\t%s's weight: %f\n", dog->name, dog->weight);
- printf("\t%s's height: %d\n", dog->name, dog->height);
- printf("\t%s's dexterity: %f\n", dog->name, dog->dexterity);
- }
- void PrintDogs(Dog* dogs, int count)
- {
- int i;
- for (i = 0; i < count; i++)
- {
- printf("%d) ", i + 1);
- PrintDog(dogs + i);
- }
- }
- Dog* FilterDogs(Dog* dogs, int count, int *resultCount)
- {
- int i;
- float dexRangeMin, dexRangeMax, weightRangeMin, weightRangeMax;
- dexRangeMin = dexRangeMax = weightRangeMin = weightRangeMax = 0;
- void* mem;
- Dog *goodDogs;
- goodDogs = (Dog*)malloc(sizeof(Dog));
- *resultCount = 0;
- system("cls");
- do {
- printf("Input dexterity range (min and max):\n");
- scanf("%f%f", &dexRangeMin, &dexRangeMax);
- if (i = dexRangeMin >= dexRangeMax)
- printf("bad range!\n");
- } while (i);
- do {
- printf("Input weight range (min and max):\n");
- scanf("%f%f", &weightRangeMin, &weightRangeMax);
- if (i = dexRangeMin >= dexRangeMax)
- printf("bad range!\n");
- } while (i);
- for (i = 0; i < count && goodDogs != NULL; i++)
- {
- if ((dogs + i)->dexterity > dexRangeMin && (dogs + i)->dexterity < dexRangeMax&&
- (dogs + i)->weight > weightRangeMin && (dogs + i)->weight < weightRangeMax) {
- if (*resultCount != 0)
- if (mem = realloc(goodDogs, (++*resultCount * sizeof(*goodDogs))))
- goodDogs = (Dog*)mem;
- else
- free(goodDogs);
- else
- *resultCount = 1;
- *(goodDogs + *resultCount - 1) = *(dogs + i);
- (goodDogs + *resultCount - 1)->name = CopyString((dogs + i)->name, MAXSTRING);
- }
- }
- return goodDogs;
- }
- char *CopyString(const char *source, int maxString)
- {
- int ii = 0;
- char *copy = malloc(maxString);
- char* dest = copy;
- while (*source != '\0')
- {
- *dest++ = *source++;
- }
- *dest = '\0';
- return copy;
- }
- void PrintMenu(int hasSource, int canOutput)
- {
- printf("\tMENU:\n");
- printf("1. Input data\n");
- printf("2. Output source");
- if(!hasSource)
- printf("<disabled>");
- printf("\n3. Process");
- if (!hasSource)
- printf("<disabled>");
- printf("\n4. Output Result");
- if (!canOutput)
- printf("<disabled>");
- printf("\n5. Help\n");
- printf("6. Exit\n");
- }
- void Help()
- {
- system("cls");
- printf("\tHELP\n");
- printf("To select menu item use their numbers\nAt first, enter a sequence of <dog>-s (menu item 1). \nTo check the input use menu item 2.");
- printf("\nThen to process input using selected range use menu item 3.\nTo get the result of program use menu item 4.\nTo finish work use menu item 6.\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement