02-线性结构2 一元多项式的乘法与加法运算(20 分)

2017-09-24  本文已影响162人  Ainevsia

传送门

中国大学MOOC-陈越、何钦铭-数据结构-2017秋

题目

02-线性结构2 一元多项式的乘法与加法运算(20 分)
设计函数分别求两个一元多项式的乘积与和。
输入格式:
输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。
输出格式:
输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0。
输入样例:
4 3 4 -5 2 6 1 -2 0
3 5 20 -7 4 3 1
输出样例:
15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
5 20 -4 4 -5 2 9 1 -2 0

目前代码

#include <iostream>
#include <cstdlib>

using namespace std;

typedef struct _node {
    int coef;
    int expon;
    struct _node *next;
} Node;

typedef struct _list {
    
} List;

int main(int argc, char const *argv[])
{
    freopen("C:\\Users\\shmh2\\Documents\\test.txt", "r", stdin);
    Node list1, list2, list3;
    Node *p;
    int n1, n2;
    cin >> n1;
    int i;
    
    p = &list1;
    for ( i=0; i<n1; i++ ) {
        p->next = (Node*)malloc(sizeof(Node));
        p = p->next;
        cin >> p->coef >> p->expon;
    }
    p->next = NULL;
    
    cin >> n2;
    p = &list2;
    for ( i=0; i<n2; i++ ) {
        p->next = (Node*)malloc(sizeof(Node));
        p = p->next;
        cin >> p->coef >> p->expon;
    }
    p->next = NULL;

    Node *p1 = list1.next;
    Node *p2 = list2.next;
    //
    p = &list3;
    int coef, expon;
    while ( p1 ) {
        while ( p2 ) {
            expon = p1->expon + p2->expon;
            coef = p1->coef * p2->coef;
                p->next = (Node*)malloc(sizeof(Node));
                p = p->next;
                p->coef = coef;
                p->expon = expon;
            //cout << coef << " " << expon;
            //if ( expon ) cout << " ";
//          if ( p1->expon == 0 ) p2 = p2->next;
//          else if ( p2->expon == 0 ) p1 = p1->next;
//          else {
//              if ( p1->expon < p2->expon) p1 = p1->next;
//              else if ( p1->expon > p2->expon) p2 = p2->next;
//              else p1 = p1->next;
//          }
            p2 = p2->next;
        }
    }
    
    
    cout << endl;
    
    
    
    
    //
    int cnt = 0;
    p1 = list1.next;
    p2 = list2.next;
    while ( p1 && p2 ) {
        cnt++;
        if ( p1->expon < p2->expon ) {
            cout << p2->coef << " " << p2->expon;
            p2 = p2->next;
        }else if ( p1->expon > p2->expon ) {
            cout << p1->coef << " " << p1->expon;
            p1 = p1->next;
        } else {
            int sum;
            sum = p1->coef + p2->coef;
            if ( sum ) {
                cout << sum << " " << p1->expon;
            }
            p1 = p1->next;
            p2 = p2->next;
            cnt++; 
        }
        if ( cnt < n1 + n2 ) cout << " ";
    }
    if ( p1 || p2 ) {
        for ( ; p1; p1=p1->next ) {
            cnt++;
            cout << p1->coef << " " << p1->expon;
            if ( cnt < n1 + n2 ) cout << " ";
        }
        for ( ; p2; p2=p2->next ) {
            cnt++;
            cout << p2->coef << " " << p2->expon;
            if ( cnt < n1 + n2 ) cout << " ";
        }
    }
    cout << "\\";
    return 0;
} 
上一篇下一篇

猜你喜欢

热点阅读