Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- void printNumberAsChars(double x, char charArray[]);
- void printNumberAsChars(int x, char charArray[]);
- int main() {
- char output[10] = { 'X', 'C', 'O', 'M', 'P', 'U', 'T', 'E', 'R', 'S' };
- double x = 5037;
- printNumberAsChars(x, output);
- cout << "\n\n";
- x = 398156.72;
- printNumberAsChars(x, output);
- cout << endl;
- return 0;
- }
- void printNumberAsChars(double x, char charArray[]) {
- int tmp = x;
- int decimalPart = 0;
- int coef = 1;
- while (x * coef - tmp != 0) {
- coef *= 10;
- tmp = x * coef;
- decimalPart *= 10;
- decimalPart += tmp % 10;
- }
- if (coef == 1) {
- printNumberAsChars((int)x, charArray);
- return;
- }
- printNumberAsChars(tmp / coef, charArray);
- cout << ".";
- printNumberAsChars(decimalPart, charArray);
- }
- void printNumberAsChars(int x, char charArray[]) {
- int xTrunc = x;
- int placeValue = 1;
- while (xTrunc >= 10) {
- placeValue *= 10;
- xTrunc /= 10;
- }
- for (int i = placeValue; i > 0; i /= 10) {
- cout << charArray[x / i];
- x %= i;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement