全部知识点
已知字典x={i : str(i+3) for i in range(3)},那么表达式 sum(x)的值为___________。
表达式{1,2,3}&{3,4,5}的值为_________。
已知函数定义def demo(x,y,op): return eval(str(x)+op+str(y)),那么表达式demo(3,5,'+')的值为_____________。
在Python中定义类时,与运算符“//”对应的特殊方法名为__________。
Python标准库os.path中用来判断指定文件是否存在的方法是_______________。
输出如下格式的九九乘法表:
1*1=1
2*1=2 2*2= 4
3*1=3 3*2= 6 3*3=9
4*1=4 4*2= 8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=20
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*6=72 9*9=81
编写程序,其功能为打印如下图所示图形。
*
***
*****
*******
要求:编写程序中包含输出语句,直接打印表达式结果。
指出下列程序片段中的错误标号,写出正确语句或解释错在何处。
1)
①int index=675; ②const int *ptr=&index; ③int *const ntptr=&index; ④*ptr=555; ⑤*ntptr=666; ⑥int another=8; ⑦ptr=&another; ⑧ntptr=&another;
2)
①int arrp; ②arrp=new int[15]; ③delete arrp;
下面程序为什么会编译错误,并改正错误(提出解决办法)。
class window
{
protected:
int basedata;
};
class border: public window
{ };
class menu: public window
{ };
class border_and_menu: public border,public menu
{
public:int show()
{
return basedata;
}改正下面程序段中的错误,写出整个正确的程序段
template<T>
void print(T *a)
{ cout<<a<<’\n’;}
void main( )
{
const int x=0;
cout<<y<<’\n’;
int y;
x=5;
int* p
p=&y;
print(p);
return 0;
}写出下面程序的执行结果:
1)
#include <iostream>
using namespace std;
class A
{
friend double count(A&);
public:
A(double t, double r):total(t),rate(r){}
private:
double total;
double rate;
};
double count(A& a)
{
a. total+=a.rate*a.total;
return a.total;
}
int main(void)
{
A x(80,0.5),y(100,0.2);
cout<<count(x)<<','<<count(y)<<'\n';
cout<<count(x)<<'\n';return 0;
}2)
#include<iostream>
using namespace std;
class Count
{
private:
static int counter;
int obj_id;
public:
Count(); //constructor
static void display_total(); //static function
void display();
~Count(); //destructor
};
int Count::counter; //definition of static data member
Count::Count() //constructor
{
counter++;
obj_id = counter;
}
Count::~Count() //destructor
{
counter--;
cout<<"Object number "<<obj_id<<" being destroyed\n";
}
void Count::display_total() //static function
{
cout <<"Number of objects created is = "<<counter<<endl;
}
void Count::display()
{
cout << "Object ID is "<<obj_id<<endl;
}
int main(void)
{
Count a1;
Count::display_total();
Count a2, a3,a4;
Count::display_total();
a2.display();
a4.display();
return 0;
}3)
#include <iostream >
using namespace std;
class BASE
{
char c;
public:
BASE(char n):c(n){}
virtual ~BASE(){cout<<c;}
};
class DERIVED:public BASE
{
char c;
public:
DERIVED(char n):BASE(n+1),c(n){}
~DERIVED(){cout<<c;}
};
int main(void)
{
DERIVED('X');
return 0;
}程序填空:
#include <iostream>
using namespace std;
class A
{
______(1)______
char name[80];
public:
A( ____(2)______ ) {____(3)______ }
};
class B_____(4)_______
{
public:
B(const char*n)_____(5)_______{}
void PrintName( ) {cout<<”name:”<<name<<endl;};
};
void main( )
{
B b1(“Ling Li”);
b1.PrintName( ) ;
}//执行结果:name: Ling Li编写程序:定义抽象基类Shape,由它派生出五个派生类:Circle(圆形)、Square(正方形)、Rectangle(长方形)、Trapezoid(梯形)和Triangle(三角形),用虚函数分别计算各种图形的面积,并求出它们的和。要求用基类指针数组。使它的每一个元素指向一个派生类的对象。
注:主函数中定义如下对象
Circle circle(12.6);
Square square(3.5);
Rectangle rectangle(4.5,8.4);
Trapezoid trapezoid(2.0,4.5,3.2);
Triangle triangle(4.5,8.4);
请在下面程序的横线处填上适当内容,以使程序完整,并使运行结果为:2/10/2004
#include <iostream.h>
static int dys[]={31,28,31,30,31,30,31,31,30,31,30,31};
class date
{int mo,da,yr;
public:
date(int m,int d,int y){mo=m;da=d;yr=y;}
date(){}
void disp(){cout<<mo<<"/"<<da<<"/"<<yr<<endl;}
void leap(int a){
if(a%4==0&&a%100!=0||a%400==0)
dys[1]=29;
else dys[1]=28;}
friend date operator+(_______(1)_______)
{day+=d.da;
d.leap(d.yr);
while(_______(2)_______)
{d.leap(d.yr);
day-=dys[d.mo-1];
if(++d.mo==13)
{d.mo=1;
d.yr++;}
}
d.da=day;
return d;
}
};
int main()
{date d1(2,10,2003),d2;
int n;
cin>>n; //从键盘输入365
while(n<0){cin>>n;}
d2=d1+n;
d2.disp();
}请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为:n=30
#include <iostream.h>
template <class T>
class Sample
{T n;
public:
Sample(){}
Sample(T i){n=i;}
Sample<T> operator+(const Sample<T> &);
void disp(){cout<<"n="<<n<<endl;}
};
template <class T>
Sample<T> Sample<T>::operator+(const Sample<T> &s){Sample<T> temp;
_______(1)_______;
return temp;
}
int main()
{Sample<int> s1(10),s2(20),s3;
s3=s1+s2;
_______(2)_______;
}请在下面程序的横线处填上适当字句,以使程序完整,并使程序的输出为
A Constructor2
B Constructor2
x1=12
x2=2
#include <iostream.h>
class A
{public:
A(){cout<<"A Constructor1"<<endl;}
A(int i){x1=i;cout<<"A Constructor2"<<endl;};
void dispa(){cout<<"x1="<<x1<<endl;}
private:
int x1;
};
class B:public A
{
public:
B(){cout<<"B Constructor1"<<endl;}
B(int i): _____(1)_____{x2=i;cout<<"B Constructor2"<<endl;}
void dispb()
{_____(2)_____;
cout<<"x2="<<x2<<endl;
}
private:
int x2;
};
int main()
{B b(2);
b.dispb();
}请在下面程序的横线处填上适当字句,以使程序完整,并使程序的输出为
构造 base 对象
调用 base::f()
构造 derive 对象
调用 derive::f()
#include <iostream.h>
class base
{
public:
base()
{cout<<"构造base对象"<<endl;
f();
}
_________(1)_________{cout<<"调用base::f()"<<endl;}
};
class derive:public base
{
public:
derive()
{cout<<"构造derive对象"<<endl;
_________(2)_________;
}
void f(){cout<<"调用derive::f()"<<endl;}
};
int main()
{
derive d;
}下面程序用STL的条件计数算法和自定义的函数对象对一个存放在整数向量类对象中的学生成绩进行统计及格人数并显示结果。请在下面程序的横线处填上适当字句,以使程序完整。
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
class Pass
{
public:
____________________(1)___________________
{
return x>=60;
}
};
int main()
{
vector<int> a;
}
void disp(){cout<<"n="<<n<<endl;}
};
intmain()
{Sample s(10);
s--;
s.disp();
}
int count;
a.push_back(78);
a.push_back(92);
a.push_back(52);
count = ____________________(2)___________________;
cout<<"count="<<count<<endl;
}读程序写结果
1.
#include <iostream.h>
class CSample
{
private:
int n;
static int k;
public:
CSample(int i){n=i;k++;}
void disp();
};
void CSample::disp()
{cout<<"n="<<n<<",k="<<k<<endl;}
int CSample::k=0;
int main()
{CSample a(10),b(20),c(30);
a.disp();
b.disp();
c.disp();
}2.
#include <iostream.h>
class Sample
{int n;
public:
Sample(){}
Sample(int m){n=m;
}
Sample operator--(int)
{Sample old=*this;
n--;
return old;
}
void disp(){cout<<"n="<<n<<endl;}
};
int main()
{Sample s(10);
s--;
s.disp();
}3.
#include <iostream.h>
#include <string.h>
template <class T>
T max(T x, T y)
{
return (x > y ? x : y);
}
char *max(char *x, char *y)
{
if (strcmp(x, y) >= 0)
return x;
else
return y;
}
int main()
{
cout << max(2, 5) << endl;
cout << max(3.14,4.12) << endl;
cout << max("China", "Beijing") << endl;
}4.
#include <stack>
#include <iostream>
#include <string>
using namespace std;
int main() {
stack<char> s;
string str;
cin >> str;//从键盘输入字符串LoveChina
for(string::iterator iter = str.begin(); iter != str.end(); ++iter)
s.push(*iter);
while (!s.empty()) {
cout << s.top();
s.pop();
}
cout << endl;
}5.
#include <iostream>
using namespace std;
int divide(int x, int y)
{
if (y == 0)
throw x;
return x / y;
}
int main()
{try {
cout << "8/3 = " << divide(8, 3) << endl;
cout << "6/0 = " << divide(6, 0) << endl;
cout << "9/2 = " << divide(9, 2) << endl;
} catch (int e) {
cout << e << " is divided by zero!" << endl;
}
cout << "That is ok." << endl;
}编程实现小型公司的工资管理。该公司主要有4类人员:经理(manager)、兼职技术人员(technician)、销售员(salesman)和销售经理(salesmanager)。要求存储这些人员的编号、姓名和月工资,计算月工资并显示全部信息。月工资计算办法是:经理拿固定月薪8000元,兼职技术人员按每小时100元领取月薪,销售员按当月销售额的4%提成,销售经理既拿固定月工资也领取销售提成,固定月工资为5000元,销售提成为所管辖部门当月销售总额的千分之五。兼职技术人员一个月工作小时数、销售员一个月销售额、销售经理所管辖部门一个月销售总额由各个类的成员函数完成设置。(要求用抽象类和类继承)
#include <iostream>
using namespace std;
//雇员类
class Employee {
protected :
int _id;
double _salary;
string _name;
public:
Employee(int id,string name)
:_id(id),_name(name)
{}
virtual ~Employee(){}
virtual double CalcSalary()=0;
};
//经理类
class Manager:public Employee{
public:
Manager(int id,string name):Employee(id,name){}
virtual double CalcSalary(){
_salary=8000;
return _salary;
};
};
//兼职技术员类
class Technician:public Employee{
int _workHour;
public:
Technician(int id,string name):Employee(id,name){}
void SetWorkHour(int workHour){
_workHour = workHour;
}
virtual double CalcSalary(){
_salary=100*_workHour;
return _salary;
};
};
//销售经理类
class Salesman:public Employee{
double _salesMount;
public :
Salesman (int id,string name):Employee(id,name){}
void SetSalesMount(double salesMount){
_salesMount=salesMount;
}
virtual double CalcSalary(){
_salary=0.04*_salesMount;
return _salary;
};
};
class SalesManager:public Employee{
double _salesTotalMount;
public:
SalesManager (int id,string name):Employee(id,name){}
void
SetSalesTotalMount(double salesTotalMount){
_salesTotalMount=salesTotalMount;
}
virtual double CalcSalary(){
_salary =5000 +0.005 * _salesTotalMount;
return _salary;
};
};
int main()
{
Manager zhang(1000, "张三");
Technician li(1000, "李四");
Salesman wang(1000, "王五");
SalesManager zhao(1000, "赵六");
li.SetWorkHour(50);
wang.SetSalesMount(175000);
zhao.SetSalesTotalMount(1500000);
Employee *pEmployeeA[]={&zhang,&li,&wang,&zhao};
for (int i = 0; i <4; i++) {
cout << pEmployeeA [i] ->CalcSalary() << endl;
}
}