Skip to content

Instantly share code, notes, and snippets.

@dgodfrey206
Last active May 22, 2019 02:06
Show Gist options
  • Save dgodfrey206/2d70f3ac882427943e4766fd4f2daec3 to your computer and use it in GitHub Desktop.
Save dgodfrey206/2d70f3ac882427943e4766fd4f2daec3 to your computer and use it in GitHub Desktop.
Self explanatory
#include <string>
#include <fstream>
#include <streambuf>
#include <ctime>
#include <iomanip>
#include <cmath>
#include <iostream>
int displayA(int n, int w) {
if (n == 0) return 0;
int l = 1 + displayA(n/10, w);
int d = n%10;
std::cout << std::setw(w) << std::right << std::string(l, 'A' + d - 1) << '\n';
return l;
}
int len(int n) {
return std::log(n)/std::log(10) + 1;
}
// wrong output
int displayB(int x) {
int w = len(x);
for (int i=1; x != 0; i++) {
std::cout << std::setw(w) << std::right << std::string(i, 'A' + x % 10 - 1) << '\n';
x /= 10;
}
return w;
}
int displayC(int x) {
std::string str = std::to_string(x);
int n = str.size();
for (int i=0; i<n; i++) {
std::cout << std::setw(n) << std::right << std::string(i + 1, 'A' + str[i] - 1 - '0') << '\n';
}
return n;
}
int displayD(int x) {
int w = len(x);
int y = std::pow(10, w - 1);
for (int i=1; y != 0; i++) {
std::cout << std::setw(w) << std::string(i, 'A' + (x / y) % 10 - 1) << '\n';
y /= 10;
}
return w;
}
int main()
{
int x = displayD(31412);
std::cout << x;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment