Advertisement
Adrian_Konca

P1 Lab9

Dec 10th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.14 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <windows.h>
  6. #define DEBUG_ACTIVE true
  7. #define NUMBER_RANGE 10
  8. #define NUMBER_COUNT 100
  9.  
  10. int* iCreateRandomizedTable(int iTableSize)
  11. {
  12.     int *iTable = (int *)malloc(sizeof(int) * iTableSize);
  13.     int i;
  14.     for (i = 0; i < iTableSize; i++)
  15.     {
  16.         iTable[i] = rand() % NUMBER_RANGE + 1;
  17.     }
  18.     return iTable;
  19. }
  20.  
  21. void vWriteTableToFile(FILE *outputFile, int iTable[], int iTableSize)
  22. {
  23.     int i;
  24.     for (i = 0; i < (iTableSize - 1); i++)
  25.     {
  26.         fprintf_s(outputFile, "%d,", iTable[i]);
  27.     }
  28.     fprintf_s(outputFile, "%d", iTable[iTableSize - 1]);
  29. }
  30.  
  31. int *iReadTableFromFile(FILE *outputFile, int iTableSize)
  32. {
  33.     int i;
  34.     int *iTable = (int *)malloc(sizeof(int) * iTableSize);
  35.     for (i = 0; i < iTableSize; i++)
  36.     {
  37.         fscanf_s(outputFile, "%d,", &iTable[i]);
  38.     }
  39.     if (DEBUG_ACTIVE)
  40.     {
  41.         for (i = 0; i < iTableSize; i++)
  42.         {
  43.             printf_s("%d,", iTable[i]);
  44.  
  45.  
  46.         }
  47.         printf_s("\n");
  48.     }
  49.     return iTable;
  50. }
  51.  
  52.  
  53. void vCreateRandomFile(int iFileIndex)
  54. {
  55.     int* iRandomizedTable;
  56.     char cFileName[20];
  57.     iRandomizedTable = iCreateRandomizedTable(100);
  58.  
  59.     sprintf_s(cFileName, "output%02d.txt", iFileIndex);
  60.     FILE *outputFile;
  61.     fopen_s(&outputFile, cFileName, "w");
  62.     vWriteTableToFile(outputFile, iRandomizedTable, NUMBER_COUNT);
  63.     free(iRandomizedTable);
  64.     fclose(outputFile);
  65. }
  66.  
  67. int *iReadRandomFile(int iFileIndex)
  68. {
  69.     int* iTable;
  70.     char cFileName[20];
  71.  
  72.     sprintf_s(cFileName, "output%02d.txt", iFileIndex);
  73.     FILE *outputFile;
  74.     fopen_s(&outputFile, cFileName, "r");
  75.     iTable = iReadTableFromFile(outputFile, NUMBER_COUNT);
  76.     fclose(outputFile);
  77.  
  78.     return iTable;
  79. }
  80.  
  81. void vCreateRandomFiles(int iFileCount)
  82. {
  83.     int i;
  84.     for (i = 0; i < iFileCount; i++)
  85.     {
  86.         vCreateRandomFile(i);
  87.     }
  88. }
  89.  
  90. void vWriteLongestNumberStreak(FILE *outputFile, int iDataTable[])
  91. {
  92.     int i;
  93.     int currentCount;
  94.     int currentNumber;
  95.     int maxCount = 0;
  96.     int maxNumber = 0;
  97.     currentCount = 1;
  98.     currentNumber = iDataTable[0];
  99.     for (i = 1; i < NUMBER_COUNT; i++)
  100.     {
  101.         if (currentNumber == iDataTable[i])
  102.         {
  103.             currentCount++;
  104.         }
  105.         else //TODO: Co robimy gdy dwie liczby mają taki sam count? Może zrobić tablicę, a potem ją posortować?
  106.         {
  107.             if (currentCount > maxCount)
  108.             {
  109.                 maxCount = currentCount;
  110.                 maxNumber = currentNumber;
  111.             }
  112.             currentNumber = iDataTable[i];
  113.             currentCount = 1;
  114.         }
  115.     }
  116.     if (currentCount > maxCount)
  117.     {
  118.         maxCount = currentCount;
  119.         maxNumber = currentNumber;
  120.     }
  121.     fprintf_s(outputFile, "Najdluzszy ciag o dlugosci %d tworzy liczba %d\n", maxCount, maxNumber);
  122. }
  123.  
  124. void vWriteNumberFrequency(FILE *outputFile, int iDataTable[])
  125. {
  126.     int **iFreqencyTable = (int **)malloc(sizeof(int*) * NUMBER_RANGE);
  127.     int i;
  128.     int j;
  129.     int x, y;
  130.     for (i = 0; i < NUMBER_RANGE; i++)
  131.     {
  132.         iFreqencyTable[i] = (int *)malloc(sizeof(int*) * 2);
  133.         iFreqencyTable[i][1] = i + 1;
  134.         iFreqencyTable[i][0] = 0;
  135.     }
  136.     for (i = 0; i < NUMBER_COUNT; i++)
  137.     {
  138.         iFreqencyTable[iDataTable[i] - 1][0]++;
  139.     }
  140.  
  141.     for (i = 0; i < NUMBER_RANGE - 1; i++)
  142.     {
  143.         for (j = 0; j < NUMBER_RANGE - i - 1; j++)
  144.         {
  145.             if (iFreqencyTable[j][0] > iFreqencyTable[j + 1][0])
  146.             {
  147.                 y = iFreqencyTable[j + 1][0];
  148.                 x = iFreqencyTable[j + 1][1];
  149.                 iFreqencyTable[j + 1][0] = iFreqencyTable[j][0];
  150.                 iFreqencyTable[j + 1][1] = iFreqencyTable[j][1];
  151.                 iFreqencyTable[j][0] = y;
  152.                 iFreqencyTable[j][1] = x;
  153.             }
  154.         }
  155.     }
  156.  
  157.     for (i = 0; i < NUMBER_RANGE; i++)
  158.     {
  159.         fprintf_s(outputFile, "Liczba %d wystepuje %d razy.\n", iFreqencyTable[i][1], iFreqencyTable[i][0]);
  160.     }
  161.    
  162.  
  163. }
  164.  
  165. void vReadRandomFiles(int iFileCount)
  166. {
  167.     int i;
  168.     FILE *outputFile;
  169.     fopen_s(&outputFile, "FINAL.txt", "w");
  170.     for (i = 0; i < iFileCount; i++)
  171.     {
  172.         char cFileName[20];
  173.         sprintf_s(cFileName, "output%02d.txt", i);
  174.         int *iTable = iReadRandomFile(i);
  175.         fprintf_s(outputFile, "PLIK O NAZWIE: %s\n", cFileName);
  176.         vWriteNumberFrequency(outputFile, iTable);
  177.         vWriteLongestNumberStreak(outputFile, iTable);
  178.         free(iTable);
  179.     }
  180.     fclose(outputFile);
  181. }
  182.  
  183. int main()
  184. {
  185.     const int iFileCount = 10;
  186.  
  187.     srand((unsigned int)time(0));
  188.     vCreateRandomFiles(iFileCount);
  189.     vReadRandomFiles(iFileCount);
  190.  
  191.     return 0;
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement