// validates string 's' passed in and extracts a front sign (if there) into char var 'sign'
bool isInt( string& s, char& sign ); // Note: string without a 'sign' is returned
#include <iostream>
#include <string>
using namespace std;
// Recursive method of adding commas to numbers as C++ strings ...
// s is string passed in, i is index of next char to add to new string,
// ns is new string returned by ref. with commas inserted ...
void addCommas( const string& s, int i, string& ns )
{
int j, k = 0;
for(j = i; j >= 0 && k < 3; --j, k++ )
{
ns = s[j] + ns; // new char is added to front of ns
if( j > 0 && k == 2) ns = "," + ns;
}
if( i-3 >= 0 ) addCommas( s, i-3, ns );
}
// For BEGINNING COMPUTER PROGRAMMING using HLA, Python, C/C++
// http://developers-heaven.net/forum/index.php/topic,46.0.html
// validates string s passed in and extracts a front sign into char var 'sign'
bool isInt( string& s, char& sign ) // Note: string without a 'sign' is returned
{
sign = 0; // default sign ... i.e no sign ...
// first ... strip off all trailing whitespace ...
int j = 0,
len = s.length();
while( s[len-1-j] == ' ' || s[len-1-j] == '\t' ) ++j;
s.erase(len-j); // i.e. erase all j trailing whitespace ...
if( s == "" ) return false; // test here for empty string ...
// if reach here ... some non-ws exits ... so now ...
// skip over all leading whitespace
j = 0;
while( s[j] == ' ' || s[j] == '\t' ) ++j;
s = s.substr(j); // i.e. skip over j leading whitesapce char's
// now check for pos or neg sign character ... (in front of other char's)
if( s.length() > 1 && (s[0] == '-' || s[0] == '+') )
{
sign = s[0];
s.erase(0,1); // or ... could use s = s.substr(1);
}
// handle special cases of '-0' ... leading ... or all zero's ...
len = s.length(); // update len ...
for( int i = 0; i < len-1; ++i ) // i < len-1 leaves last zero in place ...
{
if( s[0] == '0' ) s.erase(0,1);
else break; // since a non-zero char was found ...
}
if( s == "0" ) { sign = 0; return true; }
len = s.length(); // ok ... get updated length of string & test digits ...
for( int i = 0; i < len; ++i )
{
if( s[i] < '0' || s[i] > '9' ) return false; // since non 0..9 found
}
return true;
}
// defaults to ... 'Yes more' ... unless 'n' or 'N' was entered ...
bool more() // returns true unless 'n' or 'N' entered, then and only then, false
{
cout << "\nMore y/n ? " << flush;
int reply = cin.get();
cin.sync(); // 'flush' cin stream ...
return reply != 'n' && reply != 'N' ;
}
int main()
{
for( ; ; )
{
string str, nstr; // set to "" by class string default constructor
cout << "Please enter an integer of any size : " << flush;
getline ( cin, str );
char sign;
if( !isInt( str, sign ) ) { cout << "Error! ... "; continue; }
addCommas( str, str.length()-1, nstr );
// show sign only if we had a signed integer ... i.e if sign != 0
if( sign ) cout << sign;
cout << nstr << " with " << nstr.length() - nstr.length()/4
<< " significant digit(s)." << endl;
if( !more() ) break;
}
}
Comments :
0 comments to “Recursive Format numbers with commas”
Post a Comment