Untitled


SUBMITTED BY: Guest

DATE: Dec. 10, 2013, 12:06 a.m.

FORMAT: Text only

SIZE: 13.1 kB

HITS: 2264

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h> //includes strcmp
  4. #include <ctype.h> //isspace
  5. typedef enum
  6. {
  7. FEW_ARG, WRONG_ARG, MANY_ARG, NO_HLINE, NO_VLINE, WRONG_FILE, NO_SQUARE
  8. } ERRORS; //indicates a type of an error
  9. typedef enum
  10. {
  11. INVALID, VALID
  12. }MY_BOOL;
  13. typedef struct {
  14. int rows;
  15. int cols;
  16. char *cells;
  17. } Bitmap;
  18. void Help();
  19. void Error(ERRORS err);
  20. void Tisk_bool(MY_BOOL validni);
  21. char* alloc_Bitmap(int vyska, int sirka, Bitmap* stru);
  22. void free_Bitmap(Bitmap arr);
  23. int test(char* soubor, Bitmap* bitmapa);
  24. void test_bitmap(Bitmap* neco);
  25. char getcolor(Bitmap *bitmap, int x, int y);
  26. int find_vline(Bitmap *bitmap, int *x1, int *y1, int *x2, int *y2);
  27. int find_hline(Bitmap *bitmap, int *x1, int *y1, int *x2, int *y2);
  28. int find_square(Bitmap *bitmap, int *x1, int *y1, int *x2, int *y2);
  29. int main(int argc, char *argv[])
  30. {
  31. Bitmap struktura;
  32. int x1,x2,y1,y2;
  33. int found;
  34. switch (argc)
  35. {
  36. case 1: Error(FEW_ARG);
  37. break;
  38. case 2: if (strcmp(argv[1], "--help" ) == 0)
  39. Help();
  40. else
  41. Error(WRONG_ARG);
  42. break;
  43. case 3: if (strcmp(argv[1], "--test" ) == 0)
  44. {
  45. Tisk_bool(test(argv[2], &struktura)); //zkontroluje a vypíše
  46. }
  47. else if (strcmp(argv[1], "--hline" ) == 0)
  48. {
  49. if(test(argv[2], &struktura ) == 1) //soubor obsahuje bitmapu
  50. {
  51. found = find_hline(&struktura, &x1, &y1, &x2, &y2);
  52. if (found == 0)
  53. {
  54. Error(NO_HLINE);
  55. }
  56. else
  57. {
  58. printf("%d %d %d %d\n", x1, y1, x2, y2);
  59. }
  60. }
  61. else
  62. {
  63. Error(WRONG_FILE);
  64. }
  65. }
  66. else if (strcmp(argv[1], "--vline" ) == 0)
  67. {
  68. if(test(argv[2], &struktura ) == 1) //soubor obsahuje bitmapu
  69. {
  70. found = find_vline(&struktura, &x1, &y1, &x2, &y2);
  71. if (found == 0)
  72. {
  73. Error(NO_VLINE);
  74. }
  75. else
  76. {
  77. printf("%d %d %d %d\n", x1, y1, x2, y2);
  78. }
  79. }
  80. else
  81. {
  82. Error(WRONG_FILE);
  83. }
  84. }
  85. else if (strcmp(argv[1], "--square" ) == 0)
  86. {
  87. if(test(argv[2], &struktura ) == 1) //soubor obsahuje bitmapu
  88. {
  89. found = find_square(&struktura, &x1, &y1, &x2, &y2);
  90. if (found == 0)
  91. {
  92. Error(NO_SQUARE);
  93. }
  94. else
  95. {
  96. printf("%d %d %d %d\n", x1, y1, x2, y2);
  97. }
  98. }
  99. else
  100. {
  101. Error(WRONG_FILE);
  102. }
  103. }
  104. else
  105. Error(WRONG_ARG);
  106. break;
  107. default:
  108. Error(MANY_ARG);
  109. break;
  110. }
  111. free(struktura.cells);
  112. return 0;
  113. }
  114. void Help()
  115. {
  116. printf("Program: 3rd project for IZP - Searching of shapes\n"
  117. "Author: Eliska Chalupova (c) 2013\n \n"
  118. "Control: \n"
  119. "--help prints how to use the program\n"
  120. "--test checks if a file contains a bitmap picture or not\n"
  121. "--hline prints coordinates of the first longest horizontal line segment of a picture\n"
  122. "--vline prints coordinates of the first longest vertical line segment of a picture\n"
  123. "--square prints coordinates of the first biggest square of a picture\n");
  124. }
  125. void Error(ERRORS err)
  126. {
  127. switch(err)
  128. {
  129. case FEW_ARG: fprintf(stderr, "You have not written enough arguments. For help use --help\n");
  130. break;
  131. case WRONG_ARG: fprintf(stderr, "You have used wrong arguments. For help use --help\n");
  132. break;
  133. case MANY_ARG: fprintf(stderr, "You have written too many arguments. For help use --help\n");
  134. break;
  135. case NO_HLINE: fprintf(stderr, "There is no horizontal line in the file.\n");
  136. break;
  137. case NO_VLINE: fprintf(stderr, "There is no vertical line in the file.\n");
  138. break;
  139. case NO_SQUARE: fprintf(stderr, "There is no square in the file.\n");
  140. break;
  141. case WRONG_FILE:fprintf(stderr, "The file does not contain a correct bitmap.\n");
  142. break;
  143. }
  144. }
  145. void Tisk_bool(MY_BOOL validni)
  146. {
  147. switch (validni)
  148. {
  149. case INVALID: printf("Invalid\n");
  150. break;
  151. case VALID: printf("Valid\n");
  152. break;
  153. }
  154. }
  155. int test(char* soubor, Bitmap* bitmapa)
  156. {
  157. FILE* p_file;
  158. p_file = fopen(soubor,"r");
  159. if (p_file == NULL)
  160. {
  161. return INVALID;
  162. }
  163. else //soubor se otevøel
  164. {
  165. int RP = 1; //pro switch
  166. int pocitadlo = 0; //pocitadlo jednicek a nul
  167. int c_predchozi = 0;
  168. int radky = 0;
  169. int sloupce = 0;
  170. int c = 0;
  171. Bitmap struktura;
  172. char* alok_pole = NULL;
  173. int uz_nealokuj = 0; //aby se krok 3 neopakoval vícekrát
  174. while ((c = fgetc(p_file))!=EOF)
  175. {
  176. if(isspace(c)==0) //neni mezera
  177. {
  178. if(isdigit(c)!=0) //je číslo
  179. {
  180. int c_cislo = c - '0';
  181. switch(RP)
  182. {
  183. case 1: radky = c_predchozi*10 + c_cislo;
  184. c_predchozi = c_cislo;
  185. //printf("radky: %d", radky); //OK
  186. bitmapa->rows = radky;
  187. break;
  188. case 2: sloupce = c_predchozi*10 + c_cislo;
  189. c_predchozi = c_cislo;
  190. //printf(" sloupce: %d", sloupce); //OK
  191. bitmapa->cols = sloupce;
  192. break;
  193. case 3: if((c == '1' || c == '0') && pocitadlo <= (radky * sloupce))
  194. {
  195. alok_pole[pocitadlo] = c;
  196. pocitadlo++;
  197. }
  198. else
  199. {
  200. if (alok_pole != NULL)
  201. {
  202. free(alok_pole);
  203. alok_pole = NULL;
  204. }
  205. return INVALID;
  206. }
  207. break;
  208. }
  209. }
  210. else //není èíslo
  211. {
  212. if (alok_pole != NULL)
  213. {
  214. free(alok_pole);
  215. alok_pole = NULL;
  216. }
  217. return INVALID;
  218. }
  219. }
  220. else //je mezera
  221. {
  222. if(radky!=0 && sloupce == 0)
  223. {
  224. RP = 2;
  225. c_predchozi = 0;
  226. }
  227. if(sloupce!=0 && uz_nealokuj == 0)
  228. {
  229. alok_pole = alloc_Bitmap(radky, sloupce, &struktura); //nacte radky, sloupce a alokuje pole o urcite velikosti ve strukture
  230. uz_nealokuj = 1;
  231. RP = 3;
  232. }
  233. }
  234. }
  235. if (pocitadlo < (radky*sloupce)) //
  236. {
  237. if (alok_pole != NULL)
  238. {
  239. free(alok_pole);
  240. alok_pole = NULL;
  241. }
  242. return INVALID;
  243. }
  244. bitmapa->cells = alok_pole;
  245. }
  246. return VALID;
  247. }
  248. char* alloc_Bitmap(int vyska, int sirka, Bitmap* stru)
  249. {
  250. stru->rows = vyska;
  251. stru->cols = sirka;
  252. stru->cells = malloc((vyska*sirka)*sizeof(char));
  253. return stru->cells; //ukazatel
  254. }
  255. void free_Bitmap(Bitmap stru)
  256. {
  257. free(stru.cells);
  258. stru.cells = NULL; //!!!!!
  259. }
  260. void test_bitmap(Bitmap* neco)
  261. {
  262. for(int i = 0; i < neco->rows*neco->cols; i++)
  263. {
  264. printf("%c ",neco->cells[i]);
  265. }
  266. }
  267. char getcolor(Bitmap *bitmap, int x, int y)
  268. {
  269. return bitmap->cells[(((bitmap->cols)*y) + x)];
  270. }
  271. int find_vline(Bitmap *bitmap, int *x1, int *y1, int *x2, int *y2)
  272. {
  273. *x1 = 1;
  274. *y1 = 3;
  275. *x2 = 4;
  276. *y2 = 9;
  277. return 0;
  278. }
  279. int find_hline(Bitmap *bitmap, int *x1, int *y1, int *x2, int *y2)
  280. {
  281. int pocitadlo = 0;
  282. char predch = '0';
  283. int nejdelsi = 0;
  284. int konec = 0;
  285. char akt;
  286. for (int j = 0; j < bitmap->rows; j++)
  287. {
  288. for(int i = 0; i < bitmap->cols; i++)
  289. {
  290. akt = getcolor(bitmap, i, j);
  291. printf("%d . ", akt);
  292. //if(akt!='1' || akt!='0'){exit(100);}
  293. if (i == bitmap->cols - 1)
  294. {
  295. konec = 1;
  296. }
  297. if (akt == '1')
  298. {
  299. pocitadlo++;
  300. }
  301. if(akt == '0')
  302. {
  303. pocitadlo = 0;
  304. }
  305. if ((akt == '0' || konec == 1) && predch == '1' && pocitadlo > nejdelsi)
  306. {
  307. nejdelsi = pocitadlo;
  308. pocitadlo = 0;
  309. if (nejdelsi >= 2)
  310. {
  311. *x2 = i;
  312. *y2 = j;
  313. *x1 = i - nejdelsi;
  314. *y1 = j;
  315. }
  316. else
  317. {
  318. return 10;
  319. }
  320. }
  321. predch = akt;
  322. }
  323. pocitadlo = 0;
  324. predch = 0;
  325. konec = 0;
  326. }
  327. return 1;
  328. }
  329. int find_square(Bitmap *bitmap, int *x1, int *y1, int *x2, int *y2)
  330. {
  331. *x1 = 1;
  332. *y1 = 8;
  333. *x2 = 4;
  334. *y2 = 9;
  335. return 0;
  336. }

comments powered by Disqus