Advertisement
allia

перевод числа в строку

Dec 9th, 2020
815
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. char *stroka (int znach)
  8. {
  9.  int n = 1;
  10.  int c = 0;
  11.  
  12. while (znach/n != 0)
  13.  {
  14.    n *= 10;
  15.    c++;
  16.  }
  17.  n /= 10;
  18.  
  19.  char *result = new char[c+1];
  20.  
  21.  if (znach < 0)
  22.   {
  23.     znach = abs(znach);
  24.     result[0] = '-';
  25.     for (int i = 1; i <= c; i++)
  26.     {
  27.       result[i] = (znach/n) + 48;
  28.       znach %= n;
  29.       n /= 10;
  30.     }
  31.   }
  32.   else
  33.     for (int i = 0; i < c; i++)
  34.     {
  35.       result[i] = (znach/n) + 48;
  36.       znach %= n;
  37.       n /= 10;
  38.     }
  39.  
  40.  return result;
  41. }
  42.  
  43. int main()
  44. {
  45.   string s;
  46.   int znach;
  47.   cin >> znach;
  48.   s = stroka(znach);
  49.   cout << s;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement