欢迎来到天天文库
浏览记录
ID:49881533
大小:163.50 KB
页数:7页
时间:2020-03-03
《实验1C++基础以及抽象数据类型.doc》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、实验1C++基础以及抽象数据类型一、题目第i题:减法【题FI描述】给两个正整数A和B,输出它们的差C=A-Bo【输入】输入仅包含一行,有两个正整数A和氏【输出】输出仅包含一行,有一个正整数Co【样例输入】100200【样例输出】-100第二题:用C++的类声明定义“复数”的抽象数据类型【题ri描述】试用C++的类声明定义”复数”的抽象数据类型•要求:(0在复数内部用浮点数定义它的实部和虚部。(2)实现一•个构造函数:将两个双精度浮点数分别赋给复数的实部和虚部。(3)定义获取和修改复数的实部与虚部,以及+法运算的成员函数。(4)定义重载的流函数来输出一•个
2、复数。【输入】输入包含2行,每行包含有两个浮点数,每行表示一个复数,第一个浮点数是实部,第二个浮点数是虚部。【输出】输出求和之后的复数。【样例输入】1.452.782.13.2【样例输出】3.55+5.98i二、程序清单#include#includetemplate〃函数模板TSub(Ta,Tb){return(a-b);}intmain(){inta,b;cout«n输入减数和被减数:”;cin»a»b;cout«Sub(a,b)«endl;return0;system(,'pauseH);
3、}2、//Ch.l.htemplate〃类模板classComplex{public:Complex(Ta,Tb)〃构造函数,将两个双精度浮点数分别赋给复数的实部和虚部。{x=a;y=b;}Tget_x()〃获取该复数的实部和虚部{returnx;}Tget_y(){returny;}voidput_x(Ta)〃修改复数的实部与虚部{x=a;}voidput_y(Tb)y=b;Complexoperator+(Complexp);//”+”运算符重载private:Tx;Ty;};//Ch_l.cpp#include4、>#include#includenCh_l.hHtemplateComplexComplex::operator+(Complexp){returnComplex(x+p.x,y+p.y);}templateistream&operator>>(istream&in,Complex&p)〃输入流重载{doublea,b;cout«"请输入复数的实部和虚部:x,y"«endl;cin»a»b;p.put_x(a);p.put_y(b);returnin;}template5、>ostream&operator«(ostream&output,ComplexvT>&p)〃输岀流重载{cout«"这两个复数之和为:”;doublea,b;a=p.get_x();b=p.get_y();cout«a«,,+"«b«"i,,«endl;returnoutput;}intmain()〃主函数{Complexp1(0,0),p2(0,0);cin»p1;cin»p2;Complexp3=p1+p2;cout«p3;return0;system("pause");}三、程序调试过程中所出现的错误1、无2、C6、h_l.cppD:Ch_lCh_l.cpp(31)D:Ch_lCh_l.cpp(31)typespecifiersD:Ch_lCh_l.cpp(31)D:Ch_lCh_l.cpp(32)specifiersD:Ch_lCh_l.cpp(32)parametersD:Ch_lCh_l.cpp(34)D:Ch_lCh_l.cpp(34)'char[30]'D:Ch_lCh_l.cpp(34)D:Ch_lCh_l.cpp(35)D:Ch_lCh_l.cpp(35)expectedoperatorwithD:Ch_lC7、h_l,cpp(36)D:Ch_lCh_l.cpp(36)class/struct/uniontypeD:Ch_lCh_l.cpp(37)class/struct/uniontypeD:Ch_lCh_l.cpp(38)D:Ch_lCh_l.cpp(44)definedwhichtakesconversion)D:Ch_lCh_l.cpp(45)definedwhichtakesconversion):errorC2143:syntaxerror:missing;before&:errorC2501:^stream1:missingst8、orage-classor:errorC2061:syntaxerror:ide
4、>#include#includenCh_l.hHtemplateComplexComplex::operator+(Complexp){returnComplex(x+p.x,y+p.y);}templateistream&operator>>(istream&in,Complex&p)〃输入流重载{doublea,b;cout«"请输入复数的实部和虚部:x,y"«endl;cin»a»b;p.put_x(a);p.put_y(b);returnin;}template5、>ostream&operator«(ostream&output,ComplexvT>&p)〃输岀流重载{cout«"这两个复数之和为:”;doublea,b;a=p.get_x();b=p.get_y();cout«a«,,+"«b«"i,,«endl;returnoutput;}intmain()〃主函数{Complexp1(0,0),p2(0,0);cin»p1;cin»p2;Complexp3=p1+p2;cout«p3;return0;system("pause");}三、程序调试过程中所出现的错误1、无2、C6、h_l.cppD:Ch_lCh_l.cpp(31)D:Ch_lCh_l.cpp(31)typespecifiersD:Ch_lCh_l.cpp(31)D:Ch_lCh_l.cpp(32)specifiersD:Ch_lCh_l.cpp(32)parametersD:Ch_lCh_l.cpp(34)D:Ch_lCh_l.cpp(34)'char[30]'D:Ch_lCh_l.cpp(34)D:Ch_lCh_l.cpp(35)D:Ch_lCh_l.cpp(35)expectedoperatorwithD:Ch_lC7、h_l,cpp(36)D:Ch_lCh_l.cpp(36)class/struct/uniontypeD:Ch_lCh_l.cpp(37)class/struct/uniontypeD:Ch_lCh_l.cpp(38)D:Ch_lCh_l.cpp(44)definedwhichtakesconversion)D:Ch_lCh_l.cpp(45)definedwhichtakesconversion):errorC2143:syntaxerror:missing;before&:errorC2501:^stream1:missingst8、orage-classor:errorC2061:syntaxerror:ide
5、>ostream&operator«(ostream&output,ComplexvT>&p)〃输岀流重载{cout«"这两个复数之和为:”;doublea,b;a=p.get_x();b=p.get_y();cout«a«,,+"«b«"i,,«endl;returnoutput;}intmain()〃主函数{Complexp1(0,0),p2(0,0);cin»p1;cin»p2;Complexp3=p1+p2;cout«p3;return0;system("pause");}三、程序调试过程中所出现的错误1、无2、C
6、h_l.cppD:Ch_lCh_l.cpp(31)D:Ch_lCh_l.cpp(31)typespecifiersD:Ch_lCh_l.cpp(31)D:Ch_lCh_l.cpp(32)specifiersD:Ch_lCh_l.cpp(32)parametersD:Ch_lCh_l.cpp(34)D:Ch_lCh_l.cpp(34)'char[30]'D:Ch_lCh_l.cpp(34)D:Ch_lCh_l.cpp(35)D:Ch_lCh_l.cpp(35)expectedoperatorwithD:Ch_lC
7、h_l,cpp(36)D:Ch_lCh_l.cpp(36)class/struct/uniontypeD:Ch_lCh_l.cpp(37)class/struct/uniontypeD:Ch_lCh_l.cpp(38)D:Ch_lCh_l.cpp(44)definedwhichtakesconversion)D:Ch_lCh_l.cpp(45)definedwhichtakesconversion):errorC2143:syntaxerror:missing;before&:errorC2501:^stream1:missingst
8、orage-classor:errorC2061:syntaxerror:ide
此文档下载收益归作者所有