第10章指针
- 格式:ppt
- 大小:756.50 KB
- 文档页数:109
谭浩强版C语言的第十章《指针》答案第十章《指针》答案如下inc/testPtr.h#include <string.h>#include <ctype.h>#include <math.h>#include <assert.h>#define SIZE 1024int a2i(char *start, char *end){int size = 0, ret = 0;long base = 0;size = end - start + 1;base = (long)pow(10, size - 1);while(size-- > 0){ret += (*start++ - '0') * base;base /= 10;}return ret;}int extraNum(char *str, int arr[]){int ite = 0, counter = 0;char *start = NULL, *end = NULL;while(*str != '\0'){if(isdigit(*str)){start = end = str;while( isdigit(*end) && *end != '\0'){++end;}arr[ite++] = a2i(start, end-1);str = end;}else{str++;}}return ite;}int sortStr(char *arr[], int size){int ite1 = 0, ite2 = 0, minPos = 0;char *tmp;for(ite1 = 0; ite1 < size - 1; ite1++){minPos = ite1;for(ite2 = ite1 + 1; ite2 < size; ++ite2 ){if( strcmp(arr[ite2], arr[minPos]) < 0 ){minPos = ite2;}}if(minPos != ite1){tmp = arr[minPos];arr[minPos] = arr[ite1];arr[ite1] = tmp;}}return 0;}int sort(int arr[], int size){int minPos = 0, ite1 = 0, ite2 = 0, tmp = 0;for(ite1 = 0; ite1 < size - 1; ite1++){minPos = ite1;for(ite2 = ite1 + 1; ite2 < size; ite2++){if( arr[ite2] < arr[minPos] ){minPos = ite2;}}if(minPos != ite1){tmp = arr[ite1];arr[ite1] = arr[minPos];arr[minPos] = tmp;}}return 0;}int sortPtr(int arr[], int size){int minPos = 0, ite1 = 0, ite2 = 0, tmp = 0;for(ite1 = 0; ite1 < size - 1; ite1++){minPos = ite1;for(ite2 = ite1 + 1; ite2 < size; ite2++){if( *(arr + ite2) < *(arr + minPos) ){minPos = ite2;}}if(minPos != ite1){tmp = *(arr + ite1);*(arr + ite1) = *(arr + minPos);*(arr + minPos) = tmp;}}return 0;}int getMultiArr(int arr[][5], int n){int i = 0, j = 0, min = 0, tmp = 0, size = 5 * n;int *p = NULL, pos[size];/* copy */p = (int *)malloc(size * sizeof(int));assert(p != NULL);memcpy(p, arr, size * sizeof(int));/* sort */for(i = 0; i < size - 1; i++){min = i;for(j = i + 1; j < size; j++){if( *(p + j) < *(p + min)){min = j;}}if(min != i){tmp = *(p + min);*(p + min) = *(p + i);*(p + i) = tmp;}}/* move */for(i = 0; i < n; i++){for(j = 0; j < 5; j++){if( *p == arr[i][j] ){tmp = arr[i][j];arr[i][j] = arr[0][0];arr[0][0] = tmp;continue;}if( *(p + 1) == arr[i][j] ){tmp = arr[i][j];arr[i][j] = arr[0][4];arr[0][4] = tmp;continue;}if( *(p + 2) == arr[i][j] ){tmp = arr[i][j];arr[i][j] = arr[n - 1][0];arr[n - 1][0] = tmp;continue;}if( *(p + 3) == arr[i][j] ){tmp = arr[i][j];arr[i][j] = arr[n - 1][4];arr[n - 1][4] = tmp;continue;}if( *(p + size - 1) == arr[i][j] ){tmp = arr[i][j];arr[i][j] = arr[n/2][2];arr[n/2][2] = tmp;continue;}}}free(p);p = NULL;return 0;}int statStr(char *str){int upper = 0, lower = 0, space = 0, num = 0, other = 0;while(*str != '\0'){if(isdigit(*str)){num++;}else if (isupper(*str)){upper++;}else if (islower(*str)){lower++;}else if (isspace(*str)){space++;}else{other++;}str++;}assert(3 == upper);assert(5 == lower);assert(10 == num);assert(2 == space);assert(6 == other);return 0;}int average(int(*stu)[6], int classNum, int stuNum){int i = 0, ave = 0;for(i = 0; i < stuNum; i++){ave += (*(stu + i))[classNum];}ave /= stuNum;return ave;}int searchStu(int(*stu)[6], int stuNum){int counter = 0, i = 0, j = 1, stuCounter = 0;for (i = 0; i < stuNum; i++){counter =0;for(j = 1; j < 6; j++){if( (*(stu + i))[j] < 60 ){counter++;}}if(counter >= 2){stuCounter++;}}return stuCounter;}int moveInt(int arr[], int n, int m){int i = 0, *p = NULL;p = (int*)malloc(n * sizeof(int));assert(p != NULL);memcpy(p + m, arr, (n - m) * sizeof(int));memcpy(p, arr + n -m , m * sizeof(int));memcpy(arr, p, n * sizeof(int));free(p);p = NULL;return 0;}int myStrcmp(char *p1, char*p2){int ret = 0;while((*p1 != '\0') && (*p2 != '\0') && ( *p1 == *p2 ) ) {p1++;p2++;}if(*p1 == '\0'){ret = -1;}else if (*p2 == '\0'){ret = 1;}else{ret = (*p1 - *p2) > 0 ? 1 : -1;}return ret;}int revArr(int a[], int size){int tmp = 0, *start = NULL, *end = NULL;start = a;end = start + size - 1;size = size / 2;while(size >= 0){tmp = *(start + size);*(start + size) = *(end - size);*(end - size) = tmp;size--;}return 0;}char *getMonth(char *month[], int which) {assert(which <= 12);return ( *(month + which - 1));}int getStr(char *dest, char* src, int m) {int len = 0;len = strlen(src) + 1 - m;src = src + m - 1;memcpy(dest, src, len * sizeof(char));return 0;}int removePer3(int arr[], int size){int i = 0;for(i = 0; i < size; i++){if(((arr[i]) % 3) == 0){arr[i] = 0;}}return 0;}int getMinMax(int a[], int size){int i = 0, min = 0, max = 0, tmp = 0;min = max = 0;for(i = 0; i < size; i++){if (a[i] <= a[min]){min = i;}if(a[i] >= a[max]){max = i;}}tmp = a[0];a[0] = a[min];a[min] = tmp;tmp = a[size - 1];a[size - 1] = a[max];a[max] = tmp;}int test_10_1(){int ite = 0, iRet = 0, arr[5] = {121, 234, 456456, 543, 23};iRet = sortPtr(arr, 5);assert (23 == arr[0]);assert (121 == arr[1]);assert (234 == arr[2]);assert (543 == arr[3]);assert (456456 == arr[4]);printf("\r\nTest_10_1 Passed!");return 0;}int test_10_2(){int ite = 0, iRet = 0;char *arr[10] = { "In the IBM Rational ClearCase environment", \"An auditable history of source files and software builds is maintained in your organization", \"The efforts of your team can be coordinated into a definable"};iRet = sortStr(arr, 3);assert( strcmp(arr[0], "An auditable history of source files and software builds is maintained in your organization") == 0 );assert( strcmp(arr[1], "In the IBM Rational ClearCase environment") == 0);assert( strcmp(arr[2], "The efforts of your team can be coordinated into a definable") == 0);printf("\r\nTest_10_2 Passed!");return 0;}int test_10_3(){int ret = 0, a[10] = {7,2,3,9,1,0,7,6,5,0};ret = getMinMax(a, 10);assert(a[0] == 0);assert(a[9] == 9);printf("\r\nTest_10_3 Passed!");return 0;}int test_10_4(){int ret = 0, arr[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};ret = moveInt(arr, 9, 3);assert(6 == arr[0]);assert(7 == arr[1]);assert(8 == arr[2]);assert(0 == arr[3]);assert(1 == arr[4]);assert(2 == arr[5]);assert(3 == arr[6]);assert(4 == arr[7]);assert(5 == arr[8]);printf("\r\nTest_10_4 Passed!");return 0;}int test_10_5(){int ret = 0, i = 0, a[12] = {1,2,3,4,5,6,7,8,9,10,11,12};ret = removePer3(a, 12);assert(a[2] == 0);assert(a[5] == 0);assert(a[8] == 0);assert(a[11] == 0);printf("\r\nTest_10_5 Passed!");return 0;}int test_10_7(){int ret = 0;char s2[100] = {0}, *s1 = "hello world!";getStr(s2, s1, 7);assert( strcmp(s2, "world!") == 0);printf("\r\nTest_10_7 Passed!");return 0;}int test_10_8(){int ret = 0;char *str = "a123xABC ??#$%^ 302tab5876";ret = statStr(str);if(ret == 0){printf("\r\nTest_10_8 Passed!");}return 0;}int test_10_10(){int i = 0, j = 0, ret = 0;int arr[5][5] = { \{16, 17, 18, 19, 20}, \{11, 12, 13, 14, 15}, \{1, 2, 3, 4, 5}, \{21, 22, 23, 24, 25}, \{6, 7, 8, 9, 10}, \};ret = getMultiArr(arr, 5);assert(1 == arr[0][0] );assert(2 == arr[0][4] );assert(3 == arr[4][0] );assert(4 == arr[4][4] );assert(25 == arr[2][2] );printf("\r\nTest_10_10 Passed!");return 0;}int test_10_11(){int ite = 0, iRet = 0;char *arr[10] = { "In the", \"An aud", \"The ef", \"Proces", \"Sets o", \"Unifie", \"Out-of", \"Practi", \"Ration", \"Explor" \};iRet = sortStr(arr, 10);assert( strcmp(arr[0], "An aud") == 0 );assert( strcmp(arr[1], "Explor") == 0);assert( strcmp(arr[2], "In the") == 0);assert( strcmp(arr[3], "Out-of") == 0);assert( strcmp(arr[4], "Practi") == 0);assert( strcmp(arr[5], "Proces") == 0);assert( strcmp(arr[6], "Ration") == 0);assert( strcmp(arr[7], "Sets o") == 0);assert( strcmp(arr[8], "The ef") == 0);assert( strcmp(arr[9], "Unifie") == 0);printf("\r\nTest_10_11 Passed!");return 0;}int test_10_12(){int ite = 0, iRet = 0;char *arr[10] = { "In the IBM Rational ClearCase environment", \"An auditable history of source files and software builds is maintained in your organization", \"The efforts of your team can be coordinated into a definable", \"Process by using one of the following", \"Sets of Rational ClearCase features", \"Unified Change Management (UCM),", \"Out-of-the-box process that supports best", \"Practices for change management as described in the IBM", \"Rational Unified Process. Project managers can configure", \"Explorer. For more information about Rational ClearCase"};iRet = sortStr(arr, 10);assert( strcmp(arr[0], "An auditable history of source files and software builds is maintained in your organization") == 0 );assert( strcmp(arr[1], "Explorer. For more information about Rational ClearCase") == 0);assert( strcmp(arr[2], "In the IBM Rational ClearCase environment") == 0);assert( strcmp(arr[3], "Out-of-the-box process that supports best") == 0);assert( strcmp(arr[4], "Practices for change management as described in the IBM") == 0);assert( strcmp(arr[5], "Process by using one of the following") == 0);assert( strcmp(arr[6], "Rational Unified Process. Project managers can configure") == 0);assert( strcmp(arr[7], "Sets of Rational ClearCase features") == 0);assert( strcmp(arr[8], "The efforts of your team can be coordinated into a definable") == 0);assert( strcmp(arr[9], "Unified Change Management (UCM),") == 0);printf("\r\nTest_10_12 Passed!");return 0;}int test_10_14(){int ret = 0, a[11] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};ret = revArr(a, 11);assert(10 == a[0]);assert(9 == a[1]);assert(8 == a[2]);assert(7 == a[3]);assert(6 == a[4]);assert(5 == a[5]);assert(4 == a[6]);assert(3 == a[7]);assert(2 == a[8]);assert(1 == a[9]);assert(0 == a[10]);printf("\r\nTest_10_14 Passed!");return 0;}int test_10_15(){int ave = 0, ret = 0;int student[4][6] = { \{1, 100, 90, 80, 70, 97}, \{2, 34, 45, 56, 78, 97}, \{3, 76, 34, 68, 84, 12}, \{4, 90, 90, 90, 75, 28} \};ave = average(student, 1, 4);ret = searchStu(student, 4);assert(75 == ave);assert(2 == ret);printf("\r\nTest_10_15 Passed!");return 0;}int test_10_16(){int iRet = 0, ite = 0, arr[SIZE] = {0};char *str = "a123x456 17960? 302tab5876";iRet = extraNum(str, arr);assert(123 == arr[0]);assert(456 == arr[1]);assert(17960 == arr[2]);assert(302 == arr[3]);assert(5876 == arr[4]);printf("\r\nTest_10_16 Passed!");return 0;}int test_10_17(){int ret = 0;char *s1 = "abcd", *s2 = "abCd";ret = myStrcmp(s1, s2);assert(ret == 1);char *s3 = "aBcd", *s4 = "abCd";ret = myStrcmp(s3, s4);assert(ret == -1);char *s5 = "abcde", *s6 = "abcd";ret = myStrcmp(s5, s6);assert(ret == 1);char *s7 = "abcd", *s8 = "abcde";ret = myStrcmp(s7, s8);assert(ret == -1);char *s9 = "abcd", *s10 = "abCde";ret = myStrcmp(s9, s10);assert(ret == 1);printf("\r\nTest_10_17 Passed!");return 0;}int test_10_18(){int which = 0;char *month[12] = { \"January", \"February", \"March", \"April", \"May", \"June", \"July", \"August", \"September", \"October", \"November", \"December" \};which = 11;assert( strcmp("November", getMonth(month, which)) == 0 );which = 7;assert( strcmp("July", getMonth(month, which)) == 0 );printf("\r\nTest_10_18 Passed!");return 0;}int test_10_20(){int ite = 0, iRet = 0;char *arr[5] = { "In the IBM Rational ClearCase environment", \"An auditable history of source files and software builds is maintained in your organization", \"The efforts of your team can be coordinated into a definable", \"Process by using one of the following", \"Sets of Rational ClearCase features" \};iRet = sortStr(arr, 5);assert( strcmp(arr[0], "An auditable history of source files and software builds is maintained in your organization") == 0 );assert( strcmp(arr[1], "In the IBM Rational ClearCase environment") == 0);assert( strcmp(arr[2], "Process by using one of the following") == 0);assert( strcmp(arr[3], "Sets of Rational ClearCase features") == 0);assert( strcmp(arr[4], "The efforts of your team can be coordinated into a definable") == 0);printf("\r\nTest_10_20 Passed!");return 0;}int test_10_21(){int ite = 0, iRet = 0, arr[5] = {121, 234, 456456, 543, 23};iRet = sort(arr, 5);assert (23 == arr[0]);assert (121 == arr[1]);assert (234 == arr[2]);assert (543 == arr[3]);assert (456456 == arr[4]);printf("\r\nTest_10_21 Passed!");return 0;}int testPtr(){int iRet = 0;#if 0#endifiRet += test_10_1();iRet += test_10_2();iRet += test_10_3();iRet += test_10_4();iRet += test_10_5();iRet += test_10_7();iRet += test_10_8();iRet += test_10_10();iRet += test_10_11();iRet += test_10_12();iRet += test_10_14();iRet += test_10_15();iRet += test_10_16();iRet += test_10_17();iRet += test_10_18();iRet += test_10_20();iRet += test_10_21();return iRet;}src/#include <stdio.h>#include <stdlib.h>#include <assert.h>#include "../inc/testFile.h" #include "../inc/testBits.h" #include "../inc/testPtr.h"int main(){int iRet = 0;#if 0iRet += testFile();assert(iRet == 0);iRet += testBits();assert(iRet == 0);#endifiRet += testPtr();assert(iRet == 0);return 0;}。
第十章指针一、选择题1.以下程序的运行结果是【C】。
sub(int x,int y,int *z){*z=y-x ;}main(){ int a, b,c;sub(10,5,&a);sub(7,a,&b);sub(a,b,&c);printf(”%4d,%4d,%4d\n”,a,b,c);}A. 5,2,3 B. -5,-12,-7 C.-5,-12,-17 D. 5,-2,-72.若已定义 char s[10]; 则在下面表达式中不表示s[1]的地址的是【 B 】A)s+1 B)s++ C)&s[0]+1 D)&s[1]3.下列程序能对两个整型变量的值进行交换。
以下正确的说法是【 D】。
main(){ int a=10,b=20;printf("(1)a=%d,b=%d\n",a,b);swap(&a,&b);printf(“(2)a=%d,b=%d\n”a,b);}swap(int p, int q){ int t; t=p;p=q;q=t;}A. 该程序完全正确B. 该程序有错,只要将语句swap(&a,&b);中的参数改为a,b即可C. 该程序有错,只要将swap()函数中的形参p和q以及t均定义为指针(执行语句不变)即可D. 以上说法都不正确4.有四组对指针变量进行操作的语句,以下判断正确的选项是【】。
(1)int *p,*q; q=p;int a,*p,*q;p=q=&a;(2)int a,*p,*q; q=&a; p=*q;int a=20, *p; *p=a;(3)int a=b=0,* p; p=&a; b=* p;int a=20,*p,*q=&a; *p=*q;(4)int a=20,*p,*q=&a; p=q;int p, *q; q=&p;A.正确:(1);不正确:(2),(3),(4)B.正确:(l),(4);不正确:(2),(3)C.正确:(3);不正确:(1),(2),(4)D.以上结论都不正确5.以下程序中调用scanf函数给变量a输入数值的方法是错误的,其错误原因是【】。
章节教案章节名称第1章 C语言概述授课类型理论课(√)、实践课(√)、实习()教学时数1、了解C语言的发展历程教学目的要求2、了解C语言的特点3、掌握C程序的组成结构4、掌握C程序的上机操作环境1.1 C语言出现的历史背景1.2 C语言的特点主要知识点1.3简单的C程序介绍1.4 C程序的上机步骤3教学重点、重点:C语言的组成结构及程序书写规范。
难点难点:C语言实验环境的操作。
教学内容的衡接及学时分配问题:不能及时熟练的掌握TC集成环境的操作,对程序出错时的英文提示信息看不懂。
教学后记教法:对集成环境操作先只介绍精简操作,而后再不断的扩展操作。
而对英文提示信息方面应引导学生先要有耐心的看这些信息,而后养成积累这些提示信息的习惯。
理论内容2学时,实验2学时。
章节名称第2章程序的灵魂——算法授课类型理论课(√)、实践课(√)、实习()教学时数1、了解程序的组成要素教学目的要求2、理解算法的概念和特性3、掌握用算法描述工具来描述算法4、了解结构化程序设计方法2.1算法的概念2.2简单算法举例主要知识点2.3算法的特性2.4怎样表示一个算法2.5结构化程序设计方法4教学重点、重点:C语言程序的组成要素和使用各种算法描述工具描述算法。
难点难点:运用算法和结构化程序设计方法解决实际问题。
教学内容的衡接及学时分配引导学生选择一种适合自己习惯的算法描述工具,而后利用该工教学后记具来分析和解决各类问题,并逐步的培养自己分析问题、解决问题的能力。
基本理论概念2学时,各种算法描述工具2学时,实验2学时。
章节名称第3章数据类型、运算符与表达式授课类型理论课(√)、实践课(√)、实习()教学时数1、掌握基本数据类型及其定义方法教学目的要求2、掌握运算符的种类、运算优先级、结合性。
3、掌握不同类型数据间的转换与运算。
4、掌握表达式类型和求值规则。
3.1 C的数据类型3.2常量与变量3.3整型数据3.4实型数据主要知识点3.5字符型数据3.6变量赋初值3.7各类数值型数据间的混合运算3.8算术运算符和算术表达式3.9赋值运算符和赋值表达式3.10逗号运算符和逗号表达式教学重点、难点重点:C语言的数据类型、常量概念与特性、变量的定义与特性、运算符的优先级与结合性,各类数值型数据间的混合运算。
第10章指针和数组1.设int x[4]={10,20,30,40}, y, *p=&x[1];则执行语句y=(*--p)++;后变量y的值为_____。
2.下列程序片段中不正确的字符串赋值或初始化方式是_____。
A. char *str; str="string";B. char str[7]={'s','t','r','i','n','g','\0'};C. char str[10]; str="string";D. char str []="string";3.有以下定义及语句,则对数组a元素的不正确引用的表达式是_____。
int a[4][5];*p[2],j;for (j = 0 ; j <4 ; j++)p[j]=a[j];A. p[0][0]B. *(a+3)[4]C. *(p[1]+2)D. *(&a[0][0]+3)4.若有下列的程序段,则对数组元素的错误引用是_____。
int a[12] = {0}, *p[3], ** pp, j;for (j = 0 ; j < 3 ; j++)p[j]=&a[j*4];pp = p ;A. pp[0][1]B. a[10]C. p[3][1]D. *(*(p+2)+2)5.如有以下定义和语句,int a[5]={ 9,8,7,6,5}, *p ;p=&a[3];则*--p的值是_____。
6.若有定义:int a[3][2]={2,4,6,8,10,12}; 则*a[1]+1的值是_____。
7.下述函数通过递归方法将字符串倒置,使用时需要指定字符数组的首地址、起始下标。
请填空。
#include<stdio.h>void fun(char *s,int low,int high){if ( )return;else{char t;fun( );t=s[low];s[low]=s[high];s[high]=t;}}8.阅读以下程序说明和C程序,把应填的内容写入空格处。