ImageVerifierCode 换一换
格式:DOCX , 页数:14 ,大小:119.49KB ,
资源ID:12479396      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/12479396.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C++作业6.docx)为本站会员(b****0)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

C++作业6.docx

1、C+作业61、构建一个复数Complex类,其包含realPart实部和imaginaryPart虚部,提供构造函数可以初始化复数,同时提供实现复数加、复数减、复数乘和复数除的成员函数,并能将相应结果显示出来。测试你所设计的复数类。Complex.h#includeusing std:cout;using std:cin;using std:endl;class Complexpublic: Complex( double, double, double, double ); void complexAdd(); / 加 void complexSub(); / 减 void complexM

2、ul(); / 乘 void complexDiv(); / 除private: double realPart1, realPart2; / 记录实部 double imaginaryPart1, imaginaryPart2; / 记录虚部;Complex.cpp#includeusing std:cout;using std:cin;using std:endl;#includeusing std:setprecision;#include Complex.hComplex:Complex( double r1, double i1, double r2, double i2 ) / 将

3、私有数据结构成员初始化 realPart1 = r1; realPart2 = r2; imaginaryPart1 = i1; imaginaryPart2 = i2;void Complex:complexAdd() /* 实部与实部相加,虚部与虚部相加 cout ( realPart1 + imaginaryPart1 i ) + ( realPart2 + imaginaryPart2 i ) = realPart1 + realPart2 + imaginaryPart1 + imaginaryPart2 i endl; */ /将上述代码格式稍作调整,在负数外面加括号 cout (

4、 realPart1 +; if( imaginaryPart1 0) cout ( imaginaryPart1 ) i; else cout imaginaryPart1 i; cout ) + (; if (realPart2 0) cout ( realPart2 ) +; else cout realPart2 +; if( imaginaryPart2 0) cout ( imaginaryPart2 ) i; else cout imaginaryPart2 i; cout ) = realPart1 + realPart2 +; if(imaginaryPart1 + imag

5、inaryPart2 0 ) cout ( imaginaryPart1 + imaginaryPart2 ) i endl; else cout imaginaryPart1 + imaginaryPart2 i endl;void Complex:complexSub()/* 实部与实部相减,虚部与虚部相减 cout ( realPart1 + imaginaryPart1 i ) - ( realPart2 + imaginaryPart2 i ) = realPart1 - realPart2 + imaginaryPart1 - imaginaryPart2 i endl; */ /

6、 将上述代码格式稍作调整,在负数外加上括号 cout ( realPart1 +; if( imaginaryPart1 0) cout ( imaginaryPart1 ) i; else cout imaginaryPart1 i; cout ) - (; if (realPart2 0) cout ( realPart2 ) +; else cout realPart2 +; if( imaginaryPart2 0) cout ( imaginaryPart2 ) i; else cout imaginaryPart2 i; cout ) = realPart1 - realPart2

7、 +; if( imaginaryPart1 - imaginaryPart2 0 ) cout ( imaginaryPart1 - imaginaryPart2 ) i endl; else cout imaginaryPart1 - imaginaryPart2 i endl;void Complex:complexMul()/* 二项式乘法 cout ( realPart1 + imaginaryPart1 i ) * ( realPart2 + imaginaryPart2 i ) = realPart1 * realPart2 - imaginaryPart1 * imaginar

8、yPart2 + realPart1 * imaginaryPart2 + realPart2 * imaginaryPart1 i endl; */ / 将上述代码格式稍作调整,在负数前加上括号 cout ( realPart1 +; if( imaginaryPart1 0) cout ( imaginaryPart1 ) i; else cout imaginaryPart1 i; cout ) * ( realPart2 +; if( imaginaryPart2 0) cout ( imaginaryPart2 ) i; else cout imaginaryPart2 i; cou

9、t ) = realPart1 * realPart2 - imaginaryPart1 * imaginaryPart2 +; if( realPart1 * imaginaryPart2 + realPart2 * imaginaryPart1 0 ) cout ( realPart1 * imaginaryPart2 + realPart2 * imaginaryPart1 ) i endl; else cout realPart1 * imaginaryPart2 + realPart2 * imaginaryPart1 i endl;void Complex:complexDiv()

10、 if( realPart2 =0 & imaginaryPart2 = 0 ) cout 分母不能为0! endl; return; /* 乘以除数的共轭复数 cout ( realPart1 + imaginaryPart1 i ) / ( realPart2 + imaginaryPart2 i ) = (realPart1 * realPart2 + imaginaryPart1 * imaginaryPart2) / (realPart2*realPart2 + imaginaryPart2*imaginaryPart2 ) + (- realPart1 * imaginaryPar

11、t2 + realPart2 * imaginaryPart1) / (realPart2*realPart2 + imaginaryPart2*imaginaryPart2 ) i endl; */ / 将上述格式稍作调整,在负数外面加括号 cout ( realPart1 +; if( imaginaryPart1 0) cout ( imaginaryPart1 ) i; else cout imaginaryPart1 i; cout ) / ( realPart2 +; if( imaginaryPart2 0) cout ( imaginaryPart2 ) i; else cou

12、t imaginaryPart2 i; cout ) = setprecision(2) (realPart1 * realPart2 + imaginaryPart1 * imaginaryPart2) / (realPart2*realPart2 + imaginaryPart2*imaginaryPart2 ) +; if( - realPart1 * imaginaryPart2 + realPart2 * imaginaryPart1 0 ) cout ( setprecision(2) (- realPart1 * imaginaryPart2 + realPart2 * imag

13、inaryPart1) / (realPart2*realPart2 + imaginaryPart2*imaginaryPart2 ) ) i endl; else cout setprecision(2) (- realPart1 * imaginaryPart2 + realPart2 * imaginaryPart1) / (realPart2*realPart2 + imaginaryPart2*imaginaryPart2 ) i endl;test_Complex.cpp#includeusing std:cout;using std:cin;using std:endl;#in

14、clude Complex.hint main() double r1, r2, i1, i2; cout 请输入第一个复数的实部和虚部: r1 i1; cout 请输入第二个复数的实部和虚部: r2 i2; Complex C(r1, i1, r2, i2); C.complexAdd(); C.complexSub(); C.complexMul(); C.complexDiv(); return 0;2、对本讲中的Time类,为其增加一个tick成员函数,其作用是将存放在Time对象中的时间递增1秒。应确保你的Time对象始终处于可靠状态。编写测试函数,测试你所修改后的Time类。特别留

15、意: 递增到下一分递增到下一小时递增到下一天Time.h/ Declaration of class Time./ Member functions are defined in Time.cpp/ prevent multiple inclusions of header file#ifndef TIME_H#define TIME_H/ Time class definitionclass Time public: Time(); / constructor void setTime( int, int, int ); / set hour, minute and second void

16、printUniversal(); / print time in universal-time format void printStandard(); / print time in standard-time format void tick( int );private: int hour; / 0 - 23 (24-hour clock format) int minute; / 0 - 59 int second; / 0 - 59; / end class Time#endifTime.cpp/ Member-function definitions for class Time

17、.#include using std:cout;#include using std:setfill;using std:setw;#include Time.h / include definition of class Time from Time.h/ Time constructor initializes each data member to zero./ Ensures all Time objects start in a consistent state.Time:Time() hour = minute = second = 0; / end Time construct

18、or/ set new Time value using universal time; ensure that/ the data remains consistent by setting invalid values to zerovoid Time:setTime( int h, int m, int s ) hour = h; / validate hour minute = m; / validate minute second = s; / validate second / end function setTime/ print Time in universal-time f

19、ormat (HH:MM:SS)void Time:printUniversal() cout setfill( 0 ) setw( 2 ) hour : setw( 2 ) minute : setw( 2 ) second; / end function printUniversal/ print Time in standard-time format (HH:MM:SS AM or PM)void Time:printStandard() cout ( ( hour = 0 | hour = 12 ) ? 12 : hour % 12 ) : setfill( 0 ) setw( 2

20、) minute : setw( 2 ) second ( hour = 60) minute = minute + second/60; second = second % 60; if( minute = 60 ) hour = hour + minute/60; minute = minute%60; if( hour = 24 ) cout nwelcome to another day!; hour = hour%24; if(second 0) minute = minute + second/60 - 1; second = second%60 + 60; if( minute

21、0 ) hour = hour + minute/60 - 1; minute = minute%60 +60; if( hour 0 ) cout nwelcome to yesterday!; hour = hour%24 + 24; test_Time.cpp/ Program to test class Time./ NOTE: This file must be compiled with Time.cpp.#include using std:cin;using std:cout;using std:endl;#include Time.h / include definition

22、 of class Time from Time.hint main() Time t; / instantiate object t of class Time / output Time object ts initial values cout The initial universal time is ; t.printUniversal(); / cout nThe initial standard time is ; t.printStandard(); / int hr = -1, min = -1, sec = -1, i; while( hr = 24 | min 60 |

23、sec 60 ) cout n请输入现在的时间:(24小时制) endl; cout 请输入小时: hr; cout 请输入分钟: min; cout 请输入秒钟: sec; t.setTime( hr, min, sec ); / change time / output Time object ts new values cout nnUniversal time after setTime is ; t.printUniversal(); / cout nStandard time after setTime is ; t.printStandard(); cout endl; cout n请输入你想增加的秒数: i; t.tick(i); / output Time object ts new values cout nnUniversal time after Time tick is ; t.printUniversal(); / cout nStandard time after Time tick is ; t.printStandard(); cout endl; return 0; / end main

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1