大数处理复数——C++实现
大数处理复数——C++实现
继博主处理大数加法、大数减法、大数乘法(大数除法暂未实现,不了解博主大数实现点这里: http://www.cnblogs.com/hugeNumber/articles/5376906.html)之后的另一篇有关大数处理复数的博文。同样风格,首先了解大数处理机理。
一、解题思路
核心思想
由于大数实现,需要大量分配动态内存,而且实现机理比较复杂,运算量巨大。本文 复数处理同样提供复数常规处理(即double类),预处理命令#define DOUBLE
方便了常规复数处理与复数的大数处理间切换。
//复数类:
class complex{
private: numbertype real;//实部 numbertype imag;//虚部
public:
complex(){}
complex(numbertype r, numbertype i):real(r),imag(i)//构造函数
{}
complex(complex &obj)//拷贝构造函数
{
this->real = obj.real; //实部
this->imag = obj.imag; //虚部
}
complex (numbertype real)
{
this->real = real;
imag = 0;
}
#ifndef DOUBLE
complex (char * nptr)
{
numbertype tmp(nptr);
this->real = tmp;
imag = 0;
}
#endif
//函数实现加减乘除,不提倡使用
friend complex add(complex &a, complex &b);//加
friend complex sub(complex &a, complex &b);//减
friend complex mul(complex &a, complex &b);//乘
friend complex div(complex &a, complex &b);//除
//运算符重载
friend complex operator+(complex &a, complex &b);
friend complex operator-(complex &a, complex &b);
friend complex operator*(complex &a, complex &b);
friend complex operator/(complex &a, complex &b);
friend ostream & operator<<(ostream &out, complex &obj);
};
二、具体实现
1 复数类成员函数实现
#include <string> #include <cctype> #include <algorithm> #include <iostream> using namespace std; //复数常规处理与大数处理切换 typedef hugeNumber numbertype; //typedef double numbertype; //#define DOUBLE class complex{ numbertype real;//实部 numbertype imag;//虚部 public: complex(){} complex(numbertype r, numbertype i):real(r),imag(i) {} complex(complex &obj) { this->real = obj.real; //实部 this->imag = obj.imag; //虚部 } complex (numbertype real) { this->real = real; imag = 0; } #ifndef DOUBLE complex (char * nptr) { numbertype tmp(nptr); this->real = tmp; imag = 0; } #endif friend complex add(complex &a, complex &b);//加 friend complex sub(complex &a, complex &b);//减 friend complex mul(complex &a, complex &b);//乘 friend complex div(complex &a, complex &b);//除 //运算符重载 friend complex operator+(complex &a, complex &b); friend complex operator-(complex &a, complex &b); friend complex operator*(complex &a, complex &b); friend complex operator/(complex &a, complex &b); friend ostream & operator<<(ostream &out, complex &obj); }; //加 complex add(complex &a, complex &b) { return complex(a.real + b.real, a.imag + b.imag); } complex operator+(complex &a, complex &b) { return complex(a.real + b.real, a.imag + b.imag); } //减 complex sub(complex &a, complex &b) { return complex(a.real - b.real, a.imag - b.imag); } complex operator-(complex &a, complex &b) { return complex(a.real - b.real, a.imag - b.imag); } //乘 complex mul(complex &a, complex &b) { return complex(a.real * b.real - a.imag * b.imag , a.imag * b.real + a.real * b.imag); } complex operator*(complex &a, complex &b) { return complex(a.real * b.real - a.imag * b.imag , a.imag * b.real + a.real * b.imag); } //除 #ifdef DOUBLE complex div(complex &a, complex &b) { numbertype tmreal, tmimage, tmp; tmp = b.real * b.real + b.imag * b.imag; //tmreal = (a.real * b.real + a.imag * b.imag)/(b.real * b.real + b.imag * b.imag); //tmimage = (a.imag * b.real - a.real * b.imag)/(b.real * b.real + b.imag * b.imag); tmreal = (a.real * b.real + a.imag * b.imag)/tmp; tmimage = (a.imag * b.real - a.real * b.imag)/tmp; return complex(tmreal, tmimage); } complex operator/(complex &a, complex &b) { numbertype tmreal, tmimage, tmp; tmp = b.real * b.real + b.imag * b.imag; //tmreal = (a.real * b.real + a.imag * b.imag)/(b.real * b.real + b.imag * b.imag); //tmimage = (a.imag * b.real - a.real * b.imag)/(b.real * b.real + b.imag * b.imag); tmreal = (a.real * b.real + a.imag * b.imag)/tmp; tmimage = (a.imag * b.real - a.real * b.imag)/tmp; return complex(tmreal, tmimage); } ostream & operator<<(ostream &out, complex &obj) { out<<obj.real; if(obj.imag > 0)out<<"+"; out<<obj.imag<<" i"; return out; } #endif #ifndef DOUBLE ostream & operator<<(ostream &out, complex &obj) { out<<obj.real; if(obj.imag.getsign() == 1) out<<"+"; out<<obj.imag<<" i"; return out; } #endif //friend complex add(complex &a, complex &b);//加 //friend complex sub(complex &a, complex &b);//减 //friend complex mul(complex &a, complex &b);//乘 //friend complex div(complex &a, complex &b);//除 ////运算符重载 //friend complex operator+(complex &a, complex &b); //friend complex operator-(complex &a, complex &b); //friend complex operator*(complex &a, complex &b); //friend complex operator/(complex &a, complex &b); //friend ostream & operator<<(ostream &out, complex &obj); //friend istream & operator>>(istream &in, complex &obj); //int __wdiv(int divisor, int dividend)//除数与被除数 //{ // int quotient;//商 // unsigned char counter, sign; // sign = 0; // if(divisor < 0) // { // divisor = - divisor; // sign = 1; // } // if(dividend < 0) // { // dividend = - dividend; // sign = 1; // } // // quotient = 0; // if(divisor != 0) // { // counter = 1; // while((divisor & 0x8000U == 0)) // { // divisor <<= 1; // counter++; // } // do{ // quotient <<= 1; // if((unsigned int)divisor <=(unsigned int)dividend) // { // dividend -= divisor; // quotient |= 1; // } // *(unsigned int *)&divisor >>= 1; // }while(--counter != 0); // } // if(sign) // quotient = - quotient; // return quotient; //} // //int __wmul(int multiplier, int multiplicand) //{ // int product = 0; // do{ // if(multiplier & 1) // product += multiplicand; // multiplicand <<= 1; // multiplier >>= 1; // }while(multiplier != 0); // return product; //} void Input(hugeNumber &x, hugeNumber &y) { char str[512]; do{ cout<<"请输入一个实数(退出请输入esc):"<<endl; cin.clear(); //cin.fail()返回0; cin.sync(); //清空流 cin.getline(str,sizeof(str)); string s(str);//char * To string transform(s.begin(),s.end(),s.begin(), ::tolower);//大写转小写 const char *str1 = s.c_str();//string To char * if(strcmp(str1,"esc") == 0)exit(0); }while(!isNumber(str) ); x = hugeNumber(str); do{ cout<<"请输入一个虚数(退出请输入esc):"<<endl; cin.clear(); //cin.fail()返回0; cin.sync(); //清空流 cin.getline(str,sizeof(str)); string s(str);//char * To string transform(s.begin(),s.end(),s.begin(), ::tolower);//大写转小写 const char *str1 = s.c_str();//string To char * if(strcmp(str1,"esc") == 0)exit(0); }while(!isNumber(str) ); y = hugeNumber(str); } void Input(double &x, double &y) { char str[512]; do{ cout<<"请输入一个实数(esc:退出):"<<endl; cin>>str; string s(str);//char * To string transform(s.begin(),s.end(),s.begin(), ::tolower);//大写转小写 const char *str1 = s.c_str();//string To char * if(strcmp(str1,"esc") == 0)exit(0); }while( !Valid_Check_Double(str, &x)); do{ cout<<"请输入一个虚数(esc:退出):"<<endl; cin>>str; string s(str);//char * To string transform(s.begin(),s.end(),s.begin(), ::tolower);//大写转小写 const char *str1 = s.c_str();//string To char * if(strcmp(str1,"esc") == 0)exit(0); }while( !Valid_Check_Double(str, &y)); }
2 main测试函数:
int main() { //测试用 //numbertype x ("11111111.222222e32"); //numbertype y ("444444444.5555e37"); //numbertype x1("333333333333.4444e23"); //numbertype y1("6666666.777777e28"); numbertype x, y, x1, y1; cout<<"【请输入第一个复数:】"<<endl; Input(x, y); cout<<"【请输入第二个复数:】"<<endl; Input(x1, y1); complex a(x, y),b(x1, y1); cout<<"【您输入的两个复数分别为:】"<<endl; cout<<"a = "<<a<<endl<<"b = "<<b<<endl<<endl; cout<<endl<<"================================================================================"<<endl; //cout<<x-x1<<endl; //cout<<y-y1<<endl; ////测试一 //cout<<"a+b="<<add(a,b)<<endl; //cout<<"a-b="<<sub(a,b)<<endl; //cout<<"a*b="<<mul(a,b)<<endl; ////测试二 cout<<"a+b="<<a+b<<endl<<endl; cout<<"a+b+b="<<a+b+b<<endl<<endl; cout<<"a-b="<<a-b<<endl<<endl; cout<<"a*b="<<a*b<<endl; #ifdef DOUBLE cout<<"a/b="<<a/b<<endl; cout<<"a/b="<<div(a,b)<<endl; #endif /*hugeNumber a; cin>>a; cout<<"请输出结果:"<<endl<<a<<endl;*/ return 0; }
3 实验结果:

博文为作者原创,版权归云南师范大学信息学院所有,引用请注明出处!

浙公网安备 33010602011771号