实验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的一些认识. 在很多图像软件中,打开一幅图像的时候都会显示其缩略图,在看图软件中这样的需求更为常见.如何快速的获取缩略图的信息并提供 ...
随机推荐
- sql-server-storage-internals
https://www.simple-talk.com/sql/database-administration/sql-server-storage-internals-101/
- 【python】字符遍历
Python为我们提供了很多便捷的方式去遍历一个字符串中的字符.比如,将一个字符串转换为一个字符数组(列表): theList = list(theString) 同时,我们可以方便的通过for语句进 ...
- MVC中从控制器到视图的数据传递方法汇总
1.ViewData对象概述ViewData是一种字典集合数据,是"视图基类"和"控制器基类"的属性常见用法是在控制器中写入数据,在视图中读取数据ViewDat ...
- [Vue @Component] Load Vue Async Components
Vue provides a straight-forward syntax for loading components at runtime to help shave off initial b ...
- SuperSwipeRefreshLayout 一个功能强大的自己定义下拉刷新组件
SuperSwipeRefreshLayout 一个功能强大的自己定义下拉刷新组件. Why? 下拉刷新这样的控件.想必大家用的太多了,比方使用非常多的XListView等. 近期.项目中非常多列表都 ...
- window.setTimeout() 和window.setInterval() 的差别
setTimeout 和setInterval的功能都是经过某一个时间段后发生某件指定的事件或者方法. 如window.setTimeout("sleep()",5000);指的是 ...
- cocos2d-x-3.1 数据结构之Vector (coco2d-x 学习笔记六)
介绍 cocos2d::Vector<T>是一个封装好的能动态增长顺序訪问的容器. cocos2d::Vector<T>中的元素是按序存取的,它的低层实现数据结构是标准模版库中 ...
- 数学之路-python计算实战(17)-机器视觉-滤波去噪(中值滤波)
Blurs an image using the median filter. C++: void medianBlur(InputArray src, OutputArray dst, int ks ...
- cacheed 限制 4节点 3000万 es 批量删除 shell脚本练习 elasticsearch_action
文件分割 "www.laiwunews.cn/xinxi/25324717.html""www.zznews.cn/xinxi/10411214.html"&q ...
- 洛谷 P1498 南蛮图腾 —— 模拟
题目:https://www.luogu.org/problemnew/show/P1498 大约一年前该做的题...现在来填一下坑: 然而不怎么会模拟!还滚去看TJ了: 就是翻倍复制: \ 这个符号 ...