OAIP/15/TwoDimentionalArray.c

240 lines
4.0 KiB
C
Raw Normal View History

#include "TwoDimentionalArray.h"
int arr[MAX_LENGTH][MAX_LENGTH];
int width, height;
void inputdem() {
printf("height ");
scanf_s("%d", &height);
printf("width ");
scanf_s("%d", &width);
}
void randomEl() {
if (width == 0 || height == 0) inputdem();
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
arr[i][j] = rand() % 10 + 1;
}
}
}
void iX10j() {
if (width == 0 || height == 0) inputdem();
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
arr[i][j] = (i+1)*10+j+1;
}
}
}
void setZero() {
if (width == 0 || height == 0) inputdem();
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
arr[i][j] = 0;
}
}
}
void inputArr() {
inputdem();
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
scanf_s("%d", &arr[i][j]);
}
}
}
void oddX10() {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (arr[i][j] % 2 != 0) {
arr[i][j] = arr[i][j] * 10;
}
}
}
}
void del10() {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (arr[i][j] % 10 == 0) {
arr[i][j] = arr[i][j] / 10;
}
}
}
}
void save() {
FILE* fout = fopen("D:\\university\\<EFBFBD><EFBFBD><EFBFBD><EFBFBD>\\lab\\Temp\\test.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 ", height);
fprintf(fout, "%d\n", width);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
fprintf(fout, "%d ", arr[i][j]);
}
fprintf(fout, "\n");
}
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
fclose(fout);
}
void load() {
FILE* fin = fopen("D:\\university\\<EFBFBD><EFBFBD><EFBFBD><EFBFBD>\\lab\\Temp\\test.txt", "rt");
if (fin == NULL) {
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
return;
}
fscanf_s(fin, "%d", &height);
fscanf_s(fin, "%d", &width);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
fscanf_s(fin, "%d", &arr[i][j]);
}
}
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
fclose(fin);
}
void dublicateCol() {
int index;
printf("index ");
scanf_s("%d", &index);
for (int i = width; i > index; i--) {
for (int j = 0; j < height; j++) {
arr[j][i] = arr[j][i - 1];
}
}
width += 1;
}
void DeleteRow() {
int index;
printf("index ");
scanf_s("%d", &index);
height -= 1;
for (int i = index; i < height; i++) {
for (int j = 0; j < width; j++) {
arr[i][j] = arr[i + 1][j];
}
}
}
void zeroWhereRepeat() {
for (int j = 0; j < width; j++) {
bool DO = false;
for (int i = 0; i < height - 1; i++) {
for (int ii = i + 1; ii < height; ii++) {
if (arr[i][j] == arr[ii][j]) {
DO = true;
goto end;
}
}
}
end:
if (DO) {
for (int i = 0; i < height; i++) {
arr[i][j] = 0;
}
}
}
}
void printArr() {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
printf("%3d ", arr[i][j]);
}
printf("\n");
}
}
void menuText() {
printf("\n\n");
if (height != 0) {
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: %d\n", height);
printArr(arr, width, height);
}
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> 0\n");
printf("3) <20><><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>10\n");
printf("5) <20><><EFBFBD> % 10 <20>1del10\n");
printf("6) <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
printf("71) <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
printf("72) <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
printf("8) 0 <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
printf("9) <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
printf("10) <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
printf("\n");
printf("-1) <20><><EFBFBD><EFBFBD><EFBFBD>\n");
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> >>> ");
}
void startProg() {
srand(time(NULL));
int ans;
do {
menuText();
scanf_s("%d", &ans);
switch (ans)
{
case 1:
iX10j();
break;
case 2:
setZero();
break;
case 3:
randomEl();
break;
case 4:
oddX10();
break;
case 5:
del10();
break;
case 6:
inputArr();
break;
case 71:
save();
break;
case 72:
load();
break;
case 8:
zeroWhereRepeat();
break;
case 9:
dublicateCol();
break;
case 10:
DeleteRow();
default:
break;
}
} while (ans != -1);
}