




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、.,第六章 数组 指针与字符串,C+语言程序设计,.,2,本章主要内容,数组 指针 动态存储分配 指针与数组 指针与函数 字符串,.,3,数组的概念,数组是具有一定顺序关系的若干相同类型变量的集合体,组成数组的变量称为该数组的元素。 数组属于构造类型。,数 组,.,4,一维数组的声明与引用,一维数组的声明 类型说明符 数组名 常量表达式 ; 例如:int a10; 表示 a 为整型数组,有10个元素:a0.a9,引用 必须先声明,后使用。 只能逐个引用数组元素,而不能一次引用整个数组 例如:a0=a5+a7-a2*3,数 组,.,5,例6. 1一维数组的声明与引用,#include using
2、 namespace std; int main() int A10,B10; int i; for(i=0;i10;i+) Ai=i*2-1; B10-i-1=Ai; ,for(i=0;i10;i+) coutAi =Ai; cout Bi= Biendl; ,数 组,.,6,一维数组的存储顺序,数组元素在内存中顺次存放,它们的地址是连续的。 例如:具有10个元素的数组 a,在内存中的存放次序如下:,数组名字是数组首元素的内存地址。 数组名是一个常量,不能被赋值。,数 组,.,7,一维数组的初始化,在声明数组时对数组元素赋以初值。 例如:static int a10=0,1,2,3,4,5,
3、6,7,8,9; 可以只给一部分元素赋初值。 例如:static int a10=0,1,2,3,4; 在对全部数组元素赋初值时,可以不指定数组长度。 例如:static int a=1,2,3,4,5,数 组,.,8,#include using namespace std; int main() int i; static int f20=1,1;/初始化第0、1个数 for(i=2;i20;i+) /求第219个数 fi=fi-2+fi-1; for(i=0;i20;i+) /输出,每行5个数/ if(i%5=0) coutendl; cout.width(12); /设置输出宽度为12
4、 coutfi; ,例:用数组来处理求Fibonacci数列问题,.,9,例:用数组来处理求Fibonacci数列问题,运行结果: 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765,.,10,一维数组应用举例,循环从键盘读入若干组选择题答案,计算并输出每组答案的正确率,直到输入ctrl+z为止。 每组连续输入5个答案,每个答案可以是a.d。,数 组,#include using namespace std; int main() char key =a,c,b,a,d; char c; int ques=0,n
5、umques=5,numcorrect=0; coutEnter the numques question tests:endl; while(cin.get(c) if(c != n) if(c = keyques) numcorrect+; cout ; else cout*; else cout Score float(numcorrect)/numques*100%; ques = 0; numcorrect = 0; cout endl; continue; ques+; ,11,运行结果: acbba * Score 60% acbad Score 100% abbda * * S
6、core 40% bdcba * Score 0%,12,.,13,二维数组的声明及引用,数据类型 标识符常量表达式1常量表达式2 ; 例: int a53; 表示a为整型二维数组,其中第一维有5个下标(04),第二维有3个下标(02),数组的元素个数为15,可以用于存放5行3列的整型数据表格。,数 组,.,14,二维数组的声明 类型说明符 数组名常量表达式常量表达式 例如:float a34;,二维数组的声明及引用,存储顺序 按行存放,上例中数组a的存储顺序为:,引用 例如:b12=a23/2,下标不要越界,数 组,.,15,将所有数据写在一个内,按顺序赋值 例如:static int a3
7、4=1,2,3,4,5,6,7,8,9,10,11,12; 分行给二维数组赋初值 例如:static int a34 =1,2,3,4,5,6,7,8,9,10,11,12; 可以对部分元素赋初值 例如:static int a34=1,0,6,0,0,11;,二维数组的初始化,数 组,.,16,数组作为函数参数,数组元素作实参,与单个变量一样。 数组名作参数,形、实参数都应是数组名,类型要一样,传送的是数组首地址。对形参数组的改变会直接影响到实参数组。,数 组,.,17,例6-2 使用数组名作为函数参数,主函数中初始化一个矩阵并将每个元素都输出,然后调用子函数,分别计算每一行的元素之和,将和
8、直接存放在每行的第一个元素中,返回主函数之后输出各行元素的和。,数 组,#include using namespace std; void RowSum(int A4, int nrow) int sum; for (int i = 0; i nrow; i+) sum = 0; for(int j = 0; j 4; j+) sum += Aij; cout Sum of row i is sum endl; Ai0=sum; ,18,int main() int Table34 = 1,2,3,4,2,3,4,5,3,4,5,6; for (int i = 0; i 3; i+) for
9、 (int j = 0; j 4; j+) cout Tableij ; cout endl; RowSum(Table,3); for (int i = 0; i 3; i+) cout Tablei0 ,19,运行结果: 1 2 3 4 2 3 4 5 3 4 5 6 Sum of row 0 is 10 Sum of row 1 is 14 Sum of row 2 is 18 10 14 18,20,.,21,对象数组,声明: 类名 数组名元素个数; 访问方法: 通过下标访问 数组名下标.成员名,数 组,.,22,对象数组初始化,数组中每一个元素对象被创建时,系统都会调用类构造函数初始
10、化该对象。 通过初始化列表赋值。 例: Point A2=Point(1,2),Point(3,4); 如果没有为数组元素指定显式初始值,数组元素便使用默认值初始化(调用默认构造函数)。,数 组,.,23,数组元素所属类的构造函数,不声明构造函数,则采用默认构造函数。 各元素对象的初值要求为相同的值时,可以声明具有默认形参值的构造函数。 各元素对象的初值要求为不同的值时,需要声明带形参的构造函数。 当数组中每一个对象被删除时,系统都要调用一次析构函数。,数 组,.,24,例6-3 对象数组应用举例,/Point.h #if !defined(_POINT_H) #define _POINT_H
11、 class Point public: Point(); Point(int xx,int yy); Point(); void Move(int x,int y); int GetX() return X; int GetY() return Y; private: int X,Y; ; #endif,数 组,/6-2.cpp #include using namespace std; #include Point.h Point:Point() X=Y=0; coutDefault Constructor called.endl; Point:Point(int xx,int yy) X
12、=xx; Y=yy; cout Constructor called.endl; Point :Point() coutDestructor called.endl; void Point :Move(int x,int y) X=x; Y=y; ,25,#include #include Point.h using namespace std; int main() coutEntering main.endl; Point A2; for(int i=0;i2;i+) Ai.Move(i+10,i+20); coutExiting main.ai; coutendl; for(i=0; i
13、10; i+) coutai; coutendl; for(i=0; i10; i+) coutai; coutendl; for(p=a; p(a+10); p+) cout*p; ,45,.,46,指针数组,数组的元素是指针型 例:Point *pa2; 由pa0,pa1两个指针组成,指 针,.,47,例6-8 利用指针数组存放单位矩阵,#include using namespace std; int main() int line1=1,0,0;/声明数组,矩阵的第一行 int line2=0,1,0;/声明数组,矩阵的第二行 int line3=0,0,1;/声明数组,矩阵的第三行
14、int *p_line3; /声明整型指针数组 p_line0=line1; /初始化指针数组元素 p_line1=line2; p_line2=line3;,指 针,/输出单位矩阵 coutMatrix test:endl; for(int i=0;i3;i+) /对指针数组元素循环 for(int j=0;j3;j+)/对矩阵每一行循环 coutp_lineij ; coutendl; ,输出结果为: Matrix test: 1,0,0 0,1,0 0,0,1,48,.,49,例6-9 二维数组举例,#include using namespace std; int main() int
15、 array223=11,12,13,21,22,23; for(int i=0;i2;i+) cout*(array2+i)endl; for(int j=0;j3;j+) cout*(*(array2+i)+j) ; /或者 coutarray2ij ; cout x; splitfloat(x, ,53,运行结果: Enter three(3) floating point numbers 4.7 Integer Part is 4 Fraction Part is 0.7 8.913 Integer Part is 8 Fraction Part is 0.913 -4.7518 In
16、teger Part is -4 Fraction Part is -0.7518,54,.,55,例: 输出数组元素的内容和地址,#include #include using namespace std; void Array_Ptr(long *P, int n) int i; coutIn func, address of array is unsigned long(P)endl; coutAccessing array using pointers endl; for (i = 0; i n; i+) cout Address for index i is unsigned lon
17、g(P+i); cout Value is *(P+i)endl; ,指针与函数,int main() long list5=50, 60, 70, 80, 90; coutIn main, address of array is unsigned long(list)endl; coutarrayi; print(array,N); ,指 针,void print(const int *p, int n) cout*p; for(int i=1;in;i+) cout.*(p+i); coutendl; ,59,.,60,指针型函数,当函数的返回值是地址时,该函数就是指针形函数。 声明形式
18、存储类型 数据类型 *函数名(),指针与函数,.,61,声明形式 存储类型 数据类型 (*函数指针名)(); 含义: 数据指针指向数据存储区,而函数指针指向的是程序代码存储区。,指向函数的指针,指针与函数,.,62,例6-11函数指针,#include using namespace std; void print_stuff(float data_to_ignore); void print_message(float list_this_data); void print_float(float data_to_print); void (*function_pointer)(float)
19、; int main() float pi=(float)3.14159; float two_pi=(float)2.0*pi;,指针与函数,print_stuff(pi); function_pointer = print_stuff; function_pointer(pi); function_pointer = print_message; function_pointer(two_pi); function_pointer(13.0); function_pointer = print_float; function_pointer(pi); print_float(pi); ,6
20、3,void print_stuff(float data_to_ignore) coutThis is the print stuff function.n; void print_message(float list_this_data) coutThe data to be listed is list_this_dataendl; void print_float(float data_to_print) coutThe data to be printed is data_to_printY=yy;,指 针,.,72,指向类的非静态成员的指针,通过指向成员的指针只能访问公有成员 声明
21、指向成员的指针 声明指向公有数据成员的指针 类型说明符 类名:*指针名; 声明指向公有函数成员的指针 类型说明符 (类名:*指针名)(参数表);,指 针,.,73,指向类的非静态成员的指针,指向数据成员的指针 说明指针应该指向哪个成员 指针名= /声明对象A Point *p1= ,指 针,.,76,指向类的静态成员的指针,对类的静态成员的访问不依赖于对象 可以用普通的指针来指向和访问静态成员 例6-14 通过指针访问类的静态数据成员 例6-15 通过指针访问类的静态函数成员,指 针,.,77,例6-14通过指针访问类的静态数据成员,#include using namespace std;
22、class Point /Point类声明 public: /外部接口 Point(int xx=0, int yy=0) X=xx;Y=yy;countP+;/构造函数 Point(Point /静态数据成员定义性说明,指 针,int main() /主函数 /声明一个int型指针,指向类的静态成员 int *count= ,78,.,79,例6-15通过指针访问类的静态函数成员,#include using namespace std; class Point /Point类声明 public: /外部接口 /其他函数略 static void GetC() /静态函数成员 cout Ob
23、ject id=countPendl; private: /私有数据成员 int X,Y; static int countP; /静态数据成员引用性说明 ; / 函数实现略 int Point:countP=0; /静态数据成员定义性说明,指 针,int main() /主函数 /指向函数的指针,指向类的静态成员函数 void (*gc)()=Point:GetC; Point A(4,5); /声明对象A coutPoint A,A.GetX(),A.GetY(); gc();/输出对象序号,通过指针访问静态函数成员 Point B(A);/声明对象B coutPoint B,B.GetX
24、(),B.GetY(); gc();/输出对象序号,通过指针访问静态函数成员 ,80,.,81,动态申请内存操作符 new,new 类型名T(初值列表) 功能:在程序执行期间,申请用于存放T类型对象的内存空间,并依初值列表赋以初值。 结果值:成功:T类型的指针,指向新分配的内存。失败:0(NULL),动态存储分配,.,82,释放内存操作符delete,delete 指针P 功能:释放指针P所指向的内存。P必须是new操作的返回值。,动态存储分配,.,83,例6-16 动态创建对象举例,#include using namespace std; class Point public: Point
25、() X=Y=0; coutDefault Constructor called.n; Point(int xx,int yy) X=xx;Y=yy; cout Constructor called.n; Point() coutDestructor called.n; int GetX()return X; int GetY()return Y; void Move(int x,int y) X=x; Y=y; private: int X,Y; ;,动态存储分配,int main() coutStep One:endl; Point *Ptr1=new Point; delete Ptr1
26、; coutStep Two:endl; Ptr1=new Point(1,2); delete Ptr1; return 0; ,运行结果: Step One: Default Constructor called. Destructor called. Step Two: Constructor called. Destructor called.,84,.,85,例6-17动态创建对象数组举例,#include using namespace std; class Point /类的声明同例6-16,略 ; int main() Point *Ptr=new Point2; /创建对象数
27、组 Ptr0.Move(5,10); /通过指针访问数组元素的成员 Ptr1.Move(15,20); /通过指针访问数组元素的成员 coutDeleting.endl; delete Ptr; /删除整个对象数组 return 0; ,动态存储分配,运行结果: Default Constructor called. Default Constructor called. Deleting. Destructor called. Destructor called.,86,例6-18动态数组类,#include using namespace std; class Point /类的声明同例6
28、-16 ; class ArrayOfPoints public: ArrayOfPoints(int n) numberOfPoints=n; points=new Pointn; ArrayOfPoints() coutDeleting.number; /创建对象数组 ArrayOfPoints points(number); /通过指针访问数组元素的成员 points.Element(0).Move(5,10); /通过指针访问数组元素的成员 points.Element(1).Move(15,20); ,88,运行结果如下: Please enter the number of poi
29、nts:2 Default Constructor called. Default Constructor called. Deleting. Destructor called. Destructor called.,89,.,90,动态创建多维数组,new 类型名T下标表达式1下标表达式2; 如果内存申请成功,new运算返回一个指向新分配内存首地址的指针,是一个T类型的数组,数组元素的个数为除最左边一维外各维下标表达式的乘积。例如: char (*fp)3; fp = new char23;,动态存储分配,char (*fp)3;,fp,fp+1,91,.,92,例6-18动态创建多维数组
30、,#include using namespace std; int main() float (*cp)98; int i,j,k; cp = new float898; for (i=0; i8; i+) for (j=0; j9; j+) for (k=0; k9; k+) *(*(*(cp+i)+j)+k)=i*100+j*10+k; /通过指针访问数组元素,动态存储分配,for (i=0; i8; i+) for (j=0; j9; j+) for (k=0; k8; k+) /将指针cp作为数组名使用, /通过数组名和下标访问数组元素 coutcpijk ; coutendl; c
31、outnumber; ArrayOfPoints pointsArray1(number); pointsArray1.Element(0).Move(5,10); pointsArray1.Element(1).Move(15,20); ArrayOfPoints pointsArray2(pointsArray1); coutCopy of pointsArray1:endl; coutPoint_0 of array2: pointsArray2.Element(0).GetX() , pointsArray2.Element(0).GetY()endl; coutPoint_1 of
32、array2: pointsArray2.Element(1).GetX() , pointsArray2.Element(1).GetY()endl;,98,pointsArray1.Element(0).Move(25,30); pointsArray1.Element(1).Move(35,40); coutAfter the moving of pointsArray1:endl; coutPoint_0 of array2: pointsArray2.Element(0).GetX() , pointsArray2.Element(0).GetY()endl; coutPoint_1
33、 of array2: pointsArray2.Element(1).GetX() , pointsArray2.Element(1).GetY()endl; ,99,运行结果如下: Please enter the number of points:2 Default Constructor called. Default Constructor called. Copy of pointsArray1: Point_0 of array2: 5, 10 Point_1 of array2: 15, 20 After the moving of pointsArray1: Point_0
34、of array2: 25, 30 Point_1 of array2: 35, 40 Deleting. Destructor called. Destructor called. Deleting. 接下来程序出现异常,也就是运行错误。,100,拷贝前,拷贝后,101,.,102,例6-21对象的深拷贝,#include using namespace std; class Point /类的声明同例6-16 ; class ArrayOfPoints public: ArrayOfPoints(ArrayOfPoints,浅拷贝与深拷贝,ArrayOfPoints :ArrayOfPoi
35、nts (ArrayOfPoints int main() /同例6-20 ,103,程序的运行结果如下: Please enter the number of points:2 Default Constructor called. Default Constructor called. Default Constructor called. Default Constructor called. Copy of pointsArray1: Point_0 of array2: 5, 10 Point_1 of array2: 15, 20 After the moving of point
36、sArray1: Point_0 of array2: 5, 10 Point_1 of array2: 15, 20 Deleting. Destructor called. Destructor called. Deleting. Destructor called. Destructor called.,104,拷贝前,拷贝后,105,.,106,用字符数组存储和处理字符串,字符数组的声明和引用,例:static char str8=112,114,111,103,114,97,109,0; static char str8=p,r,o,g,r,a,m,0; static char st
37、r8=program; static char str=program;,字符串 字符串常量,例如:china 没有字符串变量,用字符数组来存放字符串 字符串以0为结束标志 字符数组的初始化,字符串,.,107,例6-22 输出一个字符串,#include using namespace std; int main() static char c10=I, ,a,m, ,a, ,b,o,y; int i; for(i=0;i10;i+) coutci; coutendl; ,运行结果: I am a boy,字符串,.,108,例6-23输出一个钻石图形,#include using namespace std; int main() static char diamond5= , ,*, ,*, ,*, *, , , ,*, ,*, ,*, , ,*; int i,j; for (i=0;i5;i+) for(j=0;j5 ,运行结果:,* * * * * * * *,字符串,.,109,字符串的输入/输出,方法 逐个字符输入输出 将整个字
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年夏季广西农村投资集团校园招聘70人笔试历年参考题库附带答案详解
- 幼儿普通话教学课件
- 第七章硫酸88课件
- 古诗鉴赏炼句教学课件
- 中职教学利息课件
- 2025年税法考试引导书试题与答案
- 2025年文化旅游演艺项目市场细分与品牌战略执行研究报告
- 共享厨房产业链上下游协同发展2025年趋势报告
- 2025年新初三英语人教新版尖子生专题复习《选词填空》
- 2025年全球铀矿资源分布与核能产业市场前景深度解读报告
- 内蒙古地区葡萄醋发酵用优势醋酸菌的筛选鉴定及应用
- 2025年华侨港澳台学生联招考试英语试卷试题(含答案详解)
- 《国防动员实施》课件
- 三年级 人教版 数学 第六单元《两位数乘一位数(不进位)口算》课件
- 中国高血压防治指南(2024年修订版)
- 广东发布智慧公路标准体系(2024版)
- 货物受理验视制度
- 非法入侵应急预案
- 保利地产在线测评题答案
- 客服专员+云客服入门考试双12阿里淘宝云客服在线+语音+专项云客服考试试题及答案
- Unit 1 You and Me 单元教学设计 2024-2025学年人教版英语七年级上册
评论
0/150
提交评论