我正在尝试使用c中的抽象数据类型编写一个程序,用于基本多项式运算。
到目前为止我已经
了.h文件
#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实施文件
#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;
}
最后我不得不创建我的测试文件来测试我的逻辑
到目前为止,这是我对主要功能所具有的:
#include <stdio.h>
#include <stdlib.h>
#include "Polynomials ADT.h"
int main()
{
int i,j;
Polynomial d = sumPoly((setTerm(1,2), setTerm(2,3)));
for (i = 0, x = d; i< n; ++i)
{
x = multiPoly(d, x); printPoly(x);
}
printf("%f", evalPoly(x,r));
}
我的问题是如何打印结果以查看我的实施文件是否有效?我不知道如何显示我的函数的结果,如sumPoly,multiPoly和evalPoly ....请帮忙!