1 #include <gtk/gtk.h>
2 #include "kmap_struct.h"
3 #include "g_library.h"
4 #include "key_table_entry.h"
5 #include "check_octet.h"
6 #include "check_quad.h"
7 #include "check_pair.h"
8 #include "mk_single.h"
9 
10 
11 /*********************** Checks the whole kmap table ,If there is no 1 Kmap, key set to 0***********************/
chk_no_1(void)12 int chk_no_1(void)
13 {
14 	int i,j;
15 	for(i=0;i<=3;i++)
16 	{
17 		for(j=0;j<=3;j++)
18 		{
19 			if(cell_array[i][j].value!='1');
20 			else
21 				return 1;/*If any 1 found return 1*/
22 		}
23 	}
24 	gtk_entry_set_text(GTK_ENTRY(key_entry1),"0");
25 	return 0;
26 }
27 
28 
29 /******************** Checks for all elements as 1 or X ****************/
chk_all_1s(void)30 int chk_all_1s(void)
31 {
32 	int i,j;
33 	for(i=0;i<=3;i++)
34 	{
35 		for(j=0;j<=3;j++)
36 		{
37 			if((cell_array[i][j].value=='1')||(cell_array[i][j].value=='X'));
38 			else
39 				return 1; /*Return 1 when any Zero appers in the Kmap Table*/
40 		}
41 	}
42 	gtk_entry_set_text(GTK_ENTRY(key_entry1),"1");
43 	return 0;
44 }
45 
46 /*################# checks all octet possibilities in Kmap #############################*/
chk_mk_octet(void)47 void chk_mk_octet(void)
48 {
49 	int i,j;
50 	for(i=0;i<=3;i++)
51 	{
52 		for(j=0;j<=3;j++)
53 		{
54 			if(cell_array[i][j].value=='1')
55 			{
56 				check_octet(i,j);
57 			}
58 		}
59 	}
60 	return;
61 }
62 
63 /*########## This function will Set status of connection ##########################*/
64 /*Logic:Take a character from the string of cell numbers(particular connection) and search it in all connection's cell_num_string
65    by calling function find_char_in_str(). If each character is found in the different connections, i.e. the length of string of cell
66   numbers(particular connection)becomes equal to the counter result INACTIVATE that connection */
set_status_of_connection(void)67 void set_status_of_connection(void)
68 {
69 	int i,j,result1,result=0;
70 	size_t length;
71 	for(i=0;i<=connection;i++)
72 	{
73 		result1=0;
74 		length=strlen(out_array[i].cell_num_string);
75 		for(j=0;j<length;j++)
76 		{
77 			result=find_char_in_str(i,out_array[i].cell_num_string[j]);
78 			result1=result1+result;
79 		}
80 		if(result1==length)
81 			out_array[i].status=INACTIVE;
82 		else
83 			out_array[i].status=ACTIVE;
84 	}
85 	return;
86 }
87 
88 
89 /*################# checks all quad possibilities in Kmap ############################*/
chk_mk_quad(void)90 void chk_mk_quad(void)
91 {
92 	int i,j;
93 	for(i=0;i<=3;i++)
94 	{
95 		for(j=0;j<=3;j++)
96 		{
97 			if(cell_array[i][j].value=='1')
98 			{
99 				check_quad(i,j);
100 			}
101 		}
102 	}
103 	return;
104 }
105 
106 /*############# MATCH CHARCTER IN out_array #########################*/
find_char_in_str(int conn_no_of_findthis,char find_this)107 int find_char_in_str(int conn_no_of_findthis,char find_this)
108 {
109 	int i,found=0;
110 	for(i=0;i<=connection;i++)
111 	{
112 		if(i!=conn_no_of_findthis)
113 		{
114 			if(strchr(out_array[i].cell_num_string,find_this)!=NULL)
115 			{
116 				found++;
117 				return found;
118 			}
119 		}
120 		else
121 		{
122 			continue;
123 		}
124 	}
125 	return found;
126 }
127 
128 
129 /*################# checks all pair possibilities in Kmap ############################*/
chk_mk_pair(void)130 void chk_mk_pair(void)
131 {
132 	int i,j;
133 	for(i=0;i<=3;i++)
134 	{
135 		for(j=0;j<=3;j++)
136 		{
137 			if(cell_array[i][j].value=='1')
138 			{
139 				check_pair(i,j);
140 			}
141 		}
142 	}
143 	return;
144 }
145 
146 /*################# checks and make all singles in Kmap #############################*/
chk_mk_single(void)147 void chk_mk_single(void)
148 {
149 	int i,j;
150 	for(i=0;i<=3;i++)
151 	{
152 		for(j=0;j<=3;j++)
153 		{
154 			if(cell_array[i][j].value=='1')
155 			{
156 				mk_single(i,j);
157 			}
158 		}
159 	}
160 	return;
161 }
162 
163 /*########### Checks whole row for non zeros.RETURNS 0 for non zeros #################*/
chk_row_nzero(int row,int col)164 int chk_row_nzero(int row,int col)
165 {
166   int i,j,check;
167   i=row;
168   j=col;
169   check=0;
170 	/*No need to check for the element itself here because it is sure to be 1 and its right we have to check*/
171   if(cell_array[row][col].value!='0')
172 	{
173 		for(;strcmp(cell_array[i][j].right->col_string,cell_array[row][col].col_string)!=0;j++)
174 		{
175 			if((cell_array[i][j].right->value=='1')||(cell_array[i][j].right->value=='X'))
176 	 		{
177 				check=0; /*check=0 confirms that the elements in row are non Zeros*/
178 				if(j==3)
179 				j=-1;
180 	 		}
181 	 		else
182 	 		{
183 				check=1; /*If all elements in row are not 1 loop will break and make check=1*/
184 				break;
185 	 		}
186   		}
187 	}
188 	else
189 	{
190 		return N_FOUND;
191 	}
192   return check;
193 }
194 
195 
196 
197 /*########### Checks whole col for non zeros.RETURNS 0 for non zeros #################*/
chk_col_nzero(int row,int col)198 int chk_col_nzero(int row,int col)
199 {
200   int i,j,check;
201   i=row;
202   j=col;
203   check=0;
204 	/*No need to check for the element itself here because it is sure 1 and its down we have to check*/
205   if(cell_array[row][col].value!='0')
206 	{
207 		for(;strcmp(cell_array[i][j].down->row_string,cell_array[row][col].row_string)!=0;i++)
208   	{
209 	 		if((cell_array[i][j].down->value=='1')||(cell_array[i][j].down->value=='X'))
210 	 		{
211 				check=0; /*check=0 confirms that the elements of column are non Zeros*/
212 				if(i==3)
213 				i=-1;
214 	 		}
215 	 		else
216 	 		{
217 				check=1; /*If all elements in col are not 1 loop will break and make check=1*/
218 				break;
219 	 		}
220   		}
221 	}
222 	else
223 	{
224 		return N_FOUND;
225 	}
226   return check;
227 }
228 
229 /*################### Set status of connection ########################*/
set_status_active()230 void set_status_active()
231 {
232 	out_array[connection].status=ACTIVE;
233 	return;
234 }
235 
236 /*##################### Finds value for variable new_row  ##########################*/
get_new_row(int row,int hozntal)237 int get_new_row(int row,int hozntal)
238 {
239 	int new_row=0;
240 	switch(hozntal)
241 	{
242 	 	case LOWER:
243 			if(row==3)
244 				new_row=0;
245 			else
246 				new_row=row+1;
247 	 			break;
248 		case UPPER:
249 			if(row==0)
250 				new_row=3;
251 			else
252 				new_row=row-1;
253 	  		break;
254 	}
255 	return new_row;
256 }
257 
258 
259 /*##################### Finds value for variable new_row  ##########################*/
get_new_col(int col,int vertical)260 int get_new_col(int col,int vertical)
261 {
262 	int new_col=0;
263 	switch(vertical)
264 		{
265 			case LEFT:
266 				if(col==0)
267 			 	new_col=3;
268 			 	else
269 					new_col=col-1;
270 			  break;
271 	   	case RIGHT:
272 				if(col==3)
273 					new_col=0;
274 				else
275 					new_col=col+1;
276 		  	break;
277 		}
278 	return new_col;
279 }
280 
281 /*######################### Set the final Output ########################*/
282 
set_output(void)283 void set_output(void)
284 {
285 	int i;
286 	char value_from_semi_final_out;
287 	strcpy(semi_final_out,"");
288 	for(i=0;i<=connection;i++)
289 	{
290 		if(out_array[i].status==ACTIVE)
291 		{
292 			if(i!=connection)
293 			strcat(out_array[i].connection_output,"+");
294 		}
295 	}
296 
297 	for(i=0;i<=connection;i++)
298 	{
299 		if(out_array[i].status==ACTIVE)
300 		{
301 			strcat(semi_final_out,out_array[i].connection_output);
302 		}
303 	}
304 	strcpy(final_out,"");
305 	i=0;
306 	while(semi_final_out[i]!='\0')
307 	{
308 		value_from_semi_final_out=semi_final_out[i];
309 		switch(value_from_semi_final_out)
310 		{
311 			case 'a':
312 				strcat(final_out,"A'");
313 				break;
314 			case 'b':
315 				strcat(final_out,"B'");
316 				break;
317 			case 'c':
318 				strcat(final_out,"C'");
319 				break;
320 			case 'd':
321 				strcat(final_out,"D'");
322 				break;
323 			case 'A':
324 				strcat(final_out,"A");
325 				break;
326 			case 'B':
327 				strcat(final_out,"B");
328 				break;
329 			case 'C':
330 				strcat(final_out,"C");
331 				break;
332 			case 'D':
333 				strcat(final_out,"D");
334 				break;
335 			case '+':
336 				strcat(final_out,"+");
337 				break;
338 		}
339 		i++;
340 	}
341 	int final_length,x,k;
342 	final_length=x=0;
343 	final_length=strlen(final_out);
344 	if(final_length>32)
345 	{
346 		x=31;
347 		while(final_out[x]!='+')
348 		{
349 			x--;
350 		}
351 		char *temp, *entry2;
352 		temp=malloc(2);
353 		entry2=malloc(33);
354 		strcpy(temp,"");
355 		strcpy(entry2,"");
356 		for(k=x;final_out[k]!='\0';k++)
357 		{
358 			sprintf(temp,"%c",final_out[k]);
359 			strcat(entry2,temp);
360 		}
361 		final_out[x]='\0';
362 		free(temp);
363 		gtk_widget_show(key_entry2);
364 		gtk_entry_set_text(GTK_ENTRY(key_entry2),entry2);
365 	}
366 	else
367 	{
368 		gtk_widget_hide(key_entry2);
369 	}
370 	gtk_entry_set_text(GTK_ENTRY(key_entry1),final_out);
371 
372 
373 	connection=-1;/*after showing the values set initialize them for further usage*/
374 	semi_final_out[0]='\0';
375 	strcpy(final_out,"");
376 	for(i=0;i<=20;i++)
377 	{
378 		out_array[i].connection_output[0]='\0';
379 	}
380 	return;
381 }
382