高素质编程师湖南大学语言课件第五章.ppt_第1页
高素质编程师湖南大学语言课件第五章.ppt_第2页
高素质编程师湖南大学语言课件第五章.ppt_第3页
高素质编程师湖南大学语言课件第五章.ppt_第4页
高素质编程师湖南大学语言课件第五章.ppt_第5页
已阅读5页,还剩33页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第五章 数组 构造数据类型之一 数组:有序数据的集合,用数组名标识 元素:属同一数据类型,用数组名和下标确定 5.1 一维数组 一维数组的定义 v定义方式: 数据类型 数组名 常量表达式; 合法标识符 表示元素个数 下标从0开始 :数组运算符 单目运算符 优先级(1) 左结合 不能用( ) 例 int a6; a0 0 1 4 5 a1 a2 a3 a4 a5 2 3 a 编译时分配连续内存 内存字节数=数组维数* sizeof(元素数据类型) 数组名表示内存首地址, 是地址常量 Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. 一维数组的引用 v数组必须先定义,后使用 v只能逐个引用数组元素,不能一次引用整个数组 v数组元素表示形式: 数组名下标 其中:下标可以是常量或整型表达式 例 int i=15; int datai; (不能用变量定义数组维数) 例 int a10; printf(“%d”,a); () 必须 for(j=0;jxi,令min=xi 3. 输出:max和min #include #define SIZE 10 main() int xSIZE,i,max,min; printf(“Enter 10 integers:n“); for(i=0;ixi) min=xi; printf(“Maximum value is %dn“,max); printf(“Minimum value is %dn“,min); Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. 例 用数组求Fibonacci数列前20个数 f0 f1 f2 f3 f4 f5 f19 . 1 1 f19 0 1 4 5 2 3 19 2 3 5 #include main() int i; int f20=1,1; for(i=2;ia1,则交换;然 后比较第二个数与第三个数;依次类推,直至第n-1个数和第 n个数比较为止第一趟冒泡排序,结果最大的数被安置在 最后一个元素位置上 (2)对前n-1个数进行第二趟冒泡排序,结果使次大的数被安置在 第n-1个元素位置 (3)重复上述过程,共经过n-1趟冒泡排序后,排序结束 Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. 例 38 49 65 76 13 27 30 97第一趟 38 49 65 13 27 30 76第二趟 38 49 13 27 30 65第三趟 38 13 27 30 49 第四趟 13 27 30 38第五趟 13 27 30 第六趟 49 38 65 97 76 13 27 30 初始关键字 n=8 38 49 76 9713 9727 9730 97 13 76 76 7627 30 13 6527 6530 65 13 13 49 4930 4927 3827 3830 38 13 27 第七趟 Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. 输入n 个数给a1 到 an for j=1 to n-1 for i=1 to n-j aiai+1 真假 aiai+1 输出a1 到 an #include main() int a11,i,j,t; printf(“Input 10 numbers:n“); for(i=1;iai+1) t=ai; ai=ai+1; ai+1=t; printf(“The sorted numbers:n“); for(i=1;i main() int a11,i,j,k,x; printf(“Input 10 numbers:n“); for(i=1;i main() int a23=1,2,3,4,5,6; int b32,i,j; printf(“array a:n“); for(i=0;imax 真假 max=aij row=i colum=j 输出:max和row,colum #include main() int a34=1,2,3,4, 9,8,7,6, -10,10,-5,2; int i,j,row=0,colum=0,max; max=a00; for(i=0;imax) max=aij; row=i; colum=j; printf(“max=%d,row=%d, colum=%dn“,max,row,colum); Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. 例 读入下表中值到数组,分别求各行、各列及表中所有数之和 12 4 6 15 7 9 8 23 3 2 5 17 12 4 6 22 15 7 9 31 8 23 3 34 2 5 17 24 37 39 35 111 #include main() int x54,i,j; for(i=0;i main() char c10=I, ,a,m, ,a, ,b,o,y; int i; for(i=0;i main() char a15,b5,c5; scanf(“%s%s%s“,a,b,c); printf(“a=%snb=%snc=%sn“,a,b,c); scanf(“%s“,a); printf(“a=%sn“,a); 运行情况: 输入:How are you? 输出:a=How b=are c=you? 输入:How are you? 输出:a=How scanf中%s输入时,遇空格 或回车结束 运行情况: 输入:How are you? Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. 例 若准备将字符串“This is a string.”记录下来, 错误的输入语句为: (A)scanf(“%20s”,s); (B)for(k=0;k main( ) char string80; printf(“Input a string:”); gets(string); puts(string); 输入: How are you? 输出: How are you ? Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. u字符串连接函数strcat 格式:strcat(字符数组1,字符数组2) 功能:把字符数组2连到字符数组1后面 返值:返回字符数组1的首地址 说明:字符数组1必须足够大 连接前,两串均以0结束;连接后,串1的0取消, 新串最后加0 u字符串拷贝函数strcpy 格式:strcpy(字符数组1,字符串2) 功能:将字符串2,拷贝到字符数组1中去 返值:返回字符数组1的首地址 说明:字符数组1必须足够大 拷贝时0一同拷贝 不能使用赋值语句为一个字符数组赋值 例 char str120,str220; str1=“Hello!”; () str2=str1; () Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. 例 strcpy与strcat举例 #include #include void main() char destination25; char blank = “ “, c= “C+“, turbo = “Turbo“; strcpy(destination, turbo); strcat(destination, blank); strcat(destination, c); printf(“%sn“, destination); Turbo C+ T r b o C + + 0 1 2 3 4 5 6 7 8 9 u 0 24 . T r b o 0 1 2 3 4 5 6 7 8 9 u 0 24 . . T r b o 0 0 1 2 3 4 5 6 7 8 9 u 24 . . Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. u字符串比较函数strcmp 格式:strcmp(字符串1,字符串2) 功能:比较两个字符串 比较规则:对两串从左向右逐个字符比较(ASCII码), 直到遇到不同字符或0为止 返值:返回int型整数,a. 若字符串1 字符串2, 返回正整数 c. 若字符串1= 字符串2, 返回零 说明:字符串比较不能用“=”,必须用strcmp u字符串长度函数strlen 格式:strlen(字符数组) 功能:计算字符串长度 返值:返回字符串实际长度,不包括0在内 例 对于以下字符串,strlen(s)的值为: (1)char s10=A,0,B,C,0,D; (2)char s =“tv0willn”; (3)char s =“x69082n”; 答案:1 3 1 Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. #include #include main() char str1 = ”Hello!“, str2 = ”How are you?”,str20; int len1,len2,len3; len1=strlen(str1); len2=strlen(str2); if(strcmp(str1, str2)0) strcpy(str,str1); strcat(str,str2); else if (strcmp(str1, str2) main() char string81; int i,num=0,word=0; char c; gets(string); for(i=0;(c=stringi)!=0;i+) if(c= ) word=0; else if(word=0) word=1; num+; printf(“There are %d words in the linen“,num); Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. 当前字符=空格 是 否 未出现新单词,使word=0,num不累加 前一字符为空格(word=0),新单词出现, word=1,num加1 前一字符为非空格(word=1),未出现新单词,num不变 否 0 是 1 1 是 1 未 0 1 否 0 是 1 2 否 1 未 1 2 是 1 未 0 2 否 0 是 1 3 是 1 未 0 3 否 0 是 1 4 否 1 未 1 4 否 1 未 1 4 否 1 未 1 4 例 输入:I am a boy. 当前字符 是否空格 word原值 新单词开始否 word新值 num值 I amaboy. Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. 例 有三个字符串,找出其中最大者 H o w 0 H e l l o 0 H i g h 0 str0 str1 str2 #include #include main() char string20,str320; int i; for(i=0;i0) strcpy(string,str0); else strcpy(string,str1); if(strcmp(str2,string)0) strcpy(string,str2); printf(“nThe largest string is:n%sn“,string); Evaluation only.Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile .Created with Aspose.Slides for .NET 3.5 Client Profile . Copyright 2004-2011 Aspose Pty Ltd.Copyright 2004-2011 Aspose Pty Ltd. 例 有十个学生的成绩,求平均分 92 85 68 75 54 88 98 45 61 79 92 85 68 54 88 98 45 75 61 79 a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a #include main() int k , a10; for(k=0;k10;k+) scanf(“%d”, for(k=0;k10;k+) sum+=ak; printf(“Ave

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论