版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
面向对象程序设计一、分析程序题(每题30分,共1道小题,总分值30分)1.下面的代码有什么问题?并请给出正确的写法。voidDoSomeThing(char*p){charstr[16];intn;assert(NULL!=p);sscanf(p,"%s%d",str,n);if(0==strcmp(str,"something")){...}}(30分)sscanf(p,"%s%d",str,n);这句该写成:sscanf(p,"%s%d",str,&n);二、编写程序实现算法(每题30分,共1道小题,总分值30分)1.写一个算法,根据输入的年月日得到该日期是该年份的第几天。(30分)#include<stdio.h>
intmain()
{
intyear,month,day;//年月日
intjudge,i;
intsum=0;//标记天数
intdate[2][12]={
{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31}};//储存闰年和非闰年每一月的天数
printf("输入年月日:");
scanf("%d%d%d",&year,&month,&day);
judge=(year%4==0)||(year%400==0&&year%100!=0);
//判断年份是不是闰年是闰年judge就是1;否则就是0
for(i=0;i<month-1;i++)//加month之前的所有天数
sum+=date[judge][i];
sum+=day;//加当天月份的天数day
printf("它是%d年中的第%d天\n",year,sum);
return0;
}三、程序设计(每题40分,共1道小题,总分值40分)1.设计实现一个复数类,1)实现拷贝构造函数;2)实现复数的乘法和除法,输出计算结果。(40分)#include<iostream>
usingnamespacestd;structplural{
doublereal,imaginary;plural()
{
real=imaginary=0.0;
}
plural(constplural&c);//复制构造函数(一定要用引用)
pluraloperator*(plural&s)
{
pluralt;
t.real=s.real*real-s.imaginary*imaginary;
t.imaginary=real*s.imaginary+imaginary*s.real;
returnt;
}
pluraloperator/(plural&s)
{
pluralt;
t.real=(s.real/real+s.imaginary/imaginary)/(real*real+imaginary*imaginary);
t.imaginary=(s.imaginary/real-s.real/imaginary)/(real*real+imaginary*imaginary);
returnt;
}
~plural(){}
};istream&operator>>(istream&in,plural&s)
{
printf("请输入实部:\n");
scanf("%lf",&s.real);
printf("请输入虚部:\n");
scanf("%lf",&s.imaginary);
returnin;
}
ostream&operator<<(ostream&ou,plural&s)
{
printf("%.4lf+%.4lfi",s.real,s.imaginary);returnou;
}
intmain(){
plurala,b,c;
cin>>a>>b;
c=a*b;
cout<<c<<endl;
c=a/b;
cout<<c<<endl;
system("pause");
}1.数据描述:编号、书名、价格、作者、出版社、出版日期、存量编写程序完成功能:将书籍记录按照价格排序(由高到低)并输出。publicclassBook{//设置属性的私有访问权限privateStringnumber;//编号privateStringname;//书名privatedoubleprice;//价格privateStringauthor;//作者privateStringpublisher;//出版社privateStringdate;//出版日期privateintstock;//存量//通过公有的get,set方法实现对属性的访问publicStringgetNumber(){returnnumber;}publicvoidsetNumber(Stringnumber){this.number=number;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){=name;}publicvoidsetPrice(doubleprice){this.price=price;}publicdoublegetPrice(){returnprice;}publicStringgetAuthor(){returnauthor;}publicvoidsetAuthor(Stringauthor){this.author=author;}publicStringgetPublisher(){returnpublisher;}publicvoidsetPublisher(Stringpublisher){this.publisher=publisher;}publicStringgetDate(){returndate;}publicvoidsetDate(Stringdate){this.date=date;}publicvoidsetStock(doublestock){this.stock=stock;}publicdoublegetStock(){returnstock;}//无参构造方法publicBook(){}//有参构造方法实现属性赋值publicBook(Stringnumber,Stringname,doubleprice,Stringauthor,Stringpublisher,Stringdate,intstock){//有四个参构造方法this.number=number;=name;this.setPrice(price);this.author=author;this.publisher=publisher;this.date=date;this.setStock(stock);}publicvoidinformation(){//展示书本信息的方法//编号、书名、价格、作者、出版社、出版日期、存量System.out.println("编号:"+this.getNumber());System.out.println("书名:"+this.getName());System.out.println("价格:"+price+"元");System.out.println("作者:"+this.getAuthor());System.out.println("出版社:"+this.getPublisher());System.out.println("出版日期:"+this.getDate());System.out.println("存量:"+stock+"本");}}//降序排序方法Collections.sort(list,newComparator<Book>(){publicintcompare(Bookb1,Bookb2){//按照价格大小进行降序排列if(b1.getPrice()<b2.getPrice()){return1;}if(b1.getPrice()==b2.getPrice()){return0;}return-1;}});publicclassTestBook{publicstaticvoidmain(String[]args){//编号、书名、价格、作者、出版社、出版日期、存量List<Book>list=newArrayList<Book>();Bookb1=newBook("0001","平凡的世界",60.0,"路遥","人民文学出版社","2006年5月4日",100);//创建对象的同时给属性赋值Bookb2=newBook("0002","西游记",50.0,"吴承恩","东方文学出版社","2007年9月10日",120);//创建对象的同时给属性赋值Bookb3=newBook("0003","倚天屠龙记",55.0,"金庸","西安文学出版社","199年7月13日",600);//创建对象的同时给属性赋值Bookb4=newBook("0004","文化苦旅",49.0,"余秋雨","青年出版社","2017年8月29日",203);//创建对象的同时给属性赋值list.add(b1);list.add(b2);list.add(b3);list.add(b4);Arrays.sort(list,Collections.sort());//按照价格大小进行降序排列for(Bookbook:list){rmation();//降序排列之后输出}}}一、程序分析题voidmain(){charstr1[]="abc";charstr2[]="abc";constcharstr3[]="abc";constcharstr4[]="abc";constchar*str5="abc";constchar*str6="abc";cout<<boolalpha<<(str1==str2)<<endl;cout<<boolalpha<<(str3==str4)<<endl;cout<<boolalpha<<(str5==str6)<<endl;}str1,str2分别为字符数组,str1,str2分别表示指向字符串”abc”的首地址,因为str1和str2各自都有存储区,他们的值是各自存储区的首地址不同。二者不等,输出false。而str3与str4类似,只是为const,即指向的数据区不能改变。二者不等,输出false。Str5与str6为字符指针,而非字符数组,他们分别指向常量字符串”abc”,“abc”存储在静态存储区。即str5和str6为串“abc”的地址,所以str5==str6。二者相等,输出true。所以输出结果为falsefalsetrue二、编写程序实现算法(每题30分,共1道小题,总分值30分)1.不调用标准库函数,自己实现strcpy。函数原型char*MyStrCpy(char*szDest,constchar*szSrc);char*MyStrCpy(char*szDest,constchar*szSrc){if((szDest==NULL)||(szSrc==NULL))//[1]throw"Invalidargument(s)";//[2]char*szDestCopy=szDest;//[3]while((*szDest++=*szSrc++)!='/0');//[4]returnszDestCopy;}三、程序设计(每题40分,共1道小题,总分值40分)1.设计实现一个复数类,并1)实现复数的加法和减法,输出计算结果;2)实现复数的“==”符号重载相等判断。//设计实现一个复数类,并//1)实现复数的加法和减法,输出计算结果;//2)实现复数的“==”符号重载相等判断。#include<iostream>usingnamespacestd;classComplex{public:Complex(){real=0;imag=0;}Complex(doubler,doublei){real=r;imag=i;}Complexoperator+(Complex&);//操作符重载,两复数相加Complexoperator-(Complex&);//操作符重载,两复数相减booloperator==(Complex&);//操作符重载,判断是否相等doublegetReal(void)const;//返回实部doublegetImag(void)const;//返回虚部doublesetReal(doublereal);//设置实部doublesetImag(doubleimag);//设置虚部doublereal;doubleimag;};ComplexComplex::operator+(Complex&c2){returnComplex(real+c2.real,imag+c2.imag);}ComplexComplex::operator-(Complex&c2){returnComplex(real-c2.real,imag-c2.imag);}boolComplex::operator==(Complex&b){returngetReal()==b.getReal()&&getImag()==b.getImag();}doubleComplex::getImag(void)const{
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 党委会议规范化管理制度
- 家长学校规范化建设制度
- 档案工作年度考核制度
- 档案学会财务制度
- 员工薪酬管理制度及规范
- 饭店定时消毒制度规范标准
- 彩铅几何体课件
- 物业电子档案保存制度
- 客运经营权制度管理规范
- 心理治疗室制度规范标准
- 音乐场所卫生管理制度
- 标书财务制度
- 四川发展控股有限责任公司会计岗笔试题
- 2026中国电信四川公用信息产业有限责任公司社会成熟人才招聘备考题库及一套答案详解
- 天津津静收费站雷击事故深度剖析与防护策略探究
- 2025山西焦煤集团所属华晋焦煤井下操作技能岗退役军人招聘50人笔试参考题库带答案解析
- 儿童骨科主任论儿童骨科
- 2026年齐齐哈尔高等师范专科学校单招(计算机)测试模拟题库必考题
- 送钱表文完整规范版本(含民俗禁忌)
- 剖宫产术后早期活动实施要点
- 《中医骨伤科学》课件-股骨颈骨折的治疗
评论
0/150
提交评论