c++でArrayを使って多項式の計算(足し算と掛け算)をするプログラムを書いています。
<期待する結果>
例えば:
多項式a=4+2xー4x^2ー6x^3
多項式b=3−2x+4x^2+6x^3
であった時に、これら2つの式を足した結果と掛けた結果を表示させたいです。
<自分が書いたコード>
c++
1#include <iostream> 2#include <cmath> 3using namespace std; 4 5struct Poly{ 6 int degree; 7 int* coeffs; 8}; 9 10//function prototype 11Poly* readPoly(); 12void outputPoly(const Poly* p, char x); 13Poly* addPoly(const Poly* a, const Poly* b); 14Poly* multPoly(const Poly* a, const Poly* b); 15void deletePoly(Poly* &p); 16 17int main(){ 18 19 //ask user to enter a polynominal and call function for reading 20 cout<<"Enter a polynominal: "<<endl; 21 Poly* a = readPoly(); 22 23 //ask user to enter another polynominal and call function for reading 24 cout<<"Enter another polynominal: "<<endl; 25 Poly* b = readPoly(); 26 27 //print first polynominal 28 cout<<"a="; 29 outputPoly(a,'x'); 30 cout<<endl; 31 32 //print second polynominal 33 cout<<"b="; 34 outputPoly(b,'x'); 35 cout<<endl; 36 37 //print result for addition 38 Poly* ad = addPoly(a,b); 39 cout<<"addition: "; 40 outputPoly(ad,'x'); 41 cout<<endl; 42 43 //print result for multiplication 44 Poly* m = multPoly(a,b); 45 cout<<"multiply: "; 46 outputPoly(m,'x'); 47 cout<<endl; 48 49 return 0; 50} 51 52Poly* readPoly(){ 53 int deg; 54 int* coefficient; 55 56 //ask user to enter a degree 57 cout<<"Degree: "<<endl;; 58 cin>>deg; 59 60 //create array 61 coefficient = new int[deg+1]; 62 63 //fill array 64 cout<<"Coefficients: "<<endl; 65 for(int i=0; i<=deg; i++){ 66 cin>>coefficient[i]; 67 } 68 69 Poly* p; 70 p = new Poly; 71 p->degree = deg; 72 p->coeffs = coefficient; 73 74 return p; 75} 76 77void outputPoly(const Poly* p, char x){ 78 79 //print polynominals 80 for(int i=1; i<=p->degree; i++){ 81 //when coefficient is zero 82 if(p->coeffs[i]==0) 83 cout<<p->coeffs[i]; 84 //when coefficient is positive num, add plus sign 85 else if(p->coeffs[i]>0) 86 cout<<"+"<<p->coeffs[i]<<"*"<<x<<"^"<<i; 87 //when coefficient is negative num, add minus sign 88 else if(p->coeffs[i]<0) 89 cout<<"-"<<p->coeffs[i]<<"*"<<x<<"^"<<i; 90 //when coefficient is 1 91 else if(p->coeffs[i]==1) 92 cout<<"+"<<x<<"^"<<i; 93 //any other cases 94 else 95 cout<< *(p->coeffs); 96 } 97} 98 99Poly* addPoly(const Poly* a, const Poly* b){ 100 101 102} 103 104Poly* multPoly(const Poly* a, const Poly* b){ 105 int i,j; 106 i=0; 107 j=0; 108 109 if(a==0) 110 return b; 111 else if(b==0) 112 return a; 113 114 Poly* res; 115 res = new Poly; 116 res->degree = a->degree+b->degree; 117 118 int* c = new int[res->degree-1]; 119 120 while(i!=res->degree){ 121 while(j!=res->degree) 122 c[i+j]+=a->c[i]*b->c[j]; 123 i++; 124 j++; 125 } 126 127 res->coeffs = c; 128 129 return res; 130} 131 132void deletePoly(Poly* &p){ 133 delete[]p->coeffs; 134 delete p; 135 p=NULL; 136} 137
<問題点>
1)肝心のaddPolyとmultPolyのところをどう書いたら良いかそもそもわからず困っています。
2)自分でmultPolyを途中まで書いてみたのですが、多項式aもしくはbが0であった場合に0でない方の多項式を戻り値としたいのですが、if文でreturn aとreturn bと書いたところエラーが表示されどう直せば良いかわかりません。
c++
1Poly* multPoly(const Poly* a, const Poly* b){ 2 int i,j; 3 i=0; 4 j=0; 5 6 if(a==0) 7 return b; 8 else if(b==0) 9 return a; 10 11 Poly* res; 12 res = new Poly; 13 res->degree = a->degree+b->degree; 14 15 int* c = new int[res->degree-1]; 16 17 while(i!=res->degree){ 18 while(j!=res->degree) 19 c[i+j]+=a->c[i]*b->c[j]; 20 i++; 21 j++; 22 } 23 24 res->coeffs = c; 25 26 return res; 27 28}
エラー文:cannot initialize return object of type 'Poly*' with an lvalue of type 'const Poly*'
初心者です。よろしければアドバイスをいただけませんか?
回答1件
あなたの回答
tips
プレビュー