第八章数组与字符串

上传人:桔**** 文档编号:571147612 上传时间:2024-08-08 格式:PPT 页数:35 大小:355.02KB
返回 下载 相关 举报
第八章数组与字符串_第1页
第1页 / 共35页
第八章数组与字符串_第2页
第2页 / 共35页
第八章数组与字符串_第3页
第3页 / 共35页
第八章数组与字符串_第4页
第4页 / 共35页
第八章数组与字符串_第5页
第5页 / 共35页
点击查看更多>>
资源描述

《第八章数组与字符串》由会员分享,可在线阅读,更多相关《第八章数组与字符串(35页珍藏版)》请在金锄头文库上搜索。

1、1安徽大学计算机教学部安徽大学计算机教学部计计算算机机程程序序设设计计基基础础第八章 数组与字符串学习目标学习目标理解数据类型及数据在内存中是如何存储的理解数据类型及数据在内存中是如何存储的理解数据类型及数据在内存中是如何存储的理解数据类型及数据在内存中是如何存储的了解数组的意义及数组的使用方法了解数组的意义及数组的使用方法了解数组的意义及数组的使用方法了解数组的意义及数组的使用方法了解字符串的意义及字符串的使用方法了解字符串的意义及字符串的使用方法了解字符串的意义及字符串的使用方法了解字符串的意义及字符串的使用方法2安徽大学计算机教学部安徽大学计算机教学部计计算算机机程程序序设设计计基基础础

2、8.1 数据类型与数据结构数据与数据结构的关系数据与数据结构的关系数据与数据结构的关系数据与数据结构的关系 数据结构指数据的组织形式,即数据的抽象机制数据结构指数据的组织形式,即数据的抽象机制数据结构指数据的组织形式,即数据的抽象机制数据结构指数据的组织形式,即数据的抽象机制数据的逻辑结构:元素间的逻辑关系数据的逻辑结构:元素间的逻辑关系数据的逻辑结构:元素间的逻辑关系数据的逻辑结构:元素间的逻辑关系 线性数据结构与非线性数据结构线性数据结构与非线性数据结构线性数据结构与非线性数据结构线性数据结构与非线性数据结构数据的物理结构:物理实现方法,与机器有关数据的物理结构:物理实现方法,与机器有关数

3、据的物理结构:物理实现方法,与机器有关数据的物理结构:物理实现方法,与机器有关 顺序方式、链接方式、索引方式、散列方式顺序方式、链接方式、索引方式、散列方式顺序方式、链接方式、索引方式、散列方式顺序方式、链接方式、索引方式、散列方式数据结构上的操作:检索、插入、删除、排序等数据结构上的操作:检索、插入、删除、排序等数据结构上的操作:检索、插入、删除、排序等数据结构上的操作:检索、插入、删除、排序等同质复合数据类型:数据元素具有同样相同的性质同质复合数据类型:数据元素具有同样相同的性质同质复合数据类型:数据元素具有同样相同的性质同质复合数据类型:数据元素具有同样相同的性质3安徽大学计算机教学部安

4、徽大学计算机教学部计计算算机机程程序序设设计计基基础础8.2 数 组数组的下标数组的下标下标从下标从下标从下标从 0 0 0 0 开始,一般使用半开半闭区间开始,一般使用半开半闭区间开始,一般使用半开半闭区间开始,一般使用半开半闭区间例:例:例:例:for( for( i i = 0; = 0; i i n n; +; +i i ) ) a a i i = = i i; ;数组的内部表示数组的内部表示按维顺序存放按维顺序存放按维顺序存放按维顺序存放数组的基地址,元素的地址数组的基地址,元素的地址数组的基地址,元素的地址数组的基地址,元素的地址数组下标越界问题数组下标越界问题程序不检查下标越界,

5、为什么?程序不检查下标越界,为什么?程序不检查下标越界,为什么?程序不检查下标越界,为什么?4安徽大学计算机教学部安徽大学计算机教学部计计算算机机程程序序设设计计基基础础数组应用示例一通过键盘输入通过键盘输入1010个整数,然后逆序打印个整数,然后逆序打印#include #include #define #define E E 10 10void void GetGet( ( intint a a, , intint n n ); );void void ReverseReverse( ( intint a a, , intint n n ); );void void SwapSwap( (

6、 intint a a, , intint x x, , intint y y ); );void void PrintPrint( ( intint a a, , intint n n ); );void void mainmain()() intint arrayarray E E; ; GetGet( ( arrayarray, , E E ); ); ReverseReverse( ( arrayarray, , E E ); ); PrintPrint( ( arrayarray, , E E ); ); 5安徽大学计算机教学部安徽大学计算机教学部计计算算机机程程序序设设计计基基础础

7、数组应用示例一void void GetGet( ( intint a a, , intint n n ) ) intint i i; for( ; for( i i = 0; = 0; i i n n; ; i i+ ) + ) scanfscanf( “%( “%d d n n“, &“, &a a i i ); ); void void ReverseReverse( ( intint a a, , intint n n ) ) intint i i; for( ; for( i i = 0; = 0; i i n n / 2; / 2; i i+ ) + ) SwapSwap( ( a

8、 a, , i i, , n n i i 1 ); 1 ); void void SwapSwap( ( intint a a, , intint x x, , intint y y ) ) intint t t; ; t t = = a a x x; ; a a x x = = a a y y; ; a a y y = = t t; ; void void PrintPrint( ( intint a a, , intint n n ) ) intint i i; for( ; for( i i = 0; = 0; i i n n; ; i i+ ) + ) printfprintf( “%

9、( “%d d n n“, “, a a i i ); ); 6安徽大学计算机教学部安徽大学计算机教学部计计算算机机程程序序设设计计基基础础数组作为函数参数传递数组时需要传递元素个数信息。为什么传递数组时需要传递元素个数信息。为什么?传递的其实是数组基地址,不提供元素个数信息传递的其实是数组基地址,不提供元素个数信息传递的其实是数组基地址,不提供元素个数信息传递的其实是数组基地址,不提供元素个数信息就无法确知元素有多少个就无法确知元素有多少个就无法确知元素有多少个就无法确知元素有多少个7安徽大学计算机教学部安徽大学计算机教学部计计算算机机程程序序设设计计基基础础数组作为函数参数不恰当的数组传递

10、方法不恰当的数组传递方法数组参数声明错误数组参数声明错误数组参数声明错误数组参数声明错误 例:例:例:例:void void ReverseReverse( ( intint a a n n ); ); 不能在参数列表中使用变量声明数组不能在参数列表中使用变量声明数组不能在参数列表中使用变量声明数组不能在参数列表中使用变量声明数组数组参数声明不恰当数组参数声明不恰当数组参数声明不恰当数组参数声明不恰当 例:例:例:例:void void ReverseReverse( ( intint a a10 );10 ); 魔数魔数魔数魔数10101010:在分析函数声明时,编译器忽略之,只对函:在分析

11、函数声明时,编译器忽略之,只对函:在分析函数声明时,编译器忽略之,只对函:在分析函数声明时,编译器忽略之,只对函数内部代码有意义数内部代码有意义数内部代码有意义数内部代码有意义 函数只能操作函数只能操作函数只能操作函数只能操作10101010个元素的整数数组个元素的整数数组个元素的整数数组个元素的整数数组8安徽大学计算机教学部安徽大学计算机教学部计计算算机机程程序序设设计计基基础础数组作为函数参数多维数组的传递方法:指定数组元素个数多维数组的传递方法:指定数组元素个数正确:正确:正确:正确:void void DisplayBoardDisplayBoard( char ( char boar

12、dboard1919 );1919 );正确:正确:正确:正确:void void DisplayBoardDisplayBoard( char ( char boardboard19, 19, intint n n ); );错误:错误:错误:错误:void void DisplayBoardDisplayBoard( char ( char boardboard, , intint mm, , intint n n ); );9安徽大学计算机教学部安徽大学计算机教学部计计算算机机程程序序设设计计基基础础数组应用示例二素数查找的埃拉托色尼筛法素数查找的埃拉托色尼筛法素数查找的埃拉托色尼筛法素

13、数查找的埃拉托色尼筛法 在公元前三世纪,希腊天文学家在公元前三世纪,希腊天文学家在公元前三世纪,希腊天文学家在公元前三世纪,希腊天文学家Eratosthenes Eratosthenes Eratosthenes Eratosthenes 发明了一发明了一发明了一发明了一种算法,用来找出某一范围内的全部素数种算法,用来找出某一范围内的全部素数种算法,用来找出某一范围内的全部素数种算法,用来找出某一范围内的全部素数 算法过程:首先列写从算法过程:首先列写从算法过程:首先列写从算法过程:首先列写从 2 2 到到到到 n n 的整数,假设的整数,假设的整数,假设的整数,假设 n n = 20 = 2

14、0 将第一个数圈起,表明已发现了一个素数,并将数列中将第一个数圈起,表明已发现了一个素数,并将数列中将第一个数圈起,表明已发现了一个素数,并将数列中将第一个数圈起,表明已发现了一个素数,并将数列中所有该素数的倍数打上所有该素数的倍数打上所有该素数的倍数打上所有该素数的倍数打上 ,因为它们一定不是素数,因为它们一定不是素数,因为它们一定不是素数,因为它们一定不是素数 重复上述步骤,直到所有整数或者被圈起或者打上叉重复上述步骤,直到所有整数或者被圈起或者打上叉重复上述步骤,直到所有整数或者被圈起或者打上叉重复上述步骤,直到所有整数或者被圈起或者打上叉10安徽大学计算机教学部安徽大学计算机教学部计计

15、算算机机程程序序设设计计基基础础数组应用示例二#include #include #define #define MaxMax 1000 1000typedeftypedef enumenum FALSEFALSE, , TRUETRUE boolbool; ;boolbool IsPrimeIsPrime MaxMax; ;void void InitInit();();void void GetPrimeGetPrime();();void void OutputOutput();();void void mainmain() () InitInit();(); GetPrimeGetPr

16、ime();(); OutputOutput();(); 11安徽大学计算机教学部安徽大学计算机教学部计计算算机机程程序序设设计计基基础础数组应用示例二void void InitInit() () intint i i; for( ; for( i i = 0; = 0; i i MaxMax; ; i i+ ) + ) IsPrimeIsPrime i i = = TRUETRUE; ; void void GetPrimeGetPrime() () intint tmptmp; ; intint i i; ; for( for( i i = 2; = 2; i i MaxMax; ; i

17、 i+ )+ ) if( if( IsPrimeIsPrime i i ) ) tmptmp = = i i + + i i; ; while( while( tmptmp MaxMax ) ) IsPrimeIsPrime tmptmp = = FALSEFALSE; ; tmptmp += += i i; ; 12安徽大学计算机教学部安徽大学计算机教学部计计算算机机程程序序设设计计基基础础数组应用示例二void void OutputOutput() () intint i i; ; intint countcount = 0; = 0; for( for( i i = 2; = 2; i

18、 i MaxMax; ; i i+ )+ ) if( if( IsPrimeIsPrime i i ) ) countcount+;+; printfprintf( “Prime %( “Prime %d d:%4:%4d d t t“, “, countcount, , i i ); ); if( if( countcount % 5 = 0 ) % 5 = 0 ) printfprintf( “( “n n“ );“ ); printfprintf( “( “n nTotalTotal :%4 :%4d d n n“, “, countcount ); ); 13安徽大学计算机教学部安徽大

19、学计算机教学部计计算算机机程程序序设设计计基基础础数组应用示例三学生成绩查询系统,假设有五名学生七门考学生成绩查询系统,假设有五名学生七门考试,要求程序完成如下功能试,要求程序完成如下功能根据输入的学号,给出每次考试成绩及平均成绩根据输入的学号,给出每次考试成绩及平均成绩根据输入的学号,给出每次考试成绩及平均成绩根据输入的学号,给出每次考试成绩及平均成绩根据输入的考试号,打印出该次考试中每个学生根据输入的考试号,打印出该次考试中每个学生根据输入的考试号,打印出该次考试中每个学生根据输入的考试号,打印出该次考试中每个学生的成绩,并给出平均成绩的成绩,并给出平均成绩的成绩,并给出平均成绩的成绩,并

