Advertisement
Guest User

Untitled

a guest
Jul 12th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. /*
  2. Name: Yoni Melki
  3. ID Number: 328788138
  4. Last Update: 11 Jul 2019
  5.  
  6. This program is a new version of the funcion printf
  7. */
  8.  
  9. #define _CRT_SECURE_NO_WARNINGS
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12.  
  13. //PROTOTYPES
  14. void printFormattedIntegers(char* format, char* numbers);
  15. void int2bin(int n);
  16. int char2int(char *token);
  17. void int2rom(int n);
  18. //END OF PROTOTYPES
  19.  
  20. void main()
  21. {
  22.  
  23. char format[100];
  24.  
  25. char numbers[100];
  26.  
  27. gets(format);
  28.  
  29. gets(numbers);
  30.  
  31. printFormattedIntegers(format, numbers);
  32.  
  33. }
  34.  
  35. int char2int(char *p)
  36. /*
  37. Function that get a char and turn it to an int
  38. */
  39. {
  40. int n = 0;
  41. while ((*p < '0' || *p > '9') && *p != EOF)
  42. p++;
  43.  
  44. while ('0' <= *p && *p <= '9' && *p != EOF)
  45. {
  46. n *= 10;
  47. n += *p - '0';
  48.  
  49. p++;
  50. }
  51.  
  52. return n;
  53. }
  54.  
  55. void printFormattedIntegers(char* format, char* numbers)
  56. /*
  57. Function that gets 2 strings, and turn all the %d to an int from numbers[]
  58. %x to an hexadecimal number, %o to an octal number, %b to a binary number
  59. and %r to a roman number
  60. */
  61. {
  62. int i;
  63. char *token = numbers;
  64. char *p = format;
  65. int n;
  66.  
  67. for (i = 0; *(p + i) != '\0'; i++)
  68. {
  69. if (*(p + i) != '%')
  70. printf("%c", *(p + i));
  71. else
  72. {
  73. n = char2int(token);
  74. while ('0' <= *token && *token <= '9')
  75. token++;
  76. while ((*token < '0' || *token > '9') && *token != EOF)
  77. token++;
  78.  
  79. switch (*(p + i + 1))
  80. {
  81. case 'd': printf("%d", n); break;
  82. case 'o': printf("%o", n); break;
  83. case 'x': printf("%x", n); break;
  84. case 'b': int2bin(n); break;
  85. case 'r': int2rom(n); break;
  86. }
  87. p++;
  88. }
  89. }
  90. printf("\n");
  91. }
  92.  
  93. void int2bin(int n)
  94. {
  95. int bin = 0;
  96. int pow = 1;
  97.  
  98. while (n > 0)
  99. {
  100. bin += (n % 2)*pow;
  101. pow *= 10;
  102. n /= 2;
  103. }
  104.  
  105. printf("%d", bin);
  106. }
  107.  
  108. void int2rom(int num)
  109. {
  110. while (num != 0)
  111. {
  112.  
  113. if (num >= 1000) // 1000 - m
  114. {
  115. printf("M");
  116. num -= 1000;
  117. }
  118.  
  119. else if (num >= 900) // 900 - cm
  120. {
  121. printf("CM");
  122. num -= 900;
  123. }
  124.  
  125. else if (num >= 500) // 500 - d
  126. {
  127. printf("D");
  128. num -= 500;
  129. }
  130.  
  131. else if (num >= 400) // 400 - cd
  132. {
  133. printf("CD");
  134. num -= 400;
  135. }
  136.  
  137. else if (num >= 100) // 100 - c
  138. {
  139. printf("C");
  140. num -= 100;
  141. }
  142.  
  143. else if (num >= 90) // 90 - xc
  144. {
  145. printf("XC");
  146. num -= 90;
  147. }
  148.  
  149. else if (num >= 50) // 50 - l
  150. {
  151. printf("L");
  152. num -= 50;
  153. }
  154.  
  155. else if (num >= 40) // 40 - xl
  156. {
  157. printf("XL");
  158. num -= 40;
  159. }
  160.  
  161. else if (num >= 10) // 10 - x
  162. {
  163. printf("X");
  164. num -= 10;
  165. }
  166.  
  167. else if (num >= 9) // 9 - ix
  168. {
  169. printf("IX");
  170. num -= 9;
  171. }
  172.  
  173. else if (num >= 5) // 5 - v
  174. {
  175. printf("V");
  176. num -= 5;
  177. }
  178.  
  179. else if (num >= 4) // 4 - iv
  180. {
  181. printf("IV");
  182. num -= 4;
  183. }
  184.  
  185. else if (num >= 1) // 1 - i
  186. {
  187. printf("I");
  188. num -= 1;
  189. }
  190.  
  191. }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement