【问题描述】
设计一个一元稀疏多项式简单计算器。
【基本要求】
一元多项式简单计算器的基本功能是:
(1)输入并建立多项式;
(2)输出多项式,输出形式为整数序列n,c1,e1,c2,e2,…,cn,en,其中n是多项式的项数,ci和ei分别是第i项的系数和指数,序列指指数降序排列;
(3)多项式a和b相加,建立多项式a+b;
(4)多项式a和b相减,建立多项式a-b。
2.概要设计
包括选择什么数据结构?
线性表。循环单链表。
数据结构采用哪种存储方式?
链式存储结构。
选择的原因?
方便多项式的排序,插入。
设计哪些运算?
设计存储结构。
设置基本运算算法:
创建一个空的单链表;循环单链表初始化;通过尾插法构建链表,在链表末尾的位置插入一个元素;按指数大小从小到大排列循环单链表
这些运算之间的调用关系
线性结构关系
3.详细设计
数据结构的类型定义
线性表:
typedef struct list
{
int coe;//多项式系数
int index;//多项式指数
struct list* next;
}mul;
每个操作的算法描述
有限性:算法必须在有限的步骤之后结束
确定性:算法的每一步都是确切的含义,无二义性。即在任何条件下,算法只有唯一的一条执行路径,即对于相同的输入只能得出相同的输出
输入:
mul* GetInto(int x, int y) //创造一个结点
输出:
void DisplayList(mul* L)//显示多项式
可行性:算法中的每一步都可以通过已经
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
typedef struct list
{
int coe;//多项式系数
int index;//多项式指数
struct list* next;
}mul;
mul* GetInto(int x, int y) //创造一个结点
{
mul* form = (mul*)malloc(sizeof(mul));
assert(form);
form->coe = x;
form->index = y;
form->next = NULL;
return form;
}
mul* InitList()//初始化多项式
{
mul* ls = GetInto(0, 0);
ls->next = ls;
return ls;
}
void CreateList(mul* L, int x, int y)//通过尾插法建立链表
{
assert(L);
mul* ls = GetInto(x, y);
mul* ln = L->next;
for (; ln->next != L; ln = ln->next) {}
ln->next = ls;
ls->next = L;
}
void DisplayList(mul* L)//显示多项式
{
int num = 1;
mul* ln = L->next;
assert(L);
while (ln != L)
{
if (ln->coe == 0)//X前的系数等于0的情况
{
ln = ln->next;
continue;
}
if (ln->index == 0)
{
printf("%d", ln->coe);
}
if (ln->coe > 0 && ln->index > 0)//X前的系数大于0的情况
{
if (num != 1)
{
printf(" + ");
}
if (ln->index == 1)//
{
if (ln->coe == 1)
{
printf("X");
}
else
{
printf("%dX", ln->coe);
}
}
else
{
if (ln->coe == 1)
printf("X^%d", ln->index);
else
printf("%dX^%d", ln->coe, ln->index);
}
}
if (ln->coe < 0 && ln->index>0)//X前的系数小于0的情况
{
printf(" - ");
if (ln->index == 1)
{
if (ln->coe == 1.0)
printf("X");
else
printf("%dX", ln->coe * (-1));
}
else
{
if (ln->coe == 1.0)
printf("X^%d", ln->index);
else
printf("%dX^%d", ln->coe * (-1), ln->index);
}
}
num = 0;
ln = ln->next;
}
}
int Take(int index, int x)
{
if (index == x)
{
return 0;
}
else if (index > x)
{
return 1;
}
else
return -1;
}
void Insert(mul* c, int x, int y)
{
mul* a = c;
assert(c);
while (a->next != c && Take(a->next->index, x) < 0)
{
a = a->next;
}
if (a->next != c && Take(a->next->index, x) == 0)
{
a->next->coe += (y);
}
else
{
mul* b = (mul*)malloc(sizeof(mul));
if (b == NULL)
{
printf("malloc fail\n");
exit(-1);
}
b->coe = y;
b->index = x;
b->next = a->next;
a->next = b;
}
}
void Add(mul* a, mul* b, mul* add)
{
mul* m;
mul* n;
for (n = a->next; n != a; n = n->next)
{
Insert(add, n->index, n->coe);
}
for (m = b->next; m != b; m = m->next)
{
Insert(add, m->index, m->coe);
}
}
void Sub(mul* a, mul* b, mul* &sub)
{
mul* m = b->next;
mul* n = b->next;
assert(a && b && sub);
for (n = a->next; n != a; n = n->next)
{
Insert(sub, n->index, n->coe);
}
for (m = b->next; m != b; m = m->next)
{
m->coe = -(m->coe);
Insert(sub, m->index, m->coe);
m->coe = -(m->coe);
}
}
int main()
{
mul* A = InitList();
mul* B = InitList();
mul* add = InitList();
mul* sub = InitList();
int i, m;
int x, y;
printf("多项式A的项数有几项:");
scanf("%d", &m);
for (i = 1; i <= m; i++)
{
printf("A的第%d项系数和指数:", i);
scanf("%d", &x);
scanf("%d", &y);
Insert(A, y, x);
}
printf("A多项式为:");
DisplayList(A);
printf("\n多项式B的项数有几项:");
scanf("%d", &m);
for (i = 1; i <= m; i++)
{
printf("B的第%d项系数和指数:", i);
scanf("%d", &x);
scanf("%d", &y);
Insert(B, y, x);
}
printf("B多项式为:");
DisplayList(B);
Add(A, B, add);
printf("\nA+B为:");
DisplayList(add);
printf("\nA-B为:");
Sub(A, B, sub);
DisplayList(sub);
}
实现的基本运算的有限次运行来实现