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;
}
第十页,编辑于星期六:四点 四分。

最近更新

二手房贷款合同范本保障双方权益 2页

二零二五白蚁防制与建筑修复一体化服务合同 3页

互联网+就业服务与管理协议 2页

互联网金融服务合同 3页

2025年度绿色环保展览会组织与管理合同3篇 43页

交通运输代理招聘服务合同范本 3页

产业园区智能照明系统设计与安装服务合同 4页

2025年度科技馆互动展厅装修及布展合同3篇 44页

人力资源外包服务合同范本 3页

人工智能研发不可撤销反担保合同 3页

仓储物流园区物业管理与租赁合同 2页

仓储租赁合同与仓储物流物流配送服务合同范本.. 3页

2025年度物流运输委托代理协议书3篇 42页

仓库储存及仓储货物安全合同协议 3页

代持股投资风险控制协议 3页

代理记账及财务咨询服务合同范本 3页

2025年度机械设备委托代购专项协议3篇 48页

企业内部保密协议范本 3页

2025年度智慧社区立体停车设施租赁服务合同3篇.. 37页

企业担保贷款合同年度专业版 3页

企业社会责任承诺书模板(责任担当) 3页

企业财务顾问及税务筹划合同 3页

2025年度房地产开发项目垫资服务协议书3篇 40页

2025年度建筑材料委托加工生产合同3篇 40页

低首付农村住房贷款合同范本 2页

住宅小区车位租赁与车位共享服务合同 3页

体育分公司赛事运营合作协议 2页

体育用品全球分销合同 3页

体育赛事担保免责合同范本 3页

供应链金融担保协议书及合同 3页