#include <iostream>
using namespace std;
typedef struct polynomial {
int coef; //系数coefficient
int exp; //指数
struct polynomial* next;
}*Link, Node;
void inputPoly(Link head);//用于从控制台读入链表的函数
void print(Link head);//打印链表用的函数
void insert(Link head, int coefficient, int exp);//向链表插入一个元素的函数
void combin2List(Link heada, Link headb, Link headab);//合并两个链表
//void free_(Link head);//合并后释放heada和headb!
int main() {
Link head1,head2,head3;
head1 = (Link)malloc(sizeof(Node));
head2 = (Link)malloc(sizeof(Node));
head3 = (Link)malloc(sizeof(Node));
head1->next = NULL;
head2->next = NULL;
head3->next = NULL;
inputPoly(head1);
print(head1);
inputPoly(head2);
print(head2);
cout << "合并该多项式后为:";
combin2List(head1,head2,head3);
print(head3);
return 0;
}
void insert(Link head, int coefficient, int exp) {
Link node;
Link q, p;
node = (Link)malloc(sizeof(Node));
node->coef = coefficient;
node->exp = exp;
node->next = NULL;
q = head;
p = head->next;
if (head->next == NULL) {
head->next = node;
}
else {
while (1) {
if (node->exp > p->exp) {//当插入结点的指数大于p结点的指数时插在p前面
node->next = p;
q->next = node;
break;//忘记输入break;导致陷入死循环!!!!!!!
}
else if (node->exp == p->exp) {//当插入结点的指数等于p结点的指数时合并
p->coef = p->coef + node->coef;
free(node);
break;
}
else if (node->exp < p->exp) {//当插入结点的指数小于p结点的指数时,向后遍历链表!
q = p;
p = p->next;
}
else if (p == NULL)
{
q->next = node;
break;
}
}
//退出循环则说明结点node的指数小于所有现有的指数,因此插在最后!
//q->next = node;//这里出错!若(node->exp == p->exp)则进行该项后将node加在了多项式最后一项!该怎么解决?goto语句?
}
}
void inputPoly(Link head) {
int coef, exp;
cout << "请输入系数coef和指数exp(当输入 0 0 时结束!):";
cin >> coef;
cin >> exp;
while (coef != 0 || exp != 0) {
insert(head, coef, exp);
cout << "请输入系数coef和指数exp(当输入 0 0 时结束!):";
cin >> coef;
cin >> exp;
}
}
void print(Link head) {
Link p;
p = head->next;
cout << "该多项式为:\n";
if (p == NULL) {
cout << "多项式为空\n";
return;
}
int first = 1;
do {
//输出符号!
if (first) {
if (p->coef == 1) {
first = 0;
}
else if (p->coef == -1) {
cout << "-";
first = 0;
}
else if (p->coef == 0)
first = 1;
else {
//cout << p->coef;
first = 0;
}
}
else {
if (p->coef == 1)
cout << "+";
else if (p->coef == -1)
cout << "-";
else if (p->coef > 0)
cout << "+";
else if (p->coef == 0);
else;
}
//输出系数!
if (p->coef != 1 && p->coef > 0)
cout << p->coef;
else if (p->coef != -1 && p->coef < 0)
cout << p->coef;
else;//系数等于0或系数等于+-1时都不需要输出1只需要输出符号即可!
//输出x
if (p->coef != 0&&p->exp!=0)
cout << "x";
else;
//输出指数!
if (p->exp < 0)
cout << "^(" << p->exp << ")";
else if (p->exp > 0 && p->exp != 1)
cout << "^" << p->exp;
else if (p->exp == 0)
; //无输出;
p = p->next;//指向下一个结点!保证循环可以结束!
} while (p != NULL);
cout << endl;
}
void combin2List(Link heada, Link headb, Link headab) {
Link pa, pb, pab;
pa = heada->next;
pb = headb->next;
pab = headab->next;
while (pa != NULL && pb != NULL) {
if (pb->exp > pa->exp) {
pab->coef = pb->coef;
pab->exp = pb->exp;
pab = pab->next;
pb = pb->next;
}
else if (pb->exp == pa->exp) {
pab->coef = pa->coef + pb->coef;
pab->exp = pb->exp;
pab = pab->next;
pa = pa->next;
pb = pb->next;
}
else if (pb->exp < pa->exp) {
pab->coef = pa->coef;
pab->exp = pa->exp;
pa = pa->next;
pab = pab->next;
}
}
while (pa != NULL) {
pab->coef = pa->coef;
pab->exp = pa->exp;
pa = pa->next;
pab = pab->next;
}
while (pb != NULL) {
pab->coef = pb->coef;
pab->exp = pb->exp;
pb = pb->next;
pab = pab->next;
}
}
/*
void free_(Link head) {
Link q,p;
q = head;
p = head->next;
while (p != NULL) {
p = p->next;
free(q->next);
}
}
*/
多项式显示和加减运算(链表)(未完成,存在各种问题!!!)气s了!
最新推荐文章于 2024-08-04 22:43:03 发布