課題1 C++版
fukusosu.h
#include <iostream>
class Fukusosu {
private:
double real;
double imaginary;
public:
Fukusosu();
Fukusosu(double _r, double _i);
Fukusosu operator*(const Fukusosu& b)const ;
Fukusosu kyoyaku() const;
Fukusosu operator/(const Fukusosu& b) const ;
Fukusosu operator=(const Fukusosu& b);
friend std::ostream& operator<<(std::ostream& os,const Fukusosu& b);
};
kadai11.cpp
#include <iostream>
#include "fukusosu.h"
int main(){
Fukusosu f[]={{1.0,0},{0,-2.0},{-3.0,4.0},{5.0,-6.0},{0.0,0.0}};
for(int i=0; i<(sizeof f)/(sizeof f[0]); ++i){
std::cout << f[i] << std::endl;
}
}
kadai12.cpp
#include <iostream>
#include "fukusosu.h"
int main(){
Fukusosu f[]={{1.0,0},{0,-2.0},{-3.0,0},{5.0,-6.0},{0.0,0.0}};
Fukusosu g[]={{2.0,0},{0,-5.0},{0,4.0},{-2.0,1.0},{0.0,0.0}};
for(int i=0; i<(sizeof f)/(sizeof f[0]); ++i){
std::cout << "(" << f[i] << ") * (" << g[i] << ") = "
<< (f[i] * g[i])
<< std::endl;
}
}
kadai13.cpp
#include <iostream>
#include "fukusosu.h"
int main(){
Fukusosu f[]={{1.0,0},{0,-2.0},{-3.0,4.0},{5.0,-6.0},{0.0,0.0}};
for(int i=0; i<(sizeof f)/(sizeof f[0]); ++i){
std::cout << f[i] << "の共役は" << f[i].kyoyaku() << std::endl;
}
}
kadai14.cpp
#include <iostream>
#include "fukusosu.h"
int main(){
Fukusosu f[]={{1.0,0},{0,-2.0},{-3.0,0},{5.0,-6.0},{0.0,0.0}};
Fukusosu g[]={{2.0,0},{0,-5.0},{0,4.0},{-2.0,1.0},{0.0,1.0}};
for(int i=0; i<(sizeof f)/(sizeof f[0]); ++i){
std::cout << "(" << f[i] << ") / (" << g[i] << ") = "
<< (f[i] / g[i]) << std::endl;
}
}
fukusosu.cpp
#include <iostream>
#include "fukusosu.h"
Fukusosu::Fukusosu(double _r, double _i):real(_r),imaginary(_i){}
Fukusosu::Fukusosu():real(0),imaginary(0){}
Fukusosu Fukusosu::operator=(const Fukusosu& b){
real = b.real;
imaginary = b.imaginary;
return *this;
}
Fukusosu Fukusosu::operator*(const Fukusosu& b) const {
Fukusosu result(real * b.real - imaginary * b.imaginary,
real * b.imaginary + imaginary * b.real);
return result;
}
Fukusosu Fukusosu::kyoyaku() const{
Fukusosu result(real,-imaginary);
return result;
}
Fukusosu Fukusosu::operator/(const Fukusosu& b) const {
Fukusosu k = b * b.kyoyaku();
Fukusosu result = *this * b.kyoyaku();
result.real /= k.real;
result.imaginary /= k.real;
return result;
}
std::ostream& operator<<(std::ostream& os, const Fukusosu& b){
if((b.real!=0)||(b.imaginary==0)){
os << b.real << std::showpos ;
}
if(b.imaginary!=0){
os << b.imaginary << 'i';
}
os << std::noshowpos;
return os;
}
坂本直志 <[email protected]>
東京電機大学工学部情報通信工学科