资源描述
C+程序设计模拟题三、用C+语言描述下列命题1、A小于B并且A大于C2、A和B中有个小于C3、A是奇数4、B和C中较大者与A相等5、年份Y是闰年四、写出下列程序的执行结果1.#include void main()int b35=11,12,13,14,15,21,22,23,24,25,31,32,33,34,35;int (*p)5;p=b;for(int j=0;j5;j+)cout*(*p+j) , ;coutendl;2.#includevoid swapl(int a,int b)int temp=a;a=b;b=temp;void swap2(int*a,int*b) int temp=*a; *a=*b,*b=temp;void swap3(int*a,int*b)int*temp=a;a=b,b=temp;void swap4(int &a,int &b)int temp=a; a=b; b=temp;void main()int a=10,b=20; swapl(a,b);couta: bendl; swap2(&a,&b);couta: bendl;swap3(&a,&b);couta: bendl;swap4(a,b);couta: bendl;3. includevoid print(int w)int i; if(w!=0)print(w-1);for(i=1;i=w;i+)coutw;coutendl;void main()print(4);4.#include#define sw(x,y) x=y;y=x;x=y;void main()int a=10,b=01; sw(a,b); couta,b;5. 假定输入10个整数:32,64,53,87,54,32,98,56,98,83。下列程序的输出结果是什么?include void main( )int a,b,c,x;a=b=c=0;for (int k=0; kx;switch(x%3)case 0:a+=x; break;case 1:b+=x; break;case 2:c+=x; break;couta b cendl;6. *#include void main( )int j,k;for (j=5; j0; j-)for (k=j; k0; k-)cout*;coutendl;7.#include void main()int nNumber;int *pPointer;nNumber = 15;pPointer = &nNumber;coutnNumber is equal to : nNumberendl; *pPointer = 25;coutnNumber is equal to :nNumberendl; 8.#include int *pPointer;void SomeFunction()pPointer = new int;*pPointer = 25;void main()SomeFunction(); coutValue of *pPointer: *pPointerendl; delete pPointer;9.#include void AddFive(int* Number)*Number = *Number + 5;void main()int nMyNumber = 18;coutMy original number is nMyNumberendl; AddFive(&nMyNumber);coutMy new number is nMyNumberendl; 10.#include void main()int Array3;Array0 = 10;Array1 = 20;Array2 = 30;int *pArray;pArray = &Array0;coutpArray points to the value.endl;cout*pArrayendl;cout*(pArray+1)endl;11. *#includeint i=4; void main()couti=iendl; i=5;int i; i=7;couti=iendl; couti=i; 12. *#includevoid fun( );void main( )fun( );fun( );void fun( )static int a=1;int i=5;a+;i+;couti=i,a=aendl;13. include class Pointpublic:Point(int xx=0, int yy=0) X=xx; Y=yy; countP+; Point(Point &p);int GetX( ) return X;int GetY( ) return Y;void GetC( ) cout Object id=countPendl;private:int X,Y;static int countP;Point:Point(Point &p)X=p.X;Y=p.Y;countP+;int Point:countP=0; void main ( ) Point A(4,5);coutPoint A,A.GetX ( ) ,A.GetY ( );A.GetC ( );Point B(A);coutPoint B,B.GetX ( ) ,B.GetY ( );B.GetC ( );14. *#include int addDefault(int x=1,int y=2);void fun(void);void main(void) int addDefault(int x=3,int y=4); coutaddDefault()endl;fun();int addDefault(int x, int y) coutx+y=;return x+y;void fun(void) coutaddDefault()endl;15. * #include void main() int m,n,a=3,b=3,number; number=237; m=number/5; n=number%5; coutm=mendl; coutn=nendl; a+=m; b*=n; couta=aendl; coutb=bendl; 16. * #include void main() int width,height,result1,result2,result3; height=3;width=5; result1=height3 & width=5; result2=height3 | width=5; result3=!(width=5); coutresult1result1endl; coutresult2result2endl; coutresult3result3endl; 17. * #include void main() int i=32,j=6; double df; df=i/j+2.3; coutdf=dfendl; i=df/3; couti=iendl;18. & # include int c10; void add(int x,int y) static int i=0; ci=x+y; i+; void main() int a10=1,2,3,4,5,6,7,8,9,10; int b10=10,9,8,7,6,5,4,3,2,1; int i=0,k=0; for(i=0;i10;i+) add(ai,bi); for(i=0;i10;i+) coutci ;19. #include void main() char s30=computer,mouse,key; int i=0,k=0; coutsendl; while(si!=0) if(si=m)/通过数组名和下标引用数组元素 k+; i+; couti=iendl; coutk=kendl; 20. #include void main() char s=C+ program; char *ps1=computer; char *ps2,*ps3=s; ps2=mouse; couts ps1 ps2 ps3endl; ps1+;ps2+=2;ps3+=4; couts+4 ps1 ps2 ps3endl; cout*s *ps1 *ps2 *ps3endl; ps1=s; 21. 如果输入的数据是24和18,则下列程序的输出结果是什么?*#include int fun1(int c,int d) int r;r=c%d; while(r!=0)c=d;d=r;r=c%d; return d; void main()int m,n,e,d;coutmn;e=fun1(m,n); d=m*n/e;couteendl;coutdendl;22. #include void main() int a34=1,2,3,4,3,4,5,6,5,6,7,8; int b43,i,j; for(i=0;i3;i+) for(j=0;j4;j+) coutaij ; coutendl; for(i=0;i4;i+) for(j=0;j3;j+) bij=aji; for(i=0;i4;i+) for(j=0;j3;j+) coutbij ; coutendl; 23. #include #include void main() const int m=5; int amm; int i,j; for(i=0;im;i+) ai0=1; aii=1; for(j=1;ji;j+) aij=ai-1j-1+ai-1j; for(i=0;im;i+) for(int k=0;k30-2*i;k+) cout ; for(j=0;j=i;j+) coutsetw(5)aij; coutendl; 24. #include void main() char s30=computer,mouse,key; int i=0,k=0; coutsendl; while(si!=0) if(si=m) k+; i+; couti=iendl; coutk=kendl; 25. #include #include struct student char num5; char name10; char sex; int age; float mathscore; ; void main() struct student st10=001,Wangping,f,19,84,002,Zhaomin,m,20,64,003,Wanghong,f,18,54,004,Lilei,m,19,92,005,Liumin,m,19,75; int i,k=0; float average=0; for(i=0;i5;i+) average+=sti.mathscore; average/=10; coutaverage=averageendl; for(i=0;i5;i+) if(sti.mathscore60) k+; coutsti.num sti.name sti.sex; cout sti.age sti.mathscoreendl; coutkendl; 26. #include void main() int b34=23,38,16,12,56,89,66,34,58,12,90,100; int i,j,m=3,n=4; for(i=0;im;i+) for(j=0;jn;j+) cout*(*(b+i)+j) ; coutendl; for(i=0;i20;i+) cout-;coutendl; for(i=0;i=00;j-) cout*(bi+j) ; coutendl; for(i=0;i20;i+) cout-;cout=0;i-) for(j=0;jn;j+) coutbij ; coutendl; 27. #include void main() int a=12,22,32,42,52,62,72,82,92,102; int i, m=5; int *p=a; for(i=0;im;i+) cout*p+ ; cout=0;i-) coutpi ; coutendl; 28. #include void main() int b24=12,36,62,14,56,98,74,63; int (*p)4=b; int i,j; for(i=0;i2;i+) for(j=0;j4;j+) cout*(*(p+i)+j) ; coutendl; for(i=0;i20;i+) cout-;coutendl; for(i=0;i=0;j-) coutpij ; coutendl; 29. #include #include void main() int func(int a,int b,char op); int (*fptr)(int a,int b,char op); fptr=func; coutfunc(3,5,+)endl; coutfptr(3,5,*)endl; int func(int a,int b,char op) switch(op) case +: coutaopb=;return a+b;break; case -: coutaopb=;return a-b;break; case *: coutaopb=;return a*b;break; default: coutOperator error!endl; return 0; 30. #include struct student char *num; char *name; int age; ; void main() int *ip;float *fp;char *cp; struct student *stp; fp=new float(32.6); cp=new char; *cp=A; ip=new int10; for(int i=0;inum=12; stp-name=Li; stp-age=18; cout*fp *cp endl; coutnum=num name=name age=ageendl; for(i=0;i10;i+) coutipi ; coutendl; delete cp;delete fp; delete ip;delete stp; 31. #include class Point public: Point(int xx=0,int yy=0) X=xx; Y=yy;cout构造函数被调用endl; Point(Point& p); int GetX( ) return X; int GetY( ) return Y; private: int X,Y;Point:Point (Point& p) X=p.X; Y=p.Y; cout拷贝构造函数被调用endl;Point fun2( ) Point A(1,2); return A;void fun1(Point p) coutp.GetX( )endl; void main( ) Point A(1,2); fun1(A); Point B(A); coutB.GetX( )endl; B=fun2(); coutB.GetX( )endl;32. *#include #include class Point public:Point(int xx=0, int yy=0) X=xx;Y=yy;cout构造函数被调用endl;Point(Point &p);int GetX() return X;int GetY() return Y;private:int X,Y; ;Point:Point(Point &p)X=p.X;Y=p.Y;coutPoint拷贝构造函数调用endl;class Distancepublic:Distance(Point xp1, Point xp2);double GetDis()return dist;private:Point p1,p2;double dist;Distance:Distance(Point xp1,Point xp2):p1(xp1),p2(xp2)coutDistance构造函数调用endl;double x=double(p1.GetX()-p2.GetX();double y=double(p1.GetY()-p2.GetY();dist=sqrt(x*x+y*y);void main()Point myp1(1,1),myp2(4,5);Distance myd(myp1,myp2);coutThe distance is:;coutmyd.GetDis()endl;33. #include class Base public: Base(int x,int y) a=x; b=y; void Show() coutBase: a,bendl; private: int a,b; ; class Derived : public Base public: Derived(int x,int y,int z) : Base(x,y),c(z) void Show() coutDerived: cShow(); pb=&d; pb-Show();
展开阅读全文