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