Lab4_n/Project7/Source.cpp
2024-11-07 08:45:54 +04:00

244 lines
4.3 KiB
C++

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <Windows.h>
#define MAX_N 8
#define MAX_M 12
int arr[MAX_N][MAX_M] = {
{10, 11, 12, 13},
{20, 21, 9, 23},
{30, 31, 32, 33}
};
int n = 3;
int m = 4;
void print() {
printf("//// print() ////\n");
for (int i = 0; i < n; i += 1) {
for (int j = 0; j < m; j += 1) {
printf("%3d", arr[i][j]);
}
printf("\n");
}
}
void fillx10() {
printf("//// fillx10 ////\n");
for (int i = 0; i < n; i += 1) {
for (int j = 0; j < m; j += 1) {
arr[i][j] = i * 10 + j;
}
}
}
void fillzero() {
printf("//// fillzero ////\n");
for (int i = 0; i < n; i += 1) {
for (int j = 0; j < m; j += 1) {
arr[i][j] = 0;
}
}
}
void randfill() {
printf("//// randfill ////\n");
for (int i = 0; i < n; i += 1) {
for (int j = 0; j < m; j += 1) {
arr[i][j] = rand() % 10;
}
}
}
void nch10() {
printf("//// nch10 ////\n");
for (int i = 0; i < n; i += 1) {
for (int j = 0; j < m; j += 1) {
if (arr[i][j] % 2 == 1) {
arr[i][j] *= 10;
}
}
}
}
void kr10() {
printf("//// kr10 ////\n");
for (int i = 0; i < n; i += 1) {
for (int j = 0; j < m; j += 1) {
if (arr[i][j] % 10 == 0) {
arr[i][j] /= 10;
}
}
}
}
void keyboardInput() {
printf("n = ");
scanf_s("%d", &n);
printf("m = ");
scanf_s("%d", &m);
printf("Ñòðîê %d:\n", n);
printf("Ñòîëáöîâ %d:\n", m);
for (int i = 0; i < n; i += 1) {
for (int j = 0; j < m; j += 1) {
scanf_s("%d", &arr[i][j]);
}
}
}
void save() {
FILE* fout = fopen("c:\\Temp\\arr1.txt", "wtt");
if (fout == NULL) {
printf("Âûõîäíîé ôàéë íå ñîçäàëñÿ");
return;
}
fprintf(fout, "%d", n);
fprintf(fout, "&%d\n", m);
for (int i = 0; i < n; i += 1) {
for (int j = 0; j < m; j += 1) {
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;
}
fprintf(fin, "%d", &n);
fprintf(fin, "&%d\n", &m);
for (int i = 0; i < n; i += 1) {
for (int j = 0; j < m; j += 1) {
fscanf(fin, "%d", &arr[i][j]);
}
}
fclose(fin);
}
void deleteRow(int delRow) {
printf("//// deleteRow(%d) ////\n", delRow);
for (int i = delRow; i < n - 1; i += 1) {
for (int j = 0; j < m; j += 1) {
arr[i][j] = arr[i + 1][j];
}
}
n -= 1;
}
void dubCollum(int dubcol) {
printf("//// dubCollum() ////\n", dubcol);
for (int i = 0; i < n; i += 1) {
for (int j = m; j > dubcol; j -= 1) {
arr[i][j] = arr[i][j - 1];
}
}
m += 1;
}
void func() {
int max = arr[0][0], min = arr[0][0];
int maxRow = 0, maxCol = 0;
for (int i = 0; i < n; i += 1) {
for (int j = 0; j < m; j += 1) {
if (arr[i][j] > max) {
max = arr[i][j];
maxRow = i;
maxCol = j;
}
if (arr[i][j] < min) {
min = arr[i][j];
}
}
}
for (int i = maxRow; i < n; i += 1) {
for (int j = 0; j <= maxCol; j += 1) {
arr[i][j] = min;
}
}
}
void main() {
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
int item;
do {
printf("\n");
print();
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("\n");
printf("0: Âûéòè èç ïðîãðàììû\n");
printf("Âûáðàííàÿ îïåðàöèÿ >>>>>> ");
scanf_s("%d", &item);
switch (item) {
case 1:
fillx10();
break;
case 2:
fillzero();
break;
case 3:
randfill();
break;
case 4:
nch10();
break;
/*case 5:
kr10();
break;*/
case 6:
keyboardInput();
break;
case 7:
save();
load();
break;
case 8:
int num;
printf("Íîìåð ñòðîêè êîòîðóþ íóæíî óäàëèòü: ");
scanf_s("%d", &num);
deleteRow(num);
break;
case 9:
int nnum;
printf("Íîìåð ñòîëáöà êîòîðûé õîòèòå äóáëèðîâàòü: ");
scanf_s("%d", &nnum);
dubCollum(nnum);
break;
case 10:
func();
break;
}
} while (item != 0);
}