• 多项式的链式写法


    #include<stdio.h>//一元稀疏多项式的计算
    #include<iostream>
    #include<stdlib.h>
    using namespace std;
    typedef struct PolyNode  // 结构体
    {
        float coef;  //系数
        int exp;     //指数
        struct PolyNode * next;
    }PolyNode;
    typedef PolyNode * Polynomial;   // 类型声明
    Polynomial  CreateList(int n)    //建表并输入
    {
        Polynomial L,p,r;
        r = L = (Polynomial )malloc(sizeof(PolyNode ));   //头节点是L不能移动
        L->next = NULL;
        float x;
        int y;
        while(n--)   //判断输入结束的条件为( x!=0 && y!=0 )
        {
        	scanf("%f %d",&x,&y);
            p = (Polynomial )malloc(sizeof(PolyNode ));
            p->coef = x;
            p->exp = y;
            p->next = NULL;
            r->next = p;
            r = p;
        }
        return L;  // 返回输入的链表
    }
    Polynomial Add(Polynomial A, Polynomial B)  //加法
    {
        Polynomial C,S;
        Polynomial pa,pb,pc;
        float x;
        pa = A->next;
        pb = B->next;
        C = (Polynomial )malloc(sizeof(PolyNode ));  // 申请头节点
        pc = C;   // 操作时使用pc指针
        pc->next = NULL;
        while (pa && pb)
        {
            if(pa->exp == pb->exp)  // 指数相等时
            {
                x = pa->coef + pb->coef;
                if (x)     // 相加完的系数不为0时
                {
                    S = (Polynomial )malloc(sizeof(PolyNode ));
                    S->coef = x;
                    S->exp = pa->exp;
                    S->next = NULL;
                    pc->next = S;
                    pc = S;
                }
                pa = pa->next;
                pb = pb->next;
            }
            else
                if(pa->exp < pb->exp)   //指数不相等且A项指数小于B项指数时
                {
                    S = (Polynomial )malloc(sizeof(PolyNode ));
                    S->coef = pa->coef;
                    S->exp = pa->exp;
                    S->next = NULL;
                    pc->next = S;
                    pc = S;
                    pa = pa->next;
                }
                else   //指数不相等且A项指数大于B项指数时
                {
                    S = (Polynomial )malloc(sizeof(PolyNode ));
                    S->coef = pb->coef;
                    S->exp = pb->exp;
                    S->next = NULL;
                    pc->next = S;
                    pc = S;
                    pb = pb->next;
                }
        }
        while (pa)   //A链表后几项多出来时
        {
            S = (Polynomial )malloc(sizeof(PolyNode ));
            S->coef = pa->coef;
            S->exp = pa->exp;
            S->next = NULL;
            pc->next = S;
            pc = S;
            pa = pa->next;
        }
        while (pb)   //B链表后几项多出来时
        {
            S = (Polynomial )malloc(sizeof(PolyNode ));
            S->coef = pb->coef;
            S->exp = pb->exp;
            S->next = NULL;
            pc->next = S;
            pc = S;
            pb = pb->next;
        }
        return C;
    }
    Polynomial Minus(Polynomial A, Polynomial B)  //减法,判断条件等都同上
    {
        Polynomial C,S;
        Polynomial pa,pb,pc;
        float x;
        pa = A->next;
        pb = B->next;
        C = (Polynomial )malloc(sizeof(PolyNode ));
        pc = C;
        pc->next = NULL;
        while (pa && pb)
        {
            if(pa->exp == pb->exp)
            {
                x = pa->coef - pb->coef; //系数相减
    //            if (x)
    //            {
                    S = (Polynomial )malloc(sizeof(PolyNode ));
                    S->coef = x;
                    S->exp = pa->exp;
                    S->next = NULL;
                    pc->next = S;
                    pc = S;
    //            }
                pa = pa->next;
                pb = pb->next;
            }
            else
                if(pa->exp < pb->exp)
                {
                    S = (Polynomial )malloc(sizeof(PolyNode ));
                    S->coef = pa->coef;
                    S->exp = pa->exp;
                    S->next = NULL;
                    pc->next = S;
                    pc = S;
                    pa = pa->next;
                }
                else
                {
                    S = (Polynomial )malloc(sizeof(PolyNode ));
                    S->coef = -pb->coef;  // Pb放上去时,需要为负 
                    S->exp = pb->exp;
                    S->next = NULL;
                    pc->next = S;
                    pc = S;
                    pb = pb->next;
                }
        }
        while (pa)
        {
            S = (Polynomial )malloc(sizeof(PolyNode ));
            S->coef = pa->coef;
            S->exp = pa->exp;
            S->next = NULL;
            pc->next = S;
            pc = S;
            pa = pa->next;
        }
        while (pb)
        {
            S = (Polynomial )malloc(sizeof(PolyNode ));
            S->coef = -pb->coef; // Pb放上去时,需要为负 
            S->exp = pb->exp;
            S->next = NULL;
            pc->next = S;
            pc = S;
            pb = pb->next;
        }
        return C;
    }
    void PrintfPolyn(Polynomial P){//输出多项式
        Polynomial t;
        t=P->next;
        if(t==NULL)
        {
            cout<<"0"<<endl;
            return;
        }
        while(t->next){
    //        if(t->exp!=0)
    //        {
    //            if(t->coef!=1&&t->coef!=-1)
                cout<<t->coef<<"X";
    //            else if(t->coef==1)
    //                cout<<"X";
    //            else
    //                cout<<"-X";
                if(t->next->coef>=0)
                    cout<<"^"<<t->exp<<"+";
                else
                    cout<<"^"<<t->exp;
    //        }
    //        else
    //            cout<<t->coef;
            t=t->next;
        }
    //    if(t->exp!=0){
    ////        if(t->coef!=1&&t->coef!=-1)
    //          cout<<t->coef<<"X";
    ////        else if(t->coef==1)
    ////            cout<<"x";
    ////        else
    ////            cout<<"-x";
    //        if(t->exp!=1)
    //            cout<<"^"<<t->exp;
    //    }
    //    else
    //        cout<<t->coef;
    	cout << t->coef << "X^" <<t->exp;
        cout<<endl;
    }
    int main()
    { 
        Polynomial  P1,P2;
        int t;
    	cin >> t;
    	while(t--){
    		int n, m; 
    		cin >> n >> m;
    		P1 = CreateList(n);
    		P2 = CreateList(m);
    		int T;
    		cin >> T;
    		while(T--){
    			int type;
    			cin >> type;
    			if(type == 1){
    				// 输出
    				PrintfPolyn(P1);
    				PrintfPolyn(P2);
    			}
    			else if (type == 2){
    				// 相加到第一个
    				P1 = Add(P1,P2);
    			}
    			else if (type == 3){
    				// 相减到第一个
    				P1 =  Minus(P1, P2);
    			}
    	    }
        }
        return 0;
    }
    
    /*
    1
    2 3
    2 0 0 4
    4 5 5 6 6 7
    5
    1
    3
    1
    2
    1
    https://blog.csdn.net/chuck_0430/article/details/8184846
    */
    

      

  • 相关阅读:
    win10 ,本地连接无法识别网络 ,无线正常,
    vba 声音
    win10 优化
    比较火和常用的命令
    手机电脑平板 查图纸、查点位图、查通病、自学维修知识等通通都有的工具
    e4a mysql
    e4a 对话框的 多选单选颜色日期时间
    e4s 文本操作 数组操作
    e4a sqlite案例
    e4a-窗口切换
  • 原文地址:https://www.cnblogs.com/zhumengdexiaobai/p/12931649.html
Copyright © 2020-2023  润新知