全国计算机等级考试二级教程-C语言程序设计_第11章_对函数的进一步讨论
无论传递什么参数,函数都有副本机制
改变一个变量,需要传入变量的地址
改变一个指针变量,需要传入指针变量的地址
//int add(int a, int b);挖取函数声明
//int ()(int a, int b);换成括号
//int (*p)(int a, int b);加上*指针名
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h>
#include<stdlib.h> int add(int a, int b)
{
return a + b;
} main()
{
//int add(int a, int b);挖取函数声明
//int ()(int a, int b);换成括号
//int (*p)(int a, int b);加上*指针名 int(*p)(int a, int b) = add;
printf("%d", p(, )); system("pause");
}
例11.2
通过给 trans 函数传送不同的函数名,求 tan x 和 cot x 值。
#include <stdio.h>
#include <math.h>
double tran(double(*) (double), double(*) (double), double); /* 函数说明语句 */
main()
{
double y, v;
v = * 3.1416 / 180.0;
y = tran(sin, cos, v); /* 第一次调用 */
printf("tan(60)=%10.6f\n", y);
y = tran(cos, sin, v); /* 第二次调用 */
printf("cot(60)=%10.6f\n", y);
}
double tran(double(*f1) (double), double(*f2) (double), double x)
{
return (*f1) (x) / (*f2)(x);
}
例11.3
用递归的方法求n!
求n!可用以下数学关系表示:
n!= 1 当n=0时
n!= n * ( n - 1 )! 当n>0时
#include <stdio.h>
int fac(int n)
{
int t;
if (n == || n == )
{
return ;
}
else
{
t = n*fac(n - );
return t;
}
}
main()
{
int m, y;
printf("Enter m:");
scanf("%d", &m);
if (m < )
{
printf("Input data error !\n");
}
else
{
y = fac(m);
printf("\n%d!=%d\n", m, y);
}
}
例11.4
用递归算法根据以下求平方根的迭代公式求某数 a 的平方根:
x1 = (x0 + a / x0) / 2
#include <stdio.h>
#include <math.h>
double mysqrt(double a, double x0)
{
double x1;
x1 = (x0 + a / x0) / 2.0;
if (fabs(x1 - x0) > 0.00001)
{
return mysqrt(a, x1);
}
else
{
return x1;
}
}
main()
{
double a;
printf("Enter a:");
scanf("%lf", &a);
printf("The sqrt of %f=%f\n", a, mysqrt(a, 1.0));
}
11.12
请编写递归函数,把输入的一个整数转换成二进制数输出。
#include <stdio.h>
void outninary(int a)
{
int d;
d = a % ;
if (a != )
{
outninary(a / );
printf("%d", d);
}
}
main()
{
int a;
scanf("%d", &a);
outninary(a);
}
11.13
请用递归算法,求 1+2+3+...+n,n 由键盘输入。
#include <stdio.h>
int sum(int n)
{
if (n != )
{
return n + sum(n - );
}
else
{
return ;
}
}
main()
{
int n, y;
scanf("%d", &n);
y = sum(n);
printf("y=%d", y);
}
11.14
请用递归算法,求数列 Fibonacci。求 n 阶 Fibonacci 数列的公式如下:
1 当 n=0 时
F(n)= 1 当 n=1 时
F(n-1)+F(n-2) 当 n>1 时
#include <stdio.h>
int f(int n)
{
if (n == || n == )
{
return ;
}
else
{
return (f(n - ) + f(n - ));
}
}
main()
{
int n, y;
scanf("%d", &n);
y = f(n);
printf("y=%d", y);
}
全国计算机等级考试二级教程-C语言程序设计_第11章_对函数的进一步讨论的更多相关文章
- 全国计算机等级考试二级教程-C语言程序设计_第4章_选择结构
switch什么时候用break,什么时候不用break 调用break:一次执行一个分支,输入一个数据,对应一个级别 不调用break:连续执行多个分支 if...else 可以处理任何情况,大于小 ...
- 全国计算机等级考试二级教程-C语言程序设计_第8章_地址和指针
面试: unsigned int *p1 = # int *p2 = # #define _CRT_SECURE_NO_WARNINGS #include<std ...
- 全国计算机等级考试二级教程-C语言程序设计_第15章_位运算
位运算,不适用于实数,仅仅适用于整数.字符. C语言的位运算只能操作整数.字符,实数是指数方式表示的,不适用于位运算. #define _CRT_SECURE_NO_WARNINGS #include ...
- 全国计算机等级考试二级教程-C语言程序设计_第9章_数组
四维数组,可用于航天卫星,三维+时间 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> ...
- 全国计算机等级考试二级教程-C语言程序设计_第14章_结构体、共用体和用户定义类型
函数的返回值是结构体类型 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> struct ...
- 全国计算机等级考试二级教程-C语言程序设计_第10章_字符串
字符型指针数组 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> //参数中,int a ...
- 全国计算机等级考试二级教程-C语言程序设计_第5章_循环结构
for循环结构的嵌套 外层循环每循环一次,内层循环会完整循环一次. 外层循环是竖. 内层循环是横. for, do...while, while的选择: 如果有固定次数,如阶乘! ,判断素数,用 fo ...
- 全国计算机等级考试二级教程-C语言程序设计_第3章_顺序结构
1输入两个整数给变量x和y:然后输出x和y:在交换x和y中的值后,在输出x和y. #include <stdio.h> main() { int x, y, t; printf(" ...
- 全国计算机等级考试二级教程-C语言程序设计_第2章_C程序设计的初步知识
正负号与被除数一致. 3 % (-5) == 3 (-3) % 5 == -3 不用求余运算符,求出余数. int x, y; 答:x - x / y * y; const int i = 10; c ...
- 全国计算机等级考试二级教程-C语言程序设计_第7章_函数
函数执行,从右到左执行 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> main() ...
随机推荐
- CentOS下安装Nginx并添加nginx_upload_module
安装前,最好能保证依赖的系统软件已经升级. yum update CentOS上安装Nginx,如果只是简单安装,不附加其他第三方模块,一句话可以搞定: yum install nginx ...
- Java 并发专题 :FutureTask 实现预加载数据 在线看电子书、浏览器浏览网页等
继续并发专题~ FutureTask 有点类似Runnable,都可以通过Thread来启动,不过FutureTask可以返回执行完毕的数据,并且FutureTask的get方法支持阻塞. 由于:Fu ...
- java设计模式--创建模式--建造者模式
对于建造者模式,小编个人理解为就是一个组装型的模式. 建造者模式 概述 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. 适用性 1.当创建复杂对象的算法应该独立于该对象的组 ...
- MFC子窗口和父窗口(SetParent,SetOwner)
一.概念和区别 在windows系统中,每个窗口对象都对应有一个数据结构,形成一个list链表.系统的窗口管理器通过这个list来获取窗口信息和管理每个窗口.这个数据结构中有四个数据用来构建list, ...
- cf468A 24 Game
A. 24 Game time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- Vim应用
:q!不保存退出 :set number显示行数 :wq保存并退出 ==先输入100,再输入==.从这行开始向下100行,进行自动缩进对齐
- convert用法(数据库中原本储存的格式是Nvarchar,如何修改成datetime格式)
查询这张表得到的数据如图 select CONVERT(nvarchar,substring([purchase-date],1,4)) +'-'+CONVERT(nvarchar,substring ...
- python中pip的使用和安装
Ubuntu下安装pip的方法 安装pip的方法: Install pip and virtualenv for Ubuntu 10.10 Maverick and newer $ sudo ...
- Javascript 【JSON对象】
var box = JSON.parse(json); //将字符串解析为JSON var json = JSON.stringify(box); //讲JSON转换为字符串 var b ...
- 我的第一个html计算器
html代码. <!DOCTYPE HTML> <html> <head> <style type="text/css"> body ...