#include<stdlib.h>
#include<stdio.h>
struct duoxiangshi//建立链表
{
int xishu; //系数
int zhishu; //指数
struct duoxiangshi *next;
};
int main()//主函数
{
int i, j;
struct duoxiangshi * create(FILE * fp);
struct duoxiangshi * xiangjia(struct duoxiangshi *head_1, struct duoxiangshi * head_2);
void Charu(struct duoxiangshi *p, int zhi, int xi);
void print1(struct duoxiangshi * head_2);
void print2(head);
struct duoxiangshi *head_1, *head_2; //建立两个指向duoxiangshi结构体的指针
FILE *fp; //文件指针建立
for (i = 0; i < 40; i++)
printf("*");
printf("\n");
printf("********** 两个一元多项式相加 **********\n");
for (i = 0; i < 40; i++)
printf("*");
printf("\n");
if ((fp = fopen("shuju.txt", "r")) == NULL) {
printf("没有找到可导入的文件\n");
return 0;
}
head_1 = create(fp); //第一个链表
head_2 = create(fp); //第二个链表
printf("将一元多项式从文件读出:\n");
printf("第一个多项式的二元组为(系数,指数):\n");
print1(head_1);
printf("\n");
printf("第二个多项式的二元组为(系数,指数):\n");
print1(head_2);
printf("\n");
fclose(fp); //关闭指针,这里必须把关闭写在外面 因为我们要读出两个链表
head_2 = xiangjia(head_1, head_2);
printf("根据文件输入数据所得多项式的二元组为:\n");
print1(head_2); //处理过的链表输出
printf("\n");
print2(head_2);
printf("\n");
return 0;
}
struct duoxiangshi *create(FILE *fp)//创建链表,数据导入
{
struct duoxiangshi *p1, *p2, *head; //链表指针建立
int n = 0;
p1 = p2 = (struct duoxiangshi*) malloc(sizeof (struct duoxiangshi)); //开辟新单元
fscanf(fp, "(%d,%d)", &p1->xishu, &p1->zhishu); //读入数据
head = NULL;
while (!(p1->xishu == 0 && p1->zhishu == 0))//系数指数同时为零时程序停止
{
n += 1;
if (n == 1) head = p1; //第一次把NULL给p1
else p2->next = p1;
p2 = p1;
p1 = (struct duoxiangshi*) malloc(sizeof (struct duoxiangshi)); //建立新节点
fscanf(fp, "(%d,%d)", &p1->xishu, &p1->zhishu);
}
p2->next = NULL; //结束链表
return head;
};
void Charu(struct duoxiangshi *p, int zhi, int xi)//插入节点
{
struct duoxiangshi *Old, *new; //当p1的指数在p2中不存在时插入到链表中来
Old = p; //记录上一个节点
while (Old->next != NULL) {//查找可插入的位置
if (zhi < p->zhishu) break; //如果p后移到了比自己大的指数前面才会停止
Old = p;
p = p->next;
}
new = (struct duoxiangshi*) malloc(sizeof (struct duoxiangshi)); //开辟新的存储单元
new->xishu = xi;
new->zhishu = zhi;
Old->next = new; //插入新链表
new->next = p;
}
struct duoxiangshi *xiangjia(struct duoxiangshi *head_1, struct duoxiangshi *head_2)//实现多项式系数的相加
{
struct duoxiangshi *p1, *p2; //建立两个指针
p1 = head_1; //p1指向第一个多项式的首节点
p2 = head_2; //p2指向第二个多项式的首节点
struct duoxiangshi *old;
old = p1;
int n; //用于判断进行相加还是插入节点
while (old->next != NULL) {
for (p2 = head_2; p2->next != NULL; p2 = p2->next) {
if (p1->zhishu == p2->zhishu) {
n = 1;
break;
} else n = 0;
}
switch (n) {
case 0:Charu(head_2, p1->zhishu, p1->xishu); //执行插入函数
break;
case 1:p2->xishu = p2->xishu + p1->xishu; //同指数系数相加
break;
}
old = p1;
p1 = p1->next;
}
return head_2; //把链表头返回给函数
}
void print1(struct duoxiangshi *head_2) {//链表输出
struct duoxiangshi *p;
p = head_2; //输出p2这个链表
if (p != NULL) {
while (p != NULL) {
printf("(%d,%d)", p->xishu, p->zhishu);
p = p->next;
}
}
}
void print2(head) //输出链表
struct duoxiangshi *head;
{
struct duoxiangshi *p;
int n = 0;
p = head;
printf("相加后的多项式 = ");
if (p)
while (p != NULL) {
if (p != head && n == 0 && p != NULL) printf("+"); //不是第一项输出+
if (p->xishu == 0)n = 1;
else
if (p->zhishu == 0) printf("%d", p->xishu); //指数为0时
else {
if (p->xishu != 1) {
printf("%d", p->xishu);
n = 0;
}
if (p->zhishu == 1) {
printf("x");
n = 0;
} else {
printf("x^%d", p->zhishu);
n = 0;
}
}
p = p->next;
}
}