vc2010
电脑
实验总设计图,这是一个清晰的 思路 概图,编程的时候也可以照着编写。
总设计方案 1.定义一个复数类以实现题目要求。 2.重载“+”“-”“<<”“>>”“=”“[]”运算符。 3.对友员函数进行定义实现复数的各种显示和运算, 4.设计主函数采用动态指针储存指定数量的复数信息,并可直接调用第“i”个。
1.运算符重载模块主要完成功能为:实现复数的直接输入输出,相加相减,以及复数与实数的的相加相减并使其满足交换律。主要使用技术:单目与双目运算符的重载 关键代码如下:friend ostream& operator<<(ostream&, CComplex&);输入输出运算符重载friend istream& operator>>(istream&, CComplex&);friend CComplex operator +(const CComplex &c1, const CComplex &c2);加减运算符重载friend CComplex operator -(const CComplex &c1, const CComplex &c2);friend CComplex operator +(const CComplex &c1, const int a);friend CComplex operator +(const int a, const CComplex &c1);friend CComplex operator -(const CComplex &c1, const int a);friend CComplex operator -(const int a, const CComplex &c1);CComplex & operator=(CComplex &s1) 赋值运算符重载
2.信息存储模块主要完成功能为:实现复数的存储及直接调用。主要使用技术:动态指针存储技术关键代码:CComplex * sss = new CComplex[10]; // 采用指针存储动态数组方式存储n个复数信息int i,k,j;cout << '复数个数:';cin >> j;for (int x = 0; x < 1; x++){ cout << '存储复数信息,输入两个数'; for (i = 0; i < j; i++) { cin >> sss[i]; cout << sss[i]; } cout << '直接输出第i个数'; cin >> k;直接调用 sss[0].display(sss, k);}return 0;}
#include 'stdafx.h'#include
CComplex operator[](int i) { return *this; } void display(CComplex *s1, int n) const { int i; for (i =n-1; i < n; i++) cout << s1[i].real << ' ' << '+' << ' ' << s1[i].image << 'i' << endl; } void print();private: double real, image;};ostream& operator<<(ostream& os, CComplex& c){ if (c.image>0) os << c.real << '+' << c.image << 'i' << endl; else if (c.image<0) os << c.real << c.image << 'i' << endl; else os << c.real << endl; return os;} istream& operator>>(istream& is, CComplex& c){ is >> c.real >> c.image; return is;}void CComplex::print(){
void CComplex::print(){ if (image>0) cout << real << '+' << image << 'i' << endl; else if (image<0) cout << real << image << 'i' << endl; else cout << real << endl;}CComplex operator +(const CComplex &c1, const CComplex &c2){ return CComplex(c1.real + c2.real, c1.image + c2.image);}CComplex operator -(const CComplex &c1, const CComplex &c2){ return CComplex(c1.real - c2.real, c1.image - c2.image);}CComplex operator +(const CComplex &c1, const int a){ return CComplex(c1.real + a, c1.image + 0);}CComplex operator +(const int a, const CComplex &c1){ return CComplex(a + c1.real, 0 + c1.image);}CComplex operator -(const CComplex &c1, const int a){ return CComplex(c1.real - a, c1.image - 0);}CComplex operator -(const int a, const CComp
int _tmain(int argc, _TCHAR* argv[]){ CComplex c1, c2, c3, c4, c5, c6, c7, c8; cout << '输入两个数c1,c2,生成复数'<