精華區beta C_and_CPP 關於我們 聯絡資訊
#include<iostream.h> #include<stdio.h> #include<conio.h> class complex { double real,exp; public: complex(double r=0 , double e=0) { real=r; exp=e; } friend complex operator+(complex p, complex q) // 加法 { return complex(p.real+q.real , p.exp+q.exp);} friend complex operator+(complex p , float c) { return complex(p.real+c , p.exp+c);} friend complex operator+(float c , complex p) { return complex(c+p.real , c+p.exp);} friend complex operator-(complex p , complex q) // 減法 { return complex(p.real-q.real , p.exp-q.exp);} friend complex operator-(complex p , double c) { return complex(p.real-c , p.exp-c);} friend complex operator-(double c , complex p) { return complex(c-p.real , c-p.exp);} friend complex operator*(complex p , complex q) // 乘法 { return complex(p.real*q.real-p.exp*q.exp , p.real*q.exp+p.exp*q.real);} friend complex operator*(complex p , double c) { return complex(p.real*c , p.exp*c);} friend complex operator*(double c , complex p) { return complex(c*p.real , c*p.exp);} friend complex operator/(complex p , complex q) // 除法 { return complex((p.real*q.real+p.exp*q.exp) /(q.real+q.real+q.exp*q.exp) , (p.exp*q.real-p.real*q.exp) /(q.real*q.real+q.exp*q.exp));} friend complex operator/(complex p , double c) { return complex(p.real/c , p.exp/c);} friend complex operator/(double c , complex p) { return complex(c/p.real , c/p.exp);} void print(); }; void main() { clrscr(); cout<<'\n'; complex w(6,5) , r(4,3) , p; cout<<" w="; w.print(); cout<<" r="; r.print(); cout<<'\n'; p=w+r; cout<<" p=w+r="; p.print(); p=p+3.0; cout<<" p+3.0="; p.print(); p=3.0+p; cout<<" 3.0+p="; p.print(); cout<<'\n'; p=w-r; cout<<" p=w-r="; p.print(); p=p-3.0; cout<<" p-3.0="; p.print(); p=3.0-p; cout<<" 3.0-p="; p.print(); cout<<'\n'; p=w*r; cout<<" p=w*r="; p.print(); p=r*5.4; cout<<" r*5.4="; p.print(); p=5.4*r; cout<<" 5.4*r="; p.print(); cout<<'\n'; p=w/r; cout<<" p=w/r="; p.print(); p=r/5.4; cout<<" r/5.4="; p.print(); p=5.4/r; cout<<" 5.4/r="; p.print(); } void complex::print() // 輸出 { //cout.setf(ios::showpos); //cout.width(5); //cout.precision(2); //cout.setf(ios::showpoint); //cout<<"("<<real<<','<<exp<<")"<<'\n'; printf("(%5.2f,%5.2f)\n",real,exp); } ------------------------------------ 輸出結果 w=( 6.00, 5.00) r=( 4.00, 3.00) p=w+r=(10.00, 8.00) p+3.0=(13.00,11.00) 3.0+p=(16.00,14.00) p=w-r=( 2.00, 2.00) p-3.0=(-1.00,-1.00) 3.0-p=( 4.00, 4.00) p=w*r=( 9.00,38.00) r*5.4=(21.60,16.20) 5.4*r=(21.60,16.20) p=w/r=( 2.29, 0.08) r/5.4=( 0.74, 0.56) 5.4/r=( 1.35, 1.80) -- 我是pepsㄚ~~ 歡迎參觀,謝謝使用....CcC -- Origin: ︿︱︿ 小魚的紫色花園 fpg.m4.ntu.edu.tw (140.112.214.200)