两个一元多项式相加(链表 || 顺序表实现)

顺序表实现:

#include 
#include 
#include 
#include 
#include 
using namespace std;
#define maxsize 1000
struct Data
{
    double coef;
	int exp;
};
struct List
{
	Data* elem;
	int length;
};
char s[100];
map  m;
bool cmp(Data a, Data  b)
{
	return a.exp < b.exp;
}
void InitList(List &L)
{
    L.elem = (Data* )new Data[maxsize];
    L.length = 0;
}
void Derivative(int len, List L)
{
    int n = 1,flag = 0;
    for(int i = 0;i < len;i ++)
    {
        double coef = L.elem[i].coef;
        int exp = L.elem[i].exp;
         if(exp == 0)
        {
            coef = 0;
        }
        else
        {
            coef *= exp;
            exp --;
        }
         if(fabs(coef) < 1e-6)
            continue;
         else
         {
            flag = 1;
            if(n != 1 && coef > 0)
                printf("+");
            if(exp == 0)
                printf("%g", coef);
            else if(coef!= 1 && exp != 0 && coef != -1)
            {
                if(exp == 1)
                    printf("%gx",coef);
                else
                    printf("%gx^%d",coef, exp);
            }
            else if(coef == 1)
            {
                if(exp == 1)
                    printf("x");
                else
                    printf("x^%d",exp);
            }
            else if(coef == -1)
            {
                if(exp == 1)
                    printf("-x");
                else
                    printf("-x^%d",exp);
            }
            n ++;
        }
    }
    if(!flag) printf("0");
    printf("\n");
}
int main()
{
	freopen("in.txt", "r", stdin);
	List L;
	int op1 = 1, op2 = 1,op3 = 1,op4 = 1;
	double a;
	int b, cases = 1,flag;
	while(scanf("%s", s) != EOF)
	{
        printf("Case %d:\n",cases++);
        InitList(L);
		op1 = op2 = op3 = op4 = 1;
		flag = 0;
		int len = strlen(s);
		for(int i = -10000;i <= 10000;i ++)
			m[i] = -1;
		int k = 0;
		for(int i = 0;i < len;i ++)
		{
            a = 0;
			b = 0;
			op1 = op2 = 1;
			if(s[0] == '-' && i == 0)
            {
                op4 = -1;
                i ++;
            }
            if(s[i+1] == '(')
            {
                Derivative(k, L);
                flag = 1;
            }
			if(s[i] == '=') break;
			if(s[i] == '(') continue;
			if(s[i] == ')')
			{

				i ++;
				op4 = 1;
				if(s[i] == '=')
                {
					 if(!flag)
                        Derivative(k, L);
                     break;
                }
				else
				{
                    flag = 1;
					Derivative(k, L);
					if(s[i] == '+')
						op3 = 1;
					else if(s[i] == '-')
						op3 = -1;

				}
			}
            if(s[i] == '-')
			{

				i ++;
				op1 = -1;
			}
			else if(s[i] == '+')
			{
				i ++;
				op1 = 1;
			}
			if(s[i] >= '0' && s[i] <= '9')
			{
				while(s[i] >= '0' && s[i] <= '9' && i < len)
				{
					a = a*10+(s[i]-'0');
					i ++;
				}
				if(s[i] == '.')
				{
					i ++;
					double x = 0.1;
					while(s[i] >= '0' && s[i] <= '9' && i < len)
					{
						a += x*(s[i]-'0');
						i ++;
						x *= 0.1;
					}
				}
				a = a*op1*op3*op4;
			}
			else if(s[i] == 'x')
			{
				a = op1*op3*op4;
			}
			if(s[i] == 'x')
			{
				i ++;
				if(s[i] == '^')
				{
					i ++;
					if(s[i] == '-')
					{
						op2 = -1;
						i ++;
					}
                    else if(s[i] == '+')
					{
						op2 = 1;
						i ++;
					}
					while(s[i] >= '0' && s[i] <= '9' && i < len)
					{
						b = b*10 + s[i] - '0';
						i ++;
					}
					b = b*op2;
					i --;
				}
				else
				{
					b = op2;
					i --;
				}
			}
			else
			{
				b = 0;
				i --;
			}
		    if(m[b] == -1)
			{
                L.elem[k].coef = a;
                L.elem[k].exp = b;
			    m[b] = k;
				k ++;
			}
			else
			{
				L.elem[m[b]].coef += a;
            }
        }
		sort(L.elem, L.elem+k, cmp);
		int flag = 0;
		int n = 1;
	    printf("%s", s);
		for(int i = 0;i < k;i ++)
		{
			if(fabs(L.elem[i].coef) < 1e-6)	continue;
			else
			{
				flag = 1;
				if(n != 1 && L.elem[i].coef > 0)
                    printf("+");
                if(L.elem[i].exp == 0)
                    printf("%g", L.elem[i].coef);
                else if(L.elem[i].coef != 1 && L.elem[i].exp != 0 && L.elem[i].coef != -1)
                {
                    if(L.elem[i].exp == 1)
                        printf("%gx",L.elem[i].coef);
                    else
                        printf("%gx^%d",L.elem[i].coef, L.elem[i].exp);
                }
                else if(L.elem[i].coef == 1)
                {
                    if(L.elem[i].exp == 1)
                        printf("x");
                    else
                        printf("x^%d",L.elem[i].exp);
                }
                else if(L.elem[i].coef == -1)
                {
                     if(L.elem[i].exp == 1)
                        printf("-x");
                    else
                        printf("-x^%d",L.elem[i].exp);
                }
				n ++;
			}
        }
		if(!flag) printf("0");
        printf("\n");
	}
	return 0;
}

链表实现:


#include 
#include 
#include 
#include 
#include 
#include 
#include "AddPolyn.h"
#include "Derivative.h"
using namespace std;
#define maxlen 1000
int main()
{
    char s[maxlen];
    int casen = 1;
    freopen("in.txt", "r", stdin);
    freopen("out.txt","w",stdout);
    while(scanf("%s", s) != EOF)
    {
        LNode* L1;
        init(L1);
        LNode* L2;
        init(L2);
        Inputpolyn(s, L1, L2);               //提取多项式中的系数和指数
        printf("Case %d :\n",casen ++);
        Deriv(L1);                           //求第一项的导数
        AddPolyn(L1, L2);                    //多项式相加
        printf("%s", s);
        print(L1);
        printf("\n");
    }
    return 0;
}


//Derivative.h
#ifndef DERIVATIVE_H_INCLUDED
#define DERIVATIVE_H_INCLUDED
void Deriv(LNode* L1)
{
    LNode* p = L1->next;
    double coef;
    int exp, n = 1, flag = 0;
    while(p)
    {
        coef = p->coef;
        exp = p->exp;
        if(exp == 0)
        {
            coef = 0;
        }
        else
        {
            coef *= exp;
            exp --;
        }
        //Êä³öµ¼Êý~~~~~~~~~~~~~~~~~~~~~~
         if(fabs(coef) < 1e-6)
        {
            p = p->next;
            continue;
        }
        else
        {
            flag = 1;
            if(n != 1 && coef > 0)
                printf("+");
            if(exp == 0)
                printf("%g", coef);
            else if(coef!= 1 && exp != 0 && coef != -1)
            {
                if(exp == 1)
                    printf("%gx",coef);
                else
                    printf("%gx^%d",coef, exp);
            }
            else if(coef == 1)
            {
                if(exp == 1)
                    printf("x");
                else
                    printf("x^%d",exp);
            }
            else if(coef == -1)
            {
                if(exp == 1)
                    printf("-x");
                else
                    printf("-x^%d",exp);
            }
            n ++;
        }
        p = p->next;
    }
    if(!flag) printf("0");
    printf("\n");
}
#endif // DERIVATIVE_H_INCLUDED

