View difference between Paste ID: rftCp4Np and C75CS9W9
SHOW: | | - or go back to the newest paste.
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <string.h>
4
5
#define INPUT_FILE      "be.txt"
6
#define OUTPUT_FILE     "ki.txt"
7
#define MAX_WORD_SIZE   200
8
9-
int read_error() {
9+
10-
	fprintf(stderr, "Read error!\n");
10+
11-
	return -2;
11+
12
int main() {
13
	FILE *in = fopen(INPUT_FILE, "r");
14
	
15
	int func, cols;
16
	fscanf(in, "%d %d", &func, &cols);
17
18
	int key[10];
19-
	if (!in) {
19+
	int i;
20-
		fprintf(stderr, "Can't read \"%s\"!\n", INPUT_FILE);
20+
21-
		return -1;
21+
		fscanf(in, "%d", &key[i]);
22
	}
23
	
24
	char *str = (char *) malloc(sizeof(char) * MAX_WORD_SIZE + 1);
25-
	if (fscanf(in, "%d %d", &func, &cols) != 2) return read_error();
25+
	fscanf(in, "%s", str);
26
	
27
	if (func == 1) {
28-
	size_t i;
28+
29
	} else {
30-
		if (fscanf(in, "%d", &key[i]) != 1) return read_error();
30+
31
	}
32
33
	FILE *out = fopen(OUTPUT_FILE, "w");
34-
	if (fscanf(in, "%s", str) != 1) return read_error();
34+
35
	fclose(out);
36
}
37
38
char *encode(const char *str, const int *key, int cols) {
39
	int rows = strlen(str) / cols;
40
	char **table = (char **) malloc(sizeof(char *) * rows);
41
42
	int i, j;
43-
	if (!out) {
43+
44-
		fprintf(stderr, "Can't write \"%s\"!\n", OUTPUT_FILE);
44+
45-
		return -3;
45+
46
			table[i][j] = str[i * cols + j];
47
		}
48
	}
49
50
	char *coded = (char *) malloc(sizeof(char) * strlen(str) + 1);
51
	for (i = 0; i < cols; i++) {
52
		for (j = 0; j < rows; j++) {
53
			coded[i * rows + j] = table[j][key[i] - 1];
54
		}
55
	}
56-
	size_t i, j;
56+
57
	return coded;
58
}
59
60-
			// printf("%c ", str[i * cols + j]);
60+
61
	int rows = strlen(str) / cols;
62
	char **table = (char **) malloc(sizeof(char *) * cols);
63-
		// printf("\n");
63+
64
	int i, j;
65
	for (i = 0; i < cols; i++) {
66
		table[key[i]] = (char *) malloc(sizeof(char) * rows + 1);
67
		for (j = 0; j < rows; j++) {
68-
		// printf("Key: %d\n", key[i]);
68+
69
		}
70-
			// printf("%d %d %d %c\n", i, j, i * rows + j, table[j][key[i] - 1]);
70+
71
72
	char *decoded = (char *) malloc(sizeof(char) * strlen(str) + 1);
73
	for (j = 0; j < rows; j++) {
74
		for (i = 1; i <= cols; i++) {
75-
	//printf("coded: '%s'\n", coded);
75+
76
		}
77
	}
78
	decoded[strlen(str)] = '\0';
79
	
80
	return decoded;
81
}
82
83-
	size_t i, j;
83+