欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++實(shí)現(xiàn)多項(xiàng)式相乘

 更新時(shí)間:2022年07月22日 15:13:11   作者:只圖成果  
這篇文章主要介紹了C++實(shí)現(xiàn)多項(xiàng)式相乘方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

C++多項(xiàng)式相乘

在這里插入圖片描述

 

在這里插入圖片描述

在這里插入圖片描述

#include <iostream>
using namespace std;
int a[2][2]; //二維數(shù)組開的秒
int b[2][2];
int ans[25]; //用來存放系數(shù),那么存放后所對應(yīng)的下標(biāo)就是指數(shù)
int main(){
    for(int i=0;i<2;i++){
        for(int j=0;j<2;j++){
            cin >> a[i][j];
        }
    }
    for(int i=0;i<2;i++){
        for(int j=0;j<2;j++){
            cin >> b[i][j];
        }
    }
    for(int i=0;i<2;i++){
        for(int j=0;j<2;j++){
            ans[a[i][1] + b[j][1]] += a[i][0] * b[j][0];
            //冪次就是對應(yīng)的下標(biāo)
        } //累加的原因是,因?yàn)槭莾蓚€(gè)相加的式子相乘,所以要合并冪次相同的項(xiàng)
    }
    for(int i = 20;i>=0;i--){
        if(ans[i] != 0){
            cout << ans[i] << " " << i << endl;
        }
    }
    return 0;
}

C++多項(xiàng)式的乘法和加法

多項(xiàng)式的乘法和加法

采用動態(tài)數(shù)組的方法

該方法較鏈?zhǔn)椒椒晕?fù)雜

#include<iostream>
? ?using namespace std;
? ?//多項(xiàng)式的乘法和加法
? ?struct node{
? ? int coef;
? ? int exp;
? ?};
? ?
? ? //****排序****
?void ?nodesort(node* pn,const int& count)
?{ ? if(count<=1) return;
? else{
? bool flag =false;
? for(int i=0;i<count-1&&!flag;++i){
? ?flag = true;
? ? for(int j=1;j<count-i;++j){
? ? ?node t;
? ?if(pn[j-1].exp<pn[j].exp) {
? ? t = pn[j];
? ? pn[j] = pn[j-1];
? ? pn[j-1] = t;
? ? flag = false;
? ?}?
? ? }
? }?
? }
?}
?
?//****輸出****?
? ?void print( node *s,const int& n)
? ?{ ??
? ?cout<<"*********output*********\n";
? ? for(int i=0;i<n;++i)
? ? { ? if(i!=n-1)
? ? ?cout<*<s[i].coef<<"x^"<<s[i].exp<<" + ";
? ? ?else cout<<s[i].coef<<"x^"<<s[i].exp<<endl;
? ? }
? ? cout<<endl;?
?}
//****合并同類項(xiàng)****
?int nodemerge(node* s,const int& n ,const int& key=0){
? if(n<1) {cerr<<"數(shù)組大小有誤\n";}
? if(n==1)return 1;
? if(n>1 && key==0){//排序并且合并?
? ?nodesort(s,n);?
? ?int count=0;
? for(int i=1;i<n;++i)
? {
? ?if(s[count].exp==s[i].exp){
? ? s[count].coef = s[count].coef + s[i].coef ;
? ??
? ?}
? ?else{
? ? s[++count] = s[i];
? ?}
? }
? return count+1;?
? }
??
? if(n>1&&key==1){//僅合并?
? ?//nodesort(s,n);?
? ?int count=0;
? for(int i=1;i<n;++i)
? {
? ?if(s[count].exp==s[i].exp){
? ? s[count].coef = s[count].coef + s[i].coef ;
? ??
? ?}
? ?else{
? ? s[++count] = s[i];
? ?}
? }
? return count+1;?
? }
?} ??

