链表c语言将两函数式相加,数据结构:用链表实现两个多项式相加,用C++或者C语言实现多项式相加,求完整代码...

#include 

#include 

using namespace std;

#define EPS 1E-6

typedef struct item {

\x09double coefficient;

\x09int power;

\x09struct item *next;

} *POLYNOMIAL,*pItem;

POLYNOMIAL Create() { // 创建多项式

\x09pItem head,p;

\x09double coe;

\x09int pwr,iterms,i;

\x09head = p = new item;

\x09cin >> iterms; // 读入多项式的项数

\x09for(i = 0; i 

\x09\x09cin >> coe >> pwr;

\x09\x09p->next = new item;

\x09\x09p->next->coefficient = coe;

\x09\x09p->next->power = pwr;

\x09\x09p = p->next;

\x09}

\x09p->next = NULL;

\x09return head;

}

void Sort(POLYNOMIAL head) { // 按幂次降排序

\x09pItem pt,q,p = head;

\x09while(p->next) {

\x09\x09q = p->next;

\x09\x09while(q->next) {

\x09\x09\x09if(p->next->power next->power) {

\x09\x09\x09\x09pt = p->next;

\x09\x09\x09\x09p->next = q->next;

\x09\x09\x09\x09q->next = p->next->next;

\x09\x09\x09\x09p->next->next = pt;

\x09\x09\x09}

\x09\x09\x09else q = q->next;

\x09\x09}

\x09\x09p = p->next;

\x09}

}

void Show(POLYNOMIAL head) { // 显示

\x09POLYNOMIAL p = head->next;

\x09int flag = 1;

\x09if(p == NULL) return;

\x09while(p) {

\x09\x09if(flag) {

\x09\x09\x09if(fabs(p->coefficient) >= EPS) {

\x09\x09\x09\x09if(p->power == 0) cout <coefficient;

\x09\x09\x09\x09else if(p->power == 1) {

\x09\x09\x09\x09\x09if(p->coefficient == 1.0) cout <

\x09\x09\x09\x09\x09else if(p->coefficient == -1.0) cout <

\x09\x09\x09\x09\x09else cout <coefficient <

\x09\x09\x09\x09}

\x09\x09\x09\x09else if(p->coefficient == 1.0) cout <power <

\x09\x09\x09\x09else if(p->coefficient == -1.0) cout <power <

\x09\x09\x09\x09else cout <coefficient <power <

\x09\x09\x09\x09flag = 0;

\x09\x09\x09}

\x09\x09}

\x09\x09else if(p->coefficient > 0.0 && fabs(p->coefficient) >= EPS) {

\x09\x09\x09if(p->power == 0) cout <coefficient <

\x09\x09\x09else if(p->power == 1) {

\x09\x09\x09\x09if(p->coefficient == 1.0) cout <

\x09\x09\x09\x09else cout <coefficient <

\x09\x09\x09}

\x09\x09\x09else if(p->coefficient == 1.0) cout <power <

\x09\x09\x09else cout <coefficient <power <

\x09\x09}

\x09\x09else if(p->coefficient coefficient) >= EPS) {

\x09\x09\x09if(p->power == 0) cout <coefficient <

\x09\x09\x09else if(p->power == 1) {

\x09\x09\x09\x09if(p->coefficient == -1.0) cout <

\x09\x09\x09\x09else cout <coefficient <

\x09\x09\x09}

\x09\x09\x09else if(p->coefficient == -1.0) cout <power <

\x09\x09\x09else cout <coefficient <power <

\x09\x09}

\x09\x09p = p->next;

\x09}

\x09cout <

}

double Power(double x,int n) {

\x09double value = 1.0;

\x09int i;

\x09for(i = 0; i 

\x09return value;

}

double Value(POLYNOMIAL head,double x) { // 多项式求值

\x09POLYNOMIAL p;

\x09double value = 0.0;

\x09for(p = head->next; p; p = p->next)

\x09\x09value += p->coefficient * Power(x,p->power);

\x09return value;

}

POLYNOMIAL Copy(POLYNOMIAL A) {

\x09POLYNOMIAL head,t,p;

\x09head = t = new item;

\x09for(p = A->next; p; p = p->next) {

\x09\x09t->next = new item;

\x09\x09t->next->coefficient = p->coefficient;

\x09\x09t->next->power = p->power;

\x09\x09t = t->next;

\x09}

\x09t->next = NULL;

\x09return head;

}

POLYNOMIAL Additive(POLYNOMIAL A, POLYNOMIAL B) { // 多项式加

\x09POLYNOMIAL head,p,q,t;

\x09head = Copy(A);

\x09for(p = B; p->next; p = p->next) {

\x09\x09q = head;

\x09\x09while(q->next) {

\x09\x09\x09if(p->next->power == q->next->power) {

\x09\x09\x09\x09q->next->coefficient += p->next->coefficient;

\x09\x09\x09\x09if(fabs(q->next->coefficient) <= EPS) {

\x09\x09\x09\x09\x09t = q->next;

\x09\x09\x09\x09\x09q->next = t->next;

\x09\x09\x09\x09\x09free(t);

\x09\x09\x09\x09}

\x09\x09\x09\x09break;

\x09\x09\x09}

\x09\x09\x09q = q->next;

\x09\x09}

\x09\x09if(q->next == NULL) {

\x09\x09\x09q->next = new item;

\x09\x09\x09q->next->coefficient = p->next->coefficient;

\x09\x09\x09q->next->power = p->next->power;

\x09\x09\x09q->next->next = NULL;

\x09\x09}

\x09}

\x09Sort(head);

\x09return head;

}

POLYNOMIAL Subtract(POLYNOMIAL A, POLYNOMIAL B) { // 多项式减

\x09POLYNOMIAL head,p,q,t;

\x09head = Copy(A);

\x09for(p = B; p->next; p = p->next) {

\x09\x09q = head;

\x09\x09while(q->next) {

\x09\x09\x09if(p->next->power == q->next->power) {

\x09\x09\x09\x09q->next->coefficient -= p->next->coefficient;

\x09\x09\x09\x09if(fabs(q->next->coefficient) <= EPS) {

\x09\x09\x09\x09\x09t = q->next;

\x09\x09\x09\x09\x09q->next = t->next;

\x09\x09\x09\x09\x09free(t);

\x09\x09\x09\x09}

\x09\x09\x09\x09break;

\x09\x09\x09}

\x09\x09\x09q = q->next;

\x09\x09}

\x09\x09if(q->next == NULL) {

\x09\x09\x09q->next = new item;

\x09\x09\x09q->next->coefficient = -p->next->coefficient;

\x09\x09\x09q->next->power = p->next->power;

\x09\x09\x09q->next->next = NULL;

\x09\x09}

\x09}

\x09Sort(head);

\x09return head;

}

POLYNOMIAL Multiplication(POLYNOMIAL A, POLYNOMIAL B) { // 多项式乘

\x09POLYNOMIAL head,t,p,q;

\x09head = t = new item;

\x09for(p = A->next; p; p = p->next) { // 完成相乘过程

\x09\x09for(q = B->next; q; q = q->next) {

\x09\x09\x09t->next = new item;

\x09\x09\x09t->next->coefficient = p->coefficient * q->coefficient;

\x09\x09\x09t->next->power = p->power + q->power;

\x09\x09\x09t = t->next;

\x09\x09}

\x09}

\x09t->next = NULL;

\x09Sort(head); // 排序

\x09p = head;

\x09while(p->next) { // 合并同类项

\x09\x09q = p->next;

\x09\x09while(q->next) {

\x09\x09\x09if(p->next->power == q->next->power) {

\x09\x09\x09\x09p->next->coefficient += q->next->coefficient;

\x09\x09\x09\x09t = q->next;

\x09\x09\x09\x09q->next = t->next;

\x09\x09\x09\x09free(t);

\x09\x09\x09}

\x09\x09\x09else q = q->next;

\x09\x09}

\x09\x09p = p->next;

\x09}

\x09return head;

}

void FreeMemory(POLYNOMIAL head) {

\x09POLYNOMIAL q,p = head;

\x09while(p) {

\x09\x09q = p;

\x09\x09p = q->next;

\x09\x09delete q;

\x09}

}

int main() {

\x09char ops[3];

\x09POLYNOMIAL A,B,C = NULL,D = NULL,E = NULL;

\x09cout <

\x09A = Create();

\x09Sort(A);

\x09cout <

\x09cout <

\x09B = Create();

\x09Sort(B);

\x09cout <

\x09cout <

\x09fflush(stdin);

\x09gets(ops);

\x09for(int i = 0; ops[i]; ++i) {

\x09\x09switch(ops[i]) {

\x09\x09\x09case '+' : C = Additive(A,B);

\x09\x09\x09\x09cout <

\x09\x09\x09\x09Show(C);

\x09\x09\x09\x09break;

\x09\x09\x09case '-' : D = Subtract(A,B);

\x09\x09\x09\x09cout <

\x09\x09\x09\x09Show(D);

\x09\x09\x09\x09break;

\x09\x09\x09case '*' : E = Multiplication(A,B);

\x09\x09\x09\x09cout <

\x09\x09\x09\x09Show(E);

\x09\x09\x09\x09break;

\x09\x09\x09default  : cout <

\x09\x09\x09}

\x09}

\x09cout <

\x09cout <

\x09if(C) cout <

\x09if(D) cout <

\x09if(E) cout <

\x09FreeMemory(A);

\x09FreeMemory(B);

\x09FreeMemory(C);

\x09FreeMemory(D);

\x09FreeMemory(E);

\x09return 0;

}

再问: VC2013运行提示226行get的用法错误,有只是多项式加法的吗·····

再答: gets()读入字符串没有问题。

这里有加、减、乘三个函数,把不需要的函数去掉就行了。

但创建函数Create()需要简单修改,以便满足题目的要求。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值