经典计算机等级考试二级上机试题及答案.doc_第1页
经典计算机等级考试二级上机试题及答案.doc_第2页
经典计算机等级考试二级上机试题及答案.doc_第3页
经典计算机等级考试二级上机试题及答案.doc_第4页
经典计算机等级考试二级上机试题及答案.doc_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

/ proj1.cpp#include using namespace std;class MyClass public: MyClass(int len) array = new intlen; arraySize = len; for(int i = 0; i arraySize; i+) arrayi = i+1; MyClass() / ERROR *found* delete array; void Print() const for(int i = 0; i arraySize; i+)/ ERROR *found* cout arrayi ; cout endl; private: int *array; int arraySize;int main()/ ERROR *found* MyClass obj=10; obj.Print(); return 0;/ proj2.cpp#include using namespace std;const int MAXNUM = 100;class Set private: int num; / 元素个数 char setdataMAXNUM; / 字符数组,用于存储集合元素public: Set(char *s); / 构造函数,用字符串s构造一个集合对象 bool InSet(char c); / 判断一个字符c是否在集合中,若在,返回true,否则返回false void Print() const; / 输出集合中所有元素;Set:Set(char *s) num = 0; while (*s)/*found* if (!InSet(*s) / TODO: 添加代码,测试元素在集合中不存在/*found* setdatanum+ = *s ; / TODO: 添加一条语句,加入元素至集合中 s+; bool Set:InSet(char c)for (int i = 0; i num; i+)/*found* if (c = setdatai) / TODO: 添加代码,测试元素c是否与集合中某元素相同/*found* return true; / TODO: 添加一条语句,进行相应处理return false;void Set:Print() const cout Set elements: endl; for(int i = 0; i num; i+) cout setdatai ; cout endl;int main() char sMAXNUM; cin.getline(s, MAXNUM-1); / 从标准输入中读入一行 Set setobj(s); / 构造对象setobj setobj.Print(); / 显示对象setobj中内容 return 0;/ proj3.cpp#includeusing std:ostream;using std:cout;using std:endl;class MyVector /表示二维向量的类 double x; /X坐标值 double y; /Y坐标值public: MyVector(double i=0.0 , double j=0.0); /构造函数 MyVector operator+( MyVector j); /重载运算符+ friend MyVector operator-( MyVector i, MyVector j); /重载运算符- friend ostream& operator( ostream& os, MyVector v); /重载运算符;/*1* *found*MyVector:MyVector(double i , double j): x(i),y(j)MyVector MyVector:operator+( MyVector j) return MyVector(x+j.x, y+j.y);MyVector operator-( MyVector i, MyVector j)/*2* *found* return MyVector(i.x-j.x,i.y-j.y);ostream& operator( ostream& os, MyVector v) os ( v.x , v.y ) ; /输出向量v的坐标 return os;int main() MyVector v1(2,3), v2(4,5), v3;/*3* *found* v3=v1+v2; coutv3endl; return 0;/ proj1.cpp#include using namespace std;class MyClass public:/ ERROR *found* void MyClass(int i) value = i; cout Constructor called. y ? x : y; / 求两个整数的最大值/ ERROR *found* int Max(int x, int y, int z = 0) / 求三个整数的最大值 if (x y) return xz ? x : z; else return yz ? y : z; int GetValue() const return value; MyClass() cout Destructor called. endl; private: int value;int main()MyClass obj(10);/ ERROR *found*cout The value is value() endl;cout Max number is obj.Max(10,20) endl;return 0;/ proj2.cpp#include using namespace std;class Day private: int year; int month; int day;public: Day(int y=2000, int m=1, int d=1); / 构造函数 int operator - (const Day &d); / 重载运算符 - void Print() const; / 输出日期;Day:Day(int y, int m, int d) year = y; month = m; day = d;int Day:operator - (const Day &d) / 实现运算符函数 - int diffs, m; int monthday13 = / 存放每月的天数 0,31,28,31,30,31,30,31,31,30,31,30,31 ; if (year = d.year & month = d.month) / 若两日期在同年同月内, 则计算其相差的天数/*found* diffs = _; / TODO: 在此增加代码/*found* _ ; / TODO: 在此增加一条语句 diffs = monthdayd.month - d.day + day; / 计算除整月以外相差的天数 for (m = d.month+1; m month; m+)/*found* _ ; / TODO: 在此增加一条语句, 依次计算两个日期间相差的整月天数 return diffs;void Day:Print( ) const cout Year = year , Month = month , Day = day y m d; Day d1(y, m, d); cin y m d; Day d2(y, m, d); int diff_days; d1.Print(); d2.Print(); diff_days = d1 - d2; / 计算d1和d2相差天数 cout Difference days: diff_days endl; return 0;/ proj3.cpp#includeusing namespace std;class MyPoint /表示平面坐标系中的点的类 double x; double y;public: MyPoint (double x,double y)this-x=x;this-y=y; double getX()const return x; double getY()const return y; void show()const cout(x,y);class MyRectangle /表示矩形的类 MyPoint up_left; /矩形的左上角顶点 MyPoint down_right; /矩形的右下角顶点public: MyRectangle(MyPoint upleft,MyPoint downright); MyPoint getUpLeft()const return up_left; /返回左上角坐标 MyPoint getDownRight()const return down_right; /返回右下角坐标 MyPoint getUpRight()const; /返回右上角坐标 MyPoint getDownLeft()const; /返回左下角坐标 double area()const; /返回矩形的面积;/*1* *found*MyRectangle:MyRectangle(_): up_left(p1),down_right(p2)MyPoint MyRectangle:getUpRight()const return MyPoint(down_right.getX(),up_left.getY();MyPoint MyRectangle:getDownLeft()const/*2* *found* return MyPoint(_);/*3* *found*_area()const return (getUpLeft().getX()-getDownRight().getX(

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论