复数计算器

基本要求:

        为复数定义一个类。重载输入和输出运算符>>和<<。实现复数的+,-,*,/等运算。输出复数时,应对实部和虚部的各种情况加以讨论。以便能输出2,2+3i,2-3i,5i,-8i,i与-i这样的复数。

        复数的四则运算可以按如下方式进行:

复数计算器

 

  1 #include<iostream>

  2 using namespace std;

  3 #include<windows.h>

  4 

  5 //复数类

  6 class complexnumber

  7 {

  8 private:

  9     float real;

 10     float imag;

 11 public:

 12     friend istream& operator >>(istream& in,complexnumber &temple);//重载输入运算符

 13     friend ostream& operator <<(ostream& out,complexnumber &temple);//重载输出运算符

 14     friend complexnumber operator +(complexnumber t1,complexnumber t2);//重载加法运算符

 15     friend complexnumber operator -(complexnumber t1,complexnumber t2);//重载减法运算符

 16     friend complexnumber operator *(complexnumber t1,complexnumber t2);//重载乘法运算符

 17     friend complexnumber operator /(complexnumber t1,complexnumber t2);//重载除法运算符

 18 };

 19 

 20 //重载输入运算符

 21 istream& operator >>(istream& in,complexnumber &temple)

 22 {

 23     cout<<"实部    虚部"<<endl;

 24     in>>temple.real>>temple.imag;

 25     return in;

 26 }

 27 

 28 //重载输出运算符

 29 ostream& operator <<(ostream& out,complexnumber &temple)

 30 {

 31     if(temple.real==0)

 32     {

 33         if(temple.imag==0)

 34         {

 35             cout<<0<<endl;

 36         }

 37         else if(temple.imag==1)

 38         {

 39             out<<"i"<<endl;

 40         }

 41         else if(temple.imag==-1)

 42         {

 43             out<<"-i"<<endl;

 44         }

 45         else

 46         {

 47             out<<temple.imag<<"i"<<endl;

 48         }

 49     }

 50     else

 51     {

 52         out<<temple.real;

 53         if(temple.imag>0)

 54         {

 55             out<<"+";

 56         }

 57         if(temple.imag!=0)

 58         {

 59             out<<temple.imag<<"i"<<endl;

 60         }

 61     }

 62     return out;

 63 }

 64 

 65 //重载加法运算符

 66 complexnumber operator +(complexnumber t1,complexnumber t2)

 67 {

 68     complexnumber temple;

 69     temple.real=t1.real+t2.real;

 70     temple.imag=t1.imag+t2.imag;

 71     return temple;

 72 }

 73 

 74 //重载减法运算符

 75 complexnumber operator -(complexnumber t1,complexnumber t2)

 76 {

 77     complexnumber temple;

 78     temple.real=t1.real-t2.real;

 79     temple.imag=t1.imag-t2.imag;

 80     return temple;

 81 }

 82 

 83 //重载乘法运算符

 84 complexnumber operator *(complexnumber t1,complexnumber t2)

 85 {

 86     complexnumber temple;

 87     temple.real=(t1.real*t2.real)-(t1.imag*t2.imag);

 88     temple.imag=(t1.imag*t2.real)+(t1.real*t2.imag);

 89     return temple;

 90 }

 91 

 92 //重载除法运算符

 93 complexnumber operator /(complexnumber t1,complexnumber t2)

 94 {

 95     complexnumber temple;

 96     temple.real=((t1.real*t2.real)+(t1.imag*t2.imag))/((t2.real*t2.real)+(t2.imag*t2.imag));

 97     temple.imag=((t1.imag*t2.real)-(t1.real*t2.imag))/((t2.real*t2.real)+(t2.imag*t2.imag));

 98     return temple;

 99 }

100 

101 int main()

102 {

103     system("color f3");

104     int select;

105     complexnumber ob1,ob2,ob;

106     do

107     {

108         cout<<"*******************************"<<endl;//菜单

109         cout<<"*         复数计算器         *"<<endl;

110         cout<<"*******************************"<<endl;

111         cout<<"*1------------------------加法*"<<endl;

112         cout<<"*2------------------------减法*"<<endl;

113         cout<<"*3------------------------乘法*"<<endl;

114         cout<<"*4------------------------除法*"<<endl;

115         cout<<"*0------------------------退出*"<<endl;

116         cout<<"*******************************"<<endl;

117         cout<<"请输入你的选择:";

118         cin>>select;

119         switch(select)

120         {

121             case 1: cout<<endl<<"加法:"<<endl;

122                     cout<<"请输入第一个复数的值:"<<endl;

123                     cin>>ob1;

124                     cout<<"第一个复数为:"<<ob1<<endl;

125                     cout<<"请输入第二个复数的值:"<<endl;

126                     cin>>ob2;

127                     cout<<"第二个复数为:"<<ob2<<endl;

128                     system("pause");

129                     ob=ob1+ob2;

130                     system("cls");

131                     cout<<"第一个复数为:"<<ob1<<endl;

132                     cout<<"第二个复数为:"<<ob2<<endl;

133                     cout<<"两数之和为:"<<ob<<endl;

134                     system("pause");

135                     system("cls");

136                 break;

137             case 2: cout<<endl<<"减法:"<<endl;

138                     cout<<"请输入第一个复数的值:"<<endl;

139                     cin>>ob1;

140                     cout<<"第一个复数为:"<<ob1<<endl;

141                     cout<<"请输入第二个复数的值:"<<endl;

142                     cin>>ob2;

143                     cout<<"第二个复数为:"<<ob2<<endl;

144                     system("pause");

145                     ob=ob1-ob2;

146                     system("cls");

147                     cout<<"第一个复数为:"<<ob1<<endl;

148                     cout<<"第二个复数为:"<<ob2<<endl;

149                     cout<<"两数之差为:"<<ob<<endl;

150                     system("pause");

151                     system("cls");

152                 break;

153             case 3: cout<<endl<<"乘法:"<<endl;

154                     cout<<"请输入第一个复数的值:"<<endl;

155                     cin>>ob1;

156                     cout<<"第一个复数为:"<<ob1<<endl;

157                     cout<<"请输入第二个复数的值:"<<endl;

158                     cin>>ob2;

159                     cout<<"第二个复数为:"<<ob2<<endl;

160                     system("pause");

161                     ob=ob1*ob2;

162                     system("cls");

163                     cout<<"第一个复数为:"<<ob1<<endl;

164                     cout<<"第二个复数为:"<<ob2<<endl;

165                     cout<<"两数之积为:"<<ob<<endl;

166                     system("pause");

167                     system("cls");

168                 break;

169             case 4: cout<<endl<<"除法:"<<endl;

170                     cout<<"请输入第一个复数的值:"<<endl;

171                     cin>>ob1;

172                     cout<<"第一个复数为:"<<ob1<<endl;

173                     cout<<"请输入第二个复数的值:"<<endl;

174                     cin>>ob2;

175                     cout<<"第二个复数为:"<<ob2<<endl;

176                     system("pause");

177                     ob=ob1/ob2;

178                     system("cls");

179                     cout<<"第一个复数为:"<<ob1<<endl;

180                     cout<<"第二个复数为:"<<ob2<<endl;

181                     cout<<"两数之商为:"<<ob<<endl;

182                     system("pause");

183                     system("cls");

184                 break;

185         }

186     }while(select!=0);

187     system("cls");

188     cout<<"欢迎使用!!"<<endl;

189     return 0;

190 }

 

你可能感兴趣的:(计算器)