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

2025-10-24 21:41:37

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

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

2、总设计方案

    1.定义一个复数类以实现题目要求。

    2.重载“+”“-”“<<”“>>”“=”“[]”运算符。

    3.对友员函数进行定义实现复数的各种显示和运算,

    4.设计主函数采用动态指针储存指定数量的复数信息,并可直接调用第“i”个。

     

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

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"

#include<iostream>

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

}

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

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

{

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

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

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

8、int _tmain(int argc, _TCHAR* argv[])

{

CComplex c1, c2, c3, c4, c5, c6, c7, c8;

cout << "输入两个数c1,c2,生成复数"<<endl;

cin >> c1 >>c2;

cout<<"c1="<<c1<<endl;

cout<<"c2="<<c2<<endl;

c3 = c1 + c2;

cout <<"c1+c2"<<"="<< c3 << endl;

c4 = c1 - c2;

cout << "c1-c2="<<c4 << endl;

int a ;

cout<<"请输入a"<<endl;

cin>>a;

c5 = c1 + a;

cout <<"c1+a="<< c5 << endl;

c6 = a + c1;

cout << "a+c1="<<c6<< endl;

c7 = c1 - a;

cout << "c1-a="<<c7 << endl;

c8 = a - c1;

cout << "a-c1="<<c8 << endl;

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;

}

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