国家C++上机试卷(一)(二)答案
(一)1.#include
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; } // ?óá?????êyμ?×?′ó?μ// ERROR **********found**********
int Max(int x, int y, int z) // ?óèy????êyμ?×?′ó?μ
{
if (x > y)
return x>z ? x : z;
else
return 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
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
using namespace std;
class MyPoint{ //±íê?????×?±ê?μ?Dμ?μ?μ?àà
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<<'('< }; class MyRectangle{ //±íê???D?μ?àà MyPoint up_left; //??D?μ?×óé????¥μ? MyPoint down_right; //??D?μ?óò?????¥μ? 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; //·μ????D?μ????y }; //**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< return 0; } (二)1. #include using namespace std; class Door{ int num; // ??o? bool closed; // true ±íê???1?×? bool locked; // true ±íê?????×? public: // ERROR *********found********* Door(int n):num(n),closed(true),locked(true){} bool isClosed()const{ return closed;} // ??1?×?ê±·μ??true£?·??ò·μ??false bool isOpened()const{ return !closed;} // ???a×?ê±·μ??true£?·??ò·μ??false bool isLocked()const{ return locked;} // ????×?ê±·μ??true£?·??ò·μ??false bool isUnlocked()const{ return !locked;} // ???′??ê±·μ??true£?·??ò·μ??false // ERROR *********found********* void open(){ // ?a?? cout< if(!closed) cout<<"??ê??a×?μ?£????è?ù?a???£"; else if(locked) cout<<"??ê???×?μ?£?′ò2??a?£"; else{ closed=false; cout<<"??′ò?aá??£"; } void close(){ // 1??? cout< if(closed) cout<<"??ê?1?×?μ?£????è?ù1????£"; else{ closed=true; cout<<"??1?é?á??£"; } } void lock(){ // ???? cout< if(locked) cout<<"??ê???×?μ?£????è?ù?????£"; else{ // ERROR *********found********* if(!closed){ cout<<"?è1???..."; closed=true; } locked=true; cout<<"????é?á??£"; } } void unlock(){ // ?a?? cout< if(!locked) cout<<"????ó?????£????è?ù?a???£"; else{ locked=false; cout<<"???aá??£"; } } }; int main(){ Door door(503); door.open(); door.unlock(); door.open(); door.open(); door.lock(); return 0; 2. #include 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< cout< //**********found********** << (is_male?"男":"女") //显示性别("男"或"女",双引号内不含空格)<<" 出生日期:" //显示出生日期 < < < } }; void sortByAge(Person ps[], int size){ //对人员数组按年龄的由小到大的顺序排序for(int i=0; i int m=i; for(int j=i+1; 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< for(i=0; i sortByAge(staff,size); cout< for(i=0; i cout< return 0; } c语言上机选择题题及答案 ————————————————————————————————作者:————————————————————————————————日期: 2 1、下列字符序列中,不可用作C语言标识符的是(c) A.abc123 B.no_1 C.a!b D._ok_0 2、正确的C语言标识符是(a) A._buy_2 B.2_buy C.?_buy D.buy? 3、不属于C语言关键字的是(D) A.int B.break C.while D.character 4、以下选项中,合法的C语言标识符是(A) A.mon B.2_abc C.3dmax D.A.dat 5、以下选项中,合法的实型常数是(C) A.5E2.0 B.E-3 C.2E02 D.1.3E 6、以下选项中,正确的字符常量是(C) A."F" B.'\\'' C.'W' D.''' 7、已知字母A的ASCII码为十进制数65,且S为字符型, 则执行语句S='A'+'6'-'3';后,S中的值为(A) A.'D' B.68 C.不确定的值D.'C' 8、在C语言中,要求运算数必须是整型的运算符是(D) A./ B.++ C.*= D.% 9、若有定义:int m=7; float x=2.5, y=4.7;则表达式x+m%3*(int)(x+y)%2/4的值是(A) A.2.500000 B.2.750000 C.3.500000 D.0.000000 10、设变量x为float类型,m为int类型,则以下能实现将x中的数值 保留小数点后两位,第三位进行四舍五入运算的表达式是(B) A.x=(x*100+0.05)/100.0 B.m=x*100+0.5, x=m/100.0 C.x=x*100+0.5/100.0 D.x=(x/100+0.5)*100.0 11、假设所有变量均为整型,则表达式(x=2, y=5, y++, x+y)的值是(B) A.7 B.8 C.6 D.2 12、已知s是字符型变量,下面不正确的赋值语句是(B) A.s='\012'; B.s= 'u+v'; C.s='1'+'2'; D.s=1+2; 13、设x、y均为float型变量,则不正确的赋值语句是(C) A.++x ; B.x*=y-2; C.y=(x%3)/10; D.x=y=0; 14、putchar函数可以向终端输出一个(D) A.整型变量表达式值。B.字符串 C.实型变量值。D.字符 15、以下程序段的输出结果是(C) int a=12345; printf(”%2d\n”, a); A.12 B.34 C.12345 D.提示出错、无结果16、有如下程序段,从键盘输入数据的正确形式应是(B) (注:└┘代表空格) float x,y,z; scanf(“x=%d,y=%d,z=%d”,&a,&y,&z); A.123 B.x=1,y=2,z=3 C.1,2,3 D.x=1└┘y=2└┘z=3 题目 --1、查找员工的编号、姓名、部门和出生日期,如果出生日期为空值, --显示日期不详,并按部门排序输出,日期格式为yyyy-mm-dd。 --2、查找与喻自强在同一个单位的员工姓名、性别、部门和职称 --3、按部门进行汇总,统计每个部门的总工资 --4、查找商品名称为14寸显示器商品的销售情况, --显示该商品的编号、销售数量、单价和金额 --5、在销售明细表中按产品编号进行汇总,统计每种产品的销售数量和金额 --6、按客户编号统计每个客户1996年的订单总金额 --7、查找有销售记录的客户编号、名称和订单总额 --8、查找在1997年中有销售记录的客户编号、名称和订单总额 --9、查找一次销售最大的销售记录 --10、查找至少有3次销售的业务员名单和销售日期 --11、用存在量词查找没有订货记录的客户名称 --12、使用左外连接查找每个客户的客户编号、名称、订货日期、订单金额 --订货日期不要显示时间,日期格式为yyyy-mm-dd --按客户编号排序,同一客户再按订单降序排序输出 --13、查找16M DRAM的销售情况,要求显示相应的销售员的姓名、 --性别,销售日期、销售数量和金额,其中性别用男、女表示 --14、查找每个人的销售记录,要求显示销售员的编号、姓名、性别、 --产品名称、数量、单价、金额和销售日期 --15、查找销售金额最大的客户名称和总货款 --16、查找销售总额少于1000元的销售员编号、姓名和销售额 --17、查找至少销售了3种商品的客户编号、客户名称、商品编号、商品名称、数量和金额--18、查找至少与世界技术开发公司销售相同的客户编号、名称和商品编号、商品名称、数 量和金额 19、查找表中所有姓刘的职工的工号,部门,薪水 20、查找所有定单金额高于20000的所有客户编号 21、统计表中员工的薪水在40000-60000之间的人数 22、查询表中的同一部门的职工的平均工资,但只查询"住址"是"上海市"的员工 c上机试题及答案文件排版存档编号:[UYTR-OUPT28-KBNTL98-UYNN208] A.类的定义 Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 659 (260 users) Total Accepted: 116 (114 users) Special Judge: No Description 定义一个Circle类,有私有数据成员radius(半径);公有成员函数getArea(),用来计算圆的面积;实现能初始化半径的构造函数,以及拷贝构造函数。要在主函数中定义两个对象,并且输出两个对象的面积。其中第二个由第一个对象初始化,圆周率取为。(15分) Input 包含一个整数。 Output 输出一行,是两个对象的面积值。具体参照样例。 Sample Input 2 Sample Output A题 #includec语言上机选择题题及标准答案
上机考试题目及答案
c上机试题及答案