一.实验目的:

  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);
}
}

最新文章

  1. AngularJs之三
  2. [Head First设计模式]山西面馆中的设计模式——观察者模式
  3. iOS 设备标识
  4. 工作随笔——mysql子查询删除原表数据
  5. 【leetcode】Substring with Concatenation of All Words (hard) ★
  6. Ubuntu 10.04 32位桌面版+OpnERP 6.1.1
  7. JS 基于面向对象的 轮播图2
  8. poj 2299 求逆序数
  9. 表单,css
  10. cookie 和 session
  11. 《Programming Hive》读书笔记(两)Hive基础知识
  12. Oracle 修改序列的初始值
  13. javascript 命名空间与运用(前端基础系列)
  14. xBIM WeXplorer xViewer 基本应用
  15. sqlserver 截取字符串(转)
  16. C++ 3D物理引擎库BulletPhysics基本使用
  17. Json常用代码
  18. CSS定位网页中的元素
  19. 第49节:Java集合框架中底层文档的List与Set
  20. ML.NET 0.9特性简介

热门文章

  1. Entity Framework性能影响因素分析
  2. 关于Android中的四大组件(Service的开启与关闭)
  3. Android首次启动时间长优化之预编译提取Odex
  4. LeetCode 500. Keyboard Row (键盘行)
  5. Unity游戏小地图生成
  6. Android 布局属性大全
  7. bzoj3663
  8. [Apple开发者帐户帮助]八、管理档案(3)创建App Store配置文件
  9. [Swift通天遁地]四、网络和线程-(7)检测服务器接口的访问状态:验证请求结果和可访问性
  10. 【原创】Vue项目中各种功能的实现