rail1/Lab_15/Lab15.cpp

236 lines
4.4 KiB
C++
Raw Permalink Normal View History

#include <stdio.h>
#include <Windows.h>
#define _CRT_SECURE_NO_WARNINGS
#define NUM_ELEMENTS 100
int arr[NUM_ELEMENTS][NUM_ELEMENTS];
int n = 3, m = 4;
void print_arr() {
printf("!!!! print() !!!!\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
printf("%3d ", arr[i][j]);
}
printf("\n");
}
}
void fillIx10() {
printf("!!!! fillIx10() !!!!\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = i * 10 + j;
}
}
}
void fillZero() {
printf("!!!! fillZero() !!!!\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = 0;
}
}
}
void randFill0_9() {
printf("!!!! randFill0_9() !!!!\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = rand() % 10;
}
}
}
void deleteRow(int delRow) {
printf("!!!! deleteRow(%d) !!!!\n", delRow);
for (int i = delRow; i < n - 1; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = arr[i + 1][j];
}
}
n--;
}
void save() {
FILE* fout = fopen("C:\\temp\\arr1.txt", "wt");
if (fout == NULL) {
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
return;
}
fprintf(fout, "%d ", n);
fprintf(fout, "%d\n", m);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
fprintf(fout, "%d ", arr[i][j]);
}
fprintf(fout, "\n");
}
fclose(fout);
}
void load() {
FILE* fin = fopen("C:\\temp\\arr1.txt", "rt");
if (fin == NULL) {
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
return;
}
fscanf_s(fin, "%d", &n);
fscanf_s(fin, "%d", &m);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
fscanf_s(fin, "%d", &arr[i][j]);
}
}
fclose(fin);
}
void duplicateColumn(int colIndex) {
printf("!!!! duplicateColumn() !!!!\n");
for (int i = 0; i < n; i++) {
arr[i][m] = arr[i][colIndex];
}
m++;
}
void inputA() {
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> (n): ");
scanf_s("%d", &n);
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> (m): ");
scanf_s("%d", &m);
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
printf("arr[%d][%d]: ", i, j);
scanf_s("%d", &arr[i][j]);
}
}
}
void oddnumx10() {
printf("!!!! oddnumx10 !!!!\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (arr[i][j] % 2 != 0) {
arr[i][j] *= 10;
}
}
}
}
void multiples10() {
printf("!!!! multiples10 !!!!\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (arr[i][j] % 10 == 0) {
arr[i][j] /= 10;
}
}
}
}
void func_11() {
int min, max, min_i, min_j;
min = arr[0][0]; max = arr[0][0];
min_i = 0; min_j = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (arr[i][j] < min) {
min = arr[i][j];
min_i = i;
min_j = j;
}
if (arr[i][j] > max) {
max = arr[i][j];
}
}
}
int mean;
mean = (max + min) / 2;
for (int i = 0; i < min_i; i++) {
for (int j = 0; j < min_j; j++) {
arr[i][j] = mean;
}
}
}
void menu1() {
printf("\n");
printf("------------------------------\n");
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:\n");
printf("1: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> i * 10 + j\n");
printf("2: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
printf("3: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
printf("4: <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> 10 <20><><EFBFBD>\n");
printf("5: <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 10 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> 10 <20><><EFBFBD>\n");
printf("6: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
printf("7: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD>\n");
printf("8: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>\n");
printf("9: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
printf("10: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
printf("11:<3A><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD> min <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> min <20> max <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
printf("\n");
printf("-1: <20><><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> >>>>>> ");
}
void main() {
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
int item;
do {
print_arr();
menu1();
scanf_s("%d", &item);
printf("\n");
switch (item) {
case 9: {
int num;
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: ");
scanf_s("%d", &num);
deleteRow(num);
}
break;
case 7:
save();
break;
case 10: {
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: ");
int c;
scanf_s("%d", &c);
duplicateColumn(c);
}
break;
case 1:
fillIx10();
break;
case 2:
fillZero();
break;
case 3:
randFill0_9();
break;
case 6:
inputA();
break;
case 5:
multiples10();
break;
case 4:
oddnumx10();
break;
case 8:
load();
break;
case 11:
func_11();
break;
}
} while (item != -1);
}