| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2297 人关注过本帖
标题:求助。多项式的乘法和加法实现总是出错。 with return value 3221225477
只看楼主 加入收藏
hzx133432051
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2018-5-4
收藏
 问题点数:0 回复次数:0 
求助。多项式的乘法和加法实现总是出错。 with return value 3221225477
#include <stdio.h>
#include <stdlib.h>

typedef struct PolyNode* Polynomial;
struct PolyNode{
    int coef;  //系数
    int expon;  //指数
    Polynomial link;
};
//将新结点连接在pRear后面,并返回一个链表P
void Attach(int c,int e,Polynomial *pRear)
{
    Polynomial P;
   
    P = (Polynomial)malloc(sizeof(struct PolyNode));
    P->coef = c;
    P->expon = e;
    P->link = NULL;
    (*pRear)->link = P;
    *pRear = P;
}

Polynomial ReadPoly()
{
    Polynomial P,Rear,t;
    int c,e,N;
   
    scanf("%d",&N);
    P = (Polynomial)malloc(sizeof(struct PolyNode));
    P->link = NULL;
    P = Rear;
    while(N--){
        scanf("%d %d",&c,&e);
        Attach(c,e,&Rear);
    }
    t = P; P = P->link;free(t);
    return P;
}
//比较指数
int Compare(int e1,int e2)
{
    int result;
    if(e1 > e2)
        result = 1;
    else if (e1 < e2)
        result = -1;
    else
        result = 0;
    return result;
}

//两个多项式相加
Polynomial Add(Polynomial P1,Polynomial P2)
{
    Polynomial front,rear,temp;
    int sum;
    rear = (Polynomial)malloc(sizeof(struct PolyNode));
    front = rear;
    while(P1&&P2)
        switch(Compare(P1->expon,P2->expon)){
            case 1:
                Attach(P1->coef,P1->expon,&rear);
                P1 = P1->link;
                break;
            case -1:
                Attach(P2->coef,P2->expon,&rear);
                P2 = P2->link;
                break;
            case 0:
                sum = P1->coef + P2->coef;
                if(sum) Attach(sum,P1->expon,&rear);
                P1 = P1->link;
                P2 = P2->link;
                break;
        }
    for(;P1;P1 = P1->link) Attach(P1->coef,P1->expon,&rear);
    for(;P2;P2 = P2->link) Attach(P2->coef,P2->expon,&rear);
    rear->link = NULL;
    temp = front;
    front = front->link;
    free(temp);
    return front;
}
//两个多项式相乘
Polynomial Mult(Polynomial P1,Polynomial P2){
    Polynomial P,Rear,t1,t2,t;
    int c,e;
   
    if(!P1||!P2)  return NULL;
   
    t1 = P1; t2 = P2;
    P = (Polynomial)malloc(sizeof(struct PolyNode)); P->link = NULL;
    Rear = P;
    while(t2){  //先用P1的第一项乘以P2,得到P
        Attach(t1->coef*t2->coef,t1->expon+t2->expon,&Rear);
        t2 = t2->link;
    }
    t1 = t1->link;
    while(t1){
        t2 = P2;Rear = P;
        while(t2){
            e = t1->expon + t2->expon;
            c = t1->coef * t2->coef;
            while(Rear->link && Rear->link->expon > e)
                Rear = Rear->link;
            if(Rear->link && Rear->link->expon == e){
                if(Rear->link->expon + c)
                    Rear->link->coef += c;
                else{
                    t = Rear->link;
                    Rear->link = t->link;
                    free(t);
                }
            }
            else{
                t = (Polynomial)malloc(sizeof(struct PolyNode));
                t->coef = c; t->expon = e;
                t->link = Rear->link;
                Rear->link = t; Rear = Rear->link;
            }
            t2 = t2->link;
        }
        t1 = t1->link;
    }
    t2 = P; P = P->link; free(t2);
   
    return P;
}
//输出多项式
void PrintPoly(Polynomial P){
    int flag = 0;
   
    if(!P){ printf("0 0\n");return;}
   
    while(P){
        if(!flag)
            flag = 1;
        else    printf(" ");
        printf("%d %d",P->coef,P->expon);
        P = P->link;
    }
    printf("\n");
}
int main(){
    Polynomial P1,P2,PP,PS;
   
    P1 = ReadPoly();
    P2 = ReadPoly();
    PP = Mult(P1,P2);
    PrintPoly(PP);
    PS = Add(P1,P2);
    PrintPoly(PS);
   
    return 0;
}
搜索更多相关主题的帖子: return struct int link while 
2018-05-19 00:39
快速回复:求助。多项式的乘法和加法实现总是出错。 with return value 3221225 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.033079 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved