Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <graphics.h>
- #include <conio.h>
- #include <stdio.h>
- #include <malloc.h>
- #include <time.h>
- void graph_mod();
- void main_screen();
- int button(int x1, int y1, int x2, int y2, int color_rectangle, char* text);
- void on_button();
- void first_mod();
- void second_mod();
- void input_range(char St[10], int x, int y, int x1, int y1, int y2);
- int main(void)
- {
- srand(time(NULL));
- graph_mod();
- main_screen();
- on_button();
- closegraph();
- return 0;
- }
- void graph_mod()
- {
- int GrDr, GrMod, rez;
- GrDr=DETECT;
- initgraph(&GrDr,&GrMod," ");
- }
- void main_screen()
- {
- setfillstyle(1, 15);
- bar(0, 0, 640, 480);
- //кнопки
- button(60, 260, 260, 300, 0, "1-ST MOD");
- button(380, 260, 580, 300, 0, "2-ND MOD");
- button(560, 440, 630, 470, 12, "EXIT");
- //текст
- setbkcolor(15);
- setcolor(0);
- outtextxy(225, 130, "CHOOSE ONE OF TWO MODES");
- outtextxy(160, 150, "(TO SELECT CLICK LEFT BUTTON OF THE MOUSE)");
- }
- int button(int x1, int y1, int x2, int y2, int color_rectangle, char* text)
- {
- setcolor(color_rectangle);
- rectangle(x1, y1, x2, y2);
- setbkcolor(15);
- int len_text = strlen(text) * 8;
- int width = x2 - x1 + 1;
- int x = x1 + (width - len_text) / 2;
- outtextxy(x, (y1 + y2)/2 - 8, text);
- }
- void on_button()
- {
- char ch;
- int x, y;
- do
- {
- getmouseclick(WM_LBUTTONDOWN, x, y);
- if((x >= 560) && (x <= 630) && (y >= 440) && (y <= 470))
- {
- ch = 'e';
- }
- if((x >= 60) && (x <= 260) && (y >= 260) && (y <= 300))
- {
- first_mod();
- }
- if((x >= 380) && (x <= 580) && (y >= 260) && (y <= 300))
- {
- second_mod();
- }
- //вурнуться на главную
- if((x >= 40) && (x <= 110) && (y >= 430) && (y <= 460))
- {
- main_screen();
- }
- }
- while(ch != 'e');
- }
- void write_to_file(double **arr, int rows, int cols) {
- FILE *file;
- file = fopen("array2.txt", "w");
- fclose(file);
- file = fopen("array2.txt", "a");
- for(int i = 0; i < rows; i++)
- {
- for(int j = 0; j < cols; j++)
- {
- // arr[i][j] = rand()% (to - from + 1) + from;
- fprintf(file, "%4.0f ", *(*(arr+i)+j));
- }
- fprintf(file, "\n");
- }
- fclose(file);
- }
- void output_to_window(double **arr, int rows, int cols) {
- char St_arr[30];
- int x;
- int y = 150;
- outtextxy(30, 110, "ARRAY:");
- for(int i = 0; i < 10; i++)
- {
- x = 30;
- for(int j = 0; j < 10; j++)
- {
- sprintf(St_arr, "%5.2f", arr[i][j]);
- outtextxy(x, y, St_arr);
- x+=55;
- }
- y +=20;
- }
- //очистка памяти
- for(int i = 0; i < rows; i++)
- {
- free(arr[i]);
- }
- free(arr);
- //кновка вернуться
- button(40, 430, 110, 460, 0, "BACK");
- button(560, 440, 630, 470, 12, "EXIT");
- on_button();
- }
- void first_mod()
- {
- //очистка экрана
- setfillstyle(1, 15);
- bar(0, 0, 640, 480);
- //динамический массив
- int rows = 100;
- int cols = 200;
- char range_min[10];
- char range_max[10];
- double **arr;
- arr = (double**)malloc(rows * sizeof(double*));
- for(int i = 0; i < rows; i++)
- {
- *(arr + i) = (double*)malloc(cols * sizeof(double));
- }
- outtextxy(30, 30, "ENTER A RANGE OF RANDOM NUMBERS");
- outtextxy(30, 50, "FROM = ");
- // ввод пределов для rand
- input_range(range_min, 85, 50, 84, 48, 65);
- outtextxy(30, 70, "TO = ");
- input_range(range_max, 85, 70, 84, 68, 100);
- int from, to;
- float Sum = 0;
- float SA;
- char St_SA[10];
- from = atof(range_min);
- to = atof(range_max);
- for (int i = 0; i < rows; i++) {
- for(int j = 0; j < cols; j++)
- {
- *(*(arr+i)+j)= ((rand()%((to-from+1)*1000))/1000.0+from);
- Sum += *(*(arr + i)+j);
- }
- }
- SA = Sum / 20000.0;
- sprintf(St_SA, "%.2f", SA);
- outtextxy(30, 90, "THE ARITHMETIC MEAN OF THE ARRAY: ");
- setcolor(12);
- outtextxy(298, 90, St_SA);
- setcolor(0);
- write_to_file(arr, rows, cols);ывод на экран массива
- output_to_window(arr, rows, cols);
- }
- void second_mod()
- {
- //очистка экрана
- setfillstyle(1, 15);
- bar(0, 0, 640, 480);
- //динамический массив
- char range_min[10];
- char charrange_max[10];
- outtextxy(30, 30, "ENTER A RANGE OF RANDOM NUMBERS");
- outtextxy(30, 50, "FROM = ");
- // ввод пределов для rand
- input_range(range_min, 85, 50, 84, 48, 65);
- outtextxy(30, 70, "TO = ");
- input_range(charrange_max, 85, 70, 84, 68, 100);
- int from, to;
- from = atof(range_min);
- to = atof(charrange_max);
- int rows = rand()% 91 + 10;
- int cols = rand()% 191 + 10;
- double **arr;
- arr = (double**)malloc(rows * sizeof(double*));
- for(int i = 0; i < rows; i++)
- {
- arr[i] = (double*)malloc(cols * sizeof(double));
- for(int j = 0; j < cols; j++)
- {
- *(*(arr+i)+j)= ((rand()%((to-from+1)*1000))/1000.0+from);
- }
- }
- // запись массива в файл
- write_to_file(arr, rows, cols);
- //Вывод на экран массива
- output_to_window(arr, rows, cols);
- }
- void input_range(char St[10], int x, int y, int x1, int y1, int y2)
- {
- char ch;
- int i = 0;
- do
- {
- ch = getch();
- if (ch >= '0' && ch <='9')
- {
- St[i] = ch;
- St[i + 1] = '\0';
- i++;
- }
- switch(ch)
- {
- case '-':
- St[i] = ch;
- St[i + 1] = '\0';
- i++;
- break;
- case 8:
- setfillstyle(1, 15);
- bar(x1, y1, x1+strlen(St) * 8, y2);
- St[strlen(St) - 1] = '\0';
- i--;
- break;
- }
- setcolor(0);
- outtextxy(x, y, St);
- }
- while(ch != 13);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement