回 帖 发 新 帖 刷新版面

主题:C++的几个问题

一、单选题(每小题2分,共12分)
    1.在每个C++程序中都必须包含有这样一个函数,该函数的函数名为________。
        A main      B MAIN      C name      D function

    2.设x和y均为bool量,则x && y为真的条件是________。
        A 它们均为真      B 其中一个为真      C 它们均为假      D 其中一个为假

    3. 下面的哪一个保留字不能作为函数的返回类型?________
        A void      B int      C new      D long

    4. 假定a为一个整型数组名,则元素a[4]的字节地址为_______。
        A a+4      B a+8      C a+16      D a+32

    5.假定AB为一个类,则执行“AB a(4), b[3], *p[2];”语句时,自动调用该类构造函数的次数为________。
         A 3      B 4      C 6      D 9

    6.假定要对类AB定义加号操作符重载成员函数,实现两个AB类对象的加法,并返回相加结果,则该成员函数的声明语句为:________  
         A  AB operator +(AB& a, AB& b);
         B  AB operator +(AB& a);
         C  operator +(AB a);
         A  AB& operator +();

二、填空题(每小题2分,共24分)
    1.C++语言中的每条基本语句以________作为结束符,每条复合语句以________作为结束符。

    2.执行“cout<<char('A'+2)<<endl;”语句后得到的输出结果为________。

    3.float和double类型的大小分别为________和________。
    4. 算术表达式 对应的C++表达式为________________。
    5. 关系表达式x+y>5的相反表达式为____________。

6.假定一个一维数组的定义为“char* a[8];”,则该数组所含元素的个数为________,

所占存储空间的字节数为________。

    7.变量分为全局和局部两种,________变量没有赋初值时,其值是不确定的。

    8. 假定a是一个二维数组,则a[i][j]的指针访问方式为____________。
         
    9. 假定一个结构类型的定义为:

           struct D{int a; union {int b; double c;}; D* d[2];};

则该类型的大小为________字节。

10.对一个类中的数据成员的初始化可以通过构造函数中的____________实现,也

可以通过构造函数中的____________实现。

11. 假定 AB为一个类,则执行”AB a[10];”语句时,系统自动调用该类构造函数的

次数为________。

12. 假定类AB中有一个公用属性的静态数据成员bb,在类外不通过对象名访问该

成员bb的写法为____________。

三、给出下列程序运行后的输出结果(每小题6分,共30分)
    1. #include<iostream.h>
       void SB(char ch) {
       switch(ch) {
         case 'A': case 'a':
         cout<<"well!";break;
         case 'B': case 'b':
         cout<<"good!";break;
         case 'C': case 'c':
         cout<<"pass!";break;
         default:
             cout<<"bad!";break;
       }
       }
       void main() {
         char a1='b',a2='C',a3='f';
       SB(a1);SB(a2);SB(a3);SB('A');
       cout<<endl;
       }
    



    2. #include<iostream.h>
       #include<string.h>
       void main() {
           char* a[5]={"student","worker","cadre","soldier","peasant"};
       char *p1, *p2;
       p1=p2=a[0];
       for(int i=0;i<5;i++) {
       if(strcmp(a[i], p1)>0) p1=a[i];
       if(strcmp(a[i], p2)<0) p2=a[i];
           }
       cout<<p1<<' '<<p2<<endl;
       }




    3. #include<iostream.h>
       int a=5;
       void main() {
       int a=10, b=20;
       cout<<a<<' '<<b<<endl;
       {  int a=0,b=0;
          for(int i=1; i<6; i++) {
               a+=i; b+=a;
          }
          cout<<a<<' '<<b<<' '<<::a<<endl;
       }
       cout<<a<<' '<<b<<endl;
       }




    4. #include<iomanip.h>
       int LB(int *a, int n) {
       int s=1;
       for(int i=0;i<n;i++)
       s*=*a++;
       return s;
       }
       void main() {
       int a[]={1,2,3,4,5,6,7,8};
       int b=LB(a,5)+LB(&a[3],3);
       cout<<"b="<<b<<endl;
       }




    5. #include<iostream.h>
       #include<string.h>
       struct Worker {
       char name[15];  //姓名
       int age;        //年龄
       float pay;      //工资
       };
       void main() {
       Worker x;
       char *t="liouting";
       int d=38; float f=493;
       strcpy(x.name,t);
       x.age=d; x.pay=f;
       cout<<x.name<<' '<<x.age<<' '<<x.pay<<endl;
       }



四、写出下列每个函数的功能(每小题6分,共24分)
    1. #include<iostream.h>
       int SA(int a, int b) {
       if(a>b) return 1;
       else if(a==b) return 0;
       else return -1;
       }




    2. float FI(int n) {
          //n为大于等于1的整数
       float x,y=0;
       do {
       cin>>x;
       n--; y+=x*x;
       } while(n>0);
       return y;
       }




    3. template<class Type>
       void WE(Type a[], Type b[], int n) {
       for(int i=0;i<n;i++)
       b[n-i-1]=a[i];
       }




    4. struct StrNode {
           char name[15];  //字符串域
       StrNode *next;  //指针域
       };
       void QB(StrNode*& f, int n) {
       if(n==0) {f=NULL; return;}
       f=new StrNode;
       cin>>f->name;
       StrNode* p=f;
       while(--n) {
       p=p->next=new StrNode;
       cin>>p->name;
       }
       p->next=NULL;
       }




五、编写程序,把从键盘上输入的一批整数(以-1作为终止输入的标志)保存到文本文件“a:xxk1.dat”中。(10分)




回复列表 (共26个回复)

沙发

怎么?没人知道??????????

板凳

一、单选题(每小题2分,共12分)
AACBDB
三、给出下列程序运行后的输出结果(每小题6分,共30分)
1.
good!pass!bad!well!
2.
worker cadre
3.
10 20
15 35 5
10 20
4.
b=240
5.
liouting 38 493

剩下的自己做。
好好学习,天天向上。
没有白吃的午餐。

3 楼

我的解答---不当处敬请指教

一、单选题(每小题2分,共12分)
1.在每个C++程序中都必须包含有这样一个函数,该函数的函数名为___A____。
A main B MAIN C name D function

2.设x和y均为bool量,则x && y为真的条件是___A_____。
A 它们均为真 B 其中一个为真 C 它们均为假 D 其中一个为假

3. 下面的哪一个保留字不能作为函数的返回类型?__C____
A void B int C new D long

4. 假定a为一个整型数组名,则元素a[4]的字节地址为__C___。
A a+4 B a+8 C a+16 D a+32

5.假定AB为一个类,则执行“AB a(4), b[3], *p[2];”语句时,自动调用该类构造函数的次数为___C___。
A 3 B 4 C 6 D 9

6.假定要对类AB定义加号操作符重载成员函数,实现两个AB类对象的加法,并返回相加结果,则该成员函数的声明语句为:___A___
A AB operator +(AB& a, AB& b);
B AB operator +(AB& a);
C operator +(AB a);
A AB& operator +();

二、填空题(每小题2分,共24分)
1.C++语言中的每条基本语句以_;(分号)_作为结束符,每条复合语句以_}(右花括号)__作为结束符。

2.执行“cout<<char('A'+2)<<endl;”语句后得到的输出结果为__C_____。

3.float和double类型的大小分别为___4字节____和__8字节_____。
4. 算术表达式 对应的C++表达式为__(x*y*y)/(3*a)+4*b-1__。
5. 关系表达式x+y>5的相反表达式为___x+y<=5____。

6.假定一个一维数组的定义为“char* a[8];”,则该数组所含元素的个数为__8___,

所占存储空间的字节数为__8字节____。

7.变量分为全局和局部两种,__局部 变量没有赋初值时,其值是不确定的。

8. 假定a是一个二维数组,则a[i][j]的指针访问方式为__ (*(*(a+i)+j)__。

9. 假定一个结构类型的定义为:

struct D{int a; union {int b; double c;}; D* d[2];};

则该类型的大小为__24__字节。

10.对一个类中的数据成员的初始化可以通过构造函数中的_函数体中赋值___实现,也

可以通过构造函数中的__形参列表初始化__实现。

11. 假定 AB为一个类,则执行”AB a[10];”语句时,系统自动调用该类构造函数的

次数为__10___。

12. 假定类AB中有一个公用属性的静态数据成员bb,在类外不通过对象名访问该

成员bb的写法为__public static bb;______。

三、给出下列程序运行后的输出结果(每小题6分,共30分)
1. #include<iostream.h>
void SB(char ch) {
switch(ch) {
case 'A': case 'a':
cout<<"well!";break;
case 'B': case 'b':
cout<<"good!";break;
case 'C': case 'c':
cout<<"pass!";break;
default:
cout<<"bad!";break;
}
}
void main() {
char a1='b',a2='C',a3='f';
SB(a1);SB(a2);SB(a3);SB('A');
cout<<endl;
}

结果:good!pass!bad!well!


2. #include<iostream.h>
#include<string.h>
void main() {
char* a[5]={"student","worker","cadre","soldier","peasant"};
char *p1, *p2;
p1=p2=a[0];
for(int i=0;i<5;i++) {
if(strcmp(a[i], p1)>0) p1=a[i];
if(strcmp(a[i], p2)<0) p2=a[i];
}
cout<<p1<<' '<<p2<<endl;
}

结果:worker cadre


3. #include<iostream.h>
int a=5;
void main() {
int a=10, b=20;
cout<<a<<' '<<b<<endl;
{ int a=0,b=0;
for(int i=1; i<6; i++) {
a+=i; b+=a;
}
cout<<a<<' '<<b<<' '<<::a<<endl;
}
cout<<a<<' '<<b<<endl;
}

结果: 10 20
15 35 5
10 20

4. #include<iomanip.h>
int LB(int *a, int n) {
int s=1;
for(int i=0;i<n;i++)
s*=*a++;
return s;
}
void main() {
int a[]={1,2,3,4,5,6,7,8};
int b=LB(a,5)+LB(&a[3],3);
cout<<"b="<<b<<endl;
}

结果:b=240


5. #include<iostream.h>
#include<string.h>
struct Worker {
char name[15]; //姓名
int age; //年龄
float pay; //工资
};
void main() {
Worker x;
char *t="liouting";
int d=38; float f=493;
strcpy(x.name,t);
x.age=d; x.pay=f;
cout<<x.name<<' '<<x.age<<' '<<x.pay<<endl;
}

结果:liouting 38 493

四、写出下列每个函数的功能(每小题6分,共24分)
1. #include<iostream.h>
int SA(int a, int b) {
if(a>b) return 1;
else if(a==b) return 0;
else return -1;
}

函数功能:比较两个整数的大小,如果第一个数大则返回1,如果第二个数大则返回-1,相等则返回0。



2. float FI(int n) {
//n为大于等于1的整数
float x,y=0;
do {
cin>>x;
n--; y+=x*x;
} while(n>0);
return y;
}

函数功能:返回1到n之间的平方和。


3. template<class Type>
void WE(Type a[], Type b[], int n) {
for(int i=0;i<n;i++)
b[n-i-1]=a[i];
}

函数功能:将数组a中的数据倒置后放到数组b中。


4. struct StrNode {
char name[15]; //字符串域
StrNode *next; //指针域
};
void QB(StrNode*& f, int n) {
if(n==0) {f=NULL; return;}
f=new StrNode;
cin>>f->name;
StrNode* p=f;
while(--n) {
p=p->next=new StrNode;
cin>>p->name;
}
p->next=NULL;
}

函数功能:创建一个链表,每个表的节点是一个15字符的字符串。


五、编写程序,把从键盘上输入的一批整数(以-1作为终止输入的标志)保存到文本文件“a:xxk1.dat”中。(10分)



程序代码如下:
#include "stdlib.h"
#include "iostream.h"
#include "fstream.h"
void main()
{
int num;
ofstream f("e:\\xxk1.dat",ios::out,ios::binary);
if(!f)
{
cout<<"文件打开失败"<<endl;
exit(1);
}
cin>>num;
while(num!=-1)
{
f.write((char *) &num,sizeof(int));
cin>>num;
}
f.close();
cout<<"文件内容写入成功!"<<endl;
}

4 楼

我的解答---不当处敬请指教

一、单选题(每小题2分,共12分)
1.在每个C++程序中都必须包含有这样一个函数,该函数的函数名为___A____。
A main B MAIN C name D function

2.设x和y均为bool量,则x && y为真的条件是___A_____。
A 它们均为真 B 其中一个为真 C 它们均为假 D 其中一个为假

3. 下面的哪一个保留字不能作为函数的返回类型?__C____
A void B int C new D long

4. 假定a为一个整型数组名,则元素a[4]的字节地址为__C___。
A a+4 B a+8 C a+16 D a+32

5.假定AB为一个类,则执行“AB a(4), b[3], *p[2];”语句时,自动调用该类构造函数的次数为___C___。
A 3 B 4 C 6 D 9

6.假定要对类AB定义加号操作符重载成员函数,实现两个AB类对象的加法,并返回相加结果,则该成员函数的声明语句为:___A___
A AB operator +(AB& a, AB& b);
B AB operator +(AB& a);
C operator +(AB a);
A AB& operator +();

二、填空题(每小题2分,共24分)
1.C++语言中的每条基本语句以_;(分号)_作为结束符,每条复合语句以_}(右花括号)__作为结束符。

2.执行“cout<<char('A'+2)<<endl;”语句后得到的输出结果为__C_____。

3.float和double类型的大小分别为___4字节____和__8字节_____。
4. 算术表达式 对应的C++表达式为__(x*y*y)/(3*a)+4*b-1__。
5. 关系表达式x+y>5的相反表达式为___x+y<=5____。

6.假定一个一维数组的定义为“char* a[8];”,则该数组所含元素的个数为__8___,

所占存储空间的字节数为__8字节____。

7.变量分为全局和局部两种,__局部 变量没有赋初值时,其值是不确定的。

8. 假定a是一个二维数组,则a[i][j]的指针访问方式为__ (*(*(a+i)+j)__。

9. 假定一个结构类型的定义为:

struct D{int a; union {int b; double c;}; D* d[2];};

则该类型的大小为__24__字节。

10.对一个类中的数据成员的初始化可以通过构造函数中的_函数体中赋值___实现,也

可以通过构造函数中的__形参列表初始化__实现。

11. 假定 AB为一个类,则执行”AB a[10];”语句时,系统自动调用该类构造函数的

次数为__10___。

12. 假定类AB中有一个公用属性的静态数据成员bb,在类外不通过对象名访问该

成员bb的写法为__public static bb;______。

三、给出下列程序运行后的输出结果(每小题6分,共30分)
1. #include<iostream.h>
void SB(char ch) {
switch(ch) {
case 'A': case 'a':
cout<<"well!";break;
case 'B': case 'b':
cout<<"good!";break;
case 'C': case 'c':
cout<<"pass!";break;
default:
cout<<"bad!";break;
}
}
void main() {
char a1='b',a2='C',a3='f';
SB(a1);SB(a2);SB(a3);SB('A');
cout<<endl;
}

结果:good!pass!bad!well!


2. #include<iostream.h>
#include<string.h>
void main() {
char* a[5]={"student","worker","cadre","soldier","peasant"};
char *p1, *p2;
p1=p2=a[0];
for(int i=0;i<5;i++) {
if(strcmp(a[i], p1)>0) p1=a[i];
if(strcmp(a[i], p2)<0) p2=a[i];
}
cout<<p1<<' '<<p2<<endl;
}

结果:worker cadre


3. #include<iostream.h>
int a=5;
void main() {
int a=10, b=20;
cout<<a<<' '<<b<<endl;
{ int a=0,b=0;
for(int i=1; i<6; i++) {
a+=i; b+=a;
}
cout<<a<<' '<<b<<' '<<::a<<endl;
}
cout<<a<<' '<<b<<endl;
}

结果: 10 20
15 35 5
10 20

4. #include<iomanip.h>
int LB(int *a, int n) {
int s=1;
for(int i=0;i<n;i++)
s*=*a++;
return s;
}
void main() {
int a[]={1,2,3,4,5,6,7,8};
int b=LB(a,5)+LB(&a[3],3);
cout<<"b="<<b<<endl;
}

结果:b=240


5. #include<iostream.h>
#include<string.h>
struct Worker {
char name[15]; //姓名
int age; //年龄
float pay; //工资
};
void main() {
Worker x;
char *t="liouting";
int d=38; float f=493;
strcpy(x.name,t);
x.age=d; x.pay=f;
cout<<x.name<<' '<<x.age<<' '<<x.pay<<endl;
}

结果:liouting 38 493

四、写出下列每个函数的功能(每小题6分,共24分)
1. #include<iostream.h>
int SA(int a, int b) {
if(a>b) return 1;
else if(a==b) return 0;
else return -1;
}

函数功能:比较两个整数的大小,如果第一个数大则返回1,如果第二个数大则返回-1,相等则返回0。



2. float FI(int n) {
//n为大于等于1的整数
float x,y=0;
do {
cin>>x;
n--; y+=x*x;
} while(n>0);
return y;
}

函数功能:返回1到n之间的平方和。


3. template<class Type>
void WE(Type a[], Type b[], int n) {
for(int i=0;i<n;i++)
b[n-i-1]=a[i];
}

函数功能:将数组a中的数据倒置后放到数组b中。


4. struct StrNode {
char name[15]; //字符串域
StrNode *next; //指针域
};
void QB(StrNode*& f, int n) {
if(n==0) {f=NULL; return;}
f=new StrNode;
cin>>f->name;
StrNode* p=f;
while(--n) {
p=p->next=new StrNode;
cin>>p->name;
}
p->next=NULL;
}

函数功能:创建一个链表,每个表的节点是一个15字符的字符串。


五、编写程序,把从键盘上输入的一批整数(以-1作为终止输入的标志)保存到文本文件“a:xxk1.dat”中。(10分)



程序代码如下:
#include "stdlib.h"
#include "iostream.h"
#include "fstream.h"
void main()
{
int num;
ofstream f("e:\\xxk1.dat",ios::out,ios::binary);
if(!f)
{
cout<<"文件打开失败"<<endl;
exit(1);
}
cin>>num;
while(num!=-1)
{
f.write((char *) &num,sizeof(int));
cin>>num;
}
f.close();
cout<<"文件内容写入成功!"<<endl;
}

5 楼

这也太小儿科了吧!

6 楼

这大概是c++考试的题目吧

7 楼

不过我也要来做一做

8 楼

我应该会做的, 但现在却远离它,我会努力回答的。

9 楼

呵呵,看起来不怎么难嘛。出点有深度的吧/

10 楼

很好的
题目不错啊
没有什么深度适合于本科的期末考试

我来回复

您尚未登录,请登录后再回复。点此登录或注册