Advertisement
Mirbek

lab4

Apr 19th, 2022
884
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.17 KB | None | 0 0
  1. #include <graphics.h>
  2. #include <conio.h>
  3. #include <stdio.h>
  4. #include <malloc.h>
  5. #include <time.h>
  6.  
  7. void graph_mod();
  8. void main_screen();
  9. int button(int x1, int y1, int x2, int y2, int color_rectangle, char* text);
  10. void on_button();
  11. void first_mod();
  12. void second_mod();
  13. void input_range(char St[10], int x, int y, int x1, int y1, int y2);
  14.  
  15. int main(void)
  16. {
  17.     srand(time(NULL));
  18.     graph_mod();
  19.     main_screen();
  20.     on_button();
  21.  
  22.  
  23.     closegraph();
  24.     return 0;
  25. }
  26.  
  27. void graph_mod()
  28. {
  29.     int GrDr, GrMod, rez;
  30.     GrDr=DETECT;
  31.     initgraph(&GrDr,&GrMod," ");
  32. }
  33.  
  34. void main_screen()
  35. {
  36.     setfillstyle(1, 15);
  37.     bar(0, 0, 640, 480);
  38.     //кнопки
  39.     button(60, 260, 260, 300, 0, "1-ST MOD");
  40.     button(380, 260, 580, 300, 0, "2-ND MOD");
  41.     button(560, 440, 630, 470, 12, "EXIT");
  42.     //текст
  43.     setbkcolor(15);
  44.     setcolor(0);
  45.     outtextxy(225, 130, "CHOOSE ONE OF TWO MODES");
  46.     outtextxy(160, 150, "(TO SELECT CLICK LEFT BUTTON OF THE MOUSE)");
  47. }
  48.  
  49. int button(int x1, int y1, int x2, int y2, int color_rectangle, char* text)
  50. {
  51.     setcolor(color_rectangle);
  52.     rectangle(x1, y1, x2, y2);
  53.     setbkcolor(15);
  54.     int len_text = strlen(text) * 8;
  55.     int width = x2 - x1 + 1;
  56.     int x = x1 + (width - len_text) / 2;
  57.     outtextxy(x, (y1 + y2)/2 - 8, text);
  58. }
  59.  
  60. void on_button()
  61. {
  62.     char ch;
  63.     int x, y;
  64.     do
  65.     {
  66.         getmouseclick(WM_LBUTTONDOWN, x, y);
  67.         if((x >= 560) && (x <= 630) && (y >= 440) && (y <= 470))
  68.         {
  69.             ch = 'e';
  70.         }
  71.         if((x >= 60) && (x <= 260) && (y >= 260) && (y <= 300))
  72.         {
  73.             first_mod();
  74.         }
  75.         if((x >= 380) && (x <= 580) && (y >= 260) && (y <= 300))
  76.         {
  77.             second_mod();
  78.         }
  79.         //вурнуться на главную
  80.         if((x >= 40) && (x <= 110) && (y >= 430) && (y <= 460))
  81.         {
  82.             main_screen();
  83.         }
  84.     }
  85.     while(ch != 'e');
  86. }
  87.  
  88. void write_to_file(double **arr, int rows, int cols) {
  89.     FILE *file;
  90.     file = fopen("array2.txt", "w");
  91.     fclose(file);
  92.     file = fopen("array2.txt", "a");
  93.     for(int i = 0; i < rows; i++)
  94.     {
  95.         for(int j = 0; j < cols; j++)
  96.         {
  97.         //    arr[i][j] = rand()% (to - from + 1) + from;
  98.             fprintf(file, "%4.0f ", *(*(arr+i)+j));
  99.         }
  100.         fprintf(file, "\n");
  101.     }
  102.     fclose(file);
  103. }
  104.  
  105. void output_to_window(double **arr, int rows, int cols) {
  106.     char St_arr[30];
  107.     int x;
  108.     int y = 150;
  109.     outtextxy(30, 110, "ARRAY:");
  110.     for(int i = 0; i < 10; i++)
  111.     {
  112.         x = 30;
  113.         for(int j = 0; j < 10; j++)
  114.         {
  115.             sprintf(St_arr, "%5.2f", arr[i][j]);
  116.             outtextxy(x, y, St_arr);
  117.             x+=55;
  118.         }
  119.         y +=20;
  120.     }
  121.     //очистка памяти
  122.     for(int i = 0; i < rows; i++)
  123.     {
  124.         free(arr[i]);
  125.     }
  126.     free(arr);
  127.     //кновка вернуться
  128.     button(40, 430, 110, 460, 0, "BACK");
  129.     button(560, 440, 630, 470, 12, "EXIT");
  130.     on_button();
  131. }
  132.  
  133. void first_mod()
  134. {
  135.     //очистка экрана
  136.     setfillstyle(1, 15);
  137.     bar(0, 0, 640, 480);
  138.     //динамический массив
  139.     int rows = 100;
  140.     int cols = 200;
  141.     char range_min[10];
  142.     char range_max[10];
  143.     double **arr;
  144.     arr = (double**)malloc(rows * sizeof(double*));
  145.     for(int i = 0; i < rows; i++)
  146.     {
  147.         *(arr + i) = (double*)malloc(cols * sizeof(double));
  148.     }
  149.     outtextxy(30, 30, "ENTER A RANGE OF RANDOM NUMBERS");
  150.     outtextxy(30, 50, "FROM = ");
  151.     // ввод пределов для rand
  152.     input_range(range_min, 85, 50, 84, 48, 65);
  153.     outtextxy(30, 70, "TO = ");
  154.     input_range(range_max, 85, 70, 84, 68, 100);
  155.     int from, to;
  156.     float Sum = 0;
  157.     float SA;
  158.     char St_SA[10];
  159.     from = atof(range_min);
  160.     to = atof(range_max);
  161.     for (int i = 0; i < rows; i++) {
  162.         for(int j = 0; j < cols; j++)
  163.         {
  164.             *(*(arr+i)+j)= ((rand()%((to-from+1)*1000))/1000.0+from);
  165.             Sum += *(*(arr + i)+j);
  166.         }
  167.     }
  168.     SA = Sum / 20000.0;
  169.     sprintf(St_SA, "%.2f", SA);
  170.     outtextxy(30, 90, "THE ARITHMETIC MEAN OF THE ARRAY: ");
  171.     setcolor(12);
  172.     outtextxy(298, 90, St_SA);
  173.     setcolor(0);
  174.  
  175.     write_to_file(arr, rows, cols);ывод на экран массива
  176.     output_to_window(arr, rows, cols);
  177. }
  178. void second_mod()
  179. {
  180.     //очистка экрана
  181.     setfillstyle(1, 15);
  182.     bar(0, 0, 640, 480);
  183.     //динамический массив
  184.     char range_min[10];
  185.     char charrange_max[10];
  186.  
  187.     outtextxy(30, 30, "ENTER A RANGE OF RANDOM NUMBERS");
  188.     outtextxy(30, 50, "FROM = ");
  189.     // ввод пределов для rand
  190.     input_range(range_min, 85, 50, 84, 48, 65);
  191.     outtextxy(30, 70, "TO = ");
  192.     input_range(charrange_max, 85, 70, 84, 68, 100);
  193.     int from, to;
  194.     from = atof(range_min);
  195.     to = atof(charrange_max);
  196.  
  197.     int rows = rand()% 91 + 10;
  198.     int cols = rand()% 191 + 10;
  199.  
  200.     double **arr;
  201.     arr = (double**)malloc(rows * sizeof(double*));
  202.  
  203.     for(int i = 0; i < rows; i++)
  204.     {
  205.         arr[i] = (double*)malloc(cols * sizeof(double));
  206.         for(int j = 0; j < cols; j++)
  207.         {
  208.             *(*(arr+i)+j)= ((rand()%((to-from+1)*1000))/1000.0+from);
  209.         }
  210.     }
  211.     // запись массива в файл
  212.     write_to_file(arr, rows, cols);
  213.     //Вывод на экран массива
  214.     output_to_window(arr, rows, cols);
  215. }
  216.  
  217. void input_range(char St[10], int x, int y, int x1, int y1, int y2)
  218. {
  219.     char ch;
  220.     int i = 0;
  221.     do
  222.     {
  223.         ch = getch();
  224.         if (ch >= '0' && ch <='9')
  225.         {
  226.             St[i] = ch;
  227.             St[i + 1] = '\0';
  228.             i++;
  229.         }
  230.         switch(ch)
  231.         {
  232.             case '-':
  233.                St[i] = ch;
  234.                 St[i + 1] = '\0';
  235.                 i++;
  236.                 break;
  237.             case 8:
  238.                 setfillstyle(1, 15);
  239.                 bar(x1, y1, x1+strlen(St) * 8, y2);
  240.                 St[strlen(St) - 1] = '\0';
  241.                 i--;
  242.                 break;
  243.         }
  244.         setcolor(0);
  245.         outtextxy(x, y, St);
  246.     }
  247.     while(ch != 13);
  248. }
  249.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement