问题 单项选择题

下面程序的功能是( )。
#include <iostream>
#include <string>
using namespace std;
int main ()

int i=1, n=0;
char s[80],*p;
p=s;
strcpy(p,"It is a book..");
for (; *p !=’ \0’ ;p++)

if(*p==’’)
i=0;
else if (i==0)
n++; i=1;

cout<<"n=" <<n<<end1;
return 0;

A.统计字符串中的单词个数

B.统计字符串中的空格个数

C.统计字符串中的字母个数

D.统计字符串中的全部字符个数

答案

参考答案:B

解析: 本题通过for循环对字符串进行扫描,遇到’\0’结束扫描。程序首先将i的值置为1,n的值置为0。在for循环中,通过if语句判断*p指向的字符是否为空格,如果是空格则将i的值置为0,而在else if语句,如果i的值为0,则n++,然后将i的值重新置为1。如此反复,最后统计出字符串中的空格个数。

填空题
填空题


阅读下列说明、图和Java代码,填补空缺。
[说明]
已知对某载客车辆(Car)进行类建模,如图13-2所示,其中类Engine表示发动机引擎,类Wheel表示车轮,类Body表示车身,类Driver表示司机,类Passenger表示乘客。


[Java代码]
class Body{ //此处代码省略 }; //车身类
class Passenger{ //此处代码省略 }; //乘客类
class Wheel{ //此处代码省略 }; //车轮类
class Driver{ //司机类
public String name; //表示第几路公交车司机
public Driver(String driverName){name=driverName; }
//构造函数
};
class Engine{ //引擎类
public String engineNo; //引擎编号
public Engine(String engineNo){this.engineNo=engineNo; }
//构造函数
};
public class Car{ //汽车类
static final int (1) =7; //定义最多载客数
static final int MAX_WHEELS=5; //定义最多轮胎数
protected Engine engine;
protected Driver driver;
protected Body body=new Body( );
protected Wheel[]wheels;
protected Passenger[]passengers;
public Car(Driver driver){ //构造函数
(2) .driver=driver;
engine=new Engine("TX6536型号引擎");
wheels = new Wheel[MAX_WHEELS];
passengers=new Passenger[MAX_PASSENGERS];
for(int index=0; index<MAX_WHEELS; index++){
wheels[index]=new Wheel( );
}
for(int index=0; index<MAX_PASSENGERS; index++){
passengers[index]=null;
}
}
int getPassengerNumber( ){ //获取车上乘客数量
//此处代码省略
}
void getOnPassenger(Passenger aPassenger ){
//乘客上车
//此处代码省略
}
void run( ){ //开车
if( (3) )(System.out.printin("司机尚未上车!"); return; }
//此处代码省略
}
public static void main(String args[]){
Driver driver=new Driver("第五路公交车司机");
Car car=new Car( (4) );
for(int index=0; index <MAX_PASSENGERS; index++)
car.getOnPassenger( (5) Passenger( ));
car.run( );
}
}