函数、函数与运算符重载03

上传人:平*** 文档编号:46184153 上传时间:2018-06-23 格式:PPT 页数:257 大小:2.66MB
返回 下载 相关 举报
函数、函数与运算符重载03_第1页
第1页 / 共257页
函数、函数与运算符重载03_第2页
第2页 / 共257页
函数、函数与运算符重载03_第3页
第3页 / 共257页
函数、函数与运算符重载03_第4页
第4页 / 共257页
函数、函数与运算符重载03_第5页
第5页 / 共257页
点击查看更多>>
资源描述

《函数、函数与运算符重载03》由会员分享,可在线阅读,更多相关《函数、函数与运算符重载03(257页珍藏版)》请在金锄头文库上搜索。

1、高级语言程序设计 C+张海威 南开大学信息技术科学学院http:/第5章 函数与重载函数的说明与使用1函数的嵌套与递归2函数与运算符重载3函数与C+程序结构42Database void main(void) float p, q, xr; coutP; coutq; couteps); A = croot; croot = S; do root = croot; croot = (2*root + S/(root*root)/3; while(fabs(croot-root)eps); B = croot; xr = A + B; couteps); A = croot;croot = S;

2、 do root = croot; croot = (2*root + S/(root*root)/3; while(fabs(croot-root)eps); B = croot;http:/函数的使用和说明v函数的引入 程序中功能相同,结构相似的代码段可以用函 数进行描述 程序的功能相对独立,用来解决某个问题 具有明显的入口和出口 入口:参数 出口:返回值Database const float eps=1e-6; croot=x; doroot=croot;croot=(2*root+x/(root*root)/3;while(fabs(crootroot)eps); return cr

3、oot; http:/函数的使用和说明v函数的作用 实现程序功能的模块化 实现程序结构的简化 实现程序代码的重用 v函数的应用场景 包含多处功能相同的代码 处理数据的类型、处理过程相同或相似 代码段具有代表性或特殊含义Database void printString();/函数原型 int main()printString();/调调用函数printStringreturn 0; void pirntString()/函数定义义cout) setw()、setwidth()等 标准文件流() open()、close()等 标准字符串处理函数() strcpy()、strcmp()等 标准

4、数学函数() sqrt()、pow()等Database 函数定义的一般形式 ( ) 通常用来实现某种特定的功能 不需要进行数据的传递 处理的数据通常与主调函数无关Database i (); 定义的一般形式() 通过调用处提供的不同实参值来计算出其对应的 函数值、或实现某种与传递过来的那些不同值有 关的某种功能。Database i(); 例如 int add(int a, int b); inline void swap(float void print (char *);Database void型参数 void printroot(void); ,* float cuberoot(fl

5、oat); float cuberoot(float x);Database void printStar(int);/函数原型 int main() printStar(10);/调用函数 void printStar(int k)/函数体#include using namespace std; void printStar(int k)/函数定义 int main()printStar(10); /调用函数http:/函数的说明与使用v函数的说明和定义 两种说明方式的区别 函数原型的参数表中,参数名可以省略;函数定义 的参数表中,必须给出参数名(省略参数名为无名 参数) 函数原型的函数体

6、,可以出现在函数调用之后;函 数定义的函数体,必须出现在调用之前 函数原型的参数表后面加分号“;”,函数定义的参 数表后面是函数体,即花括号“” 函数定义不能出现在任何函数体中,函数原型 可以出现在其它函数体中Database 有参函数调用格式( ) 例如:printStar(26);Database /函数返回值类型为空(void) return ;/与函数返回值的类型一致 return ();/与前一种写法等价Database Database y=(x*x+x+1)/2-5.5; return y; /对非void类型的函数,必须有一个/return语句,由它返回函数值 Database

7、 double f (double); int main() double z,a; z=(f(2.5)+2*f(6)/f(4.3); /调用自定义函数f couta; cout using namespace std; double f (double x)/函数f的定义 int main() double z,a; z=(f(2.5)+2*f(6)/f(4.3); /调用自定义函数f couta; cout using namespace std; void func(int a=11, int b=22, int c=33) /为参数a、b、c设置了默认值11、22与33 cout us

8、ing namespace std; void strcat(char s,char ct)int i=0,j=0;while (si!=0) i+;while (ctj!=0)si+=ctj+;si=0; http:/函数的说明与使用v函数的参数 【例5.5】主函数部分Database char b20=“是东南大学学生“;strcat(a,b);/实参为数组名cout using namespace std; void inverse(int 36, int 63);/转置矩阵 void multi(int 63,int 34,int 64);/矩阵乘法 void output(int 6

9、4); /矩阵输出 int main()int middle63, result64;int matrix136=8,10,12,23,1,3,5,7,9,2,4,6, 34,45,56,2,4,6;int matrix234=3,2,1,0,-1,-2,9,8,7,6,5,4;inverse(matrix1,middle);/实参为数组名multi(middle,matrix2,result);output(result);return 0; http:/函数的说明与使用 【例5.6】转置和乘法函数定义Database for (i=0;i using namespace std; void

10、 swap (int x, int y). int main() int a,b; cinab; cout using namespace std; void swap(int/函数原型/前两个为引用参数,可“双向传值”/后两个为赋值参数Database cout ()Database void main() int a,b; coutab; cout1) value *= number; number-; return value; Database long factorial(int number); /函数原型 int main() int i,selections,elements;

11、 /计算A(elements,selections) /以及C(elements,selections) coutselections; /输入整数selections coutelements; /输入整数elements double answer = elements; int ele = elements; Database couti;j-) if(aj #include #include #include using namespace std; const int n = 100; int main() int bn; srand(unsigned)time(NULL); for

12、(int i=0;i using namespace std; int main() couti;/输入整数 if(n=1) cout using namespace std; int main() cout using namespace std; void main(void) int t; cout t; cout “ using namespace std; void move(char,char); void hanoi(int,char,char,char); int main() int n; coutn; hanoi(n,A,B,C); return 0; Database v

13、oid swap (int int findpivot(int ,int,int); int partition(int ,int,int,int); void quickSort(int ,int,int);Database srand(unsigned)time(NULL); coutpivot); swap(al,ar); while(l1) quickSort(a,i,k-1); if(j-k)1) quickSort(a,k+1,j); Database printStar1();/调用无参函数 /以输入的k为实参调用 printStar2(k); /以输入的k与n为实参去调用pri

14、ntStar3 printStar(k,n); return 0; Database 函数名相当于:operator operator是关键字返回值类型表示使用重载运算符进行运算得到结果 的数据类型 例如,重载运算符-,表示字符串类型的减法 ,其运算符重载函数函数原型为: string operator-(string,string);Database 重载运算符(双目),*(双目),(单 目)来表示bool类型的或、与、非运算Database return TRUE; bool operator*(bool a, bool b) if(a=TRUE) return FALSE; bool o

15、perator-(bool a)if(a=FALSE) return TRUE;return FALSE; Database enum boolFALSE,TRUE; int main() bool b1=FALSE,b2=TRUE; cout #include #include #include #include using namespace std; int strIndex (string, string); string operator -(string, string);Database couta; coutb; string s = a-b; cout/ For vector #include / For sort()

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号