//***計(jì)算多項(xiàng)式加法***?
? ?void add(node* s,const int& m,node* a,const int& n)
? ? ? ?{
? ? ?node* newnode = new node[m+n];
? ? ?int i=0,j=0,temp=0;
? ? ?
? ? ?while(i<m && j<n){
? ? ? if(s[i].exp>a[j].exp){
? ? ? ?newnode[temp].coef = s[i].coef;
? ? ? ?newnode[temp].exp = s[i].exp;
? ? ? ?temp++;
? ? ? ?i++;
? ? ? ?
? ? ? }
? ? ??
? ? else if(s[i].exp<a[j].exp){
? ? ? ?newnode[temp].coef = a[j].coef;
? ? ? ?newnode[temp].exp = a[j].exp;
? ? ? ?temp++;
? ? ? ?j++;
? ? ? ?
? ? ? }
? ? else {
? ? ? ?newnode[temp].coef = a[j].coef+s[i].coef;
? ? ? ?newnode[temp].exp = a[j].exp;
? ? ? ?temp++;
? ? ? ?i++;
? ? ? ?j++;
? ? ? }
? ? ?}
? ? while(i<m)
? ? {
? ? newnode[temp].coef = s[i].coef;
? ? ? newnode[temp].exp = s[i].exp;
? ? ? ?temp++;
? ? ? ?i++;?
? ? }
? while(j<n)
? ? {
? ? ?newnode[temp].coef = a[j].coef;
? ? ? ?newnode[temp].exp = a[j].exp;
? ? ? ?temp++;
? ? ? ?j++;
? ? }
? ??
? ? ? ? ? ? temp = nodemerge(newnode,temp,1);
? ? ? ? ? ? cout<<"多項(xiàng)式加法\n";
? ? ?print(newnode,temp);
? ? ?delete[] newnode;
? ? ?return ;
? ? ?
?}

//***計(jì)算多項(xiàng)式乘法***?
? ?void ? multi(node* s,const int& m,node* a,const int& n)
?{
? ? node* pn = new node[m*n];
? ? int count = 0;
? ? for(int i=0;i<m;++i)
? ? {
? ? ?for(int j=0;j<n;++j){
? ? ? pn[count].coef = (s[i].coef) * (a[j].coef) ;
? ? ? pn[count].exp = s[i].exp + a[j].exp;
? ? ? count++;
? ? ?}
? ? }
? //***排序并且合并***
? ? ? count = nodemerge(pn,count,0);
? ? ? cout<<"多項(xiàng)式乘法\n";
? ? ? print(pn,count);
? ? ?delete[] pn;
? ? ?return ;
?}
?//****輸入數(shù)據(jù)*****
?node* node_input(const int& n)
?{ ?
? node* seq = new node[n];?
? for(int i=0;i<2*n;++i)
? ? {?
? ??
? ? ?if(i%2==0) cin>>seq[i/2].coef;
? ? ?else cin>>seq[i/2].exp;
? ?}?
? ?return seq;
?} ?
??
? ? //***銷毀****
?void delete_node(node*s){
? delete[] s;
?}?


//**測試**
?int main(){
? ? //m,n表示輸入的節(jié)點(diǎn)個(gè)數(shù)
? //示例:3x^6+4x^4+x 輸入的個(gè)數(shù)為3,每個(gè)節(jié)點(diǎn)分別為:3 6; 4 4; *1 *1?
? ? int m,n;
? ? int temp;
? ? node* seq1,*seq2;
? ??
? ? cout<<"input m value:";
? ? ? ?cin>>m;
? ? ? ?seq1 = node_input(m);?
? ? ? ?
? ? ? ?cout<<"input n value:";
? ? ? ?cin>>n;
? ? ? ?seq2 = node_input(n);?
? ? ?
? ? ? //***排序并且合并***?
? ? ? m=nodemerge(seq1,m);
? ? ? n =nodemerge(seq2,n);
? ? ?
? ? ?//test
? ? ?print(seq1,m);
? ? ?print(seq2,n);
? ? ?multi(seq1,m,seq2,n);
? ? ?add(seq1,m,seq2,n);
? ? ?
? ? ?//delete
? ? ?delete_node(seq1);
? ? ?delete_node(seq2);
? ? return 0;
? ?}

樣例測試輸出

input m value:3
1 2
1 3
2 4
input n value:4
3 5
3 76
3 4
2 5
*********output*********
2x^4 + 1x^3 + 1x^2

*********output*********
3x^76 + 5x^5 + 3x^4

多項(xiàng)式乘法
*********output*********
6x^80 + 3x^79 + 3x^78 + 10x^9 + 11x^8 + 8x^7 + 3x^6

多項(xiàng)式加法
*********output*********
3x^76 + 5x^5 + 5x^4 + 1x^3 + 1x^2

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論