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



#include <iostream>
using namespace std;
int a[2][2]; //二維數(shù)組開(kāi)的秒
int b[2][2];
int ans[25]; //用來(lái)存放系數(shù),那么存放后所對(duì)應(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];
//冪次就是對(duì)應(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)式的乘法和加法
采用動(dò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;
?}?
//**測(cè)試**
?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;
? ?}樣例測(cè)試輸出
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)文章
c++ builder TreeView控件節(jié)點(diǎn)遍歷代碼
這篇文章介紹了c++ builder TreeView控件節(jié)點(diǎn)遍歷代碼,有需要的朋友可以參考一下2013-09-09
C++實(shí)現(xiàn)LeetCode(18.四數(shù)之和)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(18.四數(shù)之和),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
詳解C語(yǔ)言中printf輸出的相關(guān)函數(shù)
這篇文章主要介紹了C語(yǔ)言中printf輸出的相關(guān)函數(shù)總結(jié),是C語(yǔ)言入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-08-08
C++實(shí)現(xiàn)掃雷小游戲(控制臺(tái))
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)掃雷小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
C語(yǔ)言實(shí)現(xiàn)餐飲結(jié)賬管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)餐飲結(jié)賬管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
C++基于QWidget和QLabel實(shí)現(xiàn)圖片縮放,拉伸與拖拽
這篇文章主要為大家詳細(xì)介紹了C++如何基于QWidget和QLabel實(shí)現(xiàn)圖片縮放、拉伸與拖拽等功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02

