-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchklines.c
More file actions
145 lines (134 loc) · 2.74 KB
/
chklines.c
File metadata and controls
145 lines (134 loc) · 2.74 KB
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
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define N 5
#define PN (N * N)
#define RESULT ((N)*((PN)+(1))/(2))
void chkdownangle(int t[N][N]);
void chklines(int t[N][N]);
void stot(char s[], int t[N][N]);
void print_t(int t[N][N]);
void init_t(int t[N][N], int n);
void chkupangle(int t[N][N]);
void chkcolumns(int t[N][N]);
void readline(char *s);
void flush(char *s);
int main(int argc, char *argv[])
{
// char string[] = "15 8 1 24 17 16 14 7 5 23 22 20 13 6 4 3 21 19 12 10 9 2 25 18 11";
char string[51];
int target[N][N] = {0};
printf("Enter an ordered line of %d that will match rows, lines and angles of %d that will match a result of %d each\n",PN,N,RESULT);
scanf("%[ 0-9]", string);
int i;
for (i=0;string[i] != '\0';i++)
{
printf("%c ", string[i]);
}
stot(string, target);
chkupangle(target);
chkdownangle(target);
chklines(target);
chkcolumns(target);
print_t(target);
return 0;
}
void readline(char *s)
{
char c;
while((c=getchar()) != EOF && c != '\n') {
if (c == ' ' || isdigit(c) || c == '\t')
*s++ = c;
else
s++;
}
*s = '\0';
}
void chkcolumns(int t[N][N])
{
int i, j, n;
for (j = 0;j < N;j++) {
n = 0;
for (i = 0;i < N;i++)
n += t[i][j];
if (n == RESULT)
printf("column %d is ok %d\n", j+1, n);
else {
printf("column %d is wrong %d\n", j+1, n);
exit(1);
}
}
}
void chklines(int t[N][N])
{
int i, j, n;
for (i = 0;i < N;i++) {
n = 0;
for (j = 0;j < N;j++)
n += t[i][j];
if (n == RESULT)
printf("line %d is ok %d\n", i+1, n);
else {
printf("line %d is wrong %d\n", i+1, n);
exit(1);
}
}
}
void chkdownangle(int t[N][N])
{
int i, j, n = 0;
for (i = N-1, j = N-1;i >= 0;i--, j--)
n += t[i][j];
if (n == RESULT)
printf("down angle is ok: %d\n", n);
else {
printf("down angle is wrong: %d\n", n);
exit(1);
}
}
void chkupangle(int t[N][N])
{
int i, j, n = 0;
for (i=0, j=0;i < N;i++, j++)
n += t[i][j];
if (n == RESULT)
printf("up angle is ok: %d\n", n);
else {
printf("up angle is wrong: %d\n", n);
exit(1);
}
}
void stot(char s[], int t[N][N])
{
int n, i, j;
int slength = strlen(s);
for (i=0;i < slength;) {
for (j=i;(!isdigit(s[j]));j++)
;
for (n=0;isdigit(s[j]);j++)
n = 10 * n + (s[j] - '0');
init_t(t, n);
i = j;
}
}
void init_t(int t[N][N], int n)
{
int i, j;
for (i = 0;i < N;i++)
for (j = 0;j < N;j++)
if (t[i][j] == 0) {
t[i][j] = n;
return;
}
}
void print_t(int t[N][N])
{
printf("\nIt is a magic square\n");
int i, j;
for (i=0;i < N;i++){
printf("\n");
for (j=0;j < N;j++)
printf("\t%d ", t[i][j]);
}
printf("\n");
}