第2章课后练习
约 4335 字大约 14 分钟
2026-11-09
习题2-01 P60
C++语言有哪些主要的特点和优点?
特点:
- 全面兼容C语言
- 支持面向对象的方法
- 数据封装,数据隐藏,继承和重用,多态性,泛型编程
优点:
- 兼容C语言,既支持面向对象的程序设计,又支持面向过程的程序设计
- C++引入了面向对象的概念,可方便地模拟出现实问题的实体和操作,使得开发人机交互类型的应用程序更为简单、快捷
习题2-02 P60
下列标志符哪些是合法的?
Program //合法
-page //不合法,不能有“-”,也不能用其作为开头
_lock //合法
test2 //合法
3in1 //不合法,不能以数字开头
@mail //不合法,不能有“@”,也不能用“@”开头
A_B_C_D //合法C++标识符的构成规则
- 以大小写字母或下划线开头
- 可以由大小写字母、下划线、数字(0~9)组成
- 不能是C++语言关键字或操作符
习题2-03 P60
解释每条语句的作用
//包含头文件,#inlcude被称为预处理指令,文件iostream
//中声明了该程序所需要的输入和输出操作的有关信息
#include<iostream>
//“using namespace”是针对命名空间的指令
//不加这个的话,cout前要加std::
using namespace std;
//主函数
int main()
{
//将双引号内的字符串输出到标准输出设备
cout<<"Hello!"<<endl;
cout<<"Welcome to C++"<<endl;
//退出main函数,以0作为返回值
return 0;
}习题2-04 P60
请写出C++语句声明一个常量PI,值为3.1416;再声明一个浮点型变量a,把PI的值赋给a。
const auto PI=3.1416;
float a=PI;习题2-05 P60
注释有什么作用?C++语言中有哪几种注释方法?它们之间有什么区别?
- 注释的作用:对程序进行注解和说明,以便于阅读,编译系统在对源程序进行编译时不理会注释部分。
- C++有两种注释方法,一种是用
/*和*/括起注释文字
/* This is
a comment.
*/- 另一种是用
//,从//开始到它所在的行尾,所有字符都被作为注释处理。
//This is a comment.
int i; //i is an integer习题2-06 P60
什么叫做表达式?x=5+7是一个表达式吗?它的值是多少?
表达式--用于计算的公式,可被定义为:
- 一个常量或标识对象的标识符是最简单的表达式,其值是常量或对象的值
- 一个表达式的值可以用来参与其他操作(即用作其他运算符的操作数),形成了更复杂的表达式
- 包括在括号中的表达式仍是一个表达式,类型和值与未加括号时的表达式相同
x=5+7是一个表达式,它的值为12
习题2-07 P60
下列表达式的值是多少?
201/4 //50
201%4 //1
201/4.0 //50.25习题2-08 P60
执行完下列语句后,a、b、c三个变量的值分别是多少?
a=30; //a=30
b=a++; //a=31,b=30
c=++a; //a=32,b=30,c=32
//综上:a=32,b=30,c=32习题2-09 P60
在一个for语句中可以给多个变量赋初值吗?如何实现?
可以,实现如下:
for(int i=1,j=2;i+j<=3;i++,j++){//i,j只在内部生效
cout<<i<<endl<<j<<endl;
}
for(i=1,j=2;i+j<=3;i++,j++){//需要在外声明i,j
cout<<i<<endl<<j<<endl;
}习题2-10 P60
执行完下列语句后,n的值为多少?
int n;
for(n=0;n<100;n++);100
习题2-11 P60
写一条for语句,计数条件为n从100到200,步长为2;然后用while和do...while实现相同的循环
for(int n=100;n<=200;n+=2){
//操作
}int n=100;
while(n<=200){
//操作
n+=2;
}int n=100;
do{
//操作
n+=2;
}while(n<=200);习题2-12 P60
if(x=3)和if(x==3)这两条语句的差别是什么?
- 一个“=”是赋值运算符,
if(x=3)括号中的表达式为赋值表达式,值为3,为非零值,故该语句等价为if(1),无论如何都会进入if - 两个“=”是关系运算符,若x与3相等则进入if,否则不进入
习题2-13 P60
已知x,y两个变量,写一条简单的if语句,把较小的值赋给原本值较大的变量。
if(x>y) x=y;
else y=x;习题2-14 P60
修改下面程序中的错误,改正后它的运行结果是什么?
#include<iostream>
using namespace std;
int main()
{
/*末尾没加分号*/
int i
int j;
i=10; //给i赋值
j=20; //给j赋值
/*左边双引号应为英文符号,右边缺一个双引号*/
cout<<“i+j=<<i+j; //输出结果
return 0;
}改正后:
#include<iostream>
using namespace std;
int main()
{
/*末尾没加分号*/
int i;
int j;
i=10; //给i赋值
j=20; //给j赋值
/*左边双引号应为英文符号,右边缺一个双引号*/
cout<<"i+j="<<i+j; //输出结果
return 0;
}输出结果:i+j=30
习题2-15 P61
/*
习题2-15 P61
编写一个程序,运行时提示输入一个数字,再把这个数字显示出来
*/
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int a;
cout<<"请输入一个数字:";
cin>>a;
cout<<"您输入的数字为:"<<a<<endl;
system("pause");
return 0;
}习题2-16 P61
/*
习题2-16 P61
C++语言有哪几种数据类型?简述其值域。编程显示你使用的计算机中的各种数据的字节数
书上表格:
=====================================================================
类型名 字节数 取值范围
=====================================================================
bool 1 false,true
---------------------------------------------------------------------
char 1 -128~127
signed char 1 -128~127
unsigned char 1 0~255
short 2 -32768~32767
unsigned short 2 0~65535
int 4 -2^31~2^31-1
unsigned int 4 0~2^32-1
long 4 -2^31~2^31-1
unsigned long 4 0~2^32-1
long long 8 -2^63~2^63-1
unsigned long long 8 0~2^64-1
---------------------------------------------------------------------
float 4 3.4*10^(-38)~3.4*10^38
double 8 1.7*10(-308)~1.7*10^308
long double 8 1.7*10(-308)~1.7*10^308
=====================================================================
程序输出结果:
bool字节数:1
char字节数:1
signed char字节数:1
unsigned char字节数:1
short字节数:2
unsigned short字节数:2
int字节数:4
unsigned int字节数:4
long字节数:4
unsigned long字节数:4
long long字节数:8
unsigned long long字节数:8
float字节数:4
double字节数:8
long double字节数:16
long double在我的计算机上为16字节,与书上不同,其余相同
*/
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
cout<<"bool字节数:"<<sizeof(bool)<<endl;
cout<<"char字节数:"<<sizeof(char)<<endl;
cout<<"signed char字节数:"<<sizeof(signed char)<<endl;
cout<<"unsigned char字节数:"<<sizeof(unsigned char)<<endl;
cout<<"short字节数:"<<sizeof(short)<<endl;
cout<<"unsigned short字节数:"<<sizeof(unsigned short)<<endl;
cout<<"int字节数:"<<sizeof(int)<<endl;
cout<<"unsigned int字节数:"<<sizeof(unsigned int)<<endl;
cout<<"long字节数:"<<sizeof(long)<<endl;
cout<<"unsigned long字节数:"<<sizeof(unsigned long)<<endl;
cout<<"long long字节数:"<<sizeof(long long)<<endl;
cout<<"unsigned long long字节数:"<<sizeof(unsigned long long)<<endl;
cout<<"float字节数:"<<sizeof(float)<<endl;
cout<<"double字节数:"<<sizeof(double)<<endl;
cout<<"long double字节数:"<<sizeof(long double)<<endl;
system("pause");
return 0;
}习题2-17 P61
/*
习题2-17 P61
输出ASCII码为32~127的字符
*/
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
for(unsigned char i=32;i<=127;i++){
cout<<i<<ends;
}
cout<<endl;
system("pause");
return 0;
}习题2-18 P61
/*
习题2-18 P61
运行下面程序,观察其输出,与你的设想是否相同?
设想:
Difference is:50
Now difference is:4294967246
程序输出结果与设想相同
*/
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
unsigned int x;
unsigned int y=100;
unsigned int z=50;
x=y-z;
cout<<"Difference is:"<<x<<endl;
x=z-y;
cout<<"\nNow difference is:"<<x<<endl;
system("pause");
return 0;
}习题2-19 P61
/*
习题2-19 P61
运行下面程序,观察其输出,体会i++和++i的区别
相同之处:执行i++和++i之后,i的值都会+1
不同之处:i++表达式表示i+1前的值,++i表达式表示i+1之后的值
*/
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int myAge=39;
int yourAge=39;
cout<<"I am:"<<myAge<<" years old."<<endl;
cout<<"You are:"<<yourAge<<" years old."<<endl;
myAge++;
++yourAge;
cout<<"One year passes..."<<endl;
cout<<"I am:"<<myAge<<" years old"<<endl;
cout<<"You are:"<<yourAge<<" years old."<<endl;
cout<<"Another year passes..."<<endl;
cout<<"I am:"<<myAge++<<" years old"<<endl;
cout<<"You are:"<<++yourAge<<" years old."<<endl;
cout<<"Let's print it again."<<endl;
cout<<"I am:"<<myAge<<" years old."<<endl;
cout<<"You are:"<<yourAge<<" years old."<<endl;
system("pause");
return 0;
}习题2-20 P62
/*
习题2-20 P62
什么是常量?什么是变量?
常量:程序运行过程中其值始终不可改变的量
变量:程序运行过程中其值可以变化的量,变量需要用名字来标识
*/
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
//整型常量:八进制,十进制,十六进制
cout<<016<<ends;
cout<<16<<ends;
cout<<0x16<<endl;
//实型常量:一般形式,指数形式
cout<<12.5<<ends;
cout<<-12.5<<endl;
cout<<0.35E+2<<ends;
cout<<-34.4E-3<<ends;
cout<<.123E-1<<ends;
cout<<12.E2<<ends;
cout<<1.E-3<<endl;
//字符常量
cout<<"a"<<ends;
cout<<"a\n\t\?"<<ends;
cout<<"\x22 \42"<<endl;
//字符串常量
cout<<"This is a string."<<endl;
cout<<"Please enter \"YES\" or \"NO\""<<endl;
//布尔常量
cout<<true<<ends<<false<<endl;
//变量的初始化
int ss=3;
double f=3.56;
char c='a';
int b;//变量的声明
b=66;//变量初始化
//初始化方式
double pi=3.1516926;
int a=pi;
int a={3};//列表初始化,不能丢失信息,double转int会丢失信息
int a{3};//列表初始化
int a(pi);
system("pause");
return 0;
}习题2-21 P62
/*
习题2-21 P62
写出下列表达式的值
2<3 && 6<9
!(4<7)
!(3>5)||(6<2)
*/
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
cout<<(2<3 && 6<9)<<endl; //1
cout<<(!(4<7))<<endl; //0
cout<<(!(3>5)||(6<2))<<endl; //1
system("pause");
return 0;
}习题2-22 P62
/*
习题2-22 P62
若a=1,b=2,c=3,下列各式的结果是什么
a|b-c
a^b&-c
a&b|c
a|b&c
*/
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int a=1,b=2,c=3;
//注意运算次序+-*/大于<><=>=大于==!=大于位大于逻辑大于赋值
cout<<(a|(b-c))<<endl; //-1
cout<<((a^b)&(-c))<<endl; //1
cout<<(a&b|c)<<endl; //3
cout<<(a|b&c)<<endl; //3
system("pause");
return 0;
}习题2-23 P62
/*
习题2-23 P62
若a=1,下列各式的结果是什么?
!a|a
~a|a
a^a
a>>2
*/
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int a=1,b=2,c=3;
//注意运算次序+-*/大于<><=>=大于==!=大于位大于逻辑大于赋值
cout<<(!a|a)<<endl; //1
cout<<(~a|a)<<endl; //-1(所有位都是1,有符号-1,无符号为2^n-1)
cout<<(a^a)<<endl; //0
cout<<(a>>2)<<endl; //0
system("pause");
return 0;
}习题2-24 P62
/*
习题2-24 P62
编写一个完整的程序,实现功能:
向用户提问“现在正在下雨吗?”,提示用户输入Y或N。若输入为Y,显示“现在正在下雨。”;
若输入为N,显示“现在没有下雨。”;否则继续提问“现在正在下雨吗?”。
*/
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
unsigned char isRain;
do{
cout<<"现在正在下雨吗?(请输入Y/N)"<<endl;
cin>>isRain;
if(isRain=='Y') cout<<"现在正在下雨。"<<endl;
else if(isRain=='N') cout<<"现在没有下雨。"<<endl;
}while(isRain!='Y'&&isRain!='N');
system("pause");
return 0;
}习题2-25 P62
/*
习题2-25 P62
编写一个完整的程序,运行时向用户提问“你考试考了多少分?(0~100)",
接收输入后判断其等级并显示出来。规则如下:
优 90<=分数<=100
良 80<=分数<=90
中 60<=分数<=80
差 0<=分数<=60
*/
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int score;
while(1)
{
cout<<"你考试考了多少分?(0~100)"<<endl;
cin>>score;
if(score>=90 && score<=100){
cout<<"您的等级为:优"<<endl;
}else if(score>=80 && score<=90){
cout<<"您的等级为:良"<<endl;
}else if(score>=60 && score<=80){
cout<<"您的等级为:中"<<endl;
}else if(score>=0 && score<=60){
cout<<"您的等级为:差"<<endl;
}
}
system("pause");
return 0;
}习题2-26 P62
/*
习题2-26 P62
实现一个简单的菜单程序,运行时显示“Menu:A(dd)D(elete)S(ort)Q(uit)Select one:”
提示用户的输入,A表示增加,D表示删除,S表示排序,Q表示退出,输入为A、D、S时分别提示
“数据已经增加、删除、排序。”输入为Q时程序结束。
(1)要求使用if...else语句进行判断,用break、continue控制程序流程
(2)要求使用switch语句
*/
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
unsigned char user_input;
while(1)//死循环
{
cout<<"Menu:A(dd)D(elete)S(ort)Q(uit)Select one:"<<endl;
cin>>user_input;
if(user_input!='Q'){//输入不为Q,打印对应信息
switch(user_input)
{
case 'A':
cout<<"数据已经增加。"<<endl;
break;
case 'D':
cout<<"数据已经删除。"<<endl;
break;
case 'S':
cout<<"数据已经排序。"<<endl;
break;
}
}else break;//输入为Q,跳出死循环
}
system("pause");
return 0;
}习题2-27 P62
/*
习题2-27 P62
用穷举法找出1~100的质数并显示出来。分别使用while、do...while、for循环语句实现
质数:除了1和自身无其他因子
*/
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int num=1,flag;
//while
cout<<"while"<<endl;
while(num<=100)
{
flag=0;
for(int i=1;i<=num;i++){
if(num%i==0){
if(i!=num && i!=1) flag++; //每有一个除1和num的因子则flag++
}
}
if(!flag) cout<<num<<ends;//flag为0的数就是质数
num++;
}
cout<<endl;
//do...while
cout<<"do...while"<<endl;
num=1;
do{
flag=0;
for(int i=1;i<=num;i++){
if(num%i==0){
if(i!=num && i!=1) flag++; //每有一个除1和num的因子则flag++
}
}
if(!flag) cout<<num<<ends;//flag为0的数就是质数
num++;
}while(num<=100);
cout<<endl;
//for
cout<<"for"<<endl;
for(num=1;num<=100;num++){
flag=0;
for(int i=1;i<=num;i++){
if(num%i==0){
if(i!=num && i!=1) flag++; //每有一个除1和num的因子则flag++
}
}
if(!flag) cout<<num<<ends;//flag为0的数就是质数
}
cout<<endl;
system("pause");
return 0;
}习题2-28 P62
/*
习题2-28 P62
比较break语句与continue语句的不同用法
break:跳出循环体/switch
continue:跳出本次循环
*/
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
for(int i=1;i<=10;i++){
if(i%2==0) continue; //i为偶数跳出此次循环
if(i>=8) break; //i大于8跳出循环
cout<<i<<ends;
}
//最后输出1 3 5 7
system("pause");
return 0;
}习题2-29 P63
/*
习题2-29 P63
在程序定义一个整型变量,赋以1~100的值,要求用户猜这个数,比较两个数的大小,
把结果提示给用户,直到猜对为止。分别使用while、do...while语句实现循环
*/
#include<stdio.h>
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
//while实现
// srand(time(0));//初始化起始值,不加这个的话,每次执行程序获得的随机数是一样的
// int num=rand()%100+1;
// int guess;
// cout<<num<<endl;//显示生成的数字(调试用)
// while(1)
// {
// cout<<"请猜数(1~100)"<<endl;
// cin>>guess;
// if(guess>num) cout<<"你猜的太大了!"<<endl;
// else if(guess<num) cout<<"你猜的太小喽!"<<endl;
// else{cout<<"你猜对了!"<<endl;break;}
// }
//while实现
//do...while实现
srand(time(0));//初始化起始值,不加这个的话,每次执行程序获得的随机数是一样的
int num=rand()%100+1;
int guess;
cout<<num<<endl;//显示生成的数字(调试用)
do{
cout<<"请猜数(1~100)"<<endl;
cin>>guess;
if(guess>num) cout<<"你猜的太大了!"<<endl;
else if(guess<num) cout<<"你猜的太小喽!"<<endl;
else{cout<<"你猜对了!"<<endl;break;}
}while(guess!=num);
//do...while实现
system("pause");
return 0;
}习题2-30 P63
/*
习题2-30 P63
口袋中有红、黄、白、黑5种颜色的球若干个。每次从口袋中取出3个不同颜色的球,问有多少种取法?
计算机穷举法
5个球对应编号为1~5
穷举方式:
取3轮,每轮3次
避免球相同:第一次取的第二次不能取,第三次也不能取;第二次取的第三次不能取
避免情况重复:第一轮第n次取的,第二轮第n次不能取,第三轮第n次也不能取
*/
#include<stdio.h>
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
cout<<""<<endl;
int sum=0;
for(int i=1;i<=5-2;i++){
for(int j=i+1;j<=5;j++){
for(int k=j+1;k<=5;k++){
cout<<i<<j<<k<<endl;
sum++;
}
}
}
cout<<"取法个数:"<<sum<<endl;
system("pause");
return 0;
}习题2-31 P63
/*
习题2-31 P63
输出九九乘法表
*/
#include<stdio.h>
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
for(int i=1;i<=9;i++){//共9行
for(int j=1;j<=i;j++){
cout<<i<<"*"<<j<<"="<<i*j<<"\t";//每行的信息
}
cout<<endl;//每行结束要换行
}
system("pause");
return 0;
}习题2-32 P63
有符号整数和无符号整数,在计算机内部是如何区分的?
- 在计算机内存中,整数以二进制补码的形式存储,在内存中无法区分符号。不加以说明,无法知道一个二进制数字的表达含义。
- 在程序中,可以对整数的符号进行区分,int为有符号的,unsigned int是无符号的,加上变量类型说明,便可以知道二进制数字是有符号整数还是无符号整数了。
- 1000 0001无符号时为128,有符号时为-127
