236 lines
4.4 KiB
C++
236 lines
4.4 KiB
C++
#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("Âûõîäíîé ôàéë íå ñîçäàëñÿ");
|
|
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("Âõîäíîé ôàéë íå íàéäåí");
|
|
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("Ââåäèòå êîëè÷åñòâî ñòðîê (n): ");
|
|
scanf_s("%d", &n);
|
|
|
|
printf("Ââåäèòå êîëè÷åñòâî ñòîëáöîâ (m): ");
|
|
scanf_s("%d", &m);
|
|
printf("Ââåäèòå ýëåìåíòû ìàññèâà:\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("Âûáåðèòå íóæíóþ âàì îïåðàöèþ:\n");
|
|
printf("1: Çàïîëíèòü çíà÷åíèÿìè i * 10 + j\n");
|
|
printf("2: çàïîëíèòü íóëÿìè\n");
|
|
printf("3: çàïîëíèòü ñëó÷àéíûìè çíà÷åíèÿìè\n");
|
|
printf("4: âñå íå÷åòíûå óâåëè÷èòü â 10 ðàç\n");
|
|
printf("5: âñå êðàòíûå 10 óìåíüøèòü â 10 ðàç\n");
|
|
printf("6: ââåäèòå ìàññèâ ñ êëàâèàòóðû\n");
|
|
printf("7: ñîõðàíèòü ìàññèâ â ôàéëå\n");
|
|
printf("8: çàãðóçèòü ìàññèâ èç ãîòîâîãî ôàéëà\n");
|
|
printf("9: óäàëèòü çàäàííóþ ñòðîêó èç ìàññèâà\n");
|
|
printf("10: ïðîäóáëèðîâàòü çàäàííûé ñòîëáåö ìàññèâà\n");
|
|
printf("11:âñå ýëåìåíòû, ñòîÿùèå âûøå è ëåâåå min ýëåìåíòà\n");
|
|
printf("çàìåíèòü íà ñðåäíåå àðèôìåòè÷åñêîå min è max ýëåìåíòîâ\n");
|
|
printf("\n");
|
|
printf("-1: Âûéòè èç ïðîãðàììû\n");
|
|
printf("Âûáðàííàÿ îïåðàöèÿ >>>>>> ");
|
|
}
|
|
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("Íîìåð ñòðîêè, êîòîðóþ íóæíî óäàëèòü: ");
|
|
scanf_s("%d", &num);
|
|
deleteRow(num);
|
|
}
|
|
break;
|
|
case 7:
|
|
save();
|
|
break;
|
|
case 10: {
|
|
printf("Èíäåêñ ñòîëáöà, ÷òîáû åãî ïðîäóáëèðîâàòü: ");
|
|
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);
|
|
} |