Advertisement
Guest User

Untitled

a guest
May 21st, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.51 KB | None | 0 0
  1. #include <stdio.h>
  2. //#include "test.h"
  3.  
  4. #define MAX_ROW 100
  5. #define MAX_COLUMN 44
  6. #define MAX_HEX 26
  7.  
  8. #define ERROR_FSRC_COR -1
  9. #define ERROR_NUMBER_COLUMN -2
  10. #define ERROR_NUMBER_ROW -3
  11. #define ERROR_INPUT -4
  12. #define ERROR_FSRC -5
  13. #define ERROR_NUMBER -6
  14.  
  15. int bin_to_hex(int num, char m[], char new_m[])
  16. {
  17. int i;
  18. int k = 0;
  19. char rc;
  20.  
  21. char t1, t2, t3, t4;
  22.  
  23. while (num % 4 != 0)
  24. {
  25. i = num;
  26.  
  27.  
  28. while (i >= 0)
  29. {
  30. m[i+1] = m[i];
  31. i--;
  32. }
  33.  
  34. m[0] = '0';
  35. num++;
  36. }
  37. //printf("%d--", num / 4);
  38.  
  39. while (k / 4 <= num / 4)
  40. {
  41. t1 = m[k];
  42. t2 = m[k+1];
  43. t3 = m[k+2];
  44. t4 = m[k+3];
  45. //printf("|%c %c %c %c|", t1, t2, t3, t4);
  46.  
  47. if ((t1 == '0') && (t2 == '0') && (t3 == '0') && (t4 == '0'))
  48. {
  49. rc = '0';
  50. }
  51. else if ((t1 == '0') && (t2 == '0') && (t3 == '0') && (t4 == '1'))
  52. {
  53. rc = '1';
  54. }
  55. else if ((t1 == '0') && (t2 == '0') && (t3 == '1') && (t4 == '0'))
  56. {
  57. rc = '2';
  58. }
  59. else if ((t1 == '0') && (t2 == '0') && (t3 == '1') && (t4 == '1'))
  60. {
  61. rc = '3';
  62. }
  63. else if ((t1 == '0') && (t2 == '1') && (t3 == '0') && (t4 == '0'))
  64. {
  65. rc = '4';
  66. }
  67. else if ((t1 == '0') && (t2 == '1') && (t3 == '0') && (t4 == '1'))
  68. {
  69. rc = '5';
  70. }
  71. else if ((t1 == '0') && (t2 == '1') && (t3 == '1') && (t4 == '0'))
  72. {
  73. rc = '6';
  74. }
  75. else if ((t1 == '0') && (t2 == '1') && (t3 == '1') && (t4 == '1'))
  76. {
  77. rc = '7';
  78. }
  79. else if ((t1 == '1') && (t2 == '0') && (t3 == '0') && (t4 == '0'))
  80. {
  81. rc = '8';
  82. }
  83. else if ((t1 == '1') && (t2 == '0') && (t3 == '0') && (t4 == '1'))
  84. {
  85. rc = '9';
  86. }
  87. else if ((t1 == '1') && (t2 == '0') && (t3 == '1') && (t4 == '0'))
  88. {
  89. rc = 'A';
  90. }
  91. else if ((t1 == '1') && (t2 == '0') && (t3 == '1') && (t4 == '1'))
  92. {
  93. rc = 'B';
  94. }
  95. else if ((t1 == '1') && (t2 == '1') && (t3 == '0') && (t4 == '0'))
  96. {
  97. rc = 'C';
  98. }
  99. else if ((t1 == '1') && (t2 == '1') && (t3 == '0') && (t4 == '1'))
  100. {
  101. rc = 'D';
  102. }
  103. else if ((t1 == '1') && (t2 == '1') && (t3 == '1') && (t4 == '0'))
  104. {
  105. rc = 'E';
  106. }
  107. else if ((t1 == '1') && (t2 == '1') && (t3 == '1') && (t4 == '1'))
  108. {
  109. rc = 'F';
  110. }
  111.  
  112. new_m[k / 4] = rc;
  113. k += 4;
  114. }
  115.  
  116. return (num / 4);
  117. }
  118.  
  119. int read_array(FILE * f, char* m, int num_m[])
  120. {
  121. int i = 0;
  122. int j;
  123.  
  124. int rc = 0;
  125.  
  126. while (1)
  127. {
  128. if (feof(f))
  129. {
  130. break;
  131. }
  132. if (i <= 100)
  133. {
  134. j = 0;
  135.  
  136. do
  137. {
  138. rc += fscanf(f, "%c", &m[i][j]);
  139.  
  140. if ((m[i][j] == '\n'))
  141. {
  142. break;
  143. }
  144. else if ((m[i][j] != '0') && (m[i][j] != '1'))
  145. {
  146. if (feof(f))
  147. {
  148. rc++;
  149. break;
  150. }
  151.  
  152. return ERROR_FSRC_COR;
  153. }
  154.  
  155. if (j >= 100)
  156. {
  157. return ERROR_NUMBER_COLUMN;
  158. }
  159.  
  160.  
  161.  
  162. j++;
  163.  
  164.  
  165. }
  166. while(1);
  167.  
  168. num_m[i] = j;
  169. i++;
  170. }
  171. else
  172. {
  173. return ERROR_NUMBER_ROW;
  174. }
  175. }
  176. if (rc == 0)
  177. {
  178. return ERROR_NUMBER;
  179. }
  180. else
  181. {
  182. return i;
  183. }
  184. }
  185.  
  186. void print_array(int * num_m, char m[][MAX_COLUMN], int num)
  187. {
  188. for (int i = 0; i < num; i++)
  189. {
  190. for (int j = 0; j < num_m[i]; j++)
  191. {
  192. printf("%c", m[i][j]);
  193. }
  194. printf("\n");
  195. }
  196. }
  197.  
  198. int main(int argc, char **argv)
  199. {
  200. FILE * fsrc;
  201. FILE * fdst;
  202. char m[MAX_ROW][MAX_COLUMN];
  203. char new_m[MAX_HEX];
  204. int num_m[MAX_ROW];
  205. int new_num_m;
  206.  
  207. int n;
  208.  
  209. if (argc != 3)
  210. {
  211. printf("exemple.exe <file_source_name>.txt "
  212. "<file_destination_name>.txt");
  213.  
  214. return ERROR_INPUT;
  215. }
  216. else
  217. {
  218. fsrc = fopen(argv[1], "r");
  219.  
  220. fdst = fopen(argv[2], "w");
  221. fclose(fdst);
  222.  
  223. fdst = fopen(argv[2], "a");
  224.  
  225.  
  226. if (!fsrc)
  227. {
  228. fclose(fsrc);
  229. return ERROR_FSRC;
  230. }
  231.  
  232. n = read_array(fsrc, *m, num_m);
  233.  
  234. if (n == ERROR_NUMBER)
  235. {
  236. printf("The file is empty.");
  237. }
  238. else if (n == ERROR_FSRC_COR)
  239. {
  240. printf("The file contains invalid characters.");
  241. }
  242. else if (n == ERROR_NUMBER_COLUMN)
  243. {
  244. printf("The file contains too long a number.");
  245. }
  246. else if (n == ERROR_NUMBER_ROW)
  247. {
  248. printf("Too many numbers in the file.");
  249. }
  250.  
  251.  
  252. for (int i = 0; i < n; i++)
  253. {
  254. new_num_m = bin_to_hex(num_m[i], m[i], new_m);
  255.  
  256. for (int j = 0; j < new_num_m; j++)
  257. {
  258. fprintf(fdst, "%c", new_m[j]);
  259. }
  260. fprintf(fdst, "\n");
  261. }
  262. }
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement