第二章课后题

一 复习题

1 函数

2 将头文件内容添加到源代码中

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

4 cout << "Hello,World!" << endl;

5 int chesses;

6 int chesses = 32;

7 cin >> chesses;

8 cout << "We have " << chesses <<" varieties of chesses," << endl;

9

​ 1)函数名叫froop 带有一个参数 参数类型是double 并且函数返回是一个整型值

​ 2)函数名是rattle 带有一个参数 参数类型是int 函数无返回值

​ 3)函数名是prune 无参数 函数返回值是整型

10 当函数没有返回值时,可以不用定义return

11

​ 1)using namespace std;

​ 2)using std::cout

​ 3)std::cout

二 编程题

1

c++ 复制代码
#include <iostream>
using namespace std;
int main()
{
	cout << "姓名" << endl;
	cout << "地址" << endl;
	return 0;
}

2

c++ 复制代码
#include <iostream>
using namespace std;
int main()
{
	double distance;
	cout << "Enter the distance (in long):";
	cin >> distance;
	cout << "The distance " << distance << "long" << " equals "
		<< 220 * distance << " yard." << endl;
	return 0;
}

3

c++ 复制代码
#include <iostream>
using namespace std;
void hanshu1();
void hanshu2();
int main()
{
	hanshu1();
	hanshu1();
	hanshu2();
	hanshu2();
	return 0;
}

void hanshu1()
{
	cout << "Three bilnd mice" << endl;
}

void hanshu2()
{
	cout << "See how they run" << endl;
}

4

c++ 复制代码
#include <iostream>
using namespace std;
int main()
{
	int age;
	cout << "Enter your age:";
	cin >> age;
	cout << "包含 " << age * 12 << " 个月" << endl;
	return 0;
}

5

c++ 复制代码
#include <iostream>
using namespace std;
void huashi(double n);
int main()
{
	double sheshi;
	cin >> sheshi;
	huashi(sheshi);
	return 0;
}

void huashi(double n)
{
	cout << "华氏温度: " << 1.8 * n + 32.0 << endl;
}

6

C++ 复制代码
#include <iostream>
using namespace std;
void guangnian(double n);
int main()
{
	double years;
	cout << "Enter the number of light years: ";
	cin >> years;
	guangnian(years);
	return 0;
}
void guangnian(double n)
{
	cout << n << " light years = " << n * 63240 << " astronomical units.";
}

7

c++ 复制代码
#include <iostream>
using namespace std;
void time(int n, int m);
int main()
{
	int j, k;
	cout << "Enter the number of hours: ";
	cin >> j;
	cout << "Enter the number of minutes: ";
	cin >> k;
	time(j, k);
	return 0;
}

void time(int n, int m)
{
	cout << "Time: " << n << ":" << m << endl;
}
相关推荐
我要见SA姐127 分钟前
用生活化类比当钥匙,一键打开 C++ 入门大门
java·开发语言·c++
船神丿男人1 小时前
C++ 背包动态规划六题:看懂倒序、正序和两层循环
c++·动态规划
郝学胜-神的一滴2 小时前
Effective STL 条款 17:用 swap 技巧去掉多余容量
c语言·开发语言·数据结构·c++·算法·rpc
随流寻影4 小时前
利用 C++ 实现快速排序及三种优化
开发语言·c++
西西弗Sisyphus4 小时前
C++ 最小 Agentic Loop 实现 - 回调函数系列 - 指针与内存地址(2)
开发语言·c++
西西弗Sisyphus6 小时前
C++ 最小 Agentic Loop 实现 - 回调函数系列 - typedef 封装(4)
c++
笑鸿的学习笔记6 小时前
C++笔记之信号量和引用计数的概念是否等价?
java·c++·笔记
real_metrix8 小时前
【教程】C/C++ 基础系列 - 内存泄漏最佳实践
c++·c·内存泄漏
fqbqrr9 小时前
2612,C++协程
c++
纪念 22910 小时前
C++ 算法模板与错题 (差分)
开发语言·c++·算法