鱼C论坛

 找回密码
 立即注册
查看: 4493|回复: 7

[技术交流] 一元多项式的计算问题,线性链表的典型应用.

[复制链接]
发表于 2011-8-20 03:08:45 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Be_envious 于 2011-8-20 03:10 编辑
#include<stdio.h>
#include<malloc.h>
//需注意的是 多项式加法默认的是 幂指数从小大依次排列 输入幂指数时应注意
//只是练习题,所以没有更深入的对多项式进行幂指数整理升序排序
//目的只是学会线性链表在多项式上的应用 
struct polynomial
//polynomial 中文意思 多项式 起这么长的名字其实是为了顺便学学英语 
{
int coefficient; //coefficient 系数 
int index; //index 指数 
struct polynomial * next; //多项式后继指针 
};

struct polynomial * creat(void); //创建函数 
//多项式相加函数 
struct polynomial * plus(struct polynomial * an,struct polynomial * bn); 
void print(struct polynomial * P);
int main(void)
{
struct polynomial * an = NULL;
struct polynomial * bn = NULL;
struct polynomial * cn = NULL;

an = creat();
bn = creat();
cn = plus(an,bn);
print(cn);
system("pause");
}

//创建函数 
struct polynomial * creat(void)
{
//分配一个没有数据的空间给头结点,这个很重要 
struct polynomial * head = (struct polynomial *)malloc(sizeof(struct polynomial));
if(NULL == head)
{
printf("No enough space!");
exit(-1);
}

struct polynomial * tail = head;
tail->next = NULL;

int NUMBER_OF_TERMS;
int coefficient;
int index;
int loop;

printf("Input number of terms: ");
scanf("%d",&NUMBER_OF_TERMS);

for(loop = 0;loop < NUMBER_OF_TERMS;++loop)
{
printf("\nInput coefficient of No.%d term: ",loop+1);
scanf("%d",&coefficient);
printf("Input index of No.%d term: x^",loop+1);
scanf("%d",&index);

struct polynomial * GENERAL_TERM = (struct polynomial *)malloc(sizeof(struct polynomial));
if(NULL == GENERAL_TERM)
{
printf("no enough space!");
exit(-1);
}
GENERAL_TERM->coefficient = coefficient;
GENERAL_TERM->index = index;

tail->next = GENERAL_TERM;
GENERAL_TERM->next = NULL;
tail = GENERAL_TERM;
}
return (head);
}

//多项式相加函数 
struct polynomial * plus(struct polynomial * an,struct polynomial * bn)
{
struct polynomial * pointer_a = an->next;
struct polynomial * pointer_b = bn->next;

//这里很重要 
struct polynomial * cn = (struct polynomial *)malloc(sizeof(struct polynomial));
if(NULL == cn)
{
printf("No enough space!");
exit(-1);
} 
struct polynomial * pointer_c = cn;

while(pointer_a && pointer_b)
{
struct polynomial * pointer_a_plus_b = (struct polynomial *)malloc(sizeof(struct polynomial));
if(NULL == (pointer_a_plus_b))
{
printf("No enough space!");
exit(-1);
}
if(pointer_a->index < pointer_b->index)
{
pointer_a_plus_b->coefficient = pointer_a->coefficient;
pointer_a_plus_b->index = pointer_a->index;
pointer_a = pointer_a->next;

pointer_c->next = pointer_a_plus_b;
pointer_a_plus_b->next = NULL;
pointer_c = pointer_a_plus_b;
}
else if(pointer_a->index == pointer_b->index)
{
pointer_a_plus_b->coefficient = pointer_a->coefficient + pointer_b->coefficient;
pointer_a_plus_b->index = pointer_a->index;
pointer_a = pointer_a->next;
pointer_b = pointer_b->next;

pointer_c->next = pointer_a_plus_b;
pointer_a_plus_b->next = NULL;
pointer_c = pointer_a_plus_b;
}
else
{
pointer_a_plus_b->coefficient = pointer_b->coefficient;
pointer_a_plus_b->index = pointer_b->index;
pointer_b = pointer_b->next;

pointer_c->next = pointer_a_plus_b;
pointer_a_plus_b->next = NULL;
pointer_c = pointer_a_plus_b;
}
}

if(pointer_a == NULL)
{
while(pointer_b)
{
struct polynomial * pointer_a_plus_b = (struct polynomial *)malloc(sizeof(struct polynomial));
if(NULL == pointer_a_plus_b)
{
printf("No enough space");
exit(-1);
}

pointer_a_plus_b->coefficient = pointer_b->coefficient;
pointer_a_plus_b->index = pointer_b->index;
pointer_b = pointer_b->next;

pointer_c->next = pointer_a_plus_b;
pointer_a_plus_b->next = NULL;
pointer_c = pointer_a_plus_b;
}
}
else
{
while(pointer_a)
{
struct polynomial * pointer_a_plus_b = (struct polynomial *)malloc(sizeof(struct polynomial));
if(NULL == pointer_a_plus_b)
{
printf("No enough space!");
exit(-1);
}

pointer_a_plus_b->coefficient = pointer_a->coefficient;
pointer_a_plus_b->index = pointer_a->index;
pointer_a = pointer_a->next;

pointer_c->next = pointer_a_plus_b;
pointer_a_plus_b = NULL;
pointer_c = pointer_a_plus_b;
}
}
return (cn);
}

//打印输出函数 
void print(struct polynomial * P)
{
int total_terms = 0;
struct polynomial * pointer = P->next;
while(pointer)
{
if(total_terms != 0)
{
printf("+");
}
printf("%dx^%d",pointer->coefficient,pointer->index);
pointer = pointer->next;
++total_terms;
}
printf("\nTotal terms : %d\n",total_terms);
}
希望大家多多交流,有不足之处望指出,日后写的代码都会完全共享,希望大家给予支持.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2011-8-20 18:45:59 | 显示全部楼层
多项式用数组更简洁哦
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2011-8-20 20:14:21 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2011-8-20 22:51:25 | 显示全部楼层
Be_envious 发表于 2011-8-20 20:14
用数组要写动态二维数组
只是现在在学习线性链表
不过谢谢你的提议

一维就够了,下标当指数,保存系数
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2011-8-20 22:54:21 | 显示全部楼层
wangyexin 发表于 2011-8-20 22:51
一维就够了,下标当指数,保存系数

好办法  学习了  
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2011-9-10 13:59:10 | 显示全部楼层
不错啊!!!!!!!!!!!!!!!!!!!!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2011-10-28 16:35:49 | 显示全部楼层
如果用一维数组,下标存指数,指数跳跃很大时,会很浪费空间的啊,还是二维好点吧~
LZ的代码写得很清晰,很容易读懂~。~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2013-4-1 11:32:49 | 显示全部楼层
不错啊!!!!!!!!!!!!!!!!!!!!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-12-4 16:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表