惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

美团技术团队
罗磊的独立博客
SecWiki News
SecWiki News
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
IT之家
IT之家
博客园 - 聂微东
T
The Exploit Database - CXSecurity.com
Recorded Future
Recorded Future
大猫的无限游戏
大猫的无限游戏
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Vercel News
Vercel News
G
GRAHAM CLULEY
D
DataBreaches.Net
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
SegmentFault 最新的问题
博客园_首页
雷峰网
雷峰网
T
Tenable Blog
Spread Privacy
Spread Privacy
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
酷 壳 – CoolShell
酷 壳 – CoolShell
Cisco Talos Blog
Cisco Talos Blog
V
Visual Studio Blog
J
Java Code Geeks
博客园 - Franky
The Cloudflare Blog
Apple Machine Learning Research
Apple Machine Learning Research
C
CERT Recently Published Vulnerability Notes
T
Threatpost
Google DeepMind News
Google DeepMind News
F
Fortinet All Blogs
P
Privacy International News Feed
T
Threat Research - Cisco Blogs
T
The Blog of Author Tim Ferriss
V
Vulnerabilities – Threatpost
Recent Announcements
Recent Announcements
Blog — PlanetScale
Blog — PlanetScale
Security Latest
Security Latest
U
Unit 42
M
MIT News - Artificial intelligence
Y
Y Combinator Blog
K
Kaspersky official blog
有赞技术团队
有赞技术团队
B
Blog
腾讯CDC

博客园 - 慢步前行

实验0 安装GLUT包及工程的创建与运行 实验2 二维图形几何变换 实验1 时间趋势可视化 《鲜活的数据-第2章 处理数据》有关代码 本博客基本不再更新,请移步至我的CSDN博客 WebGL绘制三角形 WebGL画点程序v3 WebGL画点程序v2 WebGL画点程序v1 三步实现修改hosts方式登录谷歌 Maya API编程快速入门 我的高拍仪自动阅卷系统 实验8 标准模板库STL 实验7 多态与模板 实验6 继承 实验5 运算符重载 实验4 类初步 实验3 文件操作 实验2 C++数组与指针
实验1 C++函数
慢步前行 · 2017-03-03 · via 博客园 - 慢步前行

一.实验目的:

  1. 掌握定义函数的方法、函数实参与形参的对应关系以及“值传递”的方式。
  2. 熟悉函数的嵌套调用和递归调用的方法。
  3. 熟悉全局变量、局部变量概念和使用方式。

二.实验内容:

  1. 运行调试第2章编程示例2-5减法游戏;完成练习题2.5.1,2.5.2和2.5.3;
  2. 运行调试第4章编程示例4-3素因数;完成练习题4.3.1,4.3.2,4.3.3;
  3. 运行调试第4章编程示例4-5汉诺塔;完成练习题4.5.1,4.5.2。

三.示例代码:

  1.第2章编程示例2-5减法游戏:

#include <iostream>

using namespace std;

int main() {
int total, n;

cout << "Welcome to NIM. Pick a starting total: ";
cin >> total;
while (true) {

// Pick best response and print results.

if ((total % 3) == 2) {
total = total - 2;
cout << "I am subtracting 2." << endl;
} else {
total--;
cout << "I am subtracting 1." << endl;
}
cout << "New total is " << total << endl;
if (total == 0) {
cout << "I win!" << endl;
break;
}

// Get user's response; must be 1 or 2.

cout << "Enter number to subtract (1 or 2): ";
cin >> n;
while (n < 1 || n > 2) {
cout << "Input must be 1 or 2." << endl;
cout << "Re-enter: " << endl;
cin >> n;
}
total = total - n;
cout << "New total is " << total << endl;
if (total == 0) {
cout << "You win!" << endl;
break;
}
}
system("PAUSE");
return 0;
}

  2. 第4章编程示例4-3素因数:

#include <math.h>
#include <iostream>
using namespace std;

void get_divisors(int n);

int main() {
int n;

cout << "Enter a number and press ENTER: ";
cin >> n;

get_divisors(n);

cout << endl;
system("PAUSE");
return 0;
}

// Get divisors function
// This function prints all the divisors of n,
// by finding the lowest divisor, i, and then
// rerunning itself on n/i, the remaining quotient.

void get_divisors(int n) {
int i;
double sqrt_of_n = sqrt((double) n);

for (i = 2; i <= sqrt_of_n; i++)
if (n % i == 0) { // If i divides n evenly,
cout << i << ", "; // Print i,
get_divisors(n / i); // Factor n/i,
return; // and exit.
}

// If no divisor is found, then n is prime;
// Print n and make no further calls.

cout << n;
}

  3.第4章编程示例4-5汉诺塔:

#include <cstdlib>
#include <iostream>

using namespace std;
void move_rings(int n, int src, int dest, int other);

int main()
{
int n = 3; // Stack is 3 rings high

move_rings(n, 1, 3, 2); // Move stack 1 to stack 3
system("PAUSE");
return 0;
}

void move_rings(int n, int src, int dest, int other) {
if (n == 1) {
cout << "Move from "<< src <<" to "<< dest << endl;
} else {
move_rings(n - 1, src, other, dest);
cout << "Move from "<< src <<" to "<< dest << endl;
move_rings(n - 1, other, dest, src);
}
}