Advertisement
Guest User

Untitled

a guest
Jun 26th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <locale.h>
  3.  
  4. int main(void)
  5. {
  6. setlocale(LC_NUMERIC, "");
  7. printf("%'dn", 1123456789);
  8. return 0;
  9. }
  10.  
  11. $ ./example
  12. 1,123,456,789
  13.  
  14. void printfcomma2 (int n) {
  15. if (n < 1000) {
  16. printf ("%d", n);
  17. return;
  18. }
  19. printfcomma2 (n/1000);
  20. printf (",%03d", n%1000);
  21. }
  22.  
  23. void printfcomma (int n) {
  24. if (n < 0) {
  25. printf ("-");
  26. n = -n;
  27. }
  28. printfcomma2 (n);
  29. }
  30.  
  31. #include <stdio.h>
  32.  
  33. void printfcomma (int n) {
  34. if (n < 0) {
  35. printf ("-");
  36. printfcomma (-n);
  37. return;
  38. }
  39. if (n < 1000) {
  40. printf ("%d", n);
  41. return;
  42. }
  43. printfcomma (n/1000);
  44. printf (",%03d", n%1000);
  45. }
  46.  
  47. int main (void) {
  48. int x[] = {-1234567890, -123456, -12345, -1000, -999, -1,
  49. 0, 1, 999, 1000, 12345, 123456, 1234567890};
  50. int *px = x;
  51. while (px != &(x[sizeof(x)/sizeof(*x)])) {
  52. printf ("%-15d: ", *px);
  53. printfcomma (*px);
  54. printf ("n");
  55. px++;
  56. }
  57. return 0;
  58. }
  59.  
  60. -1234567890 : -1,234,567,890
  61. -123456 : -123,456
  62. -12345 : -12,345
  63. -1000 : -1,000
  64. -999 : -999
  65. -1 : -1
  66. 0 : 0
  67. 1 : 1
  68. 999 : 999
  69. 1000 : 1,000
  70. 12345 : 12,345
  71. 123456 : 123,456
  72. 1234567890 : 1,234,567,890
  73.  
  74. void printfcomma (int n) {
  75. int n2 = 0;
  76. int scale = 1;
  77. if (n < 0) {
  78. printf ("-");
  79. n = -n;
  80. }
  81. while (n >= 1000) {
  82. n2 = n2 + scale * (n % 1000);
  83. n /= 1000;
  84. scale *= 1000;
  85. }
  86. printf ("%d", n);
  87. while (scale != 1) {
  88. scale /= 1000;
  89. n = n2 / scale;
  90. n2 = n2 % scale;
  91. printf (",%03d", n);
  92. }
  93. }
  94.  
  95. void format_commas(int n, char *out)
  96. {
  97. int c;
  98. char buf[20];
  99. char *p;
  100.  
  101. sprintf(buf, "%d", n);
  102. c = 2 - strlen(buf) % 3;
  103. for (p = buf; *p != 0; p++) {
  104. *out++ = *p;
  105. if (c == 1) {
  106. *out++ = ',';
  107. }
  108. c = (c + 1) % 3;
  109. }
  110. *--out = 0;
  111. }
  112.  
  113. #include <stdio.h>
  114. #include <locale.h>
  115.  
  116. int main()
  117. {
  118. int bignum=12345678;
  119.  
  120. setlocale(LC_ALL,"");
  121.  
  122. printf("Big number: %'dn",bignum);
  123.  
  124. return 0;
  125. }
  126.  
  127. #include <stdlib.h>
  128. #include <locale.h>
  129. #include <string.h>
  130.  
  131. #define NUL ''
  132.  
  133. static char *dupe_str(char const *input) {
  134. char *retval = malloc(strlen(input) + 1);
  135. if (retval)
  136. strcpy(retval, input);
  137. return retval;
  138. }
  139.  
  140. static int next_group(char const **grouping) {
  141. if ((*grouping)[1] == CHAR_MAX)
  142. return 0;
  143. if ((*grouping)[1] != '')
  144. ++*grouping;
  145. return **grouping;
  146. }
  147.  
  148. size_t commafmt(char *buf, /* Buffer for formatted string */
  149. int bufsize, /* Size of buffer */
  150. long N) /* Number to convert */
  151. {
  152. static char *tsep;
  153. static char *place_str;
  154. static char *neg_str;
  155. int places;
  156. int i, len = 1, posn = 1, sign = 1;
  157. char *ptr = buf + bufsize - 1;
  158.  
  159. if (tsep == NULL) {
  160. struct lconv *fmt_info = localeconv();
  161. tsep = dupe_str(fmt_info->thousands_sep);
  162. place_str = dupe_str(fmt_info->grouping);
  163. neg_str = dupe_str(fmt_info->negative_sign);
  164. places = place_str[0];
  165. }
  166.  
  167. if (2 > bufsize)
  168. {
  169. ABORT: *buf = NUL;
  170. return 0;
  171. }
  172.  
  173. *ptr-- = NUL;
  174. --bufsize;
  175. if (0L > N)
  176. {
  177. sign = -1;
  178. N = -N;
  179. }
  180.  
  181. for ( ; len <= bufsize; ++len, ++posn)
  182. {
  183. *ptr-- = (char)((N % 10L) + '0');
  184. if (0L == (N /= 10L))
  185. break;
  186. if (places && 0 == (posn % places))
  187. {
  188. places = next_group(&place_str);
  189. for (i=0; tsep[i]; i++, ++len) {
  190. *ptr-- = tsep[i];
  191. if (len >= bufsize)
  192. goto ABORT;
  193. }
  194. }
  195. if (len >= bufsize)
  196. goto ABORT;
  197. }
  198.  
  199. if (0 > sign)
  200. {
  201. if (len >= bufsize)
  202. goto ABORT;
  203. while (*ptr-- = *neg_str++)
  204. if (++len >= bufsize)
  205. goto ABORT;
  206. }
  207.  
  208. memmove(buf, ++ptr, len + 1);
  209. return (size_t)len;
  210. }
  211.  
  212. #ifdef TEST
  213.  
  214. int main() {
  215. char buffer[16];
  216.  
  217. setlocale(LC_ALL, "");
  218. commafmt(buffer, sizeof(buffer), 12345678);
  219.  
  220. printf("%sn", buffer);
  221. return 0;
  222. }
  223.  
  224. #endif
  225.  
  226. #include <stdio.h>
  227. #include <math.h>
  228.  
  229. void print_number( int n )
  230. {
  231. int order_of_magnitude = (n == 0) ? 1 : (int)pow( 10, ((int)floor(log10(abs(n))) / 3) * 3 ) ;
  232.  
  233. printf( "%d", n / order_of_magnitude ) ;
  234.  
  235. for( n = abs( n ) % order_of_magnitude, order_of_magnitude /= 1000;
  236. order_of_magnitude > 0;
  237. n %= order_of_magnitude, order_of_magnitude /= 1000 )
  238. {
  239. printf( ",%03d", abs(n / order_of_magnitude) ) ;
  240. }
  241. }
  242.  
  243. size_t str_format_int_grouped(char dst[16], int num)
  244. {
  245. char src[16];
  246. char *p_src = src;
  247. char *p_dst = dst;
  248.  
  249. const char separator = ',';
  250. int num_len, commas;
  251.  
  252. num_len = sprintf(src, "%d", num);
  253.  
  254. if (*p_src == '-') {
  255. *p_dst++ = *p_src++;
  256. num_len--;
  257. }
  258.  
  259. for (commas = 2 - num_len % 3; *p_src; commas = (commas + 1) % 3) {
  260. *p_dst++ = *p_src++;
  261. if (commas == 1) {
  262. *p_dst++ = separator;
  263. }
  264. }
  265. *--p_dst = '';
  266.  
  267. return (size_t)(p_dst - dst);
  268. }
  269.  
  270. int p(int n) {
  271. if(n < 0) {
  272. printf("-");
  273. n = -n;
  274. }
  275.  
  276. int a[sizeof(int) * CHAR_BIT / 3] = { 0 };
  277. int *pa = a;
  278. while(n > 0) {
  279. *++pa = n % 1000;
  280. n /= 1000;
  281. }
  282. printf("%d", *pa);
  283. while(pa > a + 1) {
  284. printf(",%03d", *--pa);
  285. }
  286. }
  287.  
  288. const char *formatNumber (
  289. int value,
  290. char *endOfbuffer,
  291. bool plus)
  292. {
  293. int savedValue;
  294. int charCount;
  295.  
  296. savedValue = value;
  297. if (unlikely (value < 0))
  298. value = - value;
  299. *--endOfbuffer = 0;
  300. charCount = -1;
  301. do
  302. {
  303. if (unlikely (++charCount == 3))
  304. {
  305. charCount = 0;
  306. *--endOfbuffer = ',';
  307. }
  308.  
  309. *--endOfbuffer = (char) (value % 10 + '0');
  310. }
  311. while ((value /= 10) != 0);
  312.  
  313. if (unlikely (savedValue < 0))
  314. *--endOfbuffer = '-';
  315. else if (unlikely (plus))
  316. *--endOfbuffer = '+';
  317.  
  318. return endOfbuffer;
  319. }
  320.  
  321. char buffer[16];
  322. fprintf (stderr, "test : %s.", formatNumber (1234567890, buffer + 16, true));
  323.  
  324. test : +1,234,567,890.
  325.  
  326. static void printNumber (int numbers[8], int loc, int negative)
  327. {
  328. if (negative)
  329. {
  330. printf("-");
  331. }
  332. if (numbers[1]==-1)//one number
  333. {
  334. printf("%d ", numbers[0]);
  335. }
  336. else
  337. {
  338. printf("%d,", numbers[loc]);
  339. while(loc--)
  340. {
  341. if(loc==0)
  342. {// last number
  343. printf("%03d ", numbers[loc]);
  344. break;
  345. }
  346. else
  347. { // number in between
  348. printf("%03d,", numbers[loc]);
  349. }
  350. }
  351. }
  352. }
  353.  
  354. static void getNumWcommas (long long int n, int numbers[8])
  355. {
  356. int i;
  357. int negative=0;
  358. if (n < 0)
  359. {
  360. negative = 1;
  361. n = -n;
  362. }
  363. for(i = 0; i<7; i++)
  364. {
  365. if (n < 1000)
  366. {
  367. numbers[i] = n;
  368. numbers[i+1] = -1;
  369. break;
  370. }
  371. numbers[i] = n%1000;
  372. n/=1000;
  373. }
  374.  
  375. printNumber(numbers, i, negative);// non recursive print
  376. }
  377.  
  378. -9223372036854775807: -9,223,372,036,854,775,807
  379. -1234567890 : -1,234,567,890
  380. -123456 : -123,456
  381. -12345 : -12,345
  382. -1000 : -1,000
  383. -999 : -999
  384. -1 : -1
  385. 0 : 0
  386. 1 : 1
  387. 999 : 999
  388. 1000 : 1,000
  389. 12345 : 12,345
  390. 123456 : 123,456
  391. 1234567890 : 1,234,567,890
  392. 9223372036854775807 : 9,223,372,036,854,775,807
  393.  
  394. int numberSeperated[8];
  395. long long int number = 1234567890LL;
  396. getNumWcommas(number, numberSeperated );
  397.  
  398. //Make sure output buffer is big enough and that input is a valid null terminated string
  399. void pretty_number(const char* input, char * output)
  400. {
  401. int iInputLen = strlen(input);
  402. int iOutputBufferPos = 0;
  403. for(int i = 0; i < iInputLen; i++)
  404. {
  405. if((iInputLen-i) % 3 == 0 && i != 0)
  406. {
  407. output[iOutputBufferPos++] = ',';
  408. }
  409.  
  410. output[iOutputBufferPos++] = input[i];
  411. }
  412.  
  413. output[iOutputBufferPos] = '';
  414. }
  415.  
  416. char szBuffer[512];
  417. pretty_number("1234567", szBuffer);
  418. //strcmp(szBuffer, "1,234,567") == 0
  419.  
  420. void format_number(int n, char * out) {
  421. int i;
  422. int digit;
  423. int out_index = 0;
  424.  
  425. for (i = n; i != 0; i /= 10) {
  426. digit = i % 10;
  427.  
  428. if ((out_index + 1) % 4 == 0) {
  429. out[out_index++] = ',';
  430. }
  431. out[out_index++] = digit + '0';
  432. }
  433. out[out_index] = '';
  434.  
  435. // then you reverse the out string as it was converted backwards (it's easier that way).
  436. // I'll let you figure that one out.
  437. strrev(out);
  438. }
  439.  
  440. printf("Value: %llu'%03lu'%03lu'%03lun", (value / 1000 / 1000 / 1000), (value / 1000 / 1000) % 1000, (value / 1000) % 1000, value % 1000);
  441.  
  442. #if defined(_WIN32)
  443. #define snprintf(buf,len, format,...) _snprintf_s(buf, len,len, format, __VA_ARGS__)
  444. #endif
  445.  
  446. char* format_commas(int n, char *out)
  447. {
  448. int c;
  449. char buf[100];
  450. char *p;
  451. char* q = out; // Backup pointer for return...
  452.  
  453. if (n < 0)
  454. {
  455. *out++ = '-';
  456. n = abs(n);
  457. }
  458.  
  459.  
  460. snprintf(buf, 100, "%d", n);
  461. c = 2 - strlen(buf) % 3;
  462.  
  463. for (p = buf; *p != 0; p++) {
  464. *out++ = *p;
  465. if (c == 1) {
  466. *out++ = ''';
  467. }
  468. c = (c + 1) % 3;
  469. }
  470. *--out = 0;
  471.  
  472. return q;
  473. }
  474.  
  475. size_t currentSize = getCurrentRSS();
  476. size_t peakSize = getPeakRSS();
  477.  
  478.  
  479. printf("Current size: %dn", currentSize);
  480. printf("Peak size: %dnnn", peakSize);
  481.  
  482. char* szcurrentSize = (char*)malloc(100 * sizeof(char));
  483. char* szpeakSize = (char*)malloc(100 * sizeof(char));
  484.  
  485. printf("Current size (f): %sn", format_commas((int)currentSize, szcurrentSize));
  486. printf("Peak size (f): %sn", format_commas((int)currentSize, szpeakSize));
  487.  
  488. free(szcurrentSize);
  489. free(szpeakSize);
  490.  
  491. static WCHAR buffer[10];
  492. static int pos = 0;
  493.  
  494. void printfcomma(const int &n) {
  495. if (n < 0) {
  496. wsprintf(buffer + pos, TEXT("-"));
  497. pos = lstrlen(buffer);
  498. printfcomma(-n);
  499. return;
  500. }
  501. if (n < 1000) {
  502. wsprintf(buffer + pos, TEXT("%d"), n);
  503. pos = lstrlen(buffer);
  504. return;
  505. }
  506. printfcomma(n / 1000);
  507. wsprintf(buffer + pos, TEXT(",%03d"), n % 1000);
  508. pos = lstrlen(buffer);
  509. }
  510.  
  511. void my_sprintf(const int &n)
  512. {
  513. pos = 0;
  514. printfcomma(n);
  515. }
  516.  
  517. void printfcomma ( long long unsigned int n)
  518. {
  519.  
  520. char nstring[100];
  521. int m;
  522. int ptr;
  523. int i,j;
  524.  
  525.  
  526. sprintf(nstring,"%llu",n);
  527. m=strlen(nstring);
  528.  
  529. ptr=m%3;
  530. if (ptr)
  531. { for (i=0;i<ptr;i++) // print first digits before comma
  532. printf("%c", nstring[i]);
  533. printf(",");
  534. }
  535. j=0;
  536. for (i=ptr;i<m;i++) // print the rest inserting commas
  537. {
  538. printf("%c",nstring[i]);
  539. j++;
  540. if (j%3==0)
  541. if(i<(m-1)) printf(",");
  542. }
  543.  
  544. }
  545.  
  546. // separate thousands
  547. int digit;
  548. int idx = 0;
  549. static char buffer[32];
  550. char* p = &buffer[32];
  551.  
  552. *--p = '';
  553. for (int i = fCounter; i != 0; i /= 10)
  554. {
  555. digit = i % 10;
  556.  
  557. if ((p - buffer) % 4 == 0)
  558. *--p = ' ';
  559.  
  560. *--p = digit + '0';
  561. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement