SOLVE

다항식
#include <iostream>
#include <vector>
class Multi{
public:
Multi(std::vector<double>&& vec): dim(vec.size()){
data=new double[dim];
for(int i=0; i<dim; ++i){
data[i]=vec[i];
}
}
Multi(std::vector<double>& vec): dim(vec.size()){
data=new double[dim];
for(int i=0; i<dim; ++i){
data[i]=vec[i];
}
}
~Multi(){
delete[] data;
}
Multi operator+(Multi& other){
if(dim>=other.dim){
std::vector<double> tmp(dim);
for(int i=0; i<other.dim; ++i){
tmp[i]=data[i]+other.data[i];
}
for(int i=other.dim; i<dim; ++i){
tmp[i]=data[i];
}
Multi mul(tmp);
return mul;
}else{
std::vector<double> tmp(other.dim);
for(int i=0; i<dim; ++i){
tmp[i]=data[i]+other.data[i];
}
for(int i=dim; i<other.dim; ++i){
tmp[i]=other.data[i];
}
Multi mul(tmp);
return mul;
}
}
void print_format(void){
std::cout<<"MULTI:"<<'\n';
for(int i=0; i<dim; i++){
std::cout<<data[i]<<"x^"<<i;
if(i!=dim-1) std::cout<<'+';
}
std::cout<<'\n';
}
private:
int dim;
double* data;
};
int main(void){
Multi mul1(std::vector<double>{1.0, 2.0, 3.0});
Multi mul2(std::vector<double>{2.0, 3.0, 4.0, 5.0});
Multi mul3=mul1+mul2;
mul3.print_format();
return 0;
}
complex
#include <iostream>
class complex{
public:
complex(double r, double i): r(r), i(i){}
complex& operator=(complex& c){
r=c.r; i=c.i;
return *this;
}
complex operator+(complex& c){
return complex(r+c.r, i+c.i);
}
complex operator-(complex& c){
return complex(r-c.r, i-c.i);
}
complex operator*(complex& c){
return complex(r*c.r-i*c.i, r*c.i+i*c.r);
}
double& real(){ return r; }
double& imag(){ return i; }
private:
double r, i;
};
inline double& real(complex& c){ return c.real(); }
inline double& imag(complex& c){ return c.imag(); }
inline std::ostream& operator<<(std::ostream& os, complex& c){
return os<<'('<<real(c)<<", "<<imag(c)<<')'<<std::endl;
}
int main(void){
complex c1(1, 2);
complex c2(3, 5);
complex c3=c1+c2;
std::cout<<c3;
return 0;
}