复数计算器课程设计
double Real,Image;
public:
CComplex(double real=0,double image =0) //构造函数
{
Real=real;Image=image;
}
friend istream & operator>>(istream &is,CComplex &com ); //重载输入
friend ostream & operator<<(ostream &os,CComplex &com); //重载输出CComplex operator+(CComplex &com); //加法重载CComplex operator-(CComplex &com); //减法重载CComplex operator*(CComplex &com); //乘法重载CComplex operator+=(CComplex &com); //加法赋值重载CComplex operator-=(CComplex &com); //减法赋值重载CComplex operator*=(CComplex &com); //乘法赋值重载CComplex operator++(); //自加CComplex operator--(); //自减
double mod (void); //求复数的模
int operator>(CComplex &com);
int operator<(CComplex &com);
int operator!=(CComplex &com);
int operator==(CComplex &com);
};
2.3主要算法流程图
。。。。。。
图2-1 算法流程图
3程序清单及注释
#include
#include
#include
#include
#include
#include
//#define EPS len-5 // 定义精度常数
using namespace std;
namespace NameCComplex // 定义命名空间NameCComplex
{
/*-----------------------------------------------------------------------
|部分A:
| 复数类CComplex 的声明和定义,以及结构体类型用户User 的定义
|
----------------------------------------------------------------------*/
/*---------------------------------
| 复数类CComplex 的声明
--------------------------------*/
class CComplex
{
private:
double Real, Image; // 分别为复数的实部和虚部
public:
CComplex(double real=0, double image=0) // 构造函数
{
Real = real; Image = image;
}
friend istream & operator >> (istream & is, CComplex & com); // 重载输入
friend ostream & operator << (ostream & os, CComplex & com); // 重载输出
CComplex operator + (CComplex & com); // 加法重载CComplex operator - (CComplex & com); // 减法重载
CComplex operator * (CComplex & com); // 乘法重载
CComplex operator += (CComplex & com); // 加法赋值重载CComplex operator -= (CComplex & com); // 减法赋值重载CComplex operator *= (CComplex & com); // 乘法赋值重载CComplex operator ++ (); // 自加
CComplex operator -- (); // 自减
double mod(void);
int operator > (CComplex & com);
int operator < (CComplex & com);
int operator != (CComplex & com);
int operator == (CComplex & com);
};
/*------------------------------------
| 结构体类型用户User 的定义
-----------------------------------*/
struct User
{
char szName[20]; // 用户名
int nTime; // 使用次数
int nTest; // 测试次数
double dlAve; // 平均成绩
int nAdd; // 加法次数
int nSub; // 减法次数
int nMul; // 乘法次数
double dlScore[3]; // 3次测试得分
} user;
/*---------------------------------------------------------------
| 复数类CComplex 的类外定义部分
---------------------------------------------------------------*/
// 重载运算符“++”,实部与虚部均加1
CComplex CComplex::operator ++ ()
{
Real++;
Image++;
return *this;
}
// 重载运算符“--”,实部与虚部均减1
CComplex CComplex::operator -- ()
{
Real--;
Image--;
return *this;
}
// 求复数的模,返回实部^2 + 虚部^2
double CComplex::mod()
{
return Real * Real + Image * Image;
}
// 重载运算符“>”,比较模的大小
int CComplex::operator > (CComplex & com)
{
if ( mod() > com.mod() )
return 1; // 若大,则返回1 else
return 0;
}
// 重载运算符“<”,比较模的大小
int CComplex::operator < (CComplex & com)
{
if ( mod() < com.mod() )
return 1; // 若小,则返回1 else
return 0;
}
// 重载运算符“!=”,分别判断复数的实部与虚部int CComplex::operator != (CComplex & com)
{
if ( *this==com )
return 0;
else
return 1;
}
// 重载复数的输入,a+bi 的形式
istream & operator >> (istream & is, CComplex & com) {
cout << "输入复数:";
char s[80];
is >> s; // 用字符串的形式接收复数
int len = strlen(s); // 求出字符串的长度
int n = 0, sign = 1; // 其中的n 值为当前从字符串中提取出的数字,会在下面的while 语句中得到确定的值
// sign 为状态变量,表示数值的正负符号,以辅助辨认正负值com.Image = com.Real = 0;
// 判断接收的字符串是否合法
for(int k=0; k { if ( (s[k]<'0' || s[k]>'9') && (s[k]!='+' && s[k]!='-' && s[k]!='i') ) { cout << "error" << endl; return is; // 错误,输出出错信息并返回 } } // 顺序识别字符串中各字符 for(int k=0; k { if ( n!=0 && (s[k]=='--' || s[k]=='+') ) // 当前字符是否为符号位 { com.Real = sign * n; // 是符号位,且n!=0,即n 已被赋值(通过下面的whlie语句),表明当前读取的是虚部的符号 n = 0; // 将原n*sign 值(带正负号的数值)赋给实部后,将n 清零,准备下一次继续接收并判断是否为虚部的值 } if ( s[k] == '-') // 当前字符若为负号 { sign = -1; k++; // 给符号标志变量sign 赋值,表示为负数 } if ( s[k] == '+') // 当前字符若为正号 { sign = 1; k++; // 给符号标志变量sign 赋值,表示为正数 } if ( s[k]=='i' ) // 若当前字符为“i” { if ( k!=len-1 ) // 判断字符'i' 是否为字符串中最后一个字符 cout << "error\n"; // 如果不是,说明复数数据格式错误 else com.Image = sign * n; // 是最后一个字符,复数对象已接收完,用sign*n 为虚部赋值 break;