大作业 一元多项式运算 顺序表

  1. 顺序存储结构代码:
#include
#define maxdegree 100
using namespace std;
typedef struct 
{
    int coefarray[maxdegree + 1];
    int highpower;   // 最高指数 
}polynomial;
void CreatPolyn(polynomial *L)
{
    int i, n, coef, expn;   // coef代表系数, ecpn代表指数 
    L->highpower = 0;
    for(i=0; i<=maxdegree+1; i++){//初始化 
        L->coefarray[i] = 0;
     }
     cout<<"输入多项式的项数:";
     cin>>n;
     for(i=1; i<=n; i++){
        printf("请输入第%d项的系数和指数:",i);
        scanf("%d %d", &coef, &expn);
        if(L->highpower < expn){
            L->highpower = expn;
         }
         L->coefarray[expn] = coef;
    }
}
void NewPolyn(polynomial *L)//新建 初始化 
{
    int i;
    for(i=0; icoefarray[i] = 0;
    }
    L->highpower = 0;
} 
void PrintPolyn(polynomial *L)
{
    int i;
    if(L->coefarray[0] != 0)
        cout<coefarray[0]<<"+" ;
    for(i=1; i<=L->highpower; i++){
        if(L->coefarray[i] == 0);           
        else 
        {
            if(L->coefarray[i] < 0)printf("\b");//系数小于0,回删 
                
            cout<coefarray[i]<<"x^"<highpower)cout<<"+" ;//不是最后一项输出+ 
        }
    }
    printf("\b");
} 
void AddPolyn(polynomial *L1, polynomial *L2, polynomial *L3)
{
    int i;
    if(L1->highpower > L2->highpower)
        L3->highpower = L1->highpower;
    else 
        L3->highpower = L2->highpower;
    
    for(i=0; i<=L3->highpower; i++){
        L3->coefarray[i] = L1->coefarray[i]+L2->coefarray[i];
    }
} 
void SubPolyn(polynomial *L1, polynomial *L2, polynomial *L3)
{
    int i;
    if(L1->highpower > L2->highpower)// 
        L3->highpower = L1->highpower;
    else
        L3->highpower = L2->highpower;  
    for(i=0; i<=L3->highpower; i++){
        L3->coefarray[i] = L1->coefarray[i]-L2->coefarray[i];
    }
}
void MultPolyn(polynomial *L1, polynomial *L2, polynomial *L3)
{
    int i, j;
    L3->highpower = L1->highpower+L2->highpower;//指数相加 
    for(i=0; i<=L3->highpower; i++){
        for(j=0; j<=L3->highpower; j++){
            L3->coefarray[i+j] += L1->coefarray[i]*L2->coefarray[j];//系数相乘 
            PrintPolyn(L3);cout<<"\n";
        }PrintPolyn(L3);cout<<"\n\n";
    }
}
void meau()
{
    cout<<"----------------------------\n";
    cout<<"     1.创建多项式\n";
    cout<<"     2.多项式相加\n";
    cout<<"     3.多项式相减\n";
    cout<<"     4.多项式相乘\n";
    cout<<"     0.退出\n";
    cout<<"----------------------------\n";
}
int main()
{
    polynomial *L1, *L2, *L3;
    L1 = (polynomial*)malloc(sizeof(polynomial));
    L2 = (polynomial*)malloc(sizeof(polynomial));
    L3 = (polynomial*)malloc(sizeof(polynomial));
    meau();
    int a;
    while(1)
    {
        printf("请输入要进行的操作:");
        scanf("%d", &a);
        switch(a)
        {
            case 1:         
                printf("正在创建多项式!\n");               
                CreatPolyn(L1);
                cout<<"第一个多项式为:";PrintPolyn(L1);
                cout<<"\n\n";               
                CreatPolyn(L2);
                cout<<"第二个多项式为:";PrintPolyn(L2);
                cout<<"\n";             
                cout<<"创建完毕!!\n\n";
                break;
            case 2:         
                NewPolyn(L3);           
                cout<<"相加的结果为:";    
                AddPolyn(L1, L2, L3);
                PrintPolyn(L3);
                cout<<"\n\n";
                break;
            case 3:
                NewPolyn(L3);   
                cout<<"相减的结果为:";    
                SubPolyn(L1, L2, L3);
                PrintPolyn(L3);
                cout<<"\n\n";
                break;
            case 4:
                NewPolyn(L3);
                cout<<"相乘的结果为:";
                MultPolyn(L1, L2, L3);
                PrintPolyn(L3);
                cout<<"\n\n";
                break;
        }
    }   
    return 0;
}
image.png

image.png

image.png

image.png

你可能感兴趣的:(大作业 一元多项式运算 顺序表)