收藏 分享(赏)

第7章 数组和字符串.ppt

上传人:dreamzhangning 文档编号:3331875 上传时间:2018-10-14 格式:PPT 页数:52 大小:156KB
下载 相关 举报
第7章 数组和字符串.ppt_第1页
第1页 / 共52页
第7章 数组和字符串.ppt_第2页
第2页 / 共52页
第7章 数组和字符串.ppt_第3页
第3页 / 共52页
第7章 数组和字符串.ppt_第4页
第4页 / 共52页
第7章 数组和字符串.ppt_第5页
第5页 / 共52页
点击查看更多>>
资源描述

1、长春理工大学计算机科学技术学院 陈纯毅,面向对象程序设计,第7章 数组和字符串,长春理工大学计算机科学技术学院 陈纯毅,7.1 数组基础,数组和结构类似,都是数据单元的集合。 数组与结构的区别: 结构存的是不同类型的单元。 数组存的是相同类型的单元。 结构中的每个单元名都是通过名称来访问的。 数组中的单元通过下标来访问。,长春理工大学计算机科学技术学院 陈纯毅,7.1 数组基础,int main( ) int age4;for(int j=0;jagej;for(j=0;j4;j+) cout“you entered: “agejendl;return 0; ,长春理工大学计算机科学技术学院

2、陈纯毅,7.1 数组基础,定义数组的语法 page 189, 图7-1.数据类型 数组名数组大小;数组元素,长春理工大学计算机科学技术学院 陈纯毅,7.1 数组基础,数组元素 page 190 图7-2 数组内的数据项。 第一个数组元素的编号为0。 数组元素的内存地址增加方向是由上往下。,长春理工大学计算机科学技术学院 陈纯毅,7.1 数组基础,访问数组元素 通过下标访问数组元素。 int a2; a0=1;/ a1=2;/ cina0; couta0a1;,长春理工大学计算机科学技术学院 陈纯毅,7.1 数组基础,int main( ) /求数组元素的平均值int a3,sum=0;a0=1

3、;a1=5;a2=6;for(int i=0;i3;i+) sum+=ai;coutsum/3.0;return 0; ,长春理工大学计算机科学技术学院 陈纯毅,7.1 数组基础,初始化数组 page 192,图7-3 int main() int a3=1,5,6,sum=0; /初始化结构变量for(int i=0;i3;i+) sum+=ai;coutsum/3.0;return 0; ,长春理工大学计算机科学技术学院 陈纯毅,7.1 数组基础,初始化数组 page 192,图7-3 int main() int a3=1,5,6,sum=0; /初始化结构变量for(int i=0;i

4、3;i+) sum+=ai;coutsum/3.0;return 0; ,长春理工大学计算机科学技术学院 陈纯毅,7.1 数组基础,int main() int a =1,5,6,sum=0; /初始化结构变量for(int i=0;i3;i+) sum+=ai;coutsum/3.0;return 0; ,长春理工大学计算机科学技术学院 陈纯毅,7.1 数组基础,多维数组 int main() int a32=1,2,3,4,5,6,sum=0;for(int i=0;i3;i+)for(int j=0;j2;j+) coutaij ;coutendl;return 0; ,长春理工大学计算

5、机科学技术学院 陈纯毅,7.1 数组基础,向函数传递数组 double avg( int a32 )int sum=0;for(int i=0;i3;i+)for(int j=0;j2;j+)sum+=aij;return (sum/6.0); int main( )int a32=1,2,3,4,5,6;coutavg(a);return 0; ,长春理工大学计算机科学技术学院 陈纯毅,7.1 数组基础,带有数组参数的函数声明 void display(float a36);/二维数组作为函数参数 void display(float a6);/二维数组作为函数参数 void fun(flo

6、at a);,长春理工大学计算机科学技术学院 陈纯毅,7.1 数组基础,涉及数组参数的函数调用 使用数组名作为参数。,长春理工大学计算机科学技术学院 陈纯毅,7.1 数组基础,结构数组数组可以像包含简单数据类型一样包含结构。 struct partint modelnumber;int partnumber;float cost; ;part apart30;page 200,图7-5,长春理工大学计算机科学技术学院 陈纯毅,7.2 作为类成员数据的数组,class Stack private:int st100;int top; public:Stack( ) top=0; void pus

7、h(int var) top+; sttop=var;int pop( ) top-; return sttop+1; ;,长春理工大学计算机科学技术学院 陈纯毅,7.2 作为类成员数据的数组,int main() Stack st;st.push(3);st.push(5);st.push(9);coutst.pop()endl;coutst.pop()endl;coutst.pop()endl;return 0; ,长春理工大学计算机科学技术学院 陈纯毅,7.3 对象数组,class Complex private:double real, imag; public:Complex(dou

8、ble r,double i) real=r; imag=i;Complex( ) real=0; imag=0;void SetRealAndImag(double r,double i) real=r; imag=i;void show() coutreal+imagiendl; ;,长春理工大学计算机科学技术学院 陈纯毅,7.3 对象数组,int main( ) Complex c2;c0.SetRealAndImag(3,2);c1.SetRealAndImag(7,9);for(int i=0;i2;i+)ci.show();return 0; ,长春理工大学计算机科学技术学院 陈纯

9、毅,7.3 对象数组,数组越界 int main( )Complex c2;c0.SetRealAndImag(3,6);c1.SetRealAndImag(7,8);for(int i=0;i100;i+)ci.show();return 0; ,长春理工大学计算机科学技术学院 陈纯毅,7.3 对象数组,数组越界 page 204 数组范围 int main( ) Complex c2;for(int i=0;i30;i+)ci.SetRealAndImag(i,i);for(int i=0;i30;i+)ci.show();return 0; ,长春理工大学计算机科学技术学院 陈纯毅,7.

10、4 C字符串,C字符串是char类型的数组,数组的最后一个元素为0。当处理C字符串时,遇到0就表示C字符串结束。 0和空格字符的区别? int main( )char str10=a,a,a,a,a,a,a,a,a,a;cinstr;for(int i=0;i(stri) ;return 0; ,长春理工大学计算机科学技术学院 陈纯毅,7.4 C字符串,输入 yes输出为?,长春理工大学计算机科学技术学院 陈纯毅,7.4 C字符串,输出: 121 101 115 0 97 97 97 97 97 97,长春理工大学计算机科学技术学院 陈纯毅,7.4 C字符串,避免缓冲区溢出 #include

11、“iomanip“ int main() char str10;cinsetw(10)str;coutstr;return 0; ,长春理工大学计算机科学技术学院 陈纯毅,7.4 C字符串,字符串的控制 int main() char str =“hello world“;coutstr;return 0; ,长春理工大学计算机科学技术学院 陈纯毅,7.4 C字符串,读取字间的空格 int main() char str100;cinstr;coutstr;return 0; ,长春理工大学计算机科学技术学院 陈纯毅,7.4 C字符串,输入: abc bdd dd输出?page 211, 7.

12、4.4,长春理工大学计算机科学技术学院 陈纯毅,7.4 C字符串,int main() char str100;cin.get(str,100);coutstr;return 0; ,长春理工大学计算机科学技术学院 陈纯毅,7.4 C字符串,读取多行信息 page 211, 7.4.5 int main() char str100;cin.get(str,100, $);/输入$后停止coutstr;return 0; ,长春理工大学计算机科学技术学院 陈纯毅,7.4 C字符串,字符串的复制 int main( ) char str1=“abc“;char str210;for(int i=0

13、;str1i!=0;i+)str2i=str1i;str2i=0;coutstr2;return 0; ,长春理工大学计算机科学技术学院 陈纯毅,7.4 C字符串,字符串的复制 #include int main( ) char str1=“abc“;char str210;strcpy(str2,str1);coutstr2;return 0; ,长春理工大学计算机科学技术学院 陈纯毅,7.4 C字符串,字符串数组 int main() char str1210=“abc“,“aa“;coutstr10endlstr11endl;strcpy(str10,“ok“);strcpy(str11

14、,“hello“);coutstr10endlstr11endl;return 0; ,长春理工大学计算机科学技术学院 陈纯毅,7.4 C字符串,字符串作为类成员 class Student private:char name10;int age; public:Student(char n,int a) strcpy(name,n);age=a;void show() coutname,ageendl; ;,长春理工大学计算机科学技术学院 陈纯毅,7.4 C字符串,int main() Student s(“Jim“,20);s.show();return 0; ,长春理工大学计算机科学技术

15、学院 陈纯毅,7.4 C字符串,用户自定义字符串类型 class string private:enum Sz=80;char strSz;public:string() str0=0;string(char s) strcpy(str,s);void display() coutstr;void concat(string s) if( strlen(str)+strlen(s.str)Sz) strcat(str,s.str);else cout“string too long”; ;,长春理工大学计算机科学技术学院 陈纯毅,7.5 string类,头文件string #include u

16、sing namespace std; #include int main() string s1(“man“);string s2=“hello“;string s3;couts3;couts3endl;s3=s1+string(“ “)+s2;couts3;return 0; ,长春理工大学计算机科学技术学院 陈纯毅,7.5 string类,可以把string当作一个类型,即字符串类型。string类型的字符串能够根据需要自动增加长度,而且可以使用=、+、等运算符。,长春理工大学计算机科学技术学院 陈纯毅,7.5 string类,查找string对象 int main()string s2

17、=“This is a demo.“;int n;n=s2.find(“is“);coutn;getchar();getchar();return 0; ,长春理工大学计算机科学技术学院 陈纯毅,7.5 string类,注意:字符串中的第一个字符的编号为0。 s.find_first_of(string s1) s中第一个在s1中出现的字符的编号.string s=“This is a demo. “; int n=s.find_first_of(“qd“);/ n=10 int n=s.find_first_of(“ed“);/ n=10 int n=s.find_first_of(“em“

18、);/ n=11 int n=s.find_first_of(“a em“);/ n=4 int n=s.find_first_of(“aem“);/ n=8,长春理工大学计算机科学技术学院 陈纯毅,7.5 string类,s.find_first_not_of(string s1) s中的第1个不在s1中出现的字符的编号。string s=“This is a demo. “; int n=s.find_first_not_of(“qd“);/ n=0 int n=s.find_first_not_of(“That“);/ n=2,长春理工大学计算机科学技术学院 陈纯毅,7.5 string

19、类,修改string对象string s=“This is a demo.“; s.erase(0,2); couts;输出: is is a demo.,长春理工大学计算机科学技术学院 陈纯毅,7.5 string类,修改string对象string s=“This is a demo.“; s.replace(1,5,“ii“); couts;输出: Tiis a demo.,长春理工大学计算机科学技术学院 陈纯毅,7.5 string类,修改string对象string s=“This is a demo.“; s.replace(1,5,“xx“); couts;输出: Txxs a

20、demo.,长春理工大学计算机科学技术学院 陈纯毅,7.5 string类,修改string对象string s=“This is a demo.“; s.insert(2,“-“); couts;输出: Th-is is a demo.,长春理工大学计算机科学技术学院 陈纯毅,7.5 string类,修改string对象string s=“This is a demo.“; s.append(2, -);/(个数,字符) couts;输出: This is a demo. -,长春理工大学计算机科学技术学院 陈纯毅,7.5 string类,比较string pare(int i,int n,

21、string s1,int j,int n) 从s中编号为i的字符开始,比较n个字符;从s1中编号为j的字符开始,比较n个字符;(1)若相等,则返回0; (2)若字符不等: 若s中的字符的ASCII码比s1中的字符的ASCII码小则返回-1,否则返回1。,长春理工大学计算机科学技术学院 陈纯毅,7.5 string类,比较string string s=“Im a teacher.“; pare(0,2,“Im“,0,2)endl; pare(0,2,“Ad“,0,2)endl; pare(0,2,“Xd“,0,2)endl;输出: 0 1 -1,长春理工大学计算机科学技术学院 陈纯毅,7.5

22、 string类,访问string对象中的字符 string s=“Im a teacher.“; couts2endl; couts.at(2)endl;输出: m m,长春理工大学计算机科学技术学院 陈纯毅,7.5 string类,int main( ) char str100;string s=“Im a teacher.“;s.copy(str,s.length(),0);strs.length()=0;coutstrendl;return 0; ,从s中编号为0的字符开始复制直至最后一个字符。,总共复制的字符个数,长春理工大学计算机科学技术学院 陈纯毅,7.5 string类,siz

23、e() : string对象的字符个数 length() : string对象的字符个数 capacity() : string对象实际占用的内存空间 max_size() : string对象的最大字符数C字符串以字符0结束,string字符串不是以字符0结束,它通过长度成员来表示字符串长度。,长春理工大学计算机科学技术学院 陈纯毅,7.6 小结,(1)数组:相同类型数据组成的集合,通过数组下标来访问数组元素 (2)多维数组 (3)当向函数传递数组时,实际传递的是地址 (4)C字符串以0结束,使用C字符串需要注意:程序员应确保字符串数组有足够的空间容纳下字符串的所有字符 (5)string类是C+中处理字符串的标准类型,

展开阅读全文
相关资源
猜你喜欢
相关搜索

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

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报