-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform.c
186 lines (159 loc) · 2.87 KB
/
transform.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
ID: talos
LANG: C
PROG: transform
*/
#define INPUT "transform.in" /* input file*/
#define OUTPUT "transform.out" /* output file*/
#include <stdio.h>
int rotate(char *, int);
int reflect(char *, int);
int copy_array(char *, char *, int);
int compare_array(char *, char *, int);
int main()
{
int c1, c2;
unsigned char n, size, cur;
FILE *in, *out;
/* open file for reading*/
in = fopen(INPUT, "r");
fscanf(in, "%d\n", &n);
size = n*n;
unsigned char array1[size], array2[size], copy[size];
/* read original array*/
for (c1 = 0, c2 = 0; c1 < n * (n + 1); c1++, c2++)
{
cur = getc(in);
if (cur == '\n')
{
c2--;
continue;
}
array1[c2] = cur;
}
/* read transformed array*/
for (c1 = 0, c2 = 0; c1 < n * (n + 1); c1++, c2++)
{
cur = getc(in);
if (cur == '\n')
{
c2--;
continue;
}
array2[c2] = cur;
}
fclose(in);
/* special case : n=1*/
if (n == 1)
{
if (array1[0] == array2[0])
{
c1=1;
} else
{
c1=7;
}
out = fopen(OUTPUT, "w");
fprintf(out, "%d\n", c1);
fclose(out);
return 0;
}
copy_array(array1, copy, n);
/*open file for writing */
out = fopen(OUTPUT, "w");
for (c1 = 1; c1 < 4; c1++)
{
rotate(copy, n);
if (c2 = compare_array(array2, copy, n))
{
fprintf(out, "%d\n", c1);
fclose(out);
return 0;
}
}
copy_array(array1, copy, n);
reflect(copy, n);
if ( compare_array(array2, copy, n))
{
fprintf(out, "%d\n", 4);
fclose(out);
return 0;
}
for (c1 = 1; c1 < 4; c1++)
{
rotate(copy, n);
if (c2 = compare_array(array2, copy, n))
{
fprintf(out, "%d\n", 5);
fclose(out);
return 0;
}
}
if (compare_array(array1, array2, n))
{
fprintf(out, "%d\n", 6);
fclose(out);
return 0;
}
fprintf(out, "%d\n", 7);
fclose(out);
return 0;
}
int rotate(char arr[], int n)
{
int c1, c2;
char rot[n * n];
for (c2 = 0; c2 < n; c2++)
{
for (c1 = 0; c1 < n; c1++)
{
rot[c1 * n + n - 1 - c2] = arr[n * c2 + c1];
}
}
copy_array(rot, arr, n);
return 0;
}
int reflect(char arr[], int n)
{
int c1, c2;
char ref[n * n];
rotate(arr, n);
rotate(arr, n);
for (c2 = 1; c2 <= n / 2; c2++)
{
for (c1=0; c1 < n; c1++)
{
ref[(n - c2) * n + c1]=arr[n * (c2 - 1) + c1];
}
}
for (c2 = n; c2 > n / 2; c2--)
{
for (c1=0; c1 < n; c1++)
{
ref[(n - c2) * n + c1]=arr[n * (c2 - 1) + c1];
}
}
copy_array(ref, arr, n);
return 0;
}
int copy_array(char src[], char dest[], int len)
{
int c;
for (c = 0; c < len * len; c++)
{
dest[c] = src[c];
}
return 0;
}
int compare_array(char src[], char dest[], int len)
{
int c;
for (c = 0; c < len * len; c++)
{
if (src[c] != dest[c])
{
return 0; /* arrays differ */
}
}
return 1; /* arrays match */
}