这两天复习RenMian给我留下谭浩强的C语言教程,温故而知新,夯实基础知识,
把谭浩强版C语言的第十章《指针》的书复习后把所有的编程习题做了一遍,贴出来激励自己一下,也希望有用的同学可以参考,时间和水平有限,不足之处,恳请读者批评指正
第十章《指针》答案如下
inc/testPtr.h
#include <string.h>
#include
#include
#include
#define SIZE 1024
int 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
#endif
iRet += 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/main.cc
#include
#include
#include
#include
"../inc/testFile.h"
#include
"../inc/testBits.h"
#include
"../inc/testPtr.h"
int main()
{
int iRet = 0;
#if 0
iRet += testFile();
assert(iRet == 0);
iRet += testBits();
assert(iRet == 0);
#endif
iRet += testPtr();
assert(iRet == 0);
return 0;
}