【C++】C++ primer plus第二章练习题

拼搏现实的明天。 2024-02-21 03:12 180阅读 0赞

问答题

  1. c++程序的模块叫什么?

函数。

  1. 下面的预处理器编译指令是做什么用的?
  1. #include <iostream>

包含头文件,将iostream文件的内容添加·到代码中

  1. 下面的语句是做什么用的? using namespace std;

using是预编译器指令,让其使用std命名空间

  1. 什么语句可以用来打印短语“Hello, world”,然后开始新的一行?

std::cout<<“Hello world” << std::endl;

  1. 什么语句可以用来创建名为cheeses的整数变量

int cheeses;

  1. 什么语句可以用来将值32赋给变量cheeses?

cheeses = 32
=运算符,将右侧赋值给左侧

  1. 什么语句可以用来将从键盘输入的值读入变量cheeses中?

std::cin>>cheeses;

  1. 什么语句可以用来打印“We have X varieties of cheese,”,其中X为变量cheeses的当前值。

std::cout << “We have “ << cheeses << “ varieties of cheese.” << std::endl;

“We have “ 是字符串
cheeses 是变量名

  1. 下面的函数原型指出了关于函数的哪些信息?

int froop(double t);

void rattle(int n);

int prune(void);

函数名叫froop(),带有一个参数t,参数类型是double类型,并且函数返回一个整值类型。
所以调用函数froop()时,要提供一个double类型的参数,该函数将返回一个int值

函数名叫rattle,带有一个参数n,参数类型是int类型,函数没有返回值

函数名prune,函数没有参数,不接受任何参数,返回一个int值

  1. 定义函数时,在什么情况下不必使用关键字return?

当函数没有返回值时,比如void函数,或者以后要讲的构造函数和析构函数等等。

  1. 假设你编写的main()函数包含如下代码: cout << “Please enter your PIN: “;

而编译器指出cout是一个未知标识符。导致这种问题的原因很可能是什么?指出3种修复这种问题的方法。

原因:在代码中没有声明命名空间,导致编译器不认识

解决办法:
1.using namespace std;
2.using std::cout
3.std::cout

编程题:

  1. 编写一个c++程序,显示姓名地址
  1. 1 #include<iostream>
  2. 2 using namespace std;
  3. 3 int main()
  4. 4 {
  5. 5 cout<<"xioajiejie"<<endl;
  6. 6 cout<<"heibei"<<endl;
  7. 7 return 0;
  8. 8 }

结果:

  1. [bsk@localhost one]$ ./a.out
  2. xioajiejie
  3. heibei
  1. 编写一个c++程序,要求用户输入一个以long为单位的距离,然后将它转换为码(long等于220码)
  1. 1 #include<iostream>
  2. 2 using namespace std;
  3. 3 int main()
  4. 4 {
  5. 5 double distance;
  6. 6 cout<<"Enter the distance(in long)";
  7. 7 cin >> distance;
  8. 8 cout<<"The distance "<<distance<<" long equals " <<220*distance <<endl;
  9. 9 return 0;
  10. 10 }

结果:

  1. Enter the distance(in long)2
  2. The distance 2 long equals 440

3.编写一个c++程序,他使用3个用户定义的函数(包括main(),并生成下面的输出:
Three blind mice
Three blind mice
See how they run
See how they run

其中一个函数要调用两次,该函数生成前两行;另外一个函数也调用两次,并生成其余的输出。

  1. #include<iostream>
  2. using namespace std;
  3. void print_mice(void)
  4. {
  5. cout<<"Three blind mice"<<endl; }
  6. void print_run(void)
  7. {
  8. cout<<"See how they run"<<endl; }
  9. int main()
  10. {
  11. print_mice();
  12. print_mice();
  13. print_run();
  14. print_run();
  15. return 0;
  16. }
  1. 编写一个程序,让用户输入其年龄,然后显示该年龄包含多少个月:

Enter your age:29

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int age;
  6. cout << "Enter your age:";
  7. cin >> age;
  8. cout << "your age include "<< age*12 <<" months"<< endl;
  9. return 0;
  10. }
  1. 编写一个程序,其中main()调用一个用户定义的的函数(以摄氏温度值为参数,并返回相应的华氏温度值)。

该程序按下面的格式要求用户输入摄氏温度值,并显示结果:
Please enter a Celsius value:20
20 degrees Celsius is 69 degrees Fahrenheit.

下面是转换公式:
华氏温度=1.8*摄氏温度+32.0

  1. #include<iostream>
  2. using namespace std;
  3. double CtoF(double C)
  4. {
  5. double F=1.8*C+32.0;
  6. return F;
  7. }
  8. int main()
  9. {
  10. double celsiusValue,fahrenheitValue;
  11. cout << "Please enter a Celsius value: ";
  12. cin >> celsiusValue;
  13. fahrenheitValue=CtoF(celsiusValue);
  14. cout << celsiusValue << " degrees Celsius is " << fahrenheitValue << " degress Fahrenheit" << endl;
  15. }
  1. 编写一个程序,其main()调用一个用户的函数(以光年为参数,并返回对应天文单位的值)。

该程序按下面的格式要求用户输入光年值,并显示结果:
Enter the number of light years: 4.2
4.2 light years =265608 astronomical units.

天文单位是从地球到太阳的平均距离(约150000000公里或93000000英里),光年是光一年走的距离(约10万亿公里或6万亿英里)(除太阳外,最近的恒星大约离地球4.2光年)。请使用double类型,转换公式为:1光年=63240天文单位。

  1. #include<iostream>
  2. using namespace std;
  3. double LtoA(double lYears)
  4. {
  5. double aUnits=lYears*63240;
  6. return aUnits;
  7. }
  8. int main()
  9. {
  10. double lYears;
  11. cout << "Enter the number of light years: ";
  12. cin >> lYears;
  13. double aYears=LtoA(lightYears);
  14. cout << lYears << " light Years =" << aYears << " astronomical units "<< endl;
  15. return 0;
  16. }

发表评论

表情:
评论列表 (有 0 条评论,180人围观)

还没有评论,来说两句吧...

相关阅读

    相关 操作系统第二练习题

    第四部分 判断题 进程是动态的概念。 (T) 进程执行需要处理机。 (T) 进程是有生命期的。 (T) 进程是指令的集合。 (F) 操作系统的一个重要概念是进程,因此

    相关 操作系统第二练习题

    第三部分 选择题 1、在进程管理中,当( )时,进程从阻塞状态变为就绪状态。 C A.进程被进程调度程序选中 B.等待某一事件 C.等待的事件发生

    相关 C Primer Plus 第五

    这次写这段代码,刚开始不是很顺利,主要是卡在了输入过滤上。后来和同学讨论之后,找到了解决办法。用scanf输入的类型不同返回值不同,从而决定是否继续输入。 while(sca

    相关 C++ primer第二

    阅读至2.5.2时,发现一个不知道的知识点: 如果某个类型的别名指代的是复合类型或是常量,那么它用到声明语句里面就会产生意想不到的后果,例如下面的声明语句用到了类型pstr