fork(1) download
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. char *int2str(int input)
  5. {
  6. if (input == 0)
  7. return "0";
  8. bool negative = false;
  9. if (input < 0)
  10. {
  11. input = abs(input);
  12. negative = true;
  13. }
  14. int digits = log10(input) + 1;
  15. char *output = new char[digits + (negative ? 2 : 1)];
  16. char *current_char = output;
  17. if (negative)
  18. {
  19. output[0] = '-';
  20. current_char++;
  21. }
  22. int current_num = input;
  23. for (int current_digit = digits - 1; current_digit >= 0; current_digit--)
  24. {
  25. int p = pow(10, current_digit);
  26. int d = current_num / p;
  27. current_num -= p * d;
  28. *current_char = '0' + d;
  29. current_char++;
  30. }
  31. *current_char = '\0';
  32. return output;
  33. }
  34.  
  35. int main()
  36. {
  37. printf("%s\n%s\n%s", int2str(-999), int2str(54321), int2str(0));
  38. return 0;
  39. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
-999
54321
0