1 / 19
文档名称:

3.1.8基本函数程序设计-基本函数程序设计3.1.ppt

格式:ppt   大小:425KB   页数:19页
下载后只包含 1 个 PPT 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

3.1.8基本函数程序设计-基本函数程序设计3.1.ppt

上传人:知识徜徉土豆 2025/5/9 文件大小:425 KB

下载得到文件列表

3.1.8基本函数程序设计-基本函数程序设计3.1.ppt

相关文档

文档介绍

文档介绍:该【3.1.8基本函数程序设计-基本函数程序设计3.1 】是由【知识徜徉土豆】上传分享,文档一共【19】页,该文档可以免费在线阅读,需要了解更多关于【3.1.8基本函数程序设计-基本函数程序设计3.1 】的内容,可以使用淘豆网的站内搜索功能,选择自己适合的文档,以下文字是截取该文章内的部分文字,如需要获得完整电子版,请下载此文档到您的设备,方便您编辑和打印。Chapter 4. Function and Program Struction
Basics of Functions
Form of function definition:
return-type function-name(argument declaration)
{ declarations and statements
}
a minimal function: dummy(){}
return statement:
return expression:
or return (expression):
第一页,编辑于星期六:四点 四分。
Basic of Functions
Example:
#include <>
#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 line[MAXLINE];
int found=0;
while(getline(line,MAXLINE)>0)
if(strindex(line,pattern)>=0) {
printf(“%s”,line);
found++;
}
return found;
}
第二页,编辑于星期六:四点 四分。
Basic of Functions
/*getline: get line into s,return length */
int getline(char s[], int lim)
{ int c, i;
i=0;
while(--lim>0 && (c=getchar()) !=EOF && c!=‘\n’)
s[i++] =c;
if( c==‘\n’) s[i++]=c;
s[i]=‘\0’;
return i;
}
/*strindex: return index of t in s,-1 if none */
int strindex(char s[], char t[])
{ int i, j, k;
for(i=0;s[i]!=‘\0’;i++) {
for(j=i,k=0;t[k]!=‘\0’ && s[j]==t[k];j++,k++);
if( k>0 && t[k] ==‘\0’) return;
}
return -1;
}
第三页,编辑于星期六:四点 四分。
Functions Returning Non-integers
must declare the type of value it returns
the calling routine must know that returns a non-int value.
example:
#include <>
/* atof: convert string s to double */
double atof(char s[])
{ double atof(char s[])
int i,sign;
for(i=0;isspace(s[i]); i++); /*skip white space */
sign = (s[i] ==‘-’)? -1:1;
if( s[i] ==‘+’ || s[i] == ‘-’) i++;
for( val=;isdigit(s[i]); i++)
val=*val +(s[i]-’0’);
if (s[i]==‘.’) i++;
for( power=; isdigit(s[i]); i++) {
val=*val +(s[i]-’0’);
power *=;
}
return sign *val /power;
}
第四页,编辑于星期六:四点 四分。
Functions Returning Non-integers
calling atof(): declaration (function protype)
#include <>
#define MAXLINE 100
main()
{ double sum, atof(char [ ]);
char line[MAXLINE];
int getline (char line[], int max);
sum=0;
while(getline(line,MAXLINE) >0)
printf(“\t%g\n”, sum+= atof(line));
return 0;
} calling
or : atof();
in old version
第五页,编辑于星期六:四点 四分。
External Variables
external 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 is not end_of_file indicator) if(number)
push it
else if (operator)
pop operands
do operation
push result
else if (newline)
pop and print top of stack
else error
第六页,编辑于星期六:四点 四分。
External Variables
#include <>
#include <>
#define MAXOP 100 /*max size of operand or operator */
int getop(char [ ]);
void push(double);
/* reverse Polish calculator */
main()
{ int type;
double op2;
char s[MAXOP];
第七页,编辑于星期六:四点 四分。
External Variables
while(( 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!=) push(pop()/op2);
elseprintf(“error: zero divisor\n”); break;
case ‘\n’: printf(“\\n”, pop()); break;
default: printf(“erroe: unknown command %s \n”, s);
break;
}
}
return 0;
}
第八页,编辑于星期六:四点 四分。
External Variables
#define MAXVAL 100 /* maximum depth of val stack */
int sp=0; /*next free stack position */
double val[MAXVAL]; /*val stack */
/* push: push f onto value stack */
void push(double f)
{ if(sp<MAXVAL) val[sp++]=f;
else printf(‘error: stack full,can’t push %g\n”, f);
}
/* pop: pop and return top value from stack */
double pop(void)
{ if(sp>0) return val[--sp];
else {
printf(“error: stack empty\n”);
return ;
}
}
第九页,编辑于星期六:四点 四分。
External Variables
#include <>
int getch(void);
void ungetch(int);
/* getop: get next operator or numeric operand */
int getop(char s[])
{ int i,c;
while ((s[0] = c= getch()) == ‘ ‘ || c== ‘\t’);
s[1]= ‘\0’;
if (!isdigit(c) && c!= ‘.’) return c;
i=0;
if( isdigit(c)) while (isdigit(s[++i} =c =getch()));
if(c==‘.’) while (isdigit(s[++i]=getch()));
s[i] = ‘\0’;
if (c!=EOF) ungetch(c);
return NUMBER;
}
第十页,编辑于星期六:四点 四分。