Skip to content

Instantly share code, notes, and snippets.

@steeve

steeve/gist:5240578

Created Mar 25, 2013
Embed
What would you like to do?
#include <stdlib.h>
#include <stdio.h>
/*
**
*/
#define NBRA (e->a)
#define NBRB (e->b)
#define SIZEA (e->sizea)
#define SIZEB (e->sizeb)
#define RESULT (e->result)
#define SIZERESULT (e->sizeresult)
#define CHAR_ZERO '0';
#define CBASE(i) (e->base[i])
#define IBASE(c) (e->ibase[c])
/*
**
*/
typedef struct s_env
{
char *a;
char *b;
int sizea;
int sizeb;
char *result;
int sizeresult;
char *base;
char ibase[256];
int sbase;
} t_env;
/*
**
*/
int str_len(char *s)
{
int ret;
for(ret=0;*s;s++)
ret++;
return ret;
}
void mem_set(char *buff,int c,int size)
{
for(;size > 0;size--)
buff[size]=c;
}
t_env *init_env(char *a,char *b,char *base)
{
t_env *e;
int i;
e=malloc(sizeof(*e));
mem_set((char *)e,0,sizeof(*e));
NBRA=a; NBRB=b;
SIZEA=str_len(NBRA); SIZEB=str_len(NBRB);
SIZERESULT=SIZEA+SIZEB+1;
RESULT=malloc(SIZERESULT);
mem_set(RESULT,0,SIZERESULT);
e->base=base;
e->sbase=str_len(base);
for (i=0;i<e->sbase;i++)
e->ibase[base[i]]=i;
return e;
}
void aff_res(t_env *e)
{
int i;
for(i=0;i < SIZERESULT;i++)
if(RESULT[i])
printf("%c",RESULT[i]);
printf("\n");
}
void multiplication(t_env *e)
{
int sa;
int sb;
int nbr;
int ret;
ret=0;
for(sb=SIZEB;sb > 0;sb--)
{
for(sa=SIZEA;sa > 0;sa--)
{
nbr=IBASE(NBRA[sa-1])*IBASE(NBRB[sb-1])+IBASE(RESULT[sa+sb])+IBASE(ret);
ret=nbr/e->sbase;
RESULT[sa+sb]=CBASE(nbr%e->sbase);
}
if (ret)
{
RESULT[sa+sb]=CBASE(ret);
ret=0;
}
}
}
/*
**
*/
int main(int ac, char **av)
{
t_env *e;
e=init_env("ol","oo","loutre");
multiplication(e);
aff_res(e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment