Bad Ass 2020-02-11 23:03 采纳率: 100%
浏览 386
已采纳

最近在学数据结构,为什么在C++中,在相乘函数中Z1能够被D1赋值,而Z2不能被D2赋值而一直报错?(经检查,D1, D2都是有值的)

/*多项式相乘测试*/
#include<iostream>
using namespace std;

/*建立节点结构体*/
struct PolyNode {
    int coef;
    int pow;
    struct PolyNode* next;
};
typedef struct PolyNode* Polynomial;

/*建立输入函数*/
Polynomial InputPoly() {
    Polynomial P, temp;
    P = (Polynomial)malloc(sizeof(struct PolyNode));
    temp = P;
    int N;
    cout << "Input N" << endl;
    cin >> N;
    for (N; N >= 1; N--) {
        cout << "Input coefficent and power:" << endl;
        cin >> P->coef;
        cin >> P->pow;
        P->next = (Polynomial)malloc(sizeof(struct PolyNode));
        P = P->next;
    }
    P = NULL;
    P = temp;
    return P;
}

/*声明Attach函数*/
Polynomial Attach(int c, int p, Polynomial m) {
    Polynomial single, n, first;
    single = (Polynomial)malloc(sizeof(struct PolyNode));
    single->coef = c;
    single->pow = p;
    n = m;
    first = m;
    if (single->pow > n->pow) {
        single->next = n;
        n = single;
        return n;
    }
    if (single->pow == n->pow) {
        n->coef = single->coef + n->coef;
        free(single);
        if (n->coef == 0) {
            Polynomial t = n;
            n = n->next;
            free(t);
            return n;
        }
        return n;
    }
    for (; 1; n = n->next) {
        if (single->pow < n->pow && single->pow > n->next->pow&& n->next != NULL) {
            single->next = n->next;
            n->next = single;
            n = first;
            return n;
        }
        if (single->pow == n->next->pow && n->next != NULL) {
            Polynomial t = n->next;
            t->coef = single->coef + t->coef;
            free(single);
            if (t->coef == 0) {
                n->next = t->next;
                free(t);
                n = first;
                return n;
            }
            n = first;
            return n;
        }
        if (n->next == NULL) {
            single->next = n->next;
            n->next = single;
            n = first;
            return n;
        }
    }
}

/*相乘函数*/
Polynomial Mult(Polynomial D1, Polynomial D2) {
    Polynomial Z1;
    Polynomial Z2;
    Polynomial temp, r, s;
    Z1 = D1;
    Z2 = D2;
    int flag;
    temp = (Polynomial)malloc(sizeof(struct PolyNode));
    if (!(Z1 && Z2)) {
        cout << "Zero";
        return NULL;
    }
    r = temp;
    while (Z2!=NULL) {
        temp->coef = Z1->coef * Z2->coef;
        temp->pow = Z1->pow + Z2->pow;
        Z2 = Z2->next;
        temp->next= (Polynomial)malloc(sizeof(struct PolyNode));
        temp = temp->next;
    }
    temp = NULL;
    temp = r;
    Z1 = Z1->next;
    for (; Z1 != NULL; Z1 = Z1->next){
        Z2 = D2;
        for (; Z2 != NULL; Z2 = Z2->next) {
            s = (Polynomial)malloc(sizeof(struct PolyNode));
            s->coef = Z1->coef * Z2->coef;
            s->pow = Z1->pow + Z2->pow;
            temp = Attach(s->coef, s->pow, temp);
            free(s);
        }
    }
    temp = r;
    return temp;
}

/*输出函数*/
void Output(Polynomial q) {
    Polynomial temp = q;
    cout << "The ressult is: "<<endl;
    int flag = 1;
    while (q) {
        if (!flag) {
            cout << " ";
        }
        cout << temp->coef << " " << temp->pow;
        temp = temp->next;
        flag = 0;
    }
}

/*清除链表函数*/
void Clean(Polynomial q) {
    Polynomial a, b;
    a = q;
    b = q;
    while (a) {
        a = a->next;
        free(b);
        b = a;
    }
    free(b);
}

/*主函数*/
int main() {
    Polynomial p1, p2, res;
    p1 = InputPoly();
    p2 = InputPoly();
    res = Mult(p1, p2);
    Output(res);
    Clean(res);
    return 0;
}
  • 写回答

2条回答 默认 最新

  • Charles Cedric 2020-02-12 16:29
    关注

    不同的编译器应该会有不同的处理方法,你换一个编译器试试

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?