实验1 C++函数
一.实验目的:
- 掌握定义函数的方法、函数实参与形参的对应关系以及“值传递”的方式。
- 熟悉函数的嵌套调用和递归调用的方法。
- 熟悉全局变量、局部变量概念和使用方式。
二.实验内容:
- 运行调试第2章编程示例2-5减法游戏;完成练习题2.5.1,2.5.2和2.5.3;
- 运行调试第4章编程示例4-3素因数;完成练习题4.3.1,4.3.2,4.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 C++函数的更多相关文章
- 第三次实验计算分段函数 第四次计算分段函数和循环NEW 第五次分支+循环加强版 实验报告
一.实验题目,设计思路,实现方法 第四次分支+循环 加强版 (2-2计算个人所得税,2-7 装睡,2-8计算天数) 设计思路:2-2 用if-else的语句,与计算分段函数的题类似的做法:2-7 运用 ...
- C++ 实验2:函数重载、函数模板、简单类的定义和实现
1.函数重载编程 编写重载函数add(),实现对int型,double型,Complex型数据的加法.在main()函数中定义不同类型数据,调用测试. #include <iostream> ...
- 用typedef定义函数指针的问题
在学习windows API的时候,遇到下面这段代码 以前见过的typedef的用法都是给一个数据类型取一个别名 typedef oldTypeName newTypeName 这种给数据类型 ...
- 「c++小学期」实验题目及代码
面向对象编程的C++,和平时做题用的C++还是有差距的.实验的题目都是小题目,就都做一下吧.(没放代码的为要验收的 实验一 简单C++程序设计 1. 猜价格游戏 编写C++程序完成以下功能: (1) ...
- 继承自NSObject的不常用又很有用的函数(2)
函数调用 Objective-C是一门动态语言,一个函数是由一个selector(SEL),和一个implement(IML)组成的.Selector相当于门牌号,而Implement才是真正的住户( ...
- 转:一个Sqrt函数引发的血案
转自:http://www.cnblogs.com/pkuoliver/archive/2010/10/06/1844725.html 源码下载地址:http://diducoder.com/sotr ...
- [转载]求平方根sqrt()函数的底层算法效率问题
我们平时经常会有一些数据运算的操作,需要调用sqrt,exp,abs等函数,那么时候你有没有想过:这个些函数系统是如何实现的?就拿最常用的sqrt函数来说吧,系统怎么来实现这个经常调用的函数呢? 虽然 ...
- Sqrt函数高效实现
转自一个Sqrt函数引发的血案 我们平时经常会有一些数据运算的操作,需要调用sqrt,exp,abs等函数,那么时候你有没有想过:这个些函数系统是如何实现的?就拿最常用的sqrt函数来说吧,系统怎么来 ...
- 一个Sqrt函数引发的血案(转)
作者: 码农1946 来源: 博客园 发布时间: 2013-10-09 11:37 阅读: 4556 次 推荐: 41 原文链接 [收藏] 好吧,我承认我标题党了,不过既然你来了, ...
- .net中获取图像缩略图的函数GetThumbnailImage
关于.net中获取图像缩略图的函数GetThumbnailImage的一些认识. 在很多图像软件中,打开一幅图像的时候都会显示其缩略图,在看图软件中这样的需求更为常见.如何快速的获取缩略图的信息并提供 ...
随机推荐
- HDU——2824 The Euler function
The Euler function Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HUD——1286 找新朋友
思路: 裸的欧拉函数 代码: #include<cstdio> #include<cstring> #include<cstdlib> #include<io ...
- Python实例--C#执行Python脚本,传参
# -*- coding: utf-8 -*- # 第一行的目的,是为了让代码里面,可以有中文注释信息. (否则要运行报错) # 这个 Python 脚本, 用于被 C# 来调用. # 简单测试 He ...
- spark sql读hbase
项目背景 spark sql读hbase据说官网如今在写,但还没稳定,所以我基于hbase-rdd这个项目进行了一个封装,当中会区分是否为2进制,假设是就在配置文件里指定为#b,如long#b,还实用 ...
- unity3d-23种设计模式全解析
http://www.jianshu.com/nb/4161593 2016.08.03 09:26 字数 1203 阅读 584评论 0喜欢 14 希望大家能共同学习,交流 谢谢支持zero(QQ: ...
- Android 自己定义圆圈进度并显示百分比例控件(纯代码实现)
首先,感谢公司能给我闲暇的时间,来稳固我的技术,让我不断的去探索研究,在此不胜感激. 先不说实现功能,上图看看效果 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZ ...
- DeepDive is a system to extract value from dark data.
DeepDive is a system to extract value from dark data. http://deepdive.stanford.edu/
- ASP.NET无法检测IE10浏览器,导致无法登录
今天发现在IE10中打开我开发的网站时,无法登入,页面总会自动重新退出到登录页,后经上网查资料发现这是ASP.NET 2.0.3.5和4.0的Bugs,因这些版本的.NET Framework无法识别 ...
- 无限极分类算法 thinkphp
<?php/** 本类实现的是无限级递归分类的管理*/class InfiniteLevel{ public $id_str=""; public function add_ ...
- 北大zhw大神bzoj1756代码
#include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #i ...