Advertisement
nein_yards

Untitled

Mar 11th, 2022
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void printNumberAsChars(double x, char charArray[]);
  5. void printNumberAsChars(int x, char charArray[]);
  6.  
  7. int main() {
  8. char output[10] = { 'X', 'C', 'O', 'M', 'P', 'U', 'T', 'E', 'R', 'S' };
  9. double x = 5037;
  10. printNumberAsChars(x, output);
  11. cout << "\n\n";
  12.  
  13. x = 398156.72;
  14. printNumberAsChars(x, output);
  15. cout << endl;
  16.  
  17. return 0;
  18. }
  19.  
  20. void printNumberAsChars(double x, char charArray[]) {
  21. int tmp = x;
  22. int decimalPart = 0;
  23. int coef = 1;
  24.  
  25. while (x * coef - tmp != 0) {
  26. coef *= 10;
  27. tmp = x * coef;
  28. decimalPart *= 10;
  29. decimalPart += tmp % 10;
  30. }
  31. if (coef == 1) {
  32. printNumberAsChars((int)x, charArray);
  33. return;
  34. }
  35. printNumberAsChars(tmp / coef, charArray);
  36. cout << ".";
  37. printNumberAsChars(decimalPart, charArray);
  38. }
  39. void printNumberAsChars(int x, char charArray[]) {
  40. int xTrunc = x;
  41. int placeValue = 1;
  42. while (xTrunc >= 10) {
  43. placeValue *= 10;
  44. xTrunc /= 10;
  45. }
  46.  
  47. for (int i = placeValue; i > 0; i /= 10) {
  48. cout << charArray[x / i];
  49. x %= i;
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement