复数类计算(多种运算符重载)

来源:互联网 发布:orange pi淘宝 编辑:程序博客网 时间:2024/06/15 22:30

1. 设计一个复数类CComplex


1. 设计一个复数类ccomplex (15分)

l 私有成员为,实部和虚部

l 重载“>>”、“<<”操作,实现直接输入/输出复数。

l 重载“+”、“-”操作,实现两个复数相加、减。

l 重载“+”、“-”操作,实现一个复数与一个实数相加、减,且满足交换律。

l 重载“=”操作,实现两个复数赋值。

然后在主函数中进行如下测试:

l 采用指针存储动态数组方式存储n个复数信息。

l 重载操作直接获得第i个复数。

l 设计显示函数display(ccomplex *),输出数组中所有复数。


1:总设计方案

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 * ss= 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;

clasccomplex

{

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) o<< c.real << '+' << c.image << 'i' << endl;

else if (c.image<0) o<< c.real << c.image << 'i' << endl;

else o<< c.real << endl;

return os;

}

istream& operator>>(istream& is, ccomplex& c)

{

i>> 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,生成复数"<<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 * ss= 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;

}

0 0
原创粉丝点击