20、给出平均成绩根据学号查出学生某次考试成绩根据学号查出学生某次考试成绩根据学号查出学生某次考试成绩根据学号查出学生某次考试成绩录入考试成绩录入考试成绩录入考试成绩录入考试成绩14安徽大学计算机教学部安徽大学计算机教学部计计算算机机程程序序设设计计基基础础数组应用示例三#include#include void void mainmain() () intint selectselect; ; intint i i, , j j; ; intint scorescore68; 68; intint average average = 0; = 0; intint sumsum = 0; = 0;

21、do do printfprintf( “( “本程序有本程序有本程序有本程序有4 4项功能项功能项功能项功能 n n“ ); “ ); printfprintf( “1.( “1.根据学号查询学生成绩根据学号查询学生成绩根据学号查询学生成绩根据学号查询学生成绩 n n“ );“ ); printfprintf( “2.( “2.根据考试号统计学生成绩根据考试号统计学生成绩根据考试号统计学生成绩根据考试号统计学生成绩 n n“ );“ ); printfprintf( “3.( “3.根据考试号和学号查询学生成绩根据考试号和学号查询学生成绩根据考试号和学号查询学生成绩根据考试号和学号查询学生成

22、绩 n n“ );“ ); printfprintf( “4.( “4.成绩录入成绩录入成绩录入成绩录入 n n“ );“ ); printfprintf( “0.( “0.退出退出退出退出 n n“ );“ ); printfprintf( “( “请输入请输入请输入请输入0404进行选择:进行选择:进行选择:进行选择: n n“ );“ ); scanfscanf( “%( “%d d n n“, &“, &selectselect ); );15安徽大学计算机教学部安徽大学计算机教学部计计算算机机程程序序设设计计基基础础数组应用示例三 switch( switch( select sel

23、ect ) ) case 0: case 0: printfprintf( “Ok( “Okn n“ ); exit( 0 ); break;“ ); exit( 0 ); break; case 1: case 1: printfprintf( “( “输入学号:输入学号:输入学号:输入学号:” ); ” ); scanfscanf( “%( “%d d n n“, &“, &i i ); ); for( for( j j = 1; = 1; j j 8; 8; j j+ )+ ) printfprintf( “( “第第第第%d d科成绩是科成绩是科成绩是科成绩是%d d n n“, “,

24、 j j, , scorescore i i j j ); ); sum sum += += scorescore i i j j; ; average average = = sum sum / 7;/ 7; printfprintf( “( “学生的平均成绩是学生的平均成绩是学生的平均成绩是学生的平均成绩是%d d n n“, “, average average ); ); break; break;16安徽大学计算机教学部安徽大学计算机教学部计计算算机机程程序序设设计计基基础础数组应用示例三 case 2: case 2: printfprintf( “( “输入考试号:输入考试号:输

25、入考试号:输入考试号:”); ”); scanfscanf( “%( “%d d n n“, &“, &j j ); ); for( for( i i = 1; = 1; i i 6; 6; i i+ )+ ) printfprintf( “( “第第第第%d d号学生本科成绩是号学生本科成绩是号学生本科成绩是号学生本科成绩是%d d n n“, “, i i, , scorescore i i j j ); ); sum sum += += scorescore i i j j; ; averageaverage = = sum sum / 5;/ 5; printfprintf( “( “

26、学生本科平均成绩是学生本科平均成绩是学生本科平均成绩是学生本科平均成绩是%d d n n“, “, average average ); ); break; break; case 3: case 3: printfprintf( “( “输入学生学号和考试号:输入学生学号和考试号:输入学生学号和考试号:输入学生学号和考试号:” );” ); scanfscanf( “%( “%d d % %d d n n“, &“, &i i, &, &j j ); ); printfprintf( “( “第第第第%d d号学生第号学生第号学生第号学生第%d d科考试成绩是科考试成绩是科考试成绩是科考试成

27、绩是%d d n n“, “, i i, , j j, , scorescore i i j j);); break; break;17安徽大学计算机教学部安徽大学计算机教学部计计算算机机程程序序设设计计基基础础数组应用示例三 case 4: case 4: printfprintf( “( “输入成绩:输入成绩:输入成绩:输入成绩: n n“ );“ ); for( for( i i = 1; = 1; i i 6; 6; i i+ )+ ) for( for( j j = 1; = 1; j j 8; 8; j j+ )+ ) scanfscanf( “%( “%d d n n“, &“,

28、 &scorescore i i j j ); ); break; break; default: default: break; break; while(1); while(1); 本程序的设计有什么问题?本程序的设计有什么问题?18安徽大学计算机教学部安徽大学计算机教学部计计算算机机程程序序设设计计基基础础字符串数组型字符串数组型字符串for( for( i i = 0; = 0; i i n n; +; +i i ) ) strstr i i = ( char )( = ( char )( a a + + i i ); );指针型字符串指针型字符串for( for( p p = = s

29、trstr; *; *p p != 0; + != 0; +p p ) * ) *q q = * = *p p; ;作为抽象数据的字符串作为抽象数据的字符串typedeftypedef char* string; char* string;19安徽大学计算机教学部安徽大学计算机教学部计计算算机机程程序序设设计计基基础础字符串应用示例一编写函数编写函数 FindFirstVowel(),找出字符串中找出字符串中第一个元音字母第一个元音字母intint FindFirstVowelFindFirstVowel( char ( char strstr, , intint n n ) ) intint

