复数类计算(多种运算符重载)
来源:互联网 发布:orange pi淘宝 编辑:程序博客网 时间:2024/06/15 22:30
1. 设计一个复数类CComplex
![](https://img.ishuo.cn/1512/1451435303.jpg)
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;
}
- 复数类计算(多种运算符重载)
- 关于运算符重载(两个复数计算的代码)
- 运算符重载—复数的计算
- 复数类运算符重载
- 复数类--重载运算符+,-,*,/
- 复数类--重载运算符+
- 复数类--重载运算符+,-,*,/
- 复数类--重载运算符+
- 复数类--重载运算符+,-,*,/
- 复数类--重载运算符
- 运算符重载 复数类 /
- 运算符重载 复数类 *
- 复数类 运算符重载
- 第八周项目1——3 实现复数类中的运算符重载(实数与复数的计算)
- 复数运算符重载
- 复数运算符重载
- 实现复数类中的运算符重载(扩充类)
- 运算符重载(复数类(Complex))
- CABasicAnimation的使用
- Stockbroker Grapevine
- WEB开发入门基础
- 如何使用汇编语言实现冒泡排序?
- atexit()函数调用注册的顺序
- 复数类计算(多种运算符重载)
- 数据结构之二叉树
- make Unknown module(s) in QT: quick
- Linux 下配置java环境
- Codeforces 793B Igor and his way to work 题解
- NB5.5源代码分析之服务端生成
- 数据结构与算法---起泡排序
- vb.net 教程 3-4 窗体编程 公共控件5 LinkLabel
- pandas的基本用法(一)——数据定义及使用