当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

重庆邮电大学:《C语言程序设计》第9讲 存储类型 宏定义 数组(谢竞博)

资源类别:文库,文档格式:PPT,文档页数:29,文件大小:141KB,团购合买
切忌: 将递归函数的内容写至主函数中; 将递归中的语句放至循环中; 在递归函数中写s(xn)=s(xn-1)+powxn):
点击下载完整版文档(PPT)

⊙讲 变量的存储类型 带参数的宏定义 数组 请大家及时消化我课上讲的内容,并举一反三,模仿未讲过 的例题多写程序。 如果说学习C语言有捷径的话:那就是多读程序,多写程序

第9讲 变量的存储类型 带参数的宏定义 数组 •请大家及时消化我课上讲的内容,并举一反三,模仿未讲过 的例题多写程序。 •如果说学习C语言有捷径的话:那就是多读程序,多写程序

作业答案-1 5.6 double s(float x, int n [if(n==ll) return x else return S(x, n-1)+x*x: 1、将# include 改为 windows h;将 closer(:改为 system(CLs”) cIrscro是TC中的清屏函数,VC中没有,但Vc中 般不需要。 2、1相当于(ong)1 2

2 作业答案-1 ▪ 5.6 double S(float x, int n) {if(n==1L) return x; else return S(x,n-1)+x*x; } •1、将#include改为windows.h;将clrscr();改为 system(“CLS”);clrscr()是TC中的清屏函数,VC中没有,但VC中 一般不需要。 •2、1L相当于(long)1

习题5.12 问题的关键是如何将n的问题化解为n-1 的问题(即反方向求解问题)。 根据分析,写出如下的数学函数: x n=1 S( s(x, n-1)+pow(x, n) n=1 double s(float x, int n) if(n==1) return x 》e|se return S(, n-1)+pow(x, n): }/若函数定义为foa,会有类型不一致的警告*

3 习题5.12 ▪ 问题的关键是如何将n的问题化解为n-1 的问题(即反方向求解问题)。 ▪ 根据分析,写出如下的数学函数:    − + = = = ( , 1) ( , ) ! 1 1 ( , ) s x n pow x n n x n s x n double S(float x, int n) {if(n==1) return x; else return S(x,n-1)+pow(x,n); }/*若函数定义为float,会有类型不一致的警告*/

#include<stdio. h 切忌: 将递归函数的内容写至主函数中; 将通归中的语句放至循环中 在递归函数中写s(x,n)=S(x,n-1)+pow(x,n) wnn Please puT s s=S(, n) scam(“%f,‰,&8prm("ns(%f,%)=%fw”,x,n,s) s=S(x, n: prnf(nS(%f%d)=% double s( double x,intn)用递推求解 [double sum=0; int i double s( double x,, int for(=-1<=n++)递推要用循环语句实现吻/ if(n=:1) return x sum+=pow(,i) return sum else return S(x, n-1)+pow(x, n) 递归要通过函数的依次调用实现*

4 #include #include double S(float x, int n); void main() { double x,s; int n; printf(“please input x,n:\n”); scanf(“%lf,%d”,&x,&n); s=S(x,n); printf(“\nS(%lf,%d)=%lf\n”,x,n,s); } double S(double x, int n) {if(n==1) return x; else return S(x,n-1)+pow(x,n); /*递归要通过函数的依次调用实现*/ } 习题5.12程序 #include #include double S(float x, int n); void main() { double x,s; int n; printf(“please input x,n:\n”); scanf(“%lf,%d”,&x,&n); s=S(x,n); printf(“\nS(%lf,%d)=%lf\n”,x,n,s); } double S(double x, int n)/*用递推求解*/ {double sum=0; int i; for(i=1;i<=n;i++)/*递推要用循环语句实现*/ sum+=pow(x,i); return sum; } 切忌: 将递归函数的内容写至主函数中; 将递归中的语句放至循环中; 在递归函数中写s(x,n)=S(x,n-1)+pow(x,n);

另一个具有“记忆性”的变 量类型:静态变量( static) 般的内部变量 在函数退出后失效,再次进入函数,变量 值重新初始化 静态变量 在变量类型前面用 static修饰 static int i 变量存在静态存储区,当函数结束时,内 存空间不被释放,因此,变量的值可以保 存到下次进入函数,即变量具有记忆功能

5 另一个具有“记忆性”的变 量类型:静态变量(static) ▪ 一般的内部变量 ▪ 在函数退出后失效,再次进入函数,变量 值重新初始化 ▪ 静态变量 ▪ 在变量类型前面用static修饰 ▪ static int i; ▪ 变量存在静态存储区,当函数结束时,内 存空间不被释放,因此,变量的值可以保 存到下次进入函数,即变量具有记忆功能

涉及的语法 变量的存储类型 编译器为变量分配内存的方式 它决定变量的生存期 动态存储 内存分配 根据需要临时分配 存储空间离开即程序存储区 释放 全局变量、 静态存储 静态存储区 静态变量 在程序运行期间分 配固定的存储空间动态存储区 形参、自动变 量、函数调用 不释放 的现场等

6 涉及的语法 -变量的存储类型 ▪ 编译器为变量分配内存的方式 ▪ 它决定变量的生存期 程序存储区 静态存储区 动态存储区 形参、自动变 量、函数调用 的现场等 全局变量、 静态变量 ▪ 动态存储 ▪ 根据需要临时分配 存储空间,离开即 释放 ▪ 静态存储 ▪ 在程序运行期间分 配固定的存储空间 不释放 内存分配

例-静态变量 #include Funco was called 1 time(s) void Func(void: Funco was called 2 time(s) maino Funco was called 3 time(s) int i Funco was called 4 time(s) for(i=0:i<10:i+) Funco was called 5 time(s) t FuncO Funco was called 6 time(s) Func was called 7 time(s) Funco was called 8 time(s) Funco was called 9 time(s) 函数物能:打印被调用的次数 Funco was called 10 time(s) 函数口参数:无 函数痃回值:无 times 10 vdid Func(void) static int times =1: 静态局部变量函数结束时tmes变量仍然占据 静态存储区的存储空间,不释放*/ ]PIntfcFunco was called %d time (s).\n". times++)

7 例-静态变量 #include void Func(void); main() { int i; for (i=0; i<10; i++) { Func(); } } /* 函数功能: 打印被调用的次数 函数入口参数: 无 函数返回值: 无 */ void Func(void) { static int times = 1; /*静态局部变量 函数结束时times变量仍然占据 静态存储区的存储空间,不释放*/ printf("Func() was called %d time(s).\n", times++); } Func() was called 1 time(s). Func() was called 2 time(s). Func() was called 3 time(s). Func() was called 4 time(s). Func() was called 5 time(s). Func() was called 6 time(s). Func() was called 7 time(s). Func() was called 8 time(s). Func() was called 9 time(s). Func() was called 10 time(s). times 10123

例-非静态变量 # Include void Func(void) Funco was called 1 time(s) maino Funco was called 1 time(s) int i Func o was called 1 time(s) for(i=0:i<10:i++) Funco was called 1 time(s) FuncO: Func o was called 1 time(s) Func( was called 1 time(s) Funco was called 1 time(s) Funco was called 1 time(s) 函数功能:打印被调用的次数 Func was called 1 time(s) 函数入口参数:无 Funco was called 1 time(s) 函数返回值:无 Mpid Func(void) int times 1 printf(" Funco was called %d time (s). \n". times++)

8 例-非静态变量 #include void Func(void); main() { int i; for (i=0; i<10; i++) { Func(); } } /* 函数功能: 打印被调用的次数 函数入口参数: 无 函数返回值: 无 */ void Func(void) { int times = 1; printf("Func() was called %d time(s).\n", times++); } Func() was called 1 time(s). Func() was called 1 time(s). Func() was called 1 time(s). Func() was called 1 time(s). Func() was called 1 time(s). Func() was called 1 time(s). Func() was called 1 time(s). Func() was called 1 time(s). Func() was called 1 time(s). Func() was called 1 time(s). times 12 times 12

根据存储类型可分为 自动变量(auto) 静态变量( static n寄存器变量( register)

9 根据存储类型可分为 ▪ 自动变量(auto) ▪ 静态变量(static) ▪ 寄存器变量(register)

自动变量(auto) 我们以前定义的那些变量,都默认是这种类型目 “自动”体现在 进入语句块时自动申请内存,退出时自动释放内存 标准定义格式 auto类型名变量名; 特点 动态局部变量 缺省的存储类型 丕初始化时,值是不确定的 10

10 自动变量 (auto ) ▪ 我们以前定义的那些变量,都默认是这种类型 ▪ “自动”体现在 ▪ 进入语句块时自动申请内存,退出时自动释放内存 ▪ 标准定义格式 auto 类型名 变量名; ▪ 特点: ▪ 动态局部变量 ▪ 缺省的存储类型 ▪ 不初始化时,值是不确定的

点击下载完整版文档(PPT)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共29页,试读已结束,阅读完整版请下载
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有