30、 i i; ; for( for( i i = 0; = 0; i i n n; ; i i+ ) if( + ) if( IsVowelIsVowel( ( strstr i i ) ) return ) ) return i i; ; return return 1; 1; intint IsVowelIsVowel( char ( char chch ) ) if( ! if( !isalphaisalpha( ( chch ) ) return 0;) ) return 0; chch = = touppertoupper( ( chch ); ); if(if(chch=A|A|ch

31、ch=E|E|chch=I|I|chch=O|O|chch=U) return 1;=U) return 1; return 0; return 0; 使用字符数组使用字符数组使用字符数组使用字符数组20安徽大学计算机教学部安徽大学计算机教学部计计算算机机程程序序设设计计基基础础字符串应用示例一编写函数编写函数 FindFirstVowel(),找出字符串中找出字符串中第一个元音字母第一个元音字母intint FindFirstVowelFindFirstVowel( char ( char strstr ) ) intint i i; ; for( for( i i = 0; = 0; st

32、rstr i i != 0; != 0; i i+ )+ ) if( if( IsVowelIsVowel( ( strstr i i ) ) return ) ) return i i; ; return return 1; 1; 使用字符数组使用字符数组使用字符数组使用字符数组21安徽大学计算机教学部安徽大学计算机教学部计计算算机机程程序序设设计计基基础础字符串应用示例一编写函数编写函数 FindFirstVowel(),找出字符串中找出字符串中第一个元音字母第一个元音字母intint FindFirstVowelFindFirstVowel( char* ( char* strstr )

33、 ) char* char* p p; ; for( for( p p = = strstr; *; *p p != 0; != 0; p p+ )+ ) if( if( IsVowelIsVowel( *( *p p ) ) return ( ) ) return (p p strstr); ); return return 1; 1; 使用字符指针使用字符指针使用字符指针使用字符指针22安徽大学计算机教学部安徽大学计算机教学部计计算算机机程程序序设设计计基基础础字符串应用示例一编写函数编写函数 FindFirstVowel(),找出字符串中找出字符串中第一个元音字母第一个元音字母intin

