summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SNIP/bigfac.c
blob: 5e510777936e2a86d49672c863b6fea31569d6c3 (plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
**  bigfac.c -- put into the public domain by Carl Declerck
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFFLEN 8192
#define BUFFER ((char *) malloc(BUFFLEN))

void main (void);
void multiply (char *, char *, char *);
void zero_buffer (char *);
void minus_one (char *);
int  isnull (char *);
void factorial (char *);

void main (void)
{
      char *g = BUFFER;

      printf ("Enter a number: ");
      scanf ("%s", g);
      printf ("Factorial of %s is: ", g);
      factorial (g);
      printf ("%s\n", g);
      free (g);
}

void multiply (char *g1, char *g2, char *g3)
{
      int gp1, gp2, cumpos, respos, mod, div;
      int cmod, cdiv, resoff, wdig1, wdig2, base;

      zero_buffer (g3);
      for (gp2 = strlen(g2) - 1; gp2 >= 0; gp2--)
      {
            wdig2 = *(g2 + gp2) - 48;
            resoff = strlen(g2) - gp2 - 1;
            respos = BUFFLEN - resoff - 2;
            for (gp1 = strlen(g1) - 1; gp1 >= 0; gp1--)
            {
                  wdig1 = *(g1 + gp1) - 48;
                  mod = (wdig1 * wdig2) % 10;
                  div = (wdig1 * wdig2) / 10;
                  base = *(g3 + respos) - 48;
                  cmod = (base + mod) % 10;
                  cdiv = (base + mod) / 10 + div;
                  *(g3 + respos) = (char)(cmod + 48);
                  cumpos = --respos;
                  while (cdiv > 0)
                  {
                        base = *(g3 + cumpos) - 48;
                        *(g3 + cumpos--) = (char)((base + cdiv) % 10 + 48);
                        cdiv = (base + cdiv) / 10;
                  }
            }
      }
      for (respos = 0; *(g3 + respos) == '0'; respos++)
            ;
      strcpy (g3, (char *) (g3 + respos));
      if (*g3 == 0)
            strcpy (g3, "0");
}

void zero_buffer (char *buff)
{
      int cnt;

      for (cnt= 0; cnt < BUFFLEN; cnt++)
            *(buff + cnt) = '0';
      *(buff + BUFFLEN - 1) = 0;
}

void minus_one (char *g)
{
      int p;
      char digit;

      p = strlen(g) - 1;
      digit = *(g + p);
      while (digit == '0')
      {
            *(g + p--) = '9';
            digit = *(g + p);
      }
      *(g + p) -= 1;
}

int isnull (char *g)
{
      int p, ok = 1;

      for (p = 0; p < (int)(strlen(g)); p++)
            if (*(g + p) != '0')
                  ok = 0;
      return (ok);
}

void factorial (char *g)
{
      char *h1 = BUFFER, *h2 = BUFFER;

      strcpy (h1, "1");
      while (!isnull(g))
      {
            multiply (h1, g, h2);
            strcpy (h1, h2);
            minus_one (g);
      }
      strcpy (g, h1);
      free (h1);
      free (h2);
}

/*
**  The principal function is multiply(), it 'multiplies' two
**  character-strings of arbritrary length and puts the result
**  into a third.  8192 bytes is enough for 1000!, beyond that
**  the buffer-size may need to be incremented.
*/