Place commas in right position in a postive integer 
Author Message
 Place commas in right position in a postive integer

Hey
Does some one out there know how to solve this problem using a recursive
function or procedure ???

I'd appreciate any help you can give me.



Sun, 09 Oct 2005 21:41:58 GMT  
 Place commas in right position in a postive integer


Quote:
> Hey
> Does some one out there know how to solve this problem using a recursive
> function or procedure ???

> I'd appreciate any help you can give me.

Seems like some homework or school exercise?

Sven



Sun, 09 Oct 2005 23:02:14 GMT  
 Place commas in right position in a postive integer

Quote:

> Does some one out there know how to solve this problem using a recursive
> function or procedure ???

> I'd appreciate any help you can give me.

This is in C, but you can easily convert it if needed.  Note that
putval is a private subroutine for putnum and putunum.

/*  --- file putnums.c ---

   Using binary as an example, code to ourput numbers
   in a field, while injecting commas at intervals.

   By C.B. Falconer.  Put in public domain.
*/
#include <stdio.h>
#include "putnums.h"

#ifdef TESTING   /* Add in a demonstration driver */
#  include <limits.h>

#  define BASE 10   /* Try 2 through 16 here only */
#  define GROUP 3   /* with 0, 4 or 3 here */
#endif

/* ------------------- */

/* The original call must pass in depth == 0  */
/* field is zero based, so 36 allows 37 chars */
static int putval(FILE *fp, unsigned long v, int base,
                  int field, int clump, int neg,
                  int depth)
{
   int         retval;
   static char hexchars[16] = "0123456789abcdef";

   if (depth && clump && ((depth % clump) == 0)) field--;
   if ((v / base) > 0) {
      retval = 1 + putval(fp, v/base, base, field,
                          clump, neg, depth+1);
   }
   else {
      if (neg) field--;
      while (field > depth) {
         putc(' ', fp);
         field--;
      }
      if (neg) {
         putc('-', fp);
         retval = 2;
      }
      else retval = 1;
   }
   /* Revise this for base value larger than 16 */
   putc((v % base)[hexchars], fp);

   if (depth && clump && ((depth % clump) == 0)) {
      putc(',', fp);
      retval++;
   }
   return retval;

Quote:
} /* putval */

/* ------------------- */

/* Negative field for left justification, 0 based    */
/* clump is interval for group separator, 0 for none */
int putnum(FILE *fp, long v, int base,
           int field, int clump)
{
   int retval;

   if (v < 0) retval = putval(fp, -v, base, field, clump, 1, 0);
   else       retval = putval(fp, v,  base, field, clump, 0, 0);
   while ((field + retval) <= 0) {
      field++;
      putc(' ', fp);
   }
   return retval;

Quote:
} /* putnum */

/* ------------------- */

/* Negative field for left justification, 0 based    */
/* clump is interval for group separator, 0 for none */
int putunum(FILE *fp, unsigned long v, int base,
            int field, int clump)
{
   int retval;

   retval = putval(fp, v,  base, field, clump, 0, 0);
   while ((field + retval) <= 0) {
      field++;
      putc(' ', fp);
   }
   return retval;

Quote:
} /* putunum */

/* ------------------- */

