1、dirtest.txt,下列语句中能以”读文本文件”的方式打开该文件的是(B)。 A ifstream file(“d:dirtest.txt”); Bfstream file(“d:dirtest.txt”); Cfstream file(ios:in); file.open(“d: Difstream * file=new ifstream(“d:6 定义如下变量和数组: int i; int x33=1,2,3,4,5,6,7,8,9;则以下语句的输出结果是 (C) 。 for(i=0 ;i3;i+) coutxi2-i;A159 B147 C357 D3697 void g(void*
2、)(int,int,int), int, int)的参数个数是 (A) 。A. 3个 B. 5个 C. 1个 D. 无法确定8 现需要对list类对象使用的逻辑运算符“”重载,以下函数声明(28)是正确的。.list & list:operator=(const list &a);.list list:.bool &.bool list:9 若有说明:int t32,*p=t0; 能正确表示t21的一组表达式是_(D)_ A) *(t+2)+1,*(p0+5),*t+3 B) *(t+2+1),*p21,*(t2+1) C) *(t+2)+1,*p5,*(t0+2)+1 D) *(*(t+2)
3、+1),p5,*(t2+1)10 若有说明: 能正确表示t0的一组表达式是_(27)_ A)*(t+0),p B)*t,*p C)p0,*t D)*t+0,*p011 若有说明:int t32; 则表达式:&t0+1表示_(B)_A)第1行的行指针,一级指针 B) 第1行的行指针,二级指针C)元素t01的地址 D) 元素t10的地址12 设有说明:int *p,*q,*t,x=5; 运行以下程序段,输出结果是_(C)_A) 5 随机数 B) 随机数 5 C) 出错 D) 5 5 q=&x; t=p;p=q;q=t; delete p;t*q二、 阅读程序题1. 以下程序运行后,输出结果是 (1
4、)(2)(3)(4)。(8分)4*24*#include b) return b+a; else coutb*f(+a,-b)*n; return a*f(a,b);void main()int n; n=f(1,3);n2. 程序#include iostream.hvoid fun(int *s,int n) int f=10;15x=25 if(n=1|n=2) *s=1; else fun(&f,n-2); *s=f*5;fDouble x,u,v,f(double),g(double),t(double,double (*)(double); x=4.0; u=t(x,f); v=t
5、(x,g);u=setw(4)uv=vdouble t(double a,double (*f)(double) return (*f) (a*a);double f(double x ) return x+2.0;double g(double x) return x-2.0;程序输出的前2行分别是:_ 7 , 8 u=18 v=144. 程序class base int i; base(int I=0):i(I) virtual int sum()return i;class D: public base int j; D(int I=0,int J=0):base(I),j(J) int
6、 sum()return base:sum()+j;void call(base b)coutsum=b.sum()void main()sum=10 base b(10),* pb; D d(10,47); pb=&d; call(b); call(d); call(*pb);程度输出的第一行 9 ,是第二行是 10 第三行是 11 。5. 37程序#includeint f(int x) int s=0; for(int j=2;j=x;j+) if(x%j=0) x/=j; s+=j; return s;void main(void) for(int i=3;10; if(i=f(i)
7、cout程序运行后输出的第一行是 12 第二行是 13 第三行是 14 。6. 程度void f(int j) if(!j) coutj; return; if(j%2) f(j/10);j%10; else coutf(j/10); f(0);cout f(1357); f(2468);程序运行后输出的第一行是 第二行是 第三行是 7. 程序class X void virtual f()coutX:11 void virtual g()cout22B:554422class A:public X void f()coutA:33class B:public A44 void g(int i
8、=10)coutf();px-g();程序输出的第一行是 ,第二行是 ,第三行是 _。8. 程序class Ax=1 A_1A_22 A3 A4 A int x; public: A(int a) x=a;A_1 A(A &s) x=s.x+1;A_2 A()coutA A f1(1), f2(f1); f2=A(f1); f1=A(f2);程序共输出 行,第一行是 ,第四行是 ,第六行是 。9. 程序x=4, y=10x=4, y=16x=4, y=6i=34x=8, y=7class Aint y;static int x; operator int()return x+y; A oper
9、ator +(int)return A(x+,y+); A(int x=2,int y=3)A:x=x+x;y=y+y; void print()cout,ty=int A:x=23;A a(30,5),b(10,8),c;a.print();b.print();c.print();int i=a+b;i=c+;程序输出的第一行是 ,第二行是 ,第三行是 ,第四行是 ,第五行是 。10. #include int i=1;int fac(int n) static int f=1;66,6 f=f*n; return(f); int i,p=0; for(i=1;=3;i+)p=fac(i);
10、p p=0;i+)p=p+:i+; / A, p=1;i+)p=p*-:i; / B问题1:该程序的第一行输出为( ,第二行输出为 11. 下列程序的输出结果是 。int a=100;void fun(int i,int j,int *k) j+=i; *k=j-i;void main() int a,b,c; fun(17,18,& fun(19,a,&b);18,18 ,18 fun(a,b,&c);abc12. 以下程序运行后,输出结果是 。 insert A n virtual void print()coutprint(); pb- pa=& pa-三、 完善程序1. 1. 下面程序
11、的功能是:先输入一行字符串,并从输入的字符串中找到最小的字符(其ASCII值最小),用min保存该字符。然后将该字符前的所有字符顺序向后移动一个字符的位置。最后,将保存的字符放在该字符串的第0个位置上。例如:设输入的字符串为“bcdfae”,其中字母a为最小字符,经移动处理后,输出的字符串为“abcdfe”。void fun( ) / char *strchar mun,*q,*p; ; / p=q=strmin = *p+;while(*p!=0)if( )min = *p; q = p ; / *pstr /p-,q-*q = min;char str80;cin.getline(str,
12、80);fun(str);str static int i; char *ps; A(char ) /*s ps=new charstrlen(s)+1; strcpy( ,s); /ps A() if( ) coutThe end /i=0at this time of day!n if(ps) deleteps; i+; A & operator=(A &b) if(b.ps) ps=new charstrlen( )+1; / b.ps strcpy(ps,b.ps); else ps=0; return ; /*this A s1(programming),s2(C+ languge)
13、;s2.ps void ;if(search(str,c) fp=dele; /(*p)(char *,char) else fp=add; fp( ); / (str,c)4. 班主任根据全班n个学生某课程的考试成绩建立一个链表,每个节点包括学号、成绩和该成绩在全班的名次。最后按排名顺序输出学号和成绩。程序10分struct node int grade,n; long num; node *point;node *trans(node *h,int n) /建立含报名者的档案链表 node *p; p=new node;p-numgrade; p-n=0; / node *h=p for(
14、int i=0;n-1;i+)point=new node; ; /p=p-point; cin p-point=NULL; return h;node *sort(node *head,int n) /按成绩排名次并输出 node *p,*q; int t=0,k=1; p=head; while(kt) /p-n=0 t=p- q=p; p=p- else p=p- q-n=k; ; /k+; /t=0;q-numgrade return head;node *student; int n;student=NULL;n; student=trans(student,n);nnumber:g
15、rade:排名:n student=sort(student,n);5. 以下程序定义了一个二维坐标点类Point,派生矩形类Square。矩形左下角坐标从基类继承,矩形类只定义右上角坐标,还定义表示颜色的字符串。执行下面的主程序将得到结果:矩形s1: x=1 y=3 width=5 , high=6 , color = red 矩形s2: x=1 y=3 width=5 , high=6 , color = redYellowclass Pointprivate:double x,y; Point(double xv=0,double yv=0)x=xv;y=yv; double getx(
16、)return x; double gety()return y; void Show()cout y=y;/输出对象信息class Square:public Point /带颜色的矩形(square)类 double hx,hy; char *color; Square()hx=0;hy=0; Square(double xv,double yv,double hxv,double hyv,char *s): (11) hx=hxv;hy=hyv; color=new char9; strcpy(color,s); Square( ):Point(rr) /拷贝构造函数 / Square&rr hx=rr.hx;hy=rr.hy; color =new charstrlen( )+6; /rr.color strcat(strcpy( ), /color, rr.color /Yellow void Show();void Square:Show() /输出矩形的左下角坐标、宽度、高度和颜色 Point:
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1