第3章C程序设计教程与实验指导杨国兴函数PPT学习教案

上传人:辰*** 文档编号:87480978 上传时间:2022-05-09 格式:PPTX 页数:29 大小:194.27KB
返回 下载 相关 举报
第3章C程序设计教程与实验指导杨国兴函数PPT学习教案_第1页
第1页 / 共29页
第3章C程序设计教程与实验指导杨国兴函数PPT学习教案_第2页
第2页 / 共29页
第3章C程序设计教程与实验指导杨国兴函数PPT学习教案_第3页
第3页 / 共29页
点击查看更多>>
资源描述
会计学1第第3章章C程序设计教程与实验指导杨国兴程序设计教程与实验指导杨国兴函数函数3.6 程序实例第1页/共29页第3章 函数f函数调用f函数f1函数调用f2函数f2函数调用f1函数直接调用间接调用第2页/共29页#include using namespace std;long power(int n);void main() int n; long y; cout n; y=power(n); cout n != y 1) f=n*power(n-1); else f=1; return f;第3页/共29页第3章 函数第4页/共29页第3章 函数ABC第5页/共29页#include using namespace std;void Move(char x, char y);void Hanoi(int n, char one, char two, char three);void main() int n; cout n; cout n 个盘子的移动过程为: endl; Hanoi(n, A, B, C);/函数Move()将一个盘子从x针移到y针void Move(char x, char y) cout x y endl;第3章 函数第6页/共29页/函数Hanoi()将n-1个盘子从one针借助two针移到three针void Hanoi(int n, char one, char two, char three) if(n=1) Move(one, three); else Hanoi(n-1, one, three, two); Move(one, three); Hanoi(n-1, two, one, three); 第3章 函数 返 回运行演示运行演示第7页/共29页第3章 函数第8页/共29页#include using namespace std;inline int Add(int a, int b)int x;x = a+b;return x;第3章 函数 返 回void main()int a, b, c;a = 10;b = 20;c = Add(a,b);cout a + b = c endl;c = Add(a,50);cout a + 50 = c endl;c = Add(50,50);cout 50 + 50 = c endl;第9页/共29页第3章 函数第10页/共29页#include using namespace std;int max(int x, int y); double max(double x, double y); void main() int a=10, b=20 ,c; double x=200.3, y=400.6, z; c = max(a,b); z = max(x,y); cout c z endl; 第3章 函数 返 回int max(int x, int y) cout int function y) return x; else return y; double max(double x, double y) cout float function y) return x; else return y; 第11页/共29页第3章 函数#include using namespace std;double power(double x=10.0, int n=2); void main() cout power(3, 5) endl; cout power(3) endl; cout power() endl; double power(double x, int n) int i;double s=1.0;for(i=1; i=n; i+) s *= x;return s; 第12页/共29页第3章 函数第13页/共29页第3章 函数#include using namespace std;int add(int x=5, int y=6); float add(int x=5, float y=10.0); void main() int a; float b; a= add(10,20); b= add(10); cout a= a endl; cout b= b endl; int add(int x, int y) return x+y; float add(int x, float y) return x+y; 返 回第14页/共29页第3章 函数void f1(int a) int b,c; void main() int m,n; int i,a;for(i=0; i10; i+) int b; 第15页/共29页第3章 函数int a,b; void f1( ) int x,y;void main() 第16页/共29页#include using namespace std;int i=1; /全局变量,文件作用域 void main() cout全局变量 i=iendl; /输出1 int i=5; /函数局部变量,块作用域 int i; /块局部变量,块作用域 i=7; cout块局部变量 i=iendl; /输出7 cout全局变量 i=:iendl; /输出1,:使用全局变量 cout函数局部变量 i=iendl; /输出5 cout全局变量 i=:iendl; /输出1,:使用全局变量 第3章 函数 第17页/共29页第3章 函数第18页/共29页#include using namespace std;int fact( int n);void main() int i;for(i=1; i=4; i+) cout i ! = fact(i) endl;int fact(int n) static int f=1; /仅在第一次调用函数时执行一次f *= n;return f;第3章 函数 第19页/共29页第3章 函数第20页/共29页#include using namespace std;void other(void); int i=1; / i 为全局变量,具有静态生存期。 void main(void) static int a; / a为静态局部变量,具有全局寿命,局部可见。 int b=-10; / b, c为动态局部变量,具有局部生存期。 int c=0; cout-MAIN-n; cout i: i a: a b: b c: cendl; c=c+8; other(); cout-MAIN-n; cout i: i a: a b: b c: cendl; i=i+10; other(); 第3章 函数第21页/共29页void other(void) / a,b为静态局部变量,具有全局寿命,局部可见,/ 只第一次进入函数时被初始化。 static int a=2; static int b; int c=10; / C为动态局部变量,每次进入函数时都初始化。 a=a+2; i=i+32; c=c+5; cout-OTHER-n; cout i: i a: a b: b c: cendl; b=a; 第3章 函数第22页/共29页 using namespace std;第3章 函数第23页/共29页第3章 函数第24页/共29页第3章 函数第25页/共29页第3章 函数第26页/共29页using namespace std;第3章 函数 返 回第27页/共29页第28页/共29页
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 课件教案


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!