forked from taniadovzhenko/CppPracticum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfield.c
More file actions
119 lines (84 loc) · 1.78 KB
/
field.c
File metadata and controls
119 lines (84 loc) · 1.78 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
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
// 4.0.1 b) Chessfield
// 11.1b)
typedef
struct Field{
char col; // cols
unsigned row; // row
}Field;
bool correctness(Field x){
if(!isalpha(x.col)){
return false;
}
if(x.col<'a' || x.col>'h') return false;
if(x.row==0 || x.row>8) return false;
return true;
}
int input(Field* x){
Field res;
//unsigned t;
printf("col, row=");
scanf(" %c",&res.col);// x->col
//printf("%i",g);
//printf("kop=");
scanf(" %u",&res.row); // x->row
//res.row = t;
*x =res;
return 0;
}
Field input2(){
Field res;
//unsigned t;
char c;
printf("col, row");
scanf(" %c",&с);// x->col
res.col = toupper(c);
//printf("%i",g);
//printf("kop=");
scanf(" %u",&(res.row)); // x->row
return res;
}
void output(const Field x){
printf("%C:%u ",x.col,x.row);
}
// 11.2
bool check(Field x, Field y){
if(x.col == y.col) return true;
if(x.row == y.row) return true;
if(x.col - y.col == x.row - y.row) return true;
if(x.col - y.col == y.row - x.row) return true;
return false;
}
int toString(Field x, char* buf, int sizebuf){
return snprintf(buf,sizeofbuf,"[%c:%u]",x.col,x.row);
}
int main(){
Field x,y;
//x = input2();
do{
input(&x);
} while(!correctness(x));
vyvod(x);
do{
input(&y);
}while(!correctness(y));
vyvod(y);
Field f1 = {'a',1};
Field f2 = {'a',4};
Field f3 = {'h',4};
Field f4 = {'b',2};
printf("\n %d %d %d %d", check(f1,f2),check(f2,f3),check(f1,f4), check(f2,f4));
if(check(x,y)){
printf("\nQueen takes");
output(x); output(y);
char buf1[10],buf2[10];
toString(x,10,buf1);
toString(y,10,buf2);
printf("\nQueen takes %s to %s",buf1,buf2);
}
else{
printf("\nqueen dont take");
}
}