Advertisement
xx_Biba_xx

lab1 sem2 prog

Feb 21st, 2020
482
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.96 KB | None | 0 0
  1. #include "stdio.h"
  2. #include "stdlib.h"
  3. //task
  4. //alg + prog: input struct
  5. //control output source data
  6. //form new list out of source elements, which satisfy conditions
  7. //output results
  8. //minimalize method count, struct contains 4-5 fields
  9.  
  10. typedef struct MY_DOG
  11. {
  12.     char* name;
  13.     float weight;
  14.     int height;
  15.     float dexterity;
  16. }Dog;
  17. #define MAXBUFFER 32
  18. #define MAXSTRING 64
  19. #define MAXARRAY 64
  20.  
  21. int getInt();
  22. char* getString(int);
  23. float getFloat();
  24. void InitDog(Dog*);
  25. void PrintMenu(int hasSource, int canOutput);
  26. void Help();
  27. Dog* ReadDogs(int, int*);
  28. void PrintDog(Dog*);
  29. void PrintDogs(Dog*, int);
  30. Dog* FilterDogs(Dog*, int, int*);
  31. char *CopyString(const char*, int);
  32.  
  33. int main()
  34. {
  35.     Dog* dogs = NULL, *dogResult = NULL;
  36.     int hasSource, canOutput, currentOp, exit, dogCount, dogResultCount;
  37.     exit = hasSource = canOutput = dogCount = dogResultCount = 0;
  38.     do {
  39.         PrintMenu(hasSource, canOutput);
  40.         printf("Input operation number\n");
  41.         currentOp = getInt();
  42.         switch (currentOp)
  43.         {
  44.         case 1:     //input source
  45.             dogs = ReadDogs(MAXARRAY, &dogCount);
  46.             if (dogs != NULL)
  47.                 hasSource = 1;
  48.             else
  49.                 printf("Error occured when trying to get memory for array of dogs! Please retry.\n");
  50.             break;
  51.         case 2:     //output source
  52.             if (hasSource)
  53.                 PrintDogs(dogs, dogCount);
  54.             else
  55.                 printf("No input to print!\n");
  56.             break;
  57.         case 3:     //process
  58.             if (hasSource)
  59.             {
  60.                 dogResult = FilterDogs(dogs, dogCount, &dogResultCount);
  61.                 if(dogResult!=NULL)
  62.                     canOutput = 1;
  63.                 else
  64.                     printf("Memory err!\n");
  65.             }
  66.             else
  67.                 printf("No input to process!\n");
  68.             break;
  69.         case 4:     //output result
  70.             if (canOutput)
  71.                 PrintDogs(dogResult, dogResultCount);
  72.             else
  73.                 printf("No processed data to output!\n");
  74.             break;
  75.         case 5:     //help
  76.             Help();
  77.             break;
  78.         case 6:     //exit
  79.             exit = 1;
  80.             break;
  81.             default:
  82.                 printf("Operation not found!\n");
  83.                 break;
  84.         }
  85.         system("pause");
  86.         system("cls");
  87.     } while (!exit);
  88.     for (exit = 0; exit < dogCount; exit++)
  89.         free((dogs + exit)->name);
  90.     for (exit = 0; exit < dogResultCount; exit++)
  91.         free((dogResult + exit)->name);
  92.     free(dogs);
  93.     free(dogResult);
  94.     return 0;
  95. }
  96.  
  97. int getInt()
  98. {
  99.     char line[MAXBUFFER];   //buffer
  100.     char curChar = ' ';
  101.     int temp, result;
  102.     do {
  103.         line[MAXBUFFER - 1] = '\n';
  104.         fgets(line, MAXBUFFER - 1, stdin);
  105.         temp = sscanf(line, "%d%c", &result, &curChar);
  106.         temp = !temp || temp < 0 || (curChar != '\n' && curChar != ' ');
  107.         if (temp)
  108.             printf("Error reading number. Please, try again.\n");
  109.     } while (temp); //not a number actually
  110.     if (line[MAXBUFFER - 1] != '\n') //clear all iput
  111.         while ((curChar = getchar()) != '\n' && curChar != EOF);
  112.     return result;
  113. }
  114.  
  115. char* getString(int maxLength)
  116. {
  117.     char* line = NULL;
  118.     int i;
  119.     char curChar = ' ';
  120.     line = malloc(maxLength * sizeof(char));
  121.     if (line != NULL)
  122.     {
  123.         i = 0;
  124.         while ((curChar = getchar()) != '\n')
  125.             line[i++] = curChar;
  126.         line[i] = '\0';
  127.     }
  128.     return line;
  129. }
  130.  
  131. float getFloat()
  132. {
  133.     char line[MAXBUFFER];   //buffer
  134.     char curChar = ' ';
  135.     int temp;
  136.     float result;
  137.     do {
  138.         line[MAXBUFFER - 1] = '\n';
  139.         fgets(line, MAXBUFFER - 1, stdin);
  140.         temp = sscanf(line, "%f%c", &result, &curChar);
  141.         temp = !temp || temp < 0 || (curChar != '\n' && curChar != ' ');
  142.         if (temp)
  143.             printf("Error reading number. Please, try again.\n");
  144.     } while (temp); //not a number actually
  145.     if (line[MAXBUFFER - 1] != '\n') //clear all iput
  146.         while ((curChar = getchar()) != '\n' && curChar != EOF);
  147.     return result;
  148. }
  149.  
  150. void InitDog(Dog* dog)
  151. {
  152.     printf("Enter dog's name:\n");
  153.     dog->name = getString(MAXSTRING);
  154.     printf("Enter %s's weight (float):\n", dog->name);
  155.     dog->weight = getFloat();
  156.     printf("Enter %s's dexterity (float):\n", dog->name);
  157.     dog->dexterity = getFloat();
  158.     printf("Enter %s's height (integer):\n", dog->name);
  159.     dog->height = getInt();
  160. }
  161.  
  162. Dog* ReadDogs(int maxCount, int* count)
  163. {
  164.     int responce;
  165.     Dog *dogs=NULL, *reserve=NULL;
  166.     void* mem;
  167.     *count = 1;
  168.     responce = 0;
  169.     system("cls");
  170.     dogs = (Dog*)malloc(sizeof(Dog));
  171.     do {
  172.         if (dogs == NULL) {
  173.             responce = 0;
  174.             free(reserve);
  175.         }
  176.         else {
  177.             InitDog(dogs + (*count)-1);
  178.             printf("Continue? (1 = yes, 0 = no)\n");
  179.             if (responce = getInt()) {
  180.                 if (mem = realloc(dogs, (++*count * sizeof(*dogs))))
  181.                     dogs = (Dog*)mem;
  182.                 else
  183.                     free(dogs);
  184.                 system("cls");
  185.             }
  186.         }
  187.     } while (responce);
  188.     return dogs;
  189. }
  190.  
  191. void PrintDog(Dog* dog)
  192. {
  193.     printf("Dog name: %s\n", dog->name);
  194.     printf("\t%s's weight: %f\n", dog->name, dog->weight);
  195.     printf("\t%s's height: %d\n", dog->name, dog->height);
  196.     printf("\t%s's dexterity: %f\n", dog->name, dog->dexterity);
  197. }
  198.  
  199. void PrintDogs(Dog* dogs, int count)
  200. {
  201.     int i;
  202.     for (i = 0; i < count; i++)
  203.     {
  204.         printf("%d) ", i + 1);
  205.         PrintDog(dogs + i);
  206.     }
  207. }
  208.  
  209. Dog* FilterDogs(Dog* dogs, int count, int *resultCount)
  210. {
  211.     int i;
  212.     float dexRangeMin, dexRangeMax, weightRangeMin, weightRangeMax;
  213.     dexRangeMin = dexRangeMax = weightRangeMin = weightRangeMax = 0;
  214.     void* mem;
  215.     Dog *goodDogs;
  216.     goodDogs = (Dog*)malloc(sizeof(Dog));
  217.     *resultCount = 0;
  218.  
  219.     system("cls");
  220.     do {
  221.         printf("Input dexterity range (min and max):\n");
  222.         scanf("%f%f", &dexRangeMin, &dexRangeMax);
  223.         if (i = dexRangeMin >= dexRangeMax)
  224.             printf("bad range!\n");
  225.     } while (i);
  226.     do {
  227.         printf("Input weight range (min and max):\n");
  228.         scanf("%f%f", &weightRangeMin, &weightRangeMax);
  229.         if (i = dexRangeMin >= dexRangeMax)
  230.             printf("bad range!\n");
  231.     } while (i);
  232.  
  233.     for (i = 0; i < count && goodDogs != NULL; i++)
  234.     {
  235.         if ((dogs + i)->dexterity > dexRangeMin && (dogs + i)->dexterity < dexRangeMax&&
  236.             (dogs + i)->weight > weightRangeMin && (dogs + i)->weight < weightRangeMax) {
  237.             if (*resultCount != 0)
  238.                 if (mem = realloc(goodDogs, (++*resultCount * sizeof(*goodDogs))))
  239.                     goodDogs = (Dog*)mem;
  240.                 else
  241.                     free(goodDogs);
  242.             else
  243.                 *resultCount = 1;
  244.             *(goodDogs + *resultCount - 1) = *(dogs + i);
  245.             (goodDogs + *resultCount - 1)->name = CopyString((dogs + i)->name, MAXSTRING);
  246.         }
  247.     }
  248.     return goodDogs;
  249. }
  250.  
  251. char *CopyString(const char *source, int maxString)
  252. {
  253.     int ii = 0;
  254.     char *copy = malloc(maxString);
  255.     char* dest = copy;
  256.  
  257.     while (*source != '\0')
  258.     {
  259.         *dest++ = *source++;
  260.     }
  261.     *dest = '\0';
  262.     return copy;
  263. }
  264.  
  265. void PrintMenu(int hasSource, int canOutput)
  266. {
  267.     printf("\tMENU:\n");
  268.     printf("1. Input data\n");
  269.     printf("2. Output source");
  270.     if(!hasSource)
  271.         printf("<disabled>");
  272.     printf("\n3. Process");
  273.     if (!hasSource)
  274.         printf("<disabled>");
  275.     printf("\n4. Output Result");
  276.     if (!canOutput)
  277.         printf("<disabled>");
  278.     printf("\n5. Help\n");
  279.     printf("6. Exit\n");
  280. }
  281.  
  282. void Help()
  283. {
  284.     system("cls");
  285.     printf("\tHELP\n");
  286.     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.");
  287.     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");
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement