代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
复数类
作者:
akang
/ 发布于
2015/10/19
/
1378
#ifndef _COMPLEX_H_ #define _COMPLEX_H_ #include<iostream> using namespace std; class Complex { private: double real; double imag; public: Complex(); ~Complex(); Complex(double rr,double rg); void setComplex(double rr,double rg); void getComplex(double& rr,double& rg) const; void setRealPart(double rr); double getRealPart() const; void setImaginaryPart(double rg); double getImaginaryPart() const; Complex operator+(const Complex& rc); Complex operator*(const Complex& rc); bool operator==(const Complex& rc) const; friend ostream& operator<<(ostream& os,const Complex& rc); friend istream& operator>>(istream& is,Complex& rc); }; #endif 2. [代码]Complex.cpp ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 #include"Complex.h" #include<iostream> using namespace std; Complex::Complex():real(0.0),imag(0.0) { } Complex::~Complex() { } Complex::Complex(double rr=0.0,double rg=0.0):real(rr),imag(rg) { } void Complex::setComplex(double rr,double rg) { this->real=rr; this->imag=rg; return; } void Complex::getComplex(double& rr,double& rg) const { rr=this->real; rg=this->imag; return; } void Complex::setRealPart(double rr) { this->real=rr; return; } double Complex::getRealPart() const { return this->real; } void Complex::setImaginaryPart(double rg) { this->imag=rg; return; } double Complex::getImaginaryPart() const { return this->imag; } Complex Complex::operator+(const Complex& rc) { Complex temp(0.0,0.0); temp.real=this->real+rc.real; temp.imag=this->imag+rc.imag; return temp; } Complex Complex::operator*(const Complex& rc) { Complex temp(0.0,0.0); temp.real=(this->real)*(rc.real)-(this->imag)*(rc.imag); temp.imag=(this->real)*(rc.imag)+(this->imag)*(rc.real); return temp; } bool Complex::operator==(const Complex& rc) const { if((this->real==rc.real)&&(this->imag==rc.imag))return true; return false; } ostream& operator<<(ostream& os,const Complex& rc) { os<<'('<<rc.real<<','<<rc.imag<<')'; return os; } istream& operator>>(istream& is,Complex& rc) { char ch; is>>ch; is>>rc.real; is>>ch; is>>rc.imag; is>>ch; return is; } 3. [代码]assign.cpp ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 /********************************************************************* * PROGRAM: CSCI 241 Assignment 4 * PROGRAMMER: xujiwei * LOGON ID: 13721690 * DUE DATE: due date of assignment * * FUNCTION: This program tests the functionality of the Complex * class. 2015-10-15 **********************************************************************/ #include <iostream> #include <iomanip> #include "Complex.h" using std::cin; using std::cout; using std::endl; int main() { cout<<"Testing constructor... "; Complex c1(23, 34); Complex c4(3, 4); cout<<"done\n"; cout<<"Testing default constructor use... "; Complex c2; Complex c3; cout<<"done\n\n"; cout<<"Testing stream insertion operator and constructors...\n"; cout<<"c1 = "<<c1<<endl; cout<<"c2 = "<<c2<<endl<<endl; cout<<"Testing get methods...\n"; cout<<"Real part of c4 = "<<c4.getRealPart()<<endl; cout<<"Imaginary part of c4 = "<<c4.getImaginaryPart()<<endl; double r, i; c4.getComplex(r, i); cout<<"Real part of c4 = "<<r<<endl; cout<<"Imaginary part of c4 = "<<i<<endl<<endl; cout<<"Testing set methods...\n"; c2.setComplex(3.7, 2.5); cout<<"New value of c2 = "<<c2<<endl; c2.setRealPart(-1.4); cout<<"New value of c2 = "<<c2<<endl; c2.setImaginaryPart(83); cout<<"New value of c2 = "<<c2<<endl<<endl; cout<<"Testing stream extraction operator...\n"; cout<<"Enter a complex number in the form (a, b) "; cin>>c2; cout<<"New value of c2 = "<<c2<<endl<<endl; cout<<"Testing addition operator...\n"; c3=c1+c4; cout<<"c3 = "<<c3<<endl; cout<<c4<<" + "<<c1<<" = "<<c4+c1<<endl; cout<<c4<<" + "<<c4<<" = "<<c4+c4<<endl<<endl; cout<<"Testing multiplication operator...\n"; c3=c1*c4; cout<<"c3 = "<<c3<<endl; cout<<c4<<" * "<<c1<<" = "<<c4*c1<<endl; cout<<c4<<" * "<<c4<<" = "<<c4*c4<<endl<<endl; cout<<"Testing equality operator...\n"; cout<<c1<<" and "<<c3; (c1==c3)?cout<<" are equal\n":cout<<" are not equal\n"; const Complex c5(3, 4); cout<<c4<<" and "<<c5; (c4==c5)?cout<<" are equal\n":cout<<" are not equal\n"; return 0; }
试试其它关键字
复数类
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
akang
贡献的其它代码
(
20
)
.
base64加解密
.
发邮件,密送多人,带附件,中文正文,收信信箱从exce
.
VBS脚本发送邮件,密送多人,带附件,可更换账号密码
.
石头, 剪刀, 布
.
通过正则表达式截取短信中流量信息
.
发送邮件,可带附件,可群发
.
二叉树的常用算法
.
复数类
.
向数据库添加数据
.
字符串转为十六进制
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3