试题查看

首页 > 软件水平考试 > 试题查看
【分析解答题】

【说明】 以下【C++程序】实现一个简单的小型复数类MiniComplex,该复数类能进行输入、输出、复数的加法、减法、乘法和除法运算,还可以进行复数的相等比较。【C++程序】#ifndef H_MiniComplex#define H_MiniComplex#include <iostream>using namespace std;class MiniComplex{ public: //重载流插入和提取运算符 {{U}} (1) {{/U}}ostream&operator<<(ostream &osObject,const MiniComplex&complex){ osObject<<"("<<complex.realPart<<"+"<<complex.imagPart<<"i"<<")"; return osObject; } {{U}} (2) {{/U}}istream&operator>>(istream&isObject, MiniComplex&complex){ char ch; isObject >>complex.realPart>>ch>>complex.imagPart>>ch; return isObject; } MiniComplex(double real=0,double imag=0); //构造函数 MiniComplex operator+(const MiniComplex&otherComplex)const;//重载运算符+ MiniComplex operator-(const MiniComplex&otherComplex)const;//重载运算符- MiniComplex operator*(const MiniComplex&otherComplex)const;//重载运算符* MiniComplex operator/(const MiniComplex&otherComplex)const;//重载运算符/ bool operator==(const MiniComplex&otherComplex)const; //重载运算符== private : double{{U}} (3) {{/U}}; double imagPart;};#end if#include "MiniComplex.h"bool MiniComplex::operator==(const MiniComplex&otherComplex)const{ return(realPart==otherComplex.realPart&&imagPart==ortherComplex.imagPart);}MiniComplex::MiniComplex(double real,double imag){ realPart== real;imagPart==imagPart;}MiniComplex MiniComplex::operator+(const MiniComplex&otherComplex)const{ MiniComplex temp; temp.realPart = realPart+ortherComplex. realPart; temp.imagPart = imagPart +ortherComplex. imagPart; return temp;}{{U}} (4) {{/U}}{ MiniComplex temp; temp.realPart= realPart-ortherComplex. realPart; temp.imagPart = imagPart-ortherComplex. imagPart; return temp;}MiniComplex MiniComplex::operator*(const MiniComplex&otherComplex)const{ MiniComplex temp; temp.realPart = (realPart*ortherComplex. realPart)-(imagPart *ortherComplex.imagPart); temp.imagPart = (realPart*ortherComplex. imagPart)+(imagPart *ortherComplex.realPart); return temp;}MiniComplex MiniComplex::operator/(const MiniComplex&otherComplex)const{ MiniComplex temp; float tt; tt=1/(ortherComplex.realPart*ortherComplex.realPart+ortherComplex.imagPart *ortherComplex. imagPart); temp.realPart=((realPart*ortherComplex, realPart)+(imagPart *ortherComplex. imagPart))*tt; temp.imagPart =((imagPart*ortherComplex. realPart)-(realPart*ortherComplex. imagPart))*tt; return temp;}#include <iostream>#include <MiniComplex.h>using namespace std;int main(){ MiniComplex numl(23, 34),num2(56, 35); cout<<"Initial Value of num1="<<num1<<"\n Initial Value of num2="<<num2<<end1; cout<<num1<<"+"<<num2<<"="<<num1+num2<<end1; //使用重载的加号运算符 cout<<num1<<"-"<<num2<<"="<<num1-num2<<end1; //使用重载的减号运算符 cout<<num1<<"*"<<num2<<"="<<num1*num2<<end1; //使用重载的乘号运算符 cout<<num1<<"/"<<num2<<"="<<num1/num2<<end1; //使用重载的除号运算符 {{U}} (5) {{/U}};}

查看答案解析

参考答案:

正在加载...

答案解析

正在加载...

根据网考网移动考试中心的统计,该试题:

0%的考友选择了A选项

0%的考友选择了B选项

0%的考友选择了C选项

0%的考友选择了D选项

你可能感兴趣的试题

【说明】源程序文件vectorClass.cpp,其中定义了用于表示向量的类ve【说明】下面是一个Applet程序,其功能是输出已定义好的两个变量x和chr。请阅读以下某客房管理系统的算法说明和程序流程图,根据要求回答问题1至问题4。【算法【说明】以下【C程序】能将自然数1,2,…,N2按蛇形方式逐个存入N阶矩阵。换言【说明】某超市集团为发展业务向社会公开招聘N个工种的工作人员,每个工种各有不同的【说明】Stack类是java.ntil包中专门用来实现栈的工具类。以下Java