summaryrefslogtreecommitdiff
path: root/src/pascal.cc
blob: 0491e77bc55d969155b469636455bbd893853d34 (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
#include <pascal.h>

Pascal :: Pascal()
{
  num_row = 0;
  num_col = 0;
  tab     = 0;
}

Pascal :: Pascal(const unsigned long r, const unsigned long c)
{
  unsigned long i, j;
  
  num_row = r;
  num_col = c;
  
  if (num_row * num_col > 0)
  {
    tab = new long [num_row * num_col];
    
    for (i = 0; i < num_col; i++)
      tab[i] = 1;
    
    for (i = 1; i < num_row; i++)
      tab[i * num_col] = 1;
    
    for (i = 1; i < num_row; i++)
      for (j = 1; j < num_col; j++)
        tab[i * num_col + j] =
          tab[i * num_col + j - 1] + tab[(i - 1) * num_col + j];
  }
  else
    tab = 0;
}

Pascal :: ~Pascal()
{
  if (num_row * num_col > 0)
    delete [] tab;
}

int Pascal :: resize(const unsigned long r, const unsigned long c)
{
  unsigned long i, j;
  
  if (num_row * num_col > 0)
    delete [] tab;
  
  if (num_row < r)
    num_row = r;
  
  if (num_col < c)
    num_col = c;
  
  if (num_row * num_col > 0)
  {
    tab = new long [num_row * num_col];
    
    for (i = 0; i < num_col; i++)
      tab[i] = 1;
    
    for (i = 1; i < num_row; i++)
      tab[i * num_col] = 1;
    
    for (i = 1; i < num_row; i++)
      for (j = 1; j < num_col; j++)
        tab[i * num_col + j] =
          tab[i * num_col + j - 1] + tab[(i - 1) * num_col + j];
  }
  else  //  if (!num_row && !num_col)
    tab = 0;
  
  return 0;
}

long Pascal :: get_Pascal(const unsigned long i, const unsigned long j)
{
  if (i + 1 > num_row || j + 1 > num_col)
    resize(i + 1, j + 1);
  
  assert(num_row * num_col > 0);
  
  return tab[i * num_col + j];
}