Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.09 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3.  
  4. #define period 7
  5.  
  6. float temps[period];
  7. char days[7][10] = {"pondeli", "utery", "streda" ,"ctvrtek" ,"patek" ,"sobota" ,"nedele" };
  8.  
  9. void inputTemp()
  10. {
  11.     printf("\nZadani teplot...\n\n");
  12.     for (short i = 0; i < period; i++)
  13.     {
  14.         printf("Enter temp for %s: ", days[i%7]);
  15.         scanf("%f", temps+i);
  16.     }
  17.     printf("\n");
  18. }
  19.  
  20. void readFromFile()
  21. {
  22.     printf("\nNačítani teploty ze souboru...\n\n");
  23.     printf("Please, enter input file name: ");
  24.     char fName[255];
  25.     scanf("%s", fName);
  26.     FILE *inF = fopen(fName, "r");
  27.     if (inF != NULL)
  28.     {
  29.         for (short i = 0; i < period; i++)
  30.         {
  31.             fscanf(inF, "%f", temps+i);
  32.         }
  33.         fclose(inF);
  34.         printf("\n...Reading SUCESS!...\n");
  35.     }
  36.     else
  37.         printf("File not found!!!\n");
  38.     printf("\n");
  39. }
  40.  
  41. void saveToFile()
  42. {
  43.     char fName[255];
  44.     printf("\nUloženi teploty do souboru...\n\n");
  45.     printf("Please, enter output file name: ");
  46.     scanf("%s", fName);
  47.     FILE *outF = fopen(fName, "w");
  48.     if (outF != NULL)
  49.     {
  50.         for (short i = 0; i < period; i++)
  51.         {
  52.             fprintf(outF, "%.2f\n", temps[i]);
  53.         }
  54.         fclose(outF);
  55.         printf("\n...Data was save...\n");
  56.     }
  57.     else
  58.         printf("output file ERROR!!!\n");
  59.    
  60.     printf("\n");
  61. }
  62.  
  63. void outputTemp()
  64. {
  65.     printf("\nTydenni prehled teplot...\n\n");
  66.     for (short i = 0; i < period; i++)
  67.     {  
  68.         printf("-----------------------\n");
  69.         printf("| %-10s | %7.2f|\n", days[i], temps[i]);
  70.     }
  71.     printf("-----------------------\n");
  72.     printf("\n");
  73. }
  74.  
  75. void outputDayTemp()
  76. {
  77.     int day;
  78.     printf("\nZadej den [1-po, ..., 7-ne] : ");
  79.     scanf("%d", &day);
  80.     printf("V den %d - %s je teplota %.2fst.C.\n\n\n", day, days[day-1], temps[day-1]);
  81. }
  82.  
  83. void inputDayTemp()
  84. {
  85.     int day;
  86.     printf("\nZadej den [1-po, ..., 7-ne] : ");
  87.     scanf("%d", &day);
  88.     printf("\nZadej teplotu: ");
  89.     scanf("%f", temps+(day-1));
  90.     printf("\n\n");
  91. }
  92. void stat()
  93. {  
  94.     double sum = 0, max = temps[0];
  95.     char flag = '0';
  96.     int maxIndex = 0;
  97.     for (int i = 0; i < period; i++)
  98.     {
  99.         sum = sum + temps[i];
  100.         if (temps[i] > max)
  101.         {
  102.             max = temps[i];
  103.             maxIndex = i;
  104.         }
  105.     }
  106.     printf("\n**********************************");
  107.     printf("\nPrumerna denni teplota je %.2fst.C.\n", sum/period);
  108.     printf("Maximalni teplota %.2fst.C. nastala v %s (%d den)\n", max, days[maxIndex], maxIndex+1);
  109.     for (int i = 0; i < period; i++)
  110.     {
  111.         for (int j = 0; j < period; j++)
  112.         {
  113.             if (i == j) continue;
  114.             if (temps[i] == temps[j] && i < j)
  115.             {
  116.                 if (flag == '0')
  117.                 {
  118.                     printf("Dny se stejnou teplotou %.2fst.C. : %s", temps[i], days[i]);
  119.                     flag = '1';
  120.                 }
  121.                 printf(", %s", days[j]);
  122.             }
  123.             else if (temps[i] == temps[j] && i > j)
  124.                 break;
  125.         }
  126.         if (flag == '1')
  127.         {
  128.             printf("\n");
  129.             flag = '0';
  130.         }
  131.        
  132.     }
  133.  
  134.  
  135.     printf("\n**********************************\n");
  136. }
  137. int main()
  138. {
  139.     FILE *inpF = fopen("input.txt", "r");
  140.     if (inpF != NULL)
  141.     {
  142.         for (short i = 0; i < period; i++)
  143.         {
  144.             fscanf(inpF,"%f",temps+i);
  145.         }
  146.         fclose(inpF);
  147.     }
  148.     else
  149.     {
  150.         for (short i = 0; i < period; i++)
  151.         {
  152.             temps[i] = 0;
  153.         }
  154.     }
  155.     char key;
  156.     do
  157.     {
  158.         printf("0. Konec;\n");
  159.         printf("1. Zadat teploty na klávesnici;\n");
  160.         printf("2. Načíst teploty ze souboru;\n");
  161.         printf("3. Uložit teploty do souboru;\n");
  162.         printf("4. Zobrazit teploty;\n");
  163.         printf("5. Zobrazit teplotu v zadaný den (číslo od 1 do 7);\n");
  164.         printf("6. Změna teploty v zadaný den (číslo od 1 do 7);\n");
  165.         printf("7. Statistika teplot\n   (určí se průměrná denní teplota a informace, jestli existuje více dnů se stejnou teplotou).\n");
  166.         do
  167.         {
  168.             key = getch();
  169.         } while (key != '1' && key != '2' && key != '3' && key != '4'
  170.             && key != '0' && key != '5' && key != '6' && key != '7');
  171.         switch (key)
  172.         {
  173.         case '0':
  174.             break;
  175.         case '1':
  176.             inputTemp();
  177.             break;
  178.         case '2':
  179.             readFromFile();
  180.             break;
  181.         case '3':
  182.             saveToFile();
  183.             break;
  184.         case '4':
  185.             outputTemp();
  186.             break;
  187.         case '5':
  188.             outputDayTemp();
  189.             break;
  190.         case '6':
  191.             inputDayTemp();
  192.             break;
  193.         case '7':
  194.             stat();
  195.             break;
  196.         default:
  197.             continue;
  198.             break;
  199.         }
  200.  
  201.  
  202.     } while (key != '0');
  203.  
  204.    
  205.     return 0;
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement