多语言展示
当前在线:1228今日阅读:23今日分享:25

1. 设计一个复数类CComplex (15分)  私

1.     设计一个复数类CComplex   (15分)l  私有成员为,实部和虚部l  重载“>>”、“<<”操作,实现直接输入/输出复数。l  重载“+”、“-”操作,实现两个复数相加、减。l  重载“+”、“-”操作,实现一个复数与一个实数相加、减,且满足交换律。l  重载“=”操作,实现两个复数赋值。然后在主函数中进行如下测试:l  采用指针存储动态数组方式存储n个复数信息。l  重载[]操作直接获得第i个复数。l  设计显示函数Display(CComplex *),输出数组中所有复数。l  测试上述重载后的运算符功能。
工具/原料
1

vc2010

2

电脑

方法/步骤
1

实验总设计图,这是一个清晰的 思路 概图,编程的时候也可以照着编写。

2

总设计方案    1.定义一个复数类以实现题目要求。    2.重载“+”“-”“<<”“>>”“=”“[]”运算符。    3.对友员函数进行定义实现复数的各种显示和运算,    4.设计主函数采用动态指针储存指定数量的复数信息,并可直接调用第“i”个。

3

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)   赋值运算符重载

4

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;}

5

#include 'stdafx.h'#includeusing namespace std;class CComplex  {public: CComplex(double r = 0, double i = 0) { real = r, image = i; } 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) { image = s1.image; real = s1.real; return *this; }

6

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(){

7

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

8

int _tmain(int argc, _TCHAR* argv[]){ CComplex c1, c2, c3, c4, c5, c6, c7, c8; cout << '输入两个数c1,c2,生成复数'<> c1 >>c2; cout<<'c1='<>a; c5 = c1 + a; cout <<'c1+a='<< c5 << endl; c6 = a + c1; cout << 'a+c1='<> 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;}

推荐信息