多项式Polynomial

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37717751/article/details/69390955
#include <stdio.h>
#include <malloc.h>
#include <math.h>
#include <stdlib.h>

/*将多项式存放在栈中,实现逆波兰运算
  默认多项式的项按倒序排列*/

typedef struct term {
// 存放多项式的结构体
    int coef;
    int exp;
    struct term *link;
}Term, *sTerm;

typedef struct termStack {
// 存放多项式结构体的结构体
    sTerm childTerm;
    struct termStack *link;
}TermStack, *sTermStack;

sTermStack myStack = NULL;

void createTerm(sTerm head)
{
// 创建多项式
    int coefInput, expInput;
    int count = 0;
    sTerm tmp, p;
    p = (sTerm)malloc(sizeof(Term));
    p = head;
    p->link = NULL;
    printf("Coef: ");
    scanf(" %d", &coefInput);
    while (0 != coefInput)
    {
        if (0 == count)// 多项式的第一项
        {
            p->coef = coefInput;
            printf("Exp: ");
            scanf(" %d", &expInput);
            p->exp = expInput;
            if (0 == expInput)
                break;
        }
        else
        {
            tmp = (sTerm)malloc(sizeof(Term));
            tmp->coef = coefInput;
            printf("Exp: ");
            scanf(" %d", &expInput);
            tmp->exp = expInput;
            tmp->link = p->link;
            p->link = tmp;
            p = tmp;
            if (0 == expInput)
                break;
        }
        printf("Coef: ");
        scanf(" %d", &coefInput);
        count++;
    }
}

/*void createStack(sTermStack thead, sTerm head)
{
    sTermStack p;
    p = (sTermStack)malloc(sizeof(TermStack));
    p = thead;
    p->link = NULL;
    p->childTerm = head;
}*/

void push(sTerm head)
{
// 多项式入栈
    sTermStack p;
    p = (sTermStack)malloc(sizeof(TermStack));
    p->childTerm = head;
    p->link = myStack;
    myStack = p;
}

void pop()
{
// 多项式出栈
    sTermStack tmp;
    tmp = (sTermStack)malloc(sizeof(TermStack));
    tmp = myStack;
    if (!myStack)
    {
        printf("The stack is empty!\n");
        return;
    }
    myStack = tmp->link;
    free(tmp);
}

void addTerm()
{
/*实现多项式加法
  总体思想是将第二个多项式遍历,与第一个多项式做比较,指数相同的项相加,不同的项插入*/
    sTerm p1, p2, tmp, t;
    p2 = myStack->childTerm;
    pop();
    p1 = myStack->childTerm;
    pop();
    tmp = p1;
    while (p2)
    {
        while(tmp->link)
        {
            if (tmp->exp == p2->exp)    // 指数相同
            {
                tmp->coef += p2->coef;
                break;
            }
            else if (p2->exp < tmp->exp && p2->exp > tmp->link->exp)    // 指数不同,且可以插入
            {
                t = (sTerm)malloc(sizeof(Term));
                t->coef = p2->coef;
                t->exp = p2->exp;
                t->link = tmp->link;
                tmp->link = t;
                break;
            }
            else if (p2->exp <= tmp->link->exp)
                tmp = tmp->link;
        }
        if (tmp->link == NULL)    // 插入到最后一项
        {
            if (tmp->exp == p2->exp)
                tmp->coef += p2->coef;
            else if(tmp->exp > p2->exp)
            {
                t = (sTerm)malloc(sizeof(Term));
                t->coef = p2->coef;
                t->exp = p2->exp;
                t->link = tmp->link;
                tmp->link = t;
            }
        }
        p2 = p2->link;
    }
    push(p1);
}

