+ 1

wanna do a program that will print out what is going in a calculation. for eg. 300 *488 _____ so I basically want it printed out like how it would if I was doing it on paper

15th Sep 2016, 11:02 AM
Antonio Gallimore
Antonio Gallimore - avatar
2 Answers
+ 2
Oh, I didn't quite get it last time because I was using the website and carriage returns in title are ignored there. Here you go: http://code.sololearn.com/cNemf8rTyQdc #include <iostream> using namespace std; int nbDigits(int n) { int res = 0; while (n != 0) { res++; n = n/10; } return res; } int max(int a, int b) { if (a > b) return a; return b; } int main() { int a, b, da, db, dres, i, maxd, res; char op; cout << "Please enter your operation: " << endl; cin >> a >> op >> b; switch (op) { case '+': res = a + b; break; case '-': res = a - b; break; case '*': res = a * b; break; case '/': res = a / b; break; default: cout << "Undefined operation" << endl; return 1; } da = nbDigits(a); db = nbDigits(b); dres = nbDigits(res); maxd = max(max(da, db)+1, dres); for (i = 0; i < maxd-da; i++) { cout << " "; } cout << a << endl; cout << op; for (i = 0; i < maxd-db-1; i++) { cout << " "; } cout << b << endl; for (i = 0; i < maxd; i++) { cout << "_"; } cout << endl; for (i = 0; i < maxd-dres; i++) { cout << " "; } cout << res << endl; return 0; }
15th Sep 2016, 12:14 PM
Zen
Zen - avatar
+ 1
you'll be doing some switching based off of peers of operations. the way I would do it is to have the user input the numbers as a string so you can choose which number to work with based off its location in the array. then convert that substring into an int with a stringstream and do the work on it with the second number. then just follow basic math rules and format ir. I'm sure there's better ways but that's what I can think of right now
15th Sep 2016, 11:36 AM
destro