Inserting commas

I am writing a program to recursively insert commas into an int larger than 1000. I have began writing the program but am having trouble. Here is what I have so far.

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
#include <iostream>
#include <iomanip>

using namespace std;

void commas();

int main(void)
{
int number;
int i, n;
int num[n];
int number1;

cout<<"Enter a positive integer without commas:"<<endl;
cin>>number;

int commas();

return 0;
}

void commas()
/* -----------------------------------------------------
Precondition: None.
Postcondition: comma inserted in number
-------------------------------------------------------*/
{
int counter, n;
int num[n];
int number1;
int number;

n=1;
while(number>0)
{
int numb();
num[n]=number1;
number=number-(number1*(10)^counter);
n++;
cout<<"In function commas"<<endl;
cout<<"Number now is: "<<number<<endl;
}

n=1;
while(n<4)
  {
  cout<<""<<num[n]<<"";
  n++;
  }

while(n>=4)
  {
  cout<<"','";
  cout<<""<<num[n-2]<<"";
  cout<<""<<num[n-1]<<"";
  cout<<""<<num[n]<<"";
  n=n-3;
  }

return;
}

void numb()
{
int number, number1;
int counter;

number1=number;
cout<<"Entering function numb"<<endl;
cout<<"Number is: "<<number<<endl;
counter=0;
while(number1>10)
  {
  number1=number1/10;
  counter++;
  }
return;
}
I believe things would be much easier if you handled your number as a string.
Also, if you are careful, your recursive solution can be as fast as an iterative one.

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
#include <iostream>
#include <string>

void insert_comma(const std::string & integer, std::string & result)
{
    struct local
    {
        static void insert_comma_helper(const std::string & integer,
                                        std::string & result)
        {
            int size = integer.size();

            if (size < 4) { result = integer + ',' + result; return; }

            return insert_comma_helper(
                    integer.substr(0, size - 3),
                    result = integer.substr(size - 3, 3) + ',' + result);
        }
    };

    int size = integer.size();

    if (size < 4) { result = integer; return; }

    return local::insert_comma_helper(
            integer.substr(0, size - 3),
            result = integer.substr(size - 3, 3));
}

int main()
{
    std::string integer;
    std::string result;

    while (true)
    {
        getline(std::cin,integer);
        if (integer == "") break;

        insert_comma(integer, result);
        std::cout << result << std::endl;
    }

    return 0;
}

Useful links:

tail call optimization -> http://stackoverflow.com/questions/310974/what-is-tail-call-optimization
nested functions -> http://www.respower.com/~earlye/programming/19990916.001.htm

EDIT: Damn... g++ doesn't seem to be able to optimize this... Or am I doing something wrong?
EDIT2: Mmm... Looks like the tail call optimization can only be done with primitive types...
EDIT3: You can do it in Scala.
Last edited on
Thank you very much!!
Topic archived. No new replies allowed.