void minusTerm()
{
// 减法,具体思想同加法
    sTerm p1, p2, tmp, t;
    p2 = myStack->childTerm;
    pop();
    p1 = myStack->childTerm;
    pop();
    tmp = p1;
    while (p2)
    {
        while(tmp->link)
        {
            if (tmp->exp == p2->exp)
            {
                tmp->coef -= p2->coef;
                break;
            }
            else if (p2->exp < tmp->exp && p2->exp > tmp->link->exp)
            {
                t = (sTerm)malloc(sizeof(Term));
                t->coef = (-p2->coef);
                t->exp = p2->exp;
                t->link = tmp->link;
                tmp->link = t;
                break;
            }
            else if (p2->exp <= tmp->link->exp)
                tmp = tmp->link;
        }
        if (tmp->link == NULL)
        {
            if (tmp->exp == p2->exp)
                tmp->coef -= p2->coef;
            else if(tmp->exp > p2->exp)
            {
                t = (sTerm)malloc(sizeof(Term));
                t->coef = (-p2->coef);
                t->exp = p2->exp;
                t->link = tmp->link;
                tmp->link = t;
            }
        }
        p2 = p2->link;
    }
    push(p1);
}

// (不要在意函数名,瞎28起的2333
sTerm mutiNumTerm(sTermStack thead, int coef, int exp)
{
// 用一项乘多项式
    sTerm p, tmp;
    p = tmp = (sTerm)malloc(sizeof(Term));
    p = thead->childTerm;
    pop();
    tmp = p;
    while(tmp)
    {
        tmp->coef *= coef;
        tmp->exp += exp;
        tmp = tmp->link;
    }
    return p;
}

// 这个函数想实现多项式乘多项式,不过没做完,就不要看了
/*void mutiTermTerm(sTermStack thead)
{
    sTerm p1, p2, tmp, t;
    p1 = p2 = tmp = t = (sTerm)malloc(sizeof(Term));
    p2 = thead->childTerm;
    p1 = thead->link->childTerm;
    pop();
    pop();
    t = p1;
    while (t)
    {
        tmp = p2;
        while (tmp)
        {
            t->coef *= tmp->coef;
            t->exp += tmp->exp;
            tmp = tmp->link;
        }
        t = t->link;
    }
    push(p1);
}*/

void printTerm(sTermStack thead)
{
// 打印多项式
    int count = 0;
    sTerm p;
    p = (sTerm)malloc(sizeof(Term));
    p = thead->childTerm;
    while(p)
    {
        if (0 == count)    // 第一项
        {
            if (p->coef < 0)
                printf("- ");
            if (0 == p->coef)
                continue;
            if (0 == p->exp)
                printf("%d ", abs(p->coef));
            else
            {
                if (1 == p->coef)
                    printf("x^%d ", p->exp);
                else
                    printf("%d x^%d ", abs(p->coef), p->exp);
            }
        }
        else
        {
            if (p->coef > 0)
                printf("+ ");
            else if (p->coef < 0)
                printf("- ");
            if (0 == p->coef)
                continue;
            if (0 == p->exp)
                printf("%d ", abs(p->coef));
            else
            {
                if (1 == p->coef)
                    printf("x^%d ", p->exp);
                else
                    printf("%d x^%d ", abs(p->coef), p->exp);
            }
        }
        p = p->link;
        count++;
    }
    printf("\n");
}

int main(void)
{
    int count = 0;
    int coef, exp;
    char choice;
    sTerm head;
    printf("This is a polynomials program. \nPlease enter a valid command: \n[?] Read a polynomial. \n[=] Return top polynomial. \n[-] Difference two polynomials. \n[+] Add two polynomials. \n[*] Multiply a polynomial with a term. \n[X] Multiply two polynomials. \n[Q] Quit. \n");
    scanf(" %c", &choice);
    while ('Q' != choice)
    {
        switch (choice)
        {
        case '?':
            head = (sTerm)malloc(sizeof(Term));
            createTerm(head);
            push(head);
            break;
        case '=':
            printTerm(myStack);
            break;
        case '+':
            addTerm();
            break;
        case '-':
            minusTerm();
            break;
        case '*':
            printf("Coef: ");
            scanf(" %d", &coef);
            printf("Exp: ");
            scanf(" %d", &exp);
            mutiNumTerm(myStack, coef, exp);
            break;
        case 'X':
            mutiTermTerm(myStack);
            break;
        default:
            break;
        }
        count++;
        scanf(" %c", &choice);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_37717751/article/details/69390955