Files
Le-Compte-est-Bon/oper.c
2021-01-20 16:22:59 +01:00

59 lines
1.3 KiB
C

#include <string.h>
#include <malloc.h>
#include "lceb.h"
unsigned ncombinations(nops, len)
int nops, len;
{
int result = 1;
while(len > 0){
if(len & 1)
result *= nops;
nops *= nops;
len >>=1;
}
return result;
}
char *combination(ops, len, n)
char *ops; /* string to combine */
int len; /* len of result */
int n; /* iteration # */
{
static char *res=NULL;
static int len_ops;
int i;
if (!res) { // 1st call
len_ops=strlen(ops);
res=malloc(len * sizeof(char) + 1);
}
for(i=0; i<len; ++i){
res[len -i -1] = ops[n % len_ops];
n /= len_ops;
}
return res;
}
#ifdef STANDALONE
int main(ac, av)
int ac;
char **av;
{
char *ops="+-*/", *p;
int len_ops=strlen(ops);
int i, j, nops, ncombs;
nops=atoi(*(av+1));
ncombs=ncombinations(len_ops, nops);
printf("# operators combinations : %d\nlist = ", ncombs);
for (i=0; i<ncombs; ++i) {
p=combination(ops, nops, i);
printf("[%s]\n", p);
//for (j=0; j<4; ++j)
// distribute(j, p);
//printf("\n");
}
}
#endif