I am trying to write a program using abstract data types in c for basic polynomial operations.
So far I have
.h file
#ifndef Polynomials_ADT_h
#define Polynomials_ADT_h
#include <stdlib.h>
typedef struct poly *Polynomial;
void printPoly(Polynomial);
Polynomial setTerm(int, int);
Polynomial sumPoly(Polynomial, Polynomial);
Polynomial multiPoly (Polynomial, Polynomial);
float evalPoly (Polynomial, float);
#endif
.c implementation file
#include "Polynomials ADT.h"
#include <stdlib.h>
struct poly {int n; int *a;};
Polynomial setTerm (int coeff, int exponent)
{
int i;
Polynomial d = malloc(sizeof*d);
d->a = malloc((exponent+1)*(sizeof(int)));
d->n = exponent+1;
d->a[exponent] = coeff;
for(i=0; i<exponent; i++)
d->a[i] = 0;
return d;
}
Polynomial sumPoly(Polynomial r, Polynomial s)
{
int i; Polynomial d;
if(r->n < s-> n)
{
d = r;
r = s;
s = d;
}
for (i=0; i< s->n; ++i)
{
s-> a[i]+= s-> a[i];
}
return s;
}
Polynomial multiPoly (Polynomial s, Polynomial r)
{
int i, j;
Polynomial d = setTerm(0, (r->n-1)+ (s->n-1) );
for (i=0; i < r->n; ++i)
{
for(j = 0; j< s->n; ++j)
{
d-> a[i+j]+= r->a[i]*s->a[j];
}
}
return d;
}
float evalPoly (Polynomial r, float x)
{
int i;
double d = 0.0;
for (i = r->n-1; i>=0; i--)
{
d = d*x + r-> a[i];
}
return d;
}
and finally I am stuck on creating my test file to test my logic
this is what I have for the main function so far:
#include <stdio.h>
#include <stdlib.h>
#include "Polynomials ADT.h"
int main()
{
Polynomial d = setTerm(1,2);
Polynomial r = setTerm(1,2);
a = sumPoly(d,r);
printf("%d",a);
}
My question is how do I print my results to see if my implementation file works? I am not sure of how to display the results of my functions such as sumPoly, multiPoly and evalPoly....please help!
Aucun commentaire:
Enregistrer un commentaire