//AddPolyn.h
#ifndef INPUTPOLYN_H_INCLUDED
#define INPUTPOLYN_H_INCLUDED
struct LNode
{
    double coef;   //系数
    int exp;       //指数
    LNode *next;
};
void init(LNode* &L)
{
    L = (LNode*) new LNode;
    L -> next = NULL;
}
int compare(int x, int y)
{
    if(x == y) return 0;
    if(x < y) return 1;
    return -1;
}
void Insert(double coef, int exp, LNode* &L)
{
    LNode* p = L->next;
    LNode* q = L;
    while(p != NULL )
    {
        LNode *s;
        int flag = compare(exp, p->exp);
        if(flag == -1)     //比当前指数大
        {
            q = p;
            p = p->next;
        }
        else if(flag == 1) //比当前指数小
        {
            s = (LNode* )new LNode;
            s->coef = coef;
            s->exp = exp;
            s->next = p;
            q->next = s;
            return;
        }
        else if(flag == 0) //与当前指数相等
        {
            p->coef += coef;
            return;
        }
    }
    LNode*s = (LNode*) new LNode;
    s->coef = coef;
    s->exp = exp;
    s->next = NULL;
    q->next = s;
    return;


}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~读取多项式
void Inputpolyn(char s[], LNode* &L1, LNode* &L2)
{
    int op1 = 1,op2 = 1,op4 = 1,len,p;
    double a = 0;
    int b = 0;
    len = strlen(s);
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~读第一个多项式
    for(int i = 0; i < len; i ++)
    {
        a = 0;
        b = 0;
        op1 = op2 = 1;
        if(s[i] == '=')
            break;
        if(s[0] == '-' && i == 0)
        {
            op4 = -1;
            i ++;
        }
        if(s[i] == '(') continue;
        if(s[i] == ')')
        {
            p = i;
            break;
        }
        else if(s[i+1] == '(')
        {
            p = i-1;
            break;
        }
        if(s[i] == '-')
        {
            i ++;
            op1 = -1;
        }
        else if(s[i] == '+')
        {
            i ++;
            op1 = 1;
        }
        if(s[i] >= '0' && s[i] <= '9')
        {
            while(s[i] >= '0' && s[i] <= '9' && i < len)
            {
                a = a*10+(s[i]-'0');
                i ++;
            }
            if(s[i] == '.')
            {
                i ++;
                double x = 0.1;
                while(s[i] >= '0' && s[i] <= '9' && i < len)
                {
                    a += x*(s[i]-'0');
                    i ++;
                    x *= 0.1;
                }
            }
            a = a*op1*op4;
        }
        else if(s[i] == 'x')
        {
            a = op1*op4;
        }
        if(s[i] == 'x')
        {
            i ++;
            if(s[i] == '^')
            {
                i ++;
                if(s[i] == '-')
                {
                    op2 = -1;
                    i ++;
                }
                else if(s[i] == '+')
                {
                    op2 = 1;
                    i ++;
                }
                while(s[i] >= '0' && s[i] <= '9' && i < len)
                {
                    b = b*10 + s[i] - '0';
                    i ++;
                }
                b = b*op2;
                i --;
            }
            else
            {
                b = op2;
                i --;
            }
        }
        else
        {
            b = 0;
            i --;
        }
        //i --;
        //printf("%lf %d", a, b);
        Insert(a, b, L1);
    }
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~读第二个多项式
    op4 = 1;
    for(int i = p+1; i < len; i ++)
    {
        a = 0;
        b = 0;
        op1 = op2 = 1;
        if(s[i] == '-' && i == p+1)
        {
            op4 = -1;
            i ++;
        }
        else if(s[i] == '+' && i == p+1)
        {

            op4 = 1;
            i++;
        }
        if(s[i] == '=') break;
        if(s[i] == '(') continue;
        if(s[i] == ')')
        {
            i ++;
            if(s[i] == '=')
                break;
        }
        if(s[i] == '-')
        {
            i ++;
            op1 = -1;
        }
        else if(s[i] == '+')
        {
            i ++;
            op1 = 1;
        }
        if(s[i] >= '0' && s[i] <= '9')
        {
            while(s[i] >= '0' && s[i] <= '9' && i < len)
            {
                a = a*10+(s[i]-'0');
                i ++;
            }
            if(s[i] == '.')
            {
                i ++;
                double x = 0.1;
                while(s[i] >= '0' && s[i] <= '9' && i < len)
                {
                    a += x*(s[i]-'0');
                    i ++;
                    x *= 0.1;
                }
            }
            a = a*op1*op4;
        }
        else if(s[i] == 'x')
        {
            a = op1*op4;
        }
        if(s[i] == 'x')
        {
            i ++;
            if(s[i] == '^')
            {
                i ++;
                if(s[i] == '-')
                {
                    op2 = -1;
                    i ++;
                }
                else if(s[i] == '+')
                {
                    op2 = 1;
                    i ++;
                }
                while(s[i] >= '0' && s[i] <= '9' && i < len)
                {
                    b = b*10 + s[i] - '0';
                    i ++;
                }
                b = b*op2;
                i --;
            }
            else
            {
                b = op2;
                i --;
            }
        }
        else
        {
            b = 0;
            i --;
        }
        Insert(a, b, L2);
    }
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~两个多项式相加
void AddPolyn(LNode* &L1, LNode* &L2)
{
    LNode* p1 = L1->next, *p2 = L2->next, *r = L1, *q;
    //LNode* pre1 = L1,*pre2 = L2;
    while(p1 && p2)
    {
        int flag = compare(p1->exp, p2->exp);
        if(flag == 1)
        {
            r->next = p1;
            r = p1;
            //pre1= p1;
            p1 = p1->next;
        }
        else if(flag == -1)
        {
            r->next = p2;
            r = p2;
            //pre2 = p2;
            p2 = p2->next;
        }
        else if(flag == 0)
        {
            double sum = p1->coef + p2->coef;
            if(fabs(sum) < 1e-5)
            {
                if(L1->next == p1)
                    L1->next = p1->next;
                q = p1;
                p1 = p1->next;
                delete q;
            }
            else
            {
                p1->coef = sum;
                r->next = p1;
                r = p1;
                p1 = p1->next;
            }
            //if(pre2->next == p2)
            //pre2->next = p2->next;
            q = p2;
            p2 = p2->next;

            delete q;
        }
    }
    if(p1)
        r->next = p1;
    if(p2)
        r->next = p2;
    delete L2;
    L2 = q = p1 = p2 = r = NULL;
}
void print(LNode *L)
{
    int flag = 0,n = 1;
    LNode *p = L->next;
    while(p)
    {
        double coef = p->coef;
        int exp = p->exp;
        //cout << coef <<" "<next;
            continue;
        }
        else
        {
            flag = 1;
            //if(n == 1) printf("(");
            if(n != 1 && coef > 0)
                printf("+");
            if(exp == 0)
                printf("%g", coef);
            else if(coef!= 1 && exp != 0 && coef != -1)
            {
                if(exp == 1)
                    printf("%gx",coef);
                else
                    printf("%gx^%d",coef, exp);
            }
            else if(coef == 1)
            {
                if(exp == 1)
                    printf("x");
                else
                    printf("x^%d",exp);
            }
            else if(coef == -1)
            {
                if(exp == 1)
                    printf("-x");
                else
                    printf("-x^%d",exp);
            }
            n ++;
        }
        p = p->next;
    }
    if(!flag) printf("0");
    //else printf(");");
    return;
}
#endif // INPUTPOLYN_H_INCLUDED

