-
Notifications
You must be signed in to change notification settings - Fork 0
/
proj1.c
163 lines (132 loc) · 4.48 KB
/
proj1.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
/* IZP Projekt 1 - Prace s textem
** autor: Patrik Demsky
** login: xdemsk00
** datum: 17.11.2019
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
#define MAX 102 //maximalna dlzka retazca ktora zahrna 100 znakov, koncovy znak a znak noveho riadku
int compare(char entry, char number) {
//funkcia porovnava znaky
int is_same = 0;
if(number == '1') {
if(entry == '1')
is_same = 1;
}
if(number == '2') {
if(entry == '2' || entry == 'a' || entry == 'b' || entry == 'c' || entry == 'A' || entry == 'B' || entry == 'C')
is_same = 1;
}
if(number == '3') {
if(entry == '3' || entry == 'd' || entry == 'e' || entry == 'f' || entry == 'D' || entry == 'E' || entry == 'F')
is_same = 1;
}
if(number == '4') {
if(entry == '4' || entry == 'g' || entry == 'h' || entry == 'i' || entry == 'G' || entry == 'H' || entry == 'I')
is_same = 1;
}
if(number == '5') {
if(entry == '5' || entry == 'j' || entry == 'k' || entry == 'l' || entry == 'J' || entry == 'K' || entry == 'L')
is_same = 1;
}
if(number == '6') {
if(entry == '6' || entry == 'm' || entry == 'n' || entry == 'o' || entry == 'M' || entry == 'N' || entry == 'O')
is_same = 1;
}
if(number == '7') {
if(entry == '7' || entry == 'p' || entry == 'q' || entry == 'r' || entry == 's' || entry == 'P' || entry == 'Q' || entry == 'R' || entry == 'S')
is_same = 1;
}
if(number == '8') {
if(entry == '8' || entry == 't' || entry == 'u' || entry == 'v' || entry == 'T' || entry == 'U' || entry == 'V')
is_same = 1;
}
if(number == '9') {
if(entry == '9' || entry == 'w' || entry == 'x' || entry == 'y' || entry == 'z' || entry == 'W' || entry == 'X' || entry == 'Y' || entry == 'Z')
is_same = 1;
}
if(number == '0') {
if(entry == '0' || entry == '+')
is_same = 1;
}
return is_same;
}
void search(char cislo[]) {
//Funckia ktora naskenuje dva riadky
char riadok[MAX];
char riadok_druhy[MAX];
bool naslo_zaznam = false;
while(true) {
if(fgets(riadok, MAX, stdin) == NULL) break; //vyskoci z mega whilu
if(fgets(riadok_druhy, MAX, stdin) == NULL) break; //vyskoci z mega whilu
bool found = false; // pravdivostna hodnota, ktora hlada zhodu v prvom riadku
bool found_druhy = false; // pravdivostna hodnota, ktora hlada zhodu v druhom riadku
//prejde kazdy prvok v prvom riadku
for(int i = 0; riadok[i] != '\0'; i++) {
bool is_same = false;
for(int j = 0; cislo[j] != '\0'; j++) {
//funkcia porovna znaky prveho riadku a vstupu
if(compare(riadok[i+j], cislo[j]) == 1) is_same = true;
else {
is_same = false;
break;
}
}
if(is_same == true) {
found = true;
break;
}
}
//prejde kazdy prvok v druhom riadku
for(int i = 0; riadok_druhy[i] != '\0'; i++) {
bool is_same = false;
for(int j = 0; cislo[j] != '\0'; j++) {
//funkcia porovna znaky druheho riadku a vstupu
if(compare(riadok_druhy[i+j], cislo[j]) == 1) is_same = true;
else {
is_same = false;
break;
}
}
if(is_same == true) {
found_druhy = true;
break;
}
}
// Ak program najde zhodu v prvom alebo v druhom riadku
if(found || found_druhy) {
naslo_zaznam = true;
//nahradenie znaku noveho riadku medzerou
int index;
for(index = 0; riadok[index] != '\n'; index++);
riadok[index] = '\0';
printf("%s, %s", riadok, riadok_druhy);
}
}
// program nenasiel zhodu znakov
if(naslo_zaznam == false) {
printf("Not found\n");
}
}
int main (int argc, char *argv[])
{
// Bez zadania argumentu vypise cely zoznam kontaktov
if (argc < 2) {
char riadok[MAX];
char riadok_druhy[MAX];
while(true) {
if(fgets(riadok, MAX, stdin) == NULL) break;
if(fgets(riadok_druhy, MAX, stdin) == NULL) break;
//nahradenie znaku noveho riadku medzerou
int index;
for(index = 0; riadok[index] != '\n'; index++);
riadok[index] = '\0';
printf("%s, %s", riadok, riadok_druhy);
}
}
// zadany argument, vykovana funkciu vyhladavania
else search(argv[1]);
return 0;
}