题目:一元多项式运算
基本要求:
(1) 输入并建立多项式;
(2) 输出多项式;
(3) 多项式加法
(4) 多项式减法。
测试数据:
代码展示:
代码语言:javascript复制#include<iostream>
using namespace std;
class LinkedNode
{
public:
LinkedNode(double COEF, double INDEX, LinkedNode *ptr = NULL)
{
this->coef = COEF;
this->index = INDEX;
this->next = ptr;
}
double coef;
double index;
LinkedNode *next;
};
class LinkedList
{
public:
LinkedList()
{
this->head = NULL;
this->tail = NULL;
}
void create_polumerization(); //创建多项式 并且进行排序和重复次方的合并
void add_polumerization(LinkedList *p, char a); //多项式的运算
void print_result(LinkedList *p); //打印输出多项式
void sort_polumerization(); //多项式的 排序
void delete_samenode();//相同次方进行合并
private:
LinkedNode *head;
LinkedNode *tail;
};
void LinkedList::create_polumerization()
{
double index, coef;
cout << "开始输入多项式(默认系数 为0时结束)" << endl;
for (int i = 0;;i )
{
printf("n请输入第%d项的系数:", i 1);
cin >> coef;
if (coef == 0) break;
printf("n请输入第%d项的指数:", i 1);
cin >> index;
if (tail == NULL)
{
tail = new LinkedNode(coef, index);
head = tail;
}
else
{
LinkedNode *temp = new LinkedNode(coef, index);
tail->next = temp;
tail = temp;
}
}
//进行排序
sort_polumerization();
delete_samenode();
};
void LinkedList::sort_polumerization()
{
LinkedNode *pre=NULL, *p=NULL, *t = NULL;
if (head != NULL)
{
while (head->next != t)
{
pre = head;
p = head->next;
while (p != t)
{
if (pre->index > p->index)
{
swap(pre->coef, p->coef);
swap(pre->index, p->index);
pre = p;
p = p->next;
}
else
{
pre = p;
p = p->next;
}
}
t = pre;
}
}
};
void LinkedList::delete_samenode()
{
if (head == NULL) return;
for (LinkedNode* temp = head;temp != NULL && temp->next != NULL;)
{
if (temp->index == temp->next->index)
{
LinkedNode *p = temp;
temp = temp->next;
while (p->index == temp->index)
{
p->coef = temp->coef;
p->next = temp->next;
delete temp;
temp = p->next;
}
}
else temp = temp->next;
}
}
void LinkedList::print_result(LinkedList *p)
{
double count = 0;
for (LinkedNode *temp = p->head->next;temp != NULL;temp = temp->next)
{
if (temp == p->head->next)
{
if (temp->coef == 0) continue;
else
{
count = temp->coef;
if(temp->index!=0) cout << temp->coef << "x^" << temp->index;
else cout << temp->coef;
}
}
else
{
if (temp->coef == 0) continue;
else
{
if (temp->coef > 0)
{
count = temp->coef;
if(temp->index!=0) cout << " " << temp->coef << "x^" << temp->index;
else cout<<" " << temp->coef;
}
else
{
count = temp->coef;
if(temp->index!=0) cout << temp->coef << "x^" << temp->index;
else cout << temp->coef;
}
}
}
}
if (count == 0) cout << count;
}
void LinkedList::add_polumerization(LinkedList *p, char a)
{
LinkedNode *h1 = this->head;
LinkedNode *h2 = p->head;
LinkedList *result = new LinkedList();
LinkedNode *h = new LinkedNode(0, 0);
result->head = h;
result->tail = result->head;
cout << endl;
if (a == ' ')
{
while (h1 != NULL && h2 != NULL)
{
if (h1->index < h2->index)
{
LinkedNode *temp = new LinkedNode(h1->coef, h1->index);
result->tail->next = temp;
result->tail = temp;
h1 = h1->next;
}
else if (h2->index < h1->index)
{
LinkedNode *temp = new LinkedNode(h2->coef, h2->index);
result->tail->next = temp;
result->tail = temp;
h2 = h2->next;
}
else
{
LinkedNode *temp = new LinkedNode(h1->coef h2->coef, h1->index);
result->tail->next = temp;
result->tail = temp;
h1 = h1->next;
h2 = h2->next;
}
}
while (h1 != NULL)
{
LinkedNode *temp = new LinkedNode(h1->coef, h1->index);
result->tail->next = temp;
result->tail = temp;
h1 = h1->next;
}
while (h2 != NULL)
{
LinkedNode *temp = new LinkedNode(h2->coef, h2->index);
result->tail->next = temp;
result->tail = temp;
h2 = h2->next;
}
}
else
{
while (h1 != NULL && h2 != NULL)
{
if (h1->index < h2->index)
{
LinkedNode *temp = new LinkedNode(h1->coef, h1->index);
result->tail->next = temp;
result->tail = temp;
h1 = h1->next;
}
else if (h2->index < h1->index)
{
LinkedNode *temp = new LinkedNode(-h2->coef, h2->index);
result->tail->next = temp;
result->tail = temp;
h2 = h2->next;
}
else
{
LinkedNode *temp = new LinkedNode(h1->coef - h2->coef, h1->index);
result->tail->next = temp;
result->tail = temp;
h1 = h1->next;
h2 = h2->next;
}
}
while (h1 != NULL)
{
LinkedNode *temp = new LinkedNode(h1->coef, h1->index);
result->tail->next = temp;
result->tail = temp;
h1 = h1->next;
}
while (h2 != NULL)
{
LinkedNode *temp = new LinkedNode(-h2->coef, h2->index);
result->tail->next = temp;
result->tail = temp;
h2 = h2->next;
}
}
print_result(result);
}
int main()
{
char a;
LinkedList l1;
cout << "开始输入第一个多项式" << endl;
l1.create_polumerization();
cout << endl << "开始输入第二个多项式" << endl;
LinkedList l2;
l2.create_polumerization();
cout << "请输入两个多项式之间的运算关系(' '或'-'):";
cin >> a;
l1.add_polumerization(&l2, a);
}
运行结果