1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
//----------------------------------------
void DisplayWithCommas(long number)
/* displaying any long integers with proper commas */
{
int number1, number2, number3, number4, number5;
if (number > 0)
{
number1 = number % 1000;
number = number / 1000;
number2 = number % 1000;
number = number / 1000;
number3 = number % 1000;
number = number / 1000;
number4 = number % 1000;
number = number / 1000;
number5 = number % 1000;
setfill ('0');
if (number5 != 0)
cout << number5 << "," << setw(3) << number4 << "," << setw(3) << number3 << "," << setw(3) << number2 << "," << setw(3) << number1 << endl;
else if (number4 != 0)
cout << number4 << "," << setw(3) << number3 << "," << setw(3) << number2 << "," << setw(3) << number1 << endl;
else if (number3 != 0)
cout << number3 << "," << setw(3) << number2 << "," << setw(3) << number1 << endl;
else if (number2 != 0)
cout << number2 << "," << setw(3) << number1 << endl;
else
cout << number1 << endl;
}
else
{
number1 = -number % 1000;
number = number / 1000;
number2 = -number % 1000;
number = number / 1000;
number3 = -number % 1000;
number = number / 1000;
number4 = -number % 1000;
number = number / 1000;
number5 = -number % 1000;
setfill ('0');
if (number5 != 0)
cout << "-" << number5 << "," << setw(3) << number4 << "," << setw(3) << number3 << "," << setw(3) << number2 << "," << setw(3) << number1 << endl;
else if (number4 != 0)
cout << "-" << number4 << "," << setw(3) << number3 << "," << setw(3) << number2 << "," << setw(3) << number1 << endl;
else if (number3 != 0)
cout << "-" << number3 << "," << setw(3) << number2 << "," << setw(3) << number1 << endl;
else if (number2 != 0)
cout << "-" << number2 << "," << setw(3) << number1 << endl;
else
cout << "-" << number1 << endl;
}
}
//--------------------------------------------------
int main(int argc, char *argv[])
{
long number;
cout << "Please enter a number: ";
cin >> number;
DisplayWithCommas(number);
system("PAUSE");
return EXIT_SUCCESS;
}
|