34、t FindFirstVowelFindFirstVowel( string( string strstr ) ) intint i i; ; for( for( i i = 0; = 0; i i strlenstrlen( (strstr); ); i i+ )+ ) if( if( IsVowelIsVowel( ( IthCharIthChar( (strstr, , i i) ) return ) ) return i i; ; return return 1; 1; char char IthCharIthChar( string ( string strstr, , intint

35、 i i ) ) return return strstr i i; ; 使用抽象字符串使用抽象字符串使用抽象字符串使用抽象字符串23安徽大学计算机教学部安徽大学计算机教学部计计算算机机程程序序设设计计基基础础字符串变量数组型字符串:使用前已经分配空间数组型字符串:使用前已经分配空间例:例:例:例:char char strstr9 = “Tsinghua”;9 = “Tsinghua”;Tsinghua0str指针型字符串:指针型字符串:使用前未分配空间使用前未分配空间例:例:例:例:char* char* p p; ; p p = = strstr; ;pTsinghua0str24安徽大

36、学计算机教学部安徽大学计算机教学部计计算算机机程程序序设设计计基基础础字符串应用示例二编写程序,将英语人名从姓在后的形式转换编写程序,将英语人名从姓在后的形式转换为姓在前的形式,如为姓在前的形式,如“ “First Middle Last”First Middle Last”转换转换为为“ “Last, First Middle”Last, First Middle”#include #include #include #include #define #define MaxNameMaxName 40 40static void static void InvertNameInvertNam

37、e( char ( char resultresult, char , char namename ); );25安徽大学计算机教学部安徽大学计算机教学部计计算算机机程程序序设设计计基基础础字符串应用示例二void void mainmain()() char char standardNamestandardName MaxNameMaxName+1;+1; char char invertedNameinvertedName MaxNameMaxName+1;+1; printfprintf(“This(“This program converts a name in standard p

38、rogram converts a name in standard orderordern n“);“); printfprintf(“into(“into inverted order with the last name first. inverted order with the last name first.n n“);“); printfprintf(“Indicate(“Indicate the end of input with a blank line. the end of input with a blank line.n n“);“); while(1) while(

39、1) printfprintf(“Name(“Name: “);: “); getsgets( ( standardNamestandardName ); ); if( if( strlenstrlen( (standardNamestandardName) = 0 ) break;) = 0 ) break; InvertNameInvertName( ( invertedNameinvertedName, , standardNamestandardName ); ); printfprintf(“%(“%s s n n“, “, invertedNameinvertedName ); )

40、; 26安徽大学计算机教学部安徽大学计算机教学部计计算算机机程程序序设设计计基基础础字符串应用示例二static void static void InvertNameInvertName( char ( char resultresult, char , char namename ) ) intint lenlen; ; char * char *p p; ; lenlen = = strlenstrlen( ( name name ); ); p p = = strrchrstrrchr( ( namename, );, ); if( if( p p != 0 ) != 0 ) lenl

41、en+;+; if(if(lenlen MaxNameMaxName) ) printfprintf(“Name(“Name too long too longn n“); exit(“); exit( 1); 1); if( if( p p = 0 ) = 0 ) strcpystrcpy( ( resultresult, , name name ); ); else else strcpystrcpy( ( resultresult, , p p + 1 ); + 1 ); strcatstrcat( ( resultresult, “, “ );, “, “ ); strncatstrn

42、cat( ( resultresult, , namename, , p p name name ); ); resultresult lenlen = 0; = 0; 27安徽大学计算机教学部安徽大学计算机教学部计计算算机机程程序序设设计计基基础础字符串应用示例三使用字符串处理函数编写游戏使用字符串处理函数编写游戏 hangmanhangman在游戏中,电脑随机从某个范围内选择一个单词,在游戏中,电脑随机从某个范围内选择一个单词,在游戏中,电脑随机从某个范围内选择一个单词,在游戏中,电脑随机从某个范围内选择一个单词,然后输出一列短横,每个短横代表一个字母然后输出一列短横,每个短横代表一个字母

43、然后输出一列短横,每个短横代表一个字母然后输出一列短横,每个短横代表一个字母使用者猜测该单词,如果猜到了单词中的字母,使用者猜测该单词,如果猜到了单词中的字母,使用者猜测该单词,如果猜到了单词中的字母,使用者猜测该单词,如果猜到了单词中的字母,就会显示出该字母所在的位置,而其他字母仍用就会显示出该字母所在的位置,而其他字母仍用就会显示出该字母所在的位置,而其他字母仍用就会显示出该字母所在的位置,而其他字母仍用短横表示短横表示短横表示短横表示如果所猜的字母不再单词中,就是一次不成功的如果所猜的字母不再单词中,就是一次不成功的如果所猜的字母不再单词中,就是一次不成功的如果所猜的字母不再单词中,就是

44、一次不成功的猜测猜测猜测猜测重复上述步骤,直到猜出整个单词,或者累计重复上述步骤,直到猜出整个单词,或者累计重复上述步骤,直到猜出整个单词,或者累计重复上述步骤,直到猜出整个单词,或者累计 8 8 8 8 次不成功猜测次不成功猜测次不成功猜测次不成功猜测28安徽大学计算机教学部安徽大学计算机教学部计计算算机机程程序序设设计计基基础础字符串应用示例三#include#include #include#include#include#include#include#include #include #include void void beginbegin() () / / tell user h

45、ow to play the gametell user how to play the game printfprintf( “Lets play hangman! I will pick a secret word.( “Lets play hangman! I will pick a secret word.n n“ );“ ); printfprintf( “On each turn, you guess a letter.( “On each turn, you guess a letter.n n“ );“ ); printfprintf( “If the letter is in

46、 the secret word, I will show you ( “If the letter is in the secret word, I will show you where it appears.where it appears.n n“ );“ ); printfprintf( “If you make an incorrect guess, part of ( “If you make an incorrect guess, part of n n“ );“ ); printfprintf( “your body gets strung up on the scaffol

47、d.( “your body gets strung up on the scaffold.n n“ ); “ ); printfprintf( “The object is to guess it before hanged.( “The object is to guess it before hanged.n n n n“ );“ ); return; return; 29安徽大学计算机教学部安徽大学计算机教学部计计算算机机程程序序设设计计基基础础字符串应用示例三void void runrun( char* ( char* lettersletters, , intint mm ) )

48、 / / the main part of the gamethe main part of the game /* guess marks if a guess is correct, k marks if the word is /* guess marks if a guess is correct, k marks if the word is guessed correctly */ guessed correctly */ intint i i, , j j, , k k, , l l, , p p, , q q, , z z, , guessguess, , timestimes

49、 = 8; = 8; intint abcabc26; 26; /* /* 26 marks to memorize if the letter guessed26 marks to memorize if the letter guessed */ */ intint conditioncondition20; 20; /* /* 20 marks to memorize if the letter in 20 marks to memorize if the letter in the secret word guessedthe secret word guessed */ */ cha

50、r char cccc, , tmptmp; ; /* /* mark that none of themark that none of the letters has been guessedletters has been guessed */ */ for( for( j j = 0; = 0; j j mm; ; j j+ ) + ) conditioncondition j j = = 1; 1; for( for( j j = 0; = 0; j j 26; 26; j j+ ) + ) abcabc j j = 0; = 0; k k = 0; = 0; /* The word

51、 has not been guessed */* The word has not been guessed */30安徽大学计算机教学部安徽大学计算机教学部计计算算机机程程序序设设计计基基础础字符串应用示例三 / check that the game has not ended / check that the game has not ended while( ( while( ( k k = 0 ) & ( = 0 ) & ( times times != 0 ) )!= 0 ) ) printfprintf( “the word now looks like this: “ );(

52、 “the word now looks like this: “ ); for( for( i i = 0; = 0; i i 1 ) 1 ) printfprintf( “You have %( “You have %d d guesses left. guesses left.n n n n“, “, times times ); ); else else printfprintf( “You have only one guess left.( “You have only one guess left.n n n n“ );“ );31安徽大学计算机教学部安徽大学计算机教学部计计算算

53、机机程程序序设设计计基基础础字符串应用示例三 printfprintf( “Your guess:“ );( “Your guess:“ ); guessguess = 0; = 0; tmptmp = = getchargetchar();(); if( if( tmptmp != != n n ) ) cc cc = = tmptmp; ; /* read other letters so that they dont affect the guess */* read other letters so that they dont affect the guess */ for( for

54、( q q = 0; = 0; getchargetchar() != () != n n; ; q q+);+); for( for( p p = 0; = 0; p p mm; ; p p+ )+ ) if( if( lettersletters p p = = cc cc ) ) guessguess = 1; = 1; conditioncondition p p = 1; = 1; if( if( abcabc cccc 97 = 1 ) 97 = 1 ) / / chechchech if the letter has been guessed if the letter has

55、been guessed guessguess = 2; = 2; printfprintf(“The(“The letter has been guessed, choose another letter has been guessed, choose anothern n“ );“ ); abcabc cccc 97 = 1;97 = 1; / mark that the letter has been guessed / mark that the letter has been guessed32安徽大学计算机教学部安徽大学计算机教学部计计算算机机程程序序设设计计基基础础字符串应用示

56、例三 if( if( guessguess = 0 ) = 0 ) printfprintf( “( “n nThereThere is no % is no %c c in the word. in the word.n n“, “, cc cc ); ); timestimes; ; else else if( if( guessguess = 1 ) = 1 ) printfprintf( “( “n nThatThat guess is correct. guess is correct.n n“ );“ ); / / chechchech if the conditions to e

57、nd the game has been reached if the conditions to end the game has been reached l l = 0; = 0; while( while( l l m m ) ) if( if( conditioncondition l l = = 1)1) k k = 0; break;= 0; break; k k = 1; = 1; l l+;+; / while( ( k = 0 ) & ( times != 0 ) ) / while( ( k = 0 ) & ( times != 0 ) )33安徽大学计算机教学部安徽大学

58、计算机教学部计计算算机机程程序序设设计计基基础础字符串应用示例三 if( if( timestimes = 0 ) = 0 ) printfprintf( “Poor Man!( “Poor Man!n nTheThe word is: “); word is: “); for( for( z z = 0; = 0; z z mm; ; z z+ ) + ) printfprintf( “%( “%c c“, “, lettersletters z z ); ); return; return; if( if( k k = 1 )= 1 ) printfprintf( “You have gu

59、essed the word: “ );( “You have guessed the word: “ ); for( for( z z = 0; = 0; z z mm; ; z z+ ) + ) printfprintf( “%( “%c c“, “, lettersletters z z ); ); printfprintf( “( “n nYouYou win. win.n n“ );“ ); return; return; 34安徽大学计算机教学部安徽大学计算机教学部计计算算机机程程序序设设计计基基础础字符串应用示例三void void mainmain() () intint a

60、a, , b b, , c c; ; char* char* secretsecret; ; char* char* wordswords20 = “detail“, “average“ ;20 = “detail“, “average“ ; char char lettersletters20;20; beginbegin();(); srandsrand( ( timetime( (NULLNULL) ); ) ); /* /* produce a random numberproduce a random number */ */ a a = = randrand() % 20; ()

61、% 20; /* /* as the index of the wordas the index of the word */ */ secretsecret = = wordswords a a; ; / / get the wordget the word c c = = strlenstrlen( ( secretsecret ); ); / / the number of the letters in the wordthe number of the letters in the word / change string into parameter form/ change str

62、ing into parameter form for( for( b b = 0; *( = 0; *(secretsecret+ +b b) != 0; ) != 0; b b+ ) + ) lettersletters b b = *( = *(secretsecret+ +b b); ); runrun( ( lettersletters, , c c ); ); 35安徽大学计算机教学部安徽大学计算机教学部计计算算机机程程序序设设计计基基础础作 业第第237237页:第二题(编程题)页:第二题(编程题)第第第第2 2 2 2小题小题小题小题第第238238页:第二题(编程题)页:第二题(编程题)第第第第6 6 6 6小题小题小题小题第第239239页:第二题(编程题)页:第二题(编程题)第第第第10101010、11111111小题小题小题小题

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 商业/管理/HR > 销售管理

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