C程序设计教程与实验第8章习题参考答案

更新时间:2023-11-02 10:59:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

第8章习题

一、选择题 1 C 9 C 2 C 10 A 3 C 11 B 4 C 12 D 5 C 13 A 6 A 14 C 7 C 15 C 8 D 二、 填空题 1. 2

2. 6 7 200C 3.p[5]

4. 定义p1为指向整型数据的指针变量

定义p2为指针数组,其数组元素均为指针

定义p3为指向一维数组的指针,其数组元素均为整型数 5. 5,3 6. 20

7. r+b[k] *x

8. SecondLiFirstWang 9. 25

10. 12 12

三 程序分析题 1.7

2. 1 5 3

3 9 7 3. 2 4

4. abcDDfefDbD 5. 53 四、编程题

1.输入2个字符串,每个字符串的长度均不超过80字符,用自己实现的cmpstr()函数完成这两个字符串的大小比较,cmpstr()函数的功能和字符串比较函数strcmp()的功能相同 #include #include

int cmpstr(char *s1, char *s2) {

char *p, *q; p=s1; q=s2;

while(*p!='\\0' && *q!='\\0') {

if(*p==*q) {

p++; q++; }

else break; }

return *p-*q;

}

void main() {

char str1[81], str2[81]; int c;

printf(\请输入第1个字符串:\gets(str1);

printf(\请输入第2个字符串:\gets(str2);

c=cmpstr(str1,str2); if(c>0)

printf(\字符串%s 大于 字符串%s\\n\else if(c<0)

printf(\字符串%s 小于 字符串%s\\n\else

printf(\字符串%s 等于 字符串%s\\n\

}

2.定义一个函数float reverse(int p[], int n),该函数有两个参数,第一个参数p为形参数组名,第二个参数n为该数组中的元素个数,要求使用reverse()函数将该数组中的所有元素逆序排列,并返回该数组中所有元素的平均值。 #include

float reverse(int p[],int n) {

int i,t;

float ave=0; for(i=0;i

for(i=0;i

t=*(p+i);

*(p+i)=*(p+n-i-1); *(p+n-i-1)=t; }

return ave; }

void main() {

int a[10]={1,2,3,4,5,6,7,8,9,10}; int i;

float ave;

ave=reverse(a, 10);

printf(\for(i=0;i<10;i++)

printf(\ printf(\}

3.定义一个函数delSubstr(),删除字符串中第k个字符开始的m个字符,例如删除字符串abcde第2个字符开始的3个字符,则删除后结果为ae;又如删除字符串abcde第4个字符开始的5个字符,则删除后结果为abc。 #include #include

int delSubstr(char *str, int k, int m) {

char *p, *q; if(k<=0||m<=0)

return -1;

else if(k>strlen(str))

return -2;

else if(k+m>strlen(str)) {

str[k-1]='\\0';

return 1;

} else {

p=str+k-1; q=str+k+m-1; while(*q!='\\0') *p++=*q++; *p='\\0';

return 1;

} }

void main() {

char str[81]; int k, m, state;

printf(\请输入字符串:\\n\gets(str);

printf(\请输入要删除的子串起始字符的序号和子串长度:\\n\scanf(\state = delSubstr(str, k, m); if(state<0)

printf(\要删除子串起始字符的序号或子串长度输入错误!\\n\

else

printf(\删除指定子串后的字符串为:%s\\n\

}

4.定义一个函数delSpechar(),使用字符指针删除字符串中的所有指定字符(如把字符串“I love you!”中的o字符删除,得到“I lve yu!”)。 #include #include

void delSpechar(char *str, char ch) {

char *p, *q; p=str; while(*p!='\\0') {

if(*p==ch) {

q=p;

while(*q!='\\0') {

*q=*(q+1); q++; } }

else p++; } }

void main() {

char str[81], ch;

printf(\请输入一个字符串:\gets(str);

printf(\请输入要从字符串中删除的字符:\ch=getchar();

delSpechar(str, ch);

printf(\删除所有指定字符后的字符串为:%s\\n\

}

5.求整型二维数组a[M][N]中的最大元素值及最大元素的位置(用指针法引用数组元素)。

#include #define M 3 #define N 4

void max(int (*pa)[N], int *pRow, int *pCol {

int i, j;

for(i=0; i

if(pa[i][j]>pa[*pRow][*pCol]) {

*pRow=i; *pCol=j; }

}

void main() {

int a[M][N]={{4,2,7,9},{1,3,13,0},{-5,6,-11,10}}; int row=0, col=0; max(a, &row, &col);

printf(\二维数组中的最大元素值为a[%d][%d]=%d\\n\}

6.已知字符串str[80],编写函数lstrchar(),实现在数组str中查找字符ch首次出现的位置,如果字符串中找不到该字符,则返回-1。 #include #include

int lstrchar(char *str1, char ch1) {

char *p,i=1; p=str1;

while(*p!='\\0') {

if(*p==ch1) break; else {

p++; i++; } }

if(*p=='\\0') return -1;

else return i; }

void main() {

char ch,str[80]; int poi;

printf(\请输入字符串:\gets(str);

printf(\请输入待查找的字符:\scanf(\poi=lstrchar(str,ch); if(poi==-1)

printf(\字符串中不存在指定字符!\\n\else

printf(\该字符在字符串中出现的第一个位置为%d\\n\

}

7.定义一个整型二维数组并初始化,编程求该数组所有元素的和。 要求:分别用数组下标法、一级指针法、二级指针法实现。

/*数组下标法*/

#include #define M 3 #define N 4

int sum(int b[M][N]) {

int i, j, s=0; for(i=0; i

void main() {

int a[M][N]={{4,2,7,9},{1,3,13,0},{-5,6,-11,10}};

printf(\二维数组中的所有元素之和为%d\\n\}

/*一级指针法*/ #include #define M 3 #define N 4 int sum(int *pa) {

int i,s=0;

for(i=0;i

void main() {

int a[M][N]={{4,2,7,9},{1,3,13,0},{-5,6,-11,10}};

printf(\二维数组中的所有元素之和为%d\\n\ }

/*二级指针法*/ #include #define M 3 #define N 4

int sum(int (*pa)[N]) {

int i,j,s=0;

for(i=0;i

for(j=0;j

return s;

}

void main() {

int a[M][N]={{4,2,7,9},{1,3,13,0},{-5,6,-11,10}};

printf(\二维数组中的所有元素之和为%d\\n\ }

8.输入一行数字字符存入字符数组str[80]中,用num[10]中的数组元素作为计数器来统计每个数字字符的个数。用下标为0的元素统计字符“0”的个数,用下标为1的元素统计字符“1”出现的次数,……。输出每个奇数字符出现的次数。 #include

void count(char *str, int num[]) {

int i=0;

while(str[i]!='\\0') {

switch(str[i]) {

case '0': num[0]++; break; case '1': num[1]++; break; case '2': num[2]++; break; case '3': num[3]++; break; case '4': num[4]++; break; case '5': num[5]++; break; case '6': num[6]++; break; case '7': num[7]++; break; case '8': num[8]++; break; case '9': num[9]++; break; } i++; } }

void main() {

char str[80],*p=str; int num[10]={0}, i;

printf(\请输入一行数字字符:\gets(p);

count(p,num); for(i=1;i<10;i+=2)

printf(\数字串中%d出现的次数为%d\\n\}

9.在主函数中输入10个不等长字符串,用另一函数sort()对它们排序,然后在主函数中输出已排好序的字符串。要求:每个字符串长度均不超过30字符,用指针数组进行处理。 #include #include

#define M 10 #define N 31 void sort(char *pstr[]) {

int i,j; char *t;

for(i=0; i

for(j=0; j

if(strcmp(pstr[j],pstr[j+1])>0) {

t = pstr[j];

pstr[j]=pstr[j+1]; pstr[j+1] = t; } } }

void main() {

char str[M][N], *pstr[M]={NULL}; int i;

printf(\请输入10个字符串,每个字符串均不超过30个字符:\\n\for(i=0; i

gets(str[i]); pstr[i]=str[i]; }

sort(pstr);

printf(\排好序的字符串从小到大依次为:\\n\for(i=0; i

printf(\

}

10.输入一个字符串,内有数字字符和非数字字符如123a345bcd567,将其中连续的数字作为一个整数,依次存放到整型数组a中,例如,123放在a[0],345放在a[1]中,567放在a[2]中,……,统计共有多少个整数,并输出这些整数。 #include #include

void main() {

char ch[1000],*p=ch; int a[50],*q=a,num=0,i; gets(ch);

for(; *p!='\\0'; p++) if(*p>='0' && *p<='9') {

*q=(*p)-'0'; p++;

while(*p>='0' && *p<='9') *q=(*q)*10+(*(p++)-'0'); num+=1; q+=1; }

for(i=0,q=a; i

printf(\一共输入了%d个整数。\\n\}

if(*p==ch1) break; else {p++;i++;}

if(*p=='\\0') return -1; else return i; }

void main() {

char ch,str[80]; int poi;

printf(\请输入字符串:\gets(str);

printf(\请输入待查找的字符:\scanf(\poi=lstrchar(str,ch); if(poi==-1)

printf(\字符串中不存在指定字符!\\n\else

printf(\该字符在字符串中出现的第一个位置为%d\\n\

}

本文来源:https://www.bwwdw.com/article/x5t2.html

Top