今天开始想复习一下数据结构,就从线性表开始吧。
今天是用线性表实现多个多项式相加这个题目,自变量是x。
题目描述如下:
在数学上,一个一元多项式Pn(x)可按降幂写成:Pn(x) = pn x^n + p(n-1) x^n + ……. + p2x^2 + p1x^1 + p0,它由n+1个系数唯一确定。同样的Qn(x)也可以写成如P多项式一样。当两个多项式的某一项指数相同时,可将这项的底数相加,这称为多项式相加。
如:多项式P: 5x^4 + 3x^2 + 4 多项式Q:3x^3 + 2x^2 + 2 。 则P+Q:5x^4 + 3x^3 + 5x^2 + 6
代码如下:
1.头文件 link_list.h
//head file
//History Xinspace 27 Feb First release
#ifndef _xinspace
#define _xinspace 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <pthread.h>
#define error_show(function)
do
{
fprintf(stderr, “in file:%s, in function:%s, at line %d, %s : %sn”, FILE, FUNCTION, LINE, function, strerror(errno));
exit(-1);
}while(0)
#define LEN 3 //定义多项式表头数组的最大个数,即最多一次性处理几个多项式
/定义多项式每一项的节点结构体/
typedef struct linklist
{
float ds; //每一项的底数
int zs; //每一项的指数
struct linklist *next;
}Polyn;
Polyn createPolyn(); //创建一个多项式
Polyn createNode(float _ds, int _zs); //创建一个节点
void display(Polyn Head); //显示一个多项式
void init(Polyn **Head_array); //初始化多项式表头数组
void sort(Polyn Head); //对给定的多项式进行排序,并把具有相同指数的项相加
void add(Polyn **Head_array); //将两个多项式相加
#endif
2.代码文件link_list.c
/自变量是x/
#include “link_list.h”
int main(void)
{
Polyn *Head_array[LEN]; //定义多项式的表头数组
init(&Head_array[0]); //初始化该数组
add(&Head_array[0]); //将表头数组中的多项式都加起来
return 0;
}
/input the Polyns and be ready to process/
void init(Polyn **Head_array)
{
int num;
printf(“how many polyns do you want to process(less than %d)?t”, LEN);
scanf(“%d”, &num);
int count;
if(num > LEN)
{
fprintf(stderr, “number too much!n”);
exit(1);
}
for(count = 0; count < num; count++)
{
Head_array[count] = createPolyn();
// sort(Head_array[count]);
}
// for(count = 0; count < num; count++)
// display(Head_array[count]);
// printf(“init done!n”);
}
/sort the Polyn gived and add the ds which the nodes have the same zs/
void sort(Polyn *Head)
{
if(!Head)
{
fprintf(stderr, “Head is NULL!n”);
exit(1);
}
Polyn fh, sh, f, s;
fh = Head;
sh = f = fh->next;
s = sh->next;
while(s)
{
while(s)
{
if(f->zs < s->zs)
{
fh->next = s;
sh->next = s->next;
s->next = f;
f = fh->next;
s = sh->next;
}
else if(f->zs > s->zs)
{
sh = s;
s = sh->next;
}
else
{
f->ds += s->ds;
sh->next = s->next;
free(s);
s = sh->next;
}
}
fh = f;
f = fh->next;
sh = f;
s = sh ? sh->next : sh;
}
// printf(“Sort Done!n”);
}
/add Polyns/
void add(Polyn *Head_array)
{
int count;
Polyn temp;
for(count = 1; Head_array[count]->next && count < LEN; count++)
{
printf(“Head_array[%d]n”, count);
for(temp = Head_array; temp->next; temp = temp->next);
temp->next = Head_array[count]->next;
sort(Head_array);
display(Head_array[0]);
}
}
/create the Polyn using the function createNode()/
Polyn createPolyn()
{
Polyn head = createNode(-1, -1);
Polyn *temp = head;
float ds;
int zs;
printf(“input ‘-100′ means quit!n”);
while(1)
{
printf(“input ds(float):t”);
scanf(“%f”, &ds);
if(ds == -100)
{
printf(“you quit!n”);
break;
}
else if(ds == 0)
{
fprintf(stderr, “ds is not allowed to be zero!n”);
continue;
}
printf(“input zs(int):t”);
scanf(“%d”, &zs);
Polyn *node = createNode(ds, zs);
temp->next = node;
temp = node;
}
if(!(head->next))
{
fprintf(stderr, “Head is NULL!n”);
exit(1);
}
// printf(“Create Done!n”);
return head;
}
/create the Polyn Node/
Polyn createNode(float _ds, int _zs)
{
Polyn Node = (Polyn *)malloc(sizeof(Polyn));
if(NULL == Node)
error_show(“malloc”);
Node->ds = _ds;
Node->zs = _zs;
Node->next = NULL;
return Node;
}
/display the Polyn gived/
void display(Polyn Head)
{
Polyn temp = Head->next;
if(temp)
{
printf(“%3.1fx”, temp->ds);
if(temp->zs != 0)
printf(“^%d “, temp->zs);
else
printf(” “);
}
temp = temp->next;
while(temp)
{
if(temp->ds >= 0)
printf(“+ %3.1fx”, temp->ds);
else
printf(“- %3.1fx”, temp->ds);
if(temp->zs != 0)
printf(“^%d “, temp->zs);
else
printf(” “);
temp = temp->next;
}
printf(“n”);
}
该程序最重要的函数就是sort函数,它即能把给定的多项式进行降幂排序,还能将一个线性表中两个指数相同的底数相加放到第一个底数所在的节点中。
add()函数就是利用了sort函数的这个加法功能实现加法的。
本程序的两个文件都为原创,可下载,文件放在360云盘上:
link_list.h头文件:http://l7.yunpan.cn/lk/Q8yatZSyWA2G3
link_list.c代码文件:http://l7.yunpan.cn/lk/Q8ya6Pah6G7qm