#ifdef TESTING
int main(void)
{
   int i, lgh;

   for (i = 0; i < 50; i++) putchar('0' + i % 10);
   putchar('\n');
   for (i = 0; i < 12; i++) {
      lgh = putnum(stdout, i, BASE, 36, GROUP);
      putchar(' ');
      lgh = putnum(stdout, lgh, BASE, 8, GROUP);
      puts(".");
   }
   i = INT_MAX - 4;
   do {
      i++;
      lgh = putnum(stdout, i, BASE, 36, GROUP);
      putchar(' ');
      lgh = putnum(stdout, lgh, BASE, 8, GROUP);
      puts(".");
   } while (i < INT_MAX);

   i = INT_MIN + 4;
   do {
      i--;
      lgh = putnum(stdout, i, BASE, 36, GROUP);
      putchar(' ');
      lgh = putnum(stdout, lgh, BASE, 8, GROUP);
      puts(".");
      lgh = putunum(stdout, (unsigned long)i, BASE, 36, 0);
      putchar(' ');
      lgh = putnum(stdout, lgh, BASE, 8, GROUP);
      puts(".");
   } while (i > INT_MIN);

   lgh = putunum(stdout, 1, BASE, -36, GROUP);
   putchar(' ');
   lgh = putunum(stdout, lgh, BASE, 8, GROUP);
   puts(".");

   for (i = 0; i < 4; i++) {
      lgh = putudnum(stdout, (unsigned long)-i, 36, GROUP);
      putchar(' ');
      lgh = putdnum(stdout, lgh, 8, GROUP);
      puts(".");
      lgh = putunum(stdout, (unsigned long)-i, 16, 36, 4);
      putchar(' ');
      lgh = putunum(stdout, lgh, BASE, -8, GROUP);
      puts(".");
      lgh = putunum(stdout, (unsigned long)-i, 2, -36, 4);
      putchar(' ');
      lgh = putunum(stdout, lgh, BASE, 8, GROUP);
      puts(".");
   }
   return 0;

Quote:
} /* main */

#endif
/* --- end putnums.c --- */

/* --- file putnums.h --- */
#ifndef putnums_h_
#  define putnums_h_

#  ifdef __cplusplus
      extern "C" {
#  endif

/* Using binary as an example, code to ourput numbers
   in a field, while injecting commas at intervals.

   By C.B. Falconer.  Put in public domain.
*/

/* ------------------- */

/* Negative field for left justification, 0 based    */
/* clump is interval for group separator, 0 for none */
int putnum(FILE *fp, long v, int base,
           int field, int clump);

/* ------------------- */

/* Negative field for left justification, 0 based    */
/* clump is interval for group separator, 0 for none */
int putunum(FILE *fp, unsigned long v, int base,
            int field, int clump);

/* Macros to ease use for decimal output */
#define putdnum(fp, v, field, clump) \
           putnum(fp, v, 10, field, clump)
#define putudnum(fp, v, field, clump) \
           putunum(fp, v, 10, field, clump)

#  ifdef __cplusplus
      }
#  endif
#endif
/* --- end putnums.h --- */

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!



Mon, 10 Oct 2005 00:38:21 GMT  
 Place commas in right position in a postive integer


Wed, 23 Apr 2003 09:41:58 :-

Quote:
>Hey
>Does some one out there know how to solve this problem using a recursive
>function or procedure ???

>I'd appreciate any help you can give me.

<URL:http://www.merlyn.demon.co.uk/js-maths.htm#RC>

        function RComma(S) { S = String(S)
          var RgX = /^(.* )?([-+\u00A3\u20AC]?\d+)(\d{3}\b)/
          return S == (S=S.replace(RgX, "$1$2,$3")) ? S : RComma(S) }

does it in javascript, for a string with multiple numbers optionally
signed or currency.  It seems briefer than in C.

You should be able to see that it seeks four digits followed by a word
boundary, and inserts a comma, repeating until the attempt to do so made
no change.  That's enough clues for a set problem.

--

  <URL:http://www.jibbering.com/faq/> FAQ for comp.lang.javascript by Jim Ley.
  <URL:http://www.merlyn.demon.co.uk/js-index.htm>   JS maths, dates, sources.
  <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.



Mon, 10 Oct 2005 05:59:24 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Is this the right place?

2. storing a list of integers in one integer.

3. Changing Tab-delimited file to Comma-delimited

4. Comma delimited text files

5. Want to export data to comma-delimited file.

6. "Missing comma" error with SQL statement

7. TBatchMove ASCII - comma

8. EDBEngineError - Missing Comma

9. Comma delimited ascii - IDE vs runtime difference

10. Importing comma delimited files

11. Problems importing ASCII comma delimited data

12. Comma delimited files ...

 

 
Powered by phpBB® Forum Software