程序设计基础实验报告(6)
时间:2025-07-14
时间:2025-07-14
case 2:outfile<<"to:";break; case 3:outfile<<"subject:";break; case 4:outfile<<"body:";break; default:cout<<"error!\n";exit(0); } state=0; infile.get(); while(infile.peek()!='<') { infile.get(ch); outfile.put(ch); } outfile<<endl; } } } cout<<"Convert correctly!"<<endl; }
测试数据(输入、输出)
第3_2题
算法描述:
设计一个复数类型,输入实部和虚部生成一个复数,可进行两个复数求和、两个复数求差、两个复数求积运算。 源程序: No3_2.cpp
#include<iostream.h> class complex {
public: complex(){set(0.0,0.0);} complex(double real, double imag){set(real,imag);} void set(double r, double i){real=r;imag=i;} friend complex operator +(const complex &c1,const complex &c2); friend complex operator -(const complex &c1,const complex &c2); friend complex operator *(const complex &c1,const complex &c2); void print(); private: double real, imag; };
void complex::print() { if(imag<0) cout<<real<<imag<<"i"<<endl; else cout<<real<<"+"<<imag<<"i"<<endl; }
complex operator +(const complex &c1,const complex &c2) { return complex(c1.real+c2.real,c1.imag+c2.imag); }
complex operator -(const complex &c1,const complex &c2) { return complex(c1.real-c2.real,c1.imag-c2.imag); }
complex operator *(const complex &c1,const complex &c2) { return complex(c1.real*c2.real-c1.imag*c2.imag, c1.real*c2.imag+c1.imag*c2.real); }
void main() { complex a,b,c; double r,i; cout<<"Complex a:\n";
上一篇:市场营销实践报告