国家C++上机试卷(一)(二)答案

  • 格式:doc
  • 大小:40.50 KB
  • 文档页数:7

(一)1.#include <iostream>using namespace std;class MyClass {public:// ERROR **********found**********MyClass(int i){ value = i; cout << "Constructor called." << endl; }int Max(int x, int y) { return x>y ? x : y; } // ÇóÁ½¸öÕûÊýµÄ×î´óÖµ// ERROR **********found**********int Max(int x, int y, int z) // ÇóÈý¸öÕûÊýµÄ×î´óÖµ{if (x > y)return x>z ? x : z;elsereturn y>z ? 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 " << obj.GetValue() << endl;cout << "Max number is " << obj.Max(10,20) << endl;return 0;}2.#include <iostream>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 monthday[13] = { // 存放每月的天数0,31,28,31,30,31,30,31,31,30,31,30,31};if (year == d.year && month == d.month) { // 若两日期在同年同月内, 则计算其相差的天数//**********found**********diffs = day-d.day; // TODO: 在此增加代码//**********found**********return diffs ; // TODO: 在此增加一条语句}diffs = monthday[d.month] - d.day + day; // 计算除整月以外相差的天数for (m = d.month+1; m < month; m++)//**********found**********diffs+=monthday[m]; // TODO: 在此增加一条语句, 依次计算两个日期间相差的整月天数return diffs;}void Day::Print( ) const{cout << "Year = " << year << ", Month = " << month << " , Day = " << day << endl;}int main(){ int y,m,d;cin >> 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;}3.#include<iostream>using 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(MyPoint p1,MyPoint p2): 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(up_left.getX(),down_right.getY());}//**3** **********found**********double MyRectangle::area()const{return (getUpLeft().getX()-getDownRight().getX())* (getDownRight().getY()-getUpLeft().getY());}int main( ){MyRectangle r(MyPoint(0,2),MyPoint(2,0));r.getUpLeft().show();r.getUpRight().show();r.getDownRight().show();r.getDownLeft().show();cout<<r.area()<<endl;return 0;}(二)1.#include<iostream>using namespace std;class Door{int num; // ÃźÅbool closed; // true ±íʾÃŹØ×Åbool locked; // true ±íʾÃÅËø×Åpublic:// ERROR *********found*********Door(int n):num(n),closed(true),locked(true){}bool isClosed()const{ return closed;} // ÃŹØ×Åʱ·µ»Øtrue£¬·ñÔò·µ»Øfalsebool isOpened()const{ return !closed;} // ÃÅ¿ª×Åʱ·µ»Øtrue£¬·ñÔò·µ»Øfalsebool isLocked()const{ return locked;} // ÃÅËø×Åʱ·µ»Øtrue£¬·ñÔò·µ»Øfalsebool isUnlocked()const{ return !locked;} // ÃÅÎ´ËøÊ±·µ»Øtrue£¬·ñÔò·µ»Øfalse// ERROR *********found*********void open(){ // ¿ªÃÅcout<<endl<<"´ò¿ª"<<num<<"ºÅÃÅ...";if(!closed)cout<<"ÃÅÊÆ¿ª×ŵģ¬ÎŞĞèÔÙ¿ªÃÅ¡£";else if(locked)cout<<"ÃÅÊÇËø×ŵ쬴ò²»¿ª¡£";else{closed=false;cout<<"ÃÅ´ò¿ªÁË¡£";}void close(){ // ¹ØÃÅcout<<endl<<"¹ØÉÏ"<<num<<"ºÅÃÅ...";if(closed)cout<<"ÃÅÊÆ¹Ø×ŵģ¬ÎŞĞèÔÙ¹ØÃÅ¡£";else{closed=true;cout<<"ÃŹØÉÏÁË¡£";}}void lock(){ // ËøÃÅcout<<endl<<"ËøÉÏ"<<num<<"ºÅÃÅ...";if(locked)cout<<"ÃÅÊÆËø×ŵģ¬ÎŞĞèÔÙËøÃÅ¡£";else{// ERROR *********found*********if(!closed){cout<<"ÏȹØÃÅ...";closed=true;}locked=true;cout<<"ÃÅËøÉÏÁË¡£";}}void unlock(){ // ¿ªËøcout<<endl<<"´ò¿ª"<<num<<"ºÅÃŵÄËø...";if(!locked)cout<<"ÃÅûÓĞÇÏËø£¬ÎŞĞèÔÙ¿ªËø¡£";else{locked=false;cout<<"Ëø¿ªÁË¡£";}}};int main(){Door door(503);door.open();door.unlock();door.open();door.open();door.lock();return 0;2.#include<iostream>using namespace std;class Date{ // 日期类int year,month,day; // 年、月、日public:Date(int year, int month, int day):year(year),month(month),day(day){} int getYear()const{ return year; }int getMonth()const{ return month; }int getDay()const{ return day; }};class Person{ // 人员类char name[14]; // 姓名bool is_male; // 性别,为true 时表示男性Date birth_date; // 出生日期public:Person(char *name, bool is_male, Date birth_date):is_male(is_male),birth_date(birth_date){//**********found**********strcpy(this->name,name);}const char *getName()const{ return name; }bool isMale()const{ return is_male; }Date getBirthdate()const{ return birth_date; }int compareAge(const Person &p)const{//比较两个人的年龄,返回正数、0 或int n; //负数分别表示大于、等于和小于n=p.birth_date.getYear()-birth_date.getYear();if(n!=0) return n;//**********found**********n=p.birth_date.getMonth()-birth_date.getMonth();if(n!=0) return n;return p.birth_date.getDay()-birth_date.getDay();}void show(){cout<<endl;cout<<name<<' ' //显示姓名//**********found**********<< (is_male?"男":"女") //显示性别("男"或"女",双引号内不含空格)<<" 出生日期:" //显示出生日期<<birth_date.getYear()<<"年"<<birth_date.getMonth()<<"月"<<birth_date.getDay()<<"日";}};void sortByAge(Person ps[], int size){ //对人员数组按年龄的由小到大的顺序排序for(int i=0; i<size-1; i++){ //采用挑选排序算法int m=i;for(int j=i+1; j<size; j++)if(ps[j].compareAge(ps[m])<0) m=j;if(m>i){//**********found**********Person p=ps[m];ps[m]=ps[i];ps[i]=p;}}}int main(){Person staff[]={Person("张三", true, Date(1978, 4, 20)),Person("王五", false, Date(1965,8,3)),Person("杨六", false, Date(1965,9,5)),Person("李四", true, Date(1973,5,30))};const int size=sizeof(staff)/sizeof(staff[0]);int i;cout<<endl<<"排序前:";for(i=0; i<size; i++) staff[i].show();sortByAge(staff,size);cout<<endl<<endl<<"排序后:";for(i=0; i<size; i++) staff[i].show();cout<<endl;return 0;}。