《3.1.8基本函数程序设计-基本函数程序设计3.1.ppt》由会员分享,可在线阅读,更多相关《3.1.8基本函数程序设计-基本函数程序设计3.1.ppt(19页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Chapter 4.Function and Program Struction4.1 Basics of Functions Form of function definition:return-type function-name(argument declaration)declarations and statementsa minimal function:dummy()return statement:return expression:or return(expression):第一页,编辑于星期六:四点 四分。4.1 Basic of FunctionsExample:#inc
2、lude#define MAXLINE 1000 /*maximum input line length */int getline(char line,int max);int strindex(char source,char searchfor);char pattern=“ould”;/*pattern to search for */*find all lines matching pattern*/main()char lineMAXLINE;int found=0;while(getline(line,MAXLINE)0)if(strindex(line,pattern)=0)p
3、rintf(“%s”,line);found+;return found;第二页,编辑于星期六:四点 四分。4.1 Basic of Functions/*getline:get line into s,return length*/int getline(char s,int lim)int c,i;i=0;while(-lim0&(c=getchar()!=EOF&c!=n)si+=c;if(c=n)si+=c;si=0;return i;/*strindex:return index of t in s,-1 if none*/int strindex(char s,char t)int
4、 i,j,k;for(i=0;si!=0;i+)for(j=i,k=0;tk!=0&sj=tk;j+,k+);if(k0&tk=0)return;return-1;第三页,编辑于星期六:四点 四分。4.2 Functions Returning Non-integers(1)must declare the type of value it returns(2)the calling routine must know that returns a non-int value.example:#include/*atof:convert string s to double*/double a
5、tof(char s)double atof(char s)int i,sign;for(i=0;isspace(si);i+);/*skip white space*/sign=(si=-)?-1:1;if(si=+|si=-)i+;for(val=0.0;isdigit(si);i+)val=10.0*val+(si-0);if(si=.)i+;for(power=1.0;isdigit(si);i+)val=10.0*val+(si-0);power*=10.0;return sign*val/power;第四页,编辑于星期六:四点 四分。4.2 Functions Returning
6、Non-integerscalling atof():declaration(function protype)#include#define MAXLINE 100main()double sum,atof(char );char lineMAXLINE;int getline(char line,int max);sum=0;while(getline(line,MAXLINE)0)printf(“t%gn”,sum+=atof(line);return 0;callingor:atof();in old version第五页,编辑于星期六:四点 四分。4.3 External Varia
7、blesexternal variables:defined outside of any function and available to many functions(globally accessiable)Example:calculator that provides the operators+,-,*/use reverse Polish(逆波兰)notation instead of infix(中缀).1 2 -4 5 +*(1-2)*(4+5)the main algorithm of the program:while(next operator or operand
8、is not end_of_file indicator)if(number)push itelse if(operator)pop operands do operation push resultelse if(newline)pop and print top of stackelse error第六页,编辑于星期六:四点 四分。4.3 External Variables#include#include#define MAXOP 100/*max size of operand or operator*/int getop(char );void push(double);/*reve
9、rse Polish calculator*/main()int type;double op2;char sMAXOP;第七页,编辑于星期六:四点 四分。4.3 External Variableswhile(type=getop(s)!=EOF)switch(type)case NUMBER:push(atof(s);break;case+:push(pop()+pop();break;case*:push(pop()*pop();case-:op2=pop();push(pop()-op2);break;case/:op2=pop();if(op2!=0.0)push(pop()/op2
10、);elseprintf(“error:zero divisorn”);break;case n:printf(“t.8gn”,pop();break;default:printf(“erroe:unknown command%s n”,s);break;return 0;第八页,编辑于星期六:四点 四分。4.3 External Variables#define MAXVAL 100/*maximum depth of val stack*/int sp=0;/*next free stack position*/double valMAXVAL;/*val stack*/*push:pus
11、h f onto value stack*/void push(double f)if(sp0)return val-sp;else printf(“error:stack emptyn”);return 0.0;第九页,编辑于星期六:四点 四分。4.3 External Variables#include int getch(void);void ungetch(int);/*getop:get next operator or numeric operand*/int getop(char s)int i,c;while(s0=c=getch()=|c=t);s1=0;if(!isdigi
12、t(c)&c!=.)return c;i=0;if(isdigit(c)while(isdigit(s+i=c=getch();if(c=.)while(isdigit(s+i=getch();si=0;if(c!=EOF)ungetch(c);return NUMBER;第十页,编辑于星期六:四点 四分。4.3 External Variables#define BUFSIZE 100char bufBUFSIZE;/*buffer for ungetch*/int bufp=0;/*next free position in buf*/int getch(void)/*get a char
13、acter*/return(bufp 0)?buf-bufp:getchar();void ungetch(int c)/*push character back on input*/if(bufp=BUFSIZE)printf(“ungetch:too many charactersn”);else bufpbufp+=c;第十一页,编辑于星期六:四点 四分。Getint与与getstr:getch与与ungetch的作用的作用#include char buf10;int bp=-1;char getch()if(bp=0)return bufbp-;else return getchar
14、();void ungetch(char c)buf+bp=c;int getint()int n=0;char c;while(c=getch()!=EOF)if(c=0&c=0&c=9)ungetch(c);break;else si+=c;si=0;return s;main()int i,j;char s20;i=getint();getstr(s);j=getint();printf(%d%s%dn,i,s,j);第十二页,编辑于星期六:四点 四分。4.4 Scope Rulesscope of a name(variable,function,ect);the part of th
15、e program within which the name can be used.scope of an automatic variable:the function in which the name is declaredscope of an external variable or a function:from the point at which it is declared to the edn of the fileif an external variable is to be referred to before it is defindor a name is d
16、efind in a different source file:an extern declaration is mandatoryIn file1:In file2:extern int sp;extern double val;void push(double f)void pop(void)int sp=0;double valMAXVAL;第十三页,编辑于星期六:四点 四分。4.5 Header Filesplace the definitions and declarations shared among the files in a header file.example:cal
17、c.h:#define NUMBER 0void push(double);int getop(char);int getch(void);void ungetch(int);#include#include#include“calc.h”#define MAXOP 100main()#include#include#include“calc.h”getop()stack.c:getch.cmain.c:getop.c:第十四页,编辑于星期六:四点 四分。4.6 Static Variablesstatic:(1)applied to an external variables or func
18、tion:limits the scope of that object to the rest of the source file,the name is invisiable outside the file.static charBUFSIZE;/*buffer for ungetch*/int getch(void)void ungetch(int c)(2)applied to an internal variable:the scope is the function(as automatic variable,but it remain in existence as the
19、program(not the function)is activated.void Example()static int i=1;printf(“%d timesn”,i+);main()Example();Example();Example();result:1 times2 times3 timesstatic variables is initialized only the first time function is entered第十五页,编辑于星期六:四点 四分。4.7 Register Variablesregister:advises the complier to pl
20、ace the variable in registers,in order to result in smaller and faster programs.formatregister int x;register char c;example:f(register unsigned m,register long n)register int i;notice:register declaration can only be applied to automatic variable and to the formal parameters of a functiononly a few
21、 variables can be kept in registers,excess declaration will be ignored.can not take the address of a register variablethere are restrictions on types of register variables.第十六页,编辑于星期六:四点 四分。4.8 Block Structureblock:variable can be declared in a block(follow the left brace),the scope of the variable
22、is the block,and remain in existence until matching the right brace.if(n0)int i;/*declare a new i*/for (i=0;in;i+).the scope of “i”the variable in inner scope will hide variables in outer scope of the same name.int x,y;f(double x)double y;x=y+1;g()inner first第十七页,编辑于星期六:四点 四分。4.9 Initializationin th
23、e absence of explicit initialization:(1)external and static variable are initialized to zero(2)automatic and register variables have undefined initial valuein explicit initialization:(1)for external and static variables:the initialier must be a constant expressionthe initialization is done once befo
24、re the program begins execution(2)for automatic and static variables:initializer may be any expression involving previously define valueit is dine each time the function or block is enteredint x=1;char c=a+5;int binsearch(int x,int v,int n)int low=0;int high=n-1;第十八页,编辑于星期六:四点 四分。4.9 InitializationT
25、he initialization of an array:with a list initializes enclosed in braces and separated by commas int days=31,28,31,30,31,30,31,31,30,31,30,31if there are fewer initializers(1)for external or static variables:the others will be zero(2)for automatic variables:the others will be undefinedstatic int a4=1,2;/*a2,a3 will be 0*/int f()int a4=1,2;/*a2,a3 will be undefined */for character arrays initialization:a string can be usedchar s=“hello”;char s=h,e,l,l,o,0;第十九页,编辑于星期六:四点 四分。
限制150内