




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、会计学1函数函数与运算符重载函数函数与运算符重载函数的说明与使用函数的说明与使用1函数的嵌套与递归函数的嵌套与递归2函数与运算符重载函数与运算符重载3函数与函数与C+程序结构程序结构42Database & Information System Lab第1页/共257页Database & Information System Lab3332332)3()2(2)3()2(2pqqpqqxr= 将计算公式分解为如下步骤将计算公式分解为如下步骤 令实数令实数x xr rA+BA+B 令实数令实数A, B A, B 分别为实数分别为实数R,SR,S的立方根的立方根 令令R = -q/2 + a,
2、S = -q/2R = -q/2 + a, S = -q/2a a 令令a=sqrt(q/2)a=sqrt(q/2)* *(q/2)+(q/3)(q/2)+(q/3)* *(q/3)(q/3)* *(q/3(q/3) ) 第2页/共257页Database & Information System Lab4第3页/共257页Database & Information System Lab5#include/program5-1#include/use function sqrtusing namespace std;void main(void)float p, q, xr; coutP;c
3、outq;coutThe Equation is:;coutx3+px+q=0eps);A = croot;croot = S;doroot = croot;croot = (2*root + S/(root*root)/3;while(fabs(croot-root)eps);B = croot;xr = A + B;coutendlThe real root of the equation iseps);A = croot;croot = S;doroot = croot;croot = (2*root + S/(root*root)/3;while(fabs(croot-root)eps
4、);B = croot;第6页/共257页Database & Information System Lab8第7页/共257页Database & Information System Lab9第8页/共257页Database & Information System Lab10float cuberoot(float x)/精确到小数点后精确到小数点后6位位float root , croot;const float eps=1e-6;croot=x;do root=croot; croot=(2*root+x/(root*root)/3; while(fabs(crootroot)ep
5、s);return croot;第9页/共257页Database & Information System Lab11第10页/共257页Database & Information System Lab12#inlcudeusing namespace std;void printString();/函数原型函数原型int main() printString();/调用函数调用函数printString return 0;void pirntString()/函数定义函数定义 cout“Hello!”endl; return; /函数返回函数返回第11页/共257页Database &
6、Information System Lab13第12页/共257页Database & Information System Lab14第13页/共257页从从函数形式函数形式划分划分,可分为无参函,可分为无参函数与有参函数两数与有参函数两类类从从使用角度使用角度划分划分,可将函数分为,可将函数分为:系统预定义系统预定义的的标准库函数(如标准库函数(如,sinsin,absabs等等),以及由),以及由用户用户自定义自定义的函数的函数函数分类函数分类方法方法15Database & Information System Lab第14页/共257页Database & Information
7、System Lab16第15页/共257页Database & Information System Lab17第16页/共257页Database & Information System Lab18第17页/共257页Database & Information System Lab19第18页/共257页Database & Information System Lab20void printStar() for(int i=0;i10;i+) cout“*”; coutendl;第19页/共257页与传递过来的那些不同值有关的某种功能。Database & Information S
8、ystem Lab21第20页/共257页Database & Information System Lab22void printStar(int k) for(int i=0;ik;i+) cout“*”; coutendl;第21页/共257页函数函数的使用的使用调用调用说明与定义说明与定义参数参数重载重载返回返回内联函数内联函数23Database & Information System Lab第22页/共257页Database & Information System Lab24第23页/共257页Database & Information System Lab25第24页/共
9、257页Database & Information System Lab26第25页/共257页n标识符Database & Information System Lab27第26页/共257页Database & Information System Lab28第27页/共257页Database & Information System Lab29第28页/共257页Database & Information System Lab30#includeusing namespace std;void printStar(int);/函数原型函数原型int main() printStar
10、(10);/调用函数调用函数void printStar(int k)/函数体函数体#includeusing namespace std;void printStar(int k)/函数定义函数定义int main()printStar(10); /调用函数调用函数第29页/共257页Database & Information System Lab31第30页/共257页Database & Information System Lab32第31页/共257页Database & Information System Lab33第32页/共257页Database & Informatio
11、n System Lab34第33页/共257页Database & Information System Lab35第34页/共257页n数值型n引用类型Database & Information System Lab36第35页/共257页nDatabase & Information System Lab37第36页/共257页Database & Information System Lab38第一种格式的第一种格式的returnreturn用于立即从被调函数中返回用于立即从被调函数中返回, , 当函数类型为当函数类型为voidvoid时,应使用这种格式的返回语句。时,应使用这种格式
12、的返回语句。当函数类型为当函数类型为非非voidvoid型时,应使用第二或第三种格式的型时,应使用第二或第三种格式的returnreturn语句,此两种格式的语句效果完全相同(可将第二种格式看成是第三种格式的省略形式),系统此时都将计算出表达式的值,并语句,此两种格式的语句效果完全相同(可将第二种格式看成是第三种格式的省略形式),系统此时都将计算出表达式的值,并“携带携带”该值立即从被调函数中返回该值立即从被调函数中返回第37页/共257页Database & Information System Lab39第38页/共257页Database & Information System Lab
13、40第39页/共257页Database & Information System Lab41第40页/共257页Database & Information System Lab42第41页/共257页Database & Information System Lab43 f=(xf=(x* *x+x+1)/2-5.5; x+x+1)/2-5.5; 不可给函数名不可给函数名f f赋值。赋值。 return (f); return (f); 返回值类型应该是返回值类型应该是doubledouble,而非指针类型(函数名相当于一个指针)。而非指针类型(函数名相当于一个指针)。 f(x)=(xf(
14、x)=(x* *x+x+1)/2-5.5; x+x+1)/2-5.5; 赋值号左端非变量(也即赋值号左端非变量(也即f(x)f(x)非左值)。非左值)。 第42页/共257页Database & Information System Lab44第43页/共257页Database & Information System Lab45第44页/共257页Database & Information System Lab46第45页/共257页Database & Information System Lab47#includeusing namespace std;void func(int a
15、=11, int b=22, int c=33) /为参数为参数a、b、c设置了默认值设置了默认值11、22与与33couta=a, b=b, c=cendl; 第46页/共257页Database & Information System Lab48第47页/共257页Database & Information System Lab49第48页/共257页Database & Information System Lab50 void func(int a, int b=2, int c=3); /OK! v o i d f u n c ( i n t a = 1 , i n t b ,
16、i n t c = 3 ) ; /ERROR! v o i d f u n c ( i n t a = 1 , i n t b = 2 , i n t c ) ; /ERROR! 对第一个函数说明,采用如下的调用语句:对第一个函数说明,采用如下的调用语句: func(1, 22, 333); /OK! 调用时给出所有实参调用时给出所有实参 func(); / ERROR! 参数参数a没有默认值没有默认值 func(10,20); /OK! 参数参数c默认为默认为3 func(5, ,9);/ERROR!调用处也只能缺省后面的连续若干个实参调用处也只能缺省后面的连续若干个实参 第49页/共257
17、页Database & Information System Lab51第50页/共257页Database & Information System Lab52第51页/共257页Database & Information System Lab53第52页/共257页return ab;Database & Information System Lab54第53页/共257页n实参为a10,形参可以定义为x6Database & Information System Lab55第54页/共257页Database & Information System Lab56第55页/共257页Da
18、tabase & Information System Lab57#includeusing namespace std;void strcat(char s,char ct) int i=0,j=0; while (si!=0) i+; while (ctj!=0) si+=ctj+; si=0;第56页/共257页Database & Information System Lab58int main (void) char a40=李明李明; char b20=是东南大学学生是东南大学学生; strcat(a,b);/实参为数组名实参为数组名 coutaendl;/打印字符数组打印字符数组
19、a return 0;第57页/共257页Database & Information System Lab59第58页/共257页Database & Information System Lab60#includeusing namespace std;void inverse(int 36, int 63);/转置矩阵转置矩阵void multi(int 63,int 34,int 64);/矩阵乘法矩阵乘法void output(int 64); /矩阵输出矩阵输出int main() int middle63, result64; int matrix136=8,10,12,23,1
20、,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;第59页/共257页Database & Information System Lab61void inverse(int matrix136,int middle63)/转置转置 int i,j; for (i=0;i3;i+) for (j=0;j6;j+) mid
21、dleji=matrix1ij; return;void multi(int middle63,int matrix234,int result64) int i,j,k; /矩阵乘法矩阵乘法 for (i=0;i6;i+) for (j=0;j4;j+) resultij = 0; for (k=0;k3;k+) resultij+=middleik*matrix2kj; return; 第60页/共257页Database & Information System Lab62void output(int result64) /矩阵输出矩阵输出 cout resultn; int i,j;
22、 for (i=0;i6;i+) for (j=0;j4;j+) cout setw(4)resultij ; coutn; return; 第61页/共257页参的变量引用是一种方法n还可以用指针做形参操作变量地址Database & Information System Lab63第62页/共257页Database & Information System Lab64第63页/共257页Database & Information System Lab65第64页/共257页Database & Information System Lab66第65页/共257页Database & I
23、nformation System Lab67void swap (int x, int y) int temp = x; x = y; y = temp;第66页/共257页Database & Information System Lab68第67页/共257页Database & Information System Lab69第68页/共257页Database & Information System Lab70第69页/共257页Database & Information System Lab71第70页/共257页Database & Information System La
24、b72第71页/共257页Database & Information System Lab73第72页/共257页Database & Information System Lab74第73页/共257页Database & Information System Lab75第74页/共257页Database & Information System Lab76第75页/共257页Database & Information System Lab77第76页/共257页Database & Information System Lab78第77页/共257页Database & Inform
25、ation System Lab79第78页/共257页Database & Information System Lab80第79页/共257页Database & Information System Lab81第80页/共257页Database & Information System Lab82第81页/共257页Database & Information System Lab83第82页/共257页Database & Information System Lab84第83页/共257页Database & Information System Lab85第84页/共257页Da
26、tabase & Information System Lab86第85页/共257页Database & Information System Lab87第86页/共257页Database & Information System Lab88第87页/共257页Database & Information System Lab89第88页/共257页Database & Information System Lab90第89页/共257页Database & Information System Lab91第90页/共257页Database & Information System La
27、b92第91页/共257页函数的说明与使用函数的说明与使用1函数的嵌套与递归函数的嵌套与递归2函数与运算符重载函数与运算符重载3函数与函数与C+程序结构程序结构493Database & Information System Lab第92页/共257页Database & Information System Lab94第93页/共257页Database & Information System Lab95第94页/共257页Database & Information System Lab96第95页/共257页Database & Information System Lab97第96页
28、/共257页Database & Information System Lab98第97页/共257页Database & Information System Lab99第98页/共257页Database & Information System Lab100第99页/共257页Database & Information System Lab101第100页/共257页Database & Information System Lab102第101页/共257页Database & Information System Lab103第102页/共257页Database & Inform
29、ation System Lab104第103页/共257页Database & Information System Lab105第104页/共257页Database & Information System Lab106第105页/共257页Database & Information System Lab107第106页/共257页a运行状态继续运行a函数Database & Information System Lab108第107页/共257页Database & Information System Lab109第108页/共257页运行状态继续运行主函数Database & I
30、nformation System Lab110第109页/共257页Database & Information System Lab111第110页/共257页Database & Information System Lab112第111页/共257页Database & Information System Lab113第112页/共257页Database & Information System Lab114第113页/共257页Database & Information System Lab115第114页/共257页Database & Information System
31、Lab116第115页/共257页Database & Information System Lab117第116页/共257页Database & Information System Lab118第117页/共257页Database & Information System Lab119第118页/共257页Database & Information System Lab120第119页/共257页Database & Information System Lab121第120页/共257页Database & Information System Lab122第121页/共257页D
32、atabase & Information System Lab123第122页/共257页Database & Information System Lab124第123页/共257页Database & Information System Lab125第124页/共257页Database & Information System Lab126第125页/共257页Database & Information System Lab127第126页/共257页Database & Information System Lab128第127页/共257页Database & Informat
33、ion System Lab129第128页/共257页Database & Information System Lab130第129页/共257页Database & Information System Lab131第130页/共257页Database & Information System Lab132第131页/共257页Database & Information System Lab133第132页/共257页Database & Information System Lab134第133页/共257页Database & Information System Lab135第
34、134页/共257页Database & Information System Lab136A柱B柱C柱第135页/共257页Database & Information System Lab137第136页/共257页source, char target),用来输出搬动一个盘子的提示信息。Database & Information System Lab138第137页/共257页Database & Information System Lab139第138页/共257页Database & Information System Lab140第139页/共257页Database & I
35、nformation System Lab141第140页/共257页Database & Information System Lab142hanoi(2,A,C,B)A Chanoi(3,A,B,C) hanoi(1,A,B,C)hanoi(1,B,C,A)C BB AA CB Chanoi(1,A,B,C)A Bhanoi(1,C,A,B)A Chanoi(2,B,A,C)汉诺塔程序执行框汉诺塔程序执行框图图输入盘子数:输入盘子数:3第141页/共257页Database & Information System Lab143A B C第142页/共257页Database & Info
36、rmation System Lab144A B C第143页/共257页Database & Information System Lab145A B C第144页/共257页Database & Information System Lab146A B C第145页/共257页Database & Information System Lab147A B C第146页/共257页Database & Information System Lab148A B C第147页/共257页Database & Information System Lab149A B C第148页/共257页Dat
37、abase & Information System Lab150A B C第149页/共257页Database & Information System Lab151第150页/共257页Database & Information System Lab152第151页/共257页Database & Information System Lab153第152页/共257页Database & Information System Lab154第153页/共257页Database & Information System Lab155第154页/共257页Database & Infor
38、mation System Lab156第155页/共257页Database & Information System Lab157第156页/共257页函数的说明与使用函数的说明与使用1函数的嵌套与递归函数的嵌套与递归2函数与运算符重载函数与运算符重载3函数与函数与C+程序结构程序结构4158Database & Information System Lab第157页/共257页Database & Information System Lab159第158页/共257页Database & Information System Lab160第159页/共257页Database & In
39、formation System Lab161第160页/共257页Database & Information System Lab162第161页/共257页Database & Information System Lab163第162页/共257页Database & Information System Lab164第163页/共257页Database & Information System Lab165第164页/共257页Database & Information System Lab166第165页/共257页Database & Information System L
40、ab167第166页/共257页Database & Information System Lab168第167页/共257页Database & Information System Lab169第168页/共257页Database & Information System Lab170第169页/共257页Database & Information System Lab171第170页/共257页Database & Information System Lab172第171页/共257页Database & Information System Lab173第172页/共257页Da
41、tabase & Information System Lab174第173页/共257页Database & Information System Lab175第174页/共257页Database & Information System Lab176第175页/共257页Database & Information System Lab177第176页/共257页Database & Information System Lab178第177页/共257页Database & Information System Lab179第178页/共257页Database & Informati
42、on System Lab180第179页/共257页Database & Information System Lab181第180页/共257页Database & Information System Lab182第181页/共257页Database & Information System Lab183第182页/共257页Database & Information System Lab184第183页/共257页函数的说明与使用函数的说明与使用1函数的嵌套与递归函数的嵌套与递归2函数与运算符重载函数与运算符重载3函数与函数与C+程序结构程序结构4185Database & Inf
43、ormation System Lab第184页/共257页Database & Information System Lab186生存期生存期作用域作用域存储类型存储类型函数数据函数数据传递传递全局变量全局变量局部变量局部变量SPSP框架框架标准库标准库函数函数标准库标准库函数函数第185页/共257页Database & Information System Lab187第186页/共257页Database & Information System Lab188第187页/共257页Database & Information System Lab189#include / For ve
44、ctor#include / For sort()#include / For greater()#include #include / For srand() & rand()#include / For time()using namespace std;第188页/共257页Database & Information System Lab190第189页/共257页Database & Information System Lab191第190页/共257页Database & Information System Lab192标准库标准库函数函数生存期生存期作用域作用域存储类型存储类
45、型函数数据函数数据传递传递全局变量全局变量局部变量局部变量SPSP框架框架SPSP框架框架第191页/共257页Database & Information System Lab193第192页/共257页Database & Information System Lab194第193页/共257页Database & Information System Lab195第194页/共257页Database & Information System Lab196SPSP框架框架标准库标准库函数函数生存期生存期作用域作用域存储类型存储类型函数数据函数数据传递传递全局变量全局变量局部变量局部变量全
46、局变量全局变量局部变量局部变量第195页/共257页Database & Information System Lab197栈区(函数局部数据)栈区(函数局部数据)全局数据区全局数据区( (全局、静态全局、静态) )代码区(程序代码)代码区(程序代码)(主函数局部数据)(主函数局部数据)自由存储区(堆、动态数据自由存储区(堆、动态数据) )第196页/共257页Database & Information System Lab198第197页/共257页Database & Information System Lab199第198页/共257页Database & Information S
47、ystem Lab200第199页/共257页Database & Information System Lab201第200页/共257页Database & Information System Lab202全局变量全局变量局部变量局部变量SPSP框架框架标准库标准库函数函数生存期生存期作用域作用域存储类型存储类型函数数据函数数据传递传递函数数据函数数据传递传递第201页/共257页参数参数传递传递返回语句返回语句赋值参数赋值参数引用参数引用参数指针类型参数指针类型参数全局变量全局变量数组类型参数数组类型参数203Database & Information System Lab第202页
48、/共257页Database & Information System Lab204第203页/共257页Database & Information System Lab205第204页/共257页Database & Information System Lab206第205页/共257页Database & Information System Lab207第206页/共257页Database & Information System Lab208第207页/共257页Database & Information System Lab209第208页/共257页Database & I
49、nformation System Lab210第209页/共257页Database & Information System Lab211函数数据函数数据传递传递全局变量全局变量局部变量局部变量SPSP框架框架标准库标准库函数函数生存期生存期作用域作用域存储类型存储类型存储类型存储类型第210页/共257页Database & Information System Lab212第211页/共257页存储类型存储类型静态静态寄存器寄存器外部外部自动自动213Database & Information System Lab第212页/共257页Database & Information S
50、ystem Lab214第213页/共257页Database & Information System Lab215第214页/共257页Database & Information System Lab216第215页/共257页Database & Information System Lab217第216页/共257页Database & Information System Lab218第217页/共257页Database & Information System Lab219第218页/共257页Database & Information System Lab220第219页/
51、共257页Database & Information System Lab221第220页/共257页Database & Information System Lab222第221页/共257页Database & Information System Lab223第222页/共257页Database & Information System Lab224第223页/共257页Database & Information System Lab225第224页/共257页Database & Information System Lab226第225页/共257页Database & Information System Lab227第226页/共257页Database & Information System Lab228存储类型存储类型函数数据函数数据传递传递全局变量全局变量局部
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 情景模拟金融理财师考试试题及答案
- 2024项目管理考试综合分析试题及答案
- 2024年项目管理考试应对策略试题及答案
- 2025年国际金融理财师考试知识架构与内容广度试题及答案
- 明确路径2024年福建事业单位考试试题及答案
- 项目管理需求变更控制技巧试题及答案
- 铁路防护棚搭设施工方案
- 2024年微生物检验学前沿试题及答案
- 2025注册会计师考试技巧与方法论试题及答案
- 2024年农艺师考试的科目覆盖概要 知识面试题及答案
- 纺纱织造工艺流程培训教材实用课件
- 中美关系新时代52张课件
- 广东省广州市广外、铁一、广附三校2022-2023学年高一下学期期末联考物理试题(无答案)
- 《通达信炒股软件从入门到精通》读书笔记模板
- 科研诚信问题课件
- 高频电刀之负极板的正确使用方法
- 关于高中班级管理论文
- 21秋国家开放大学《公共部门人力资源管理》单元自测题参考答案
- 东北抗联英雄人物智慧树知到答案章节测试2023年牡丹江师范学院
- 2023年郑州信息科技职业学院单招考试职业适应性测试模拟试题及答案解析
- 国开电大2022年《小学数学教学研究》形考任务1-4答
评论
0/150
提交评论