测试数据:

(2x+5x^8-3.1x^11)+(7-5x^8+11x^9)=
(6x^-3-x+4.4x^2-1.2x^9+1.2x^9)-(-6x^-3+5.4x^2-x^2+7.8x^15)=
(1+x+x^2+x^3+x^4+x^5)+(-x^3-x^4)=
(x+x^3)+(-x-x^3)=
(x+x^100)+(x^100+x^200)=
(x+x^2+x^3)+0=
(7-5x^8+11x^9)+(2x+5x^8-3.1x^11)=
-(-6x^-3+5.4x^2-x^2+7.8x^15)+(6x^-3-x+4.4x^2-1.2x^9+1.2x^9)=
(-x^3-x^4)+(1+x+x^2+x^3+x^4+x^5)=
(-x-x^3)+(x+x^3)=
(x+x^100)+(x^100+x^200)=
0+(x+x^2+x^3)=
(x+x)=
(1+x-x)+x=
(x-9+x^100)+(x-x^8)=
(-x+x)=
(23+10)=

输出结果(输出第一行为第一项的导数值):

Case 1 :
2+40x^7-34.1x^10
(2x+5x^8-3.1x^11)+(7-5x^8+11x^9)=7+2x+11x^9-3.1x^11
Case 2 :
-18x^-4-1+8.8x
(6x^-3-x+4.4x^2-1.2x^9+1.2x^9)-(-6x^-3+5.4x^2-x^2+7.8x^15)=12x^-3-x-7.8x^15
Case 3 :
1+2x+3x^2+4x^3+5x^4
(1+x+x^2+x^3+x^4+x^5)+(-x^3-x^4)=1+x+x^2+x^5
Case 4 :
1+3x^2
(x+x^3)+(-x-x^3)=0
Case 5 :
1+100x^99
(x+x^100)+(x^100+x^200)=x+2x^100+x^200
Case 6 :
1+2x+3x^2
(x+x^2+x^3)+0=x+x^2+x^3
Case 7 :
-40x^7+99x^8
(7-5x^8+11x^9)+(2x+5x^8-3.1x^11)=7+2x+11x^9-3.1x^11
Case 8 :
-18x^-4-8.8x-117x^14
-(-6x^-3+5.4x^2-x^2+7.8x^15)+(6x^-3-x+4.4x^2-1.2x^9+1.2x^9)=12x^-3-x-7.8x^15
Case 9 :
-3x^2-4x^3
(-x^3-x^4)+(1+x+x^2+x^3+x^4+x^5)=1+x+x^2+x^5
Case 10 :
-1-3x^2
(-x-x^3)+(x+x^3)=0
Case 11 :
1+100x^99
(x+x^100)+(x^100+x^200)=x+2x^100+x^200
Case 12 :
0
0+(x+x^2+x^3)=x+x^2+x^3
Case 13 :
2
(x+x)=2x
Case 14 :
0
(1+x-x)+x=1+x
Case 15 :
1+100x^99
(x-9+x^100)+(x-x^8)=-9+2x-x^8+x^100
Case 16 :
0
(-x+x)=0
Case 17 :
0
(23+10)=33


你可能感兴趣的:(数据结构)