C程序设计(第5版)谭浩强习题解答
第8章 善于利用指针
1. 输入3个整数,要求按由小到大的顺序输出。
#include <stdio.h>
int main()
{
void swap(int *p1, int *p2);
int n1, n2, n3;
int *p1, *p2, *p3;
printf("input three integer n1,n2,n3:");
scanf("%d,%d,%d", &n1, &n2, &n3);
p1 = &n1;
p2 = &n2;
p3 = &n3;
if (n1 > n2) swap(p1, p2);
if (n1 > n3) swap(p1, p3);
if (n2 > n3) swap(p2, p3);
printf("Now,the order is:%d,%d,%d\n", n1, n2, n3);
return 0;
}
void swap(int *p1, int *p2)
{
int p;
p = *p1; *p1 = *p2; *p2 = p;
}
2. 输入3个字符串,要求按由小到大的顺序输出。
#include <stdio.h>
#include <string.h>
int main()
{
void swap(char *, char *);
char str1[20], str2[20], str3[20];
printf("input three line:\n");
gets(str1);
gets(str2);
gets(str3);
if (strcmp(str1, str2) > 0) swap(str1, str2);
if (strcmp(str1, str3) > 0) swap(str1, str3);
if (strcmp(str2, str3) > 0) swap(str2, str3);
printf("Now,the order is:\n");
printf("%s\n%s\n%s\n", str1, str2, str3);
return 0;
}
void swap(char *p1, char *p2)
{
char p[20];
strcpy(p, p1); strcpy(p1, p2); strcpy(p2, p);
}
3. 输入10个整数,将其中最小的数与第一个数对换, 把最大的数与最后一个数对换。写3个函数:①输人10个数;②进行处理;③输出10个数。
#include <stdio.h>
int main()
{
void input(int *);
void max_min_value(int *);
void output(int *);
int number[10];
input(number);
max_min_value(number);
output(number);
return 0;
}
void input(int *number)
{
int i;
printf("input 10 numbers:");
for (i = 0; i < 10; i++)
scanf("%d", &number[i]);
}
void max_min_value(int *number)
{
int *max, *min, *p, temp;
max = min = number;
for (p = number + 1; p < number + 10; p++)
if (*p > *max) max = p;
else if (*p < *min) min = p;
temp = number[0]; number[0] = *min; *min = temp;
if (max == number) max = min;
temp = number[9]; number[9] = *max; *max = temp;
}
void output(int *number)
{
int *p;
printf("Now,they are: ");
for (p = number; p < number + 10; p++)
printf("%d ", *p);
printf("\n");
}
4. 有n个整数,使前面各数顺序向后移m个位置,最后m个数变成最前面m个数,见图8.43。 写一函数实现以上功能,在主函数中输人n个整数和输出调整后的n个数。
#include <stdio.h>
int main()
{
void move(int[20], int, int);
int number[20], n, m, i;
printf("how many numbers?");
scanf("%d", &n);
printf("input %d numbers:\n", n);
for (i = 0; i < n; i++)
scanf("%d", &number[i]);
printf("how many place you want move?");
scanf("%d", &m);
move(number, n, m);
printf("Now,they are:\n");
for (i = 0; i < n; i++)
printf("%d ", number[i]);
printf("\n");
return 0;
}
void move(int array[20], int n, int m)
{
int *p, array_end;
array_end = *(array + n - 1);
for (p = array + n - 1; p > array; p--)
*p = *(p - 1);
*array = array_end;
m--;
if (m > 0) move(array, n, m);
}
5. 有n个人围成一圈,顺序排号。从第1个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。
#include <stdio.h>
int main()
{
int i, k, m, n, num[50], *p;
printf("\ninput number of person: n=");
scanf("%d", &n);
p = num;
for (i = 0; i < n; i++)
*(p + i) = i + 1;
i = 0;
k = 0;
m = 0;
while (m < n - 1)
{
if (*(p + i) != 0) k++;
if (k == 3)
{
*(p + i) = 0;
k = 0;
m++;
}
i++;
if (i == n) i = 0;
}
while (*p == 0) p++;
printf("The last one is NO.%d\n", *p);
return 0;
}
6. 写一函数,求一个字符串的长度。在main函数中输入字符串,并输出其长度。
#include <stdio.h>
int main()
{
int length(char *p);
int len;
char str[20];
printf("input string: ");
scanf("%s", str);
len = length(str);
printf("The length of string is %d.\n", len);
return 0;
}
int length(char *p)
{
int n;
n = 0;
while (*p != '\0')
{
n++;
p++;
}
return(n);
}
7. 有一字符串,包含n个字符。写一函数,将此字符串中从第m个字符开始的全部字符复制成为另一个字符串。
#include <stdio.h>
#include <string.h>
int main()
{
void copystr(char *, char *, int);
int m;
char str1[20], str2[20];
printf("input string:");
gets(str1);
printf("which character that begin to copy?");
scanf("%d", &m);
if (strlen(str1) < m)
printf("input error!");
else
{
copystr(str1, str2, m);
printf("result:%s\n", str2);
}
return 0;
}
void copystr(char *p1, char *p2, int m)
{
int n;
n = 0;
while (n < m - 1)
{
n++;
p1++;
}
while (*p1 != '\0')
{
*p2 = *p1;
p1++;
p2++;
}
*p2 = '\0';
}
8. 输入一行文字,找出其中大写字母、小写字母、空格、数字以及其他字符各有多少。
#include <stdio.h>
int main()
{
int upper = 0, lower = 0, digit = 0, space = 0, other = 0, i = 0;
char *p, s[20];
printf("input string: ");
while ((s[i] = getchar()) != '\n') i++;
p = &s[0];
while (*p != '\n')
{
if (('A' <= *p) && (*p <= 'Z'))
++upper;
else if (('a' <= *p) && (*p <= 'z'))
++lower;
else if (*p == ' ')
++space;
else if ((*p <= '9') && (*p >= '0'))
++digit;
else
++other;
p++;
}
printf("upper case:%d lower case:%d", upper, lower);
printf(" space:%d digit:%d other:%d\n", space, digit, other);
return 0;
}
9. 写一函数,将一个3x3的整型矩阵转置。
#include <stdio.h>
int main()
{
void move(int *pointer);
int a[3][3], *p, i;
printf("input matrix:\n");
for (i = 0; i < 3; i++)
scanf("%d %d %d", &a[i][0], &a[i][1], &a[i][2]);
p = &a[0][0];
move(p);
printf("Now,matrix:\n");
for (i = 0; i < 3; i++)
printf("%d %d %d\n", a[i][0], a[i][1], a[i][2]);
return 0;
}
void move(int *pointer)
{
int i, j, t;
for (i = 0; i < 3; i++)
for (j = i; j < 3; j++)
{
t = *(pointer + 3 * i + j);
*(pointer + 3 * i + j) = *(pointer + 3 * j + i);
*(pointer + 3 * j + i) = t;
}
}
10. 将一个5x5的矩阵中最大的元素放在中心,4个角分别放4个最小的元素(顺序为从左到右,从上到下依次从小到大存放),写一函数实现之。用main函数调用。
//8.10.1
#include <stdio.h>
int main()
{
void change(int *p);
int a[5][5], *p, i, j;
printf("input matrix:\n");
for (i = 0; i < 5; i++)
for (j = 0; j < 5; j++)
scanf("%d", &a[i][j]);
p = &a[0][0];
change(p);
printf("Now,matrix:\n");
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
printf("%d ", a[i][j]);
printf("\n");
}
return 0;
}
void change(int *p)
{
int i, j, temp;
int *pmax, *pmin;
pmax = p;
pmin = p;
for (i = 0; i < 5; i++)
for (j = i; j < 5; j++)
{
if (*pmax < *(p + 5 * i + j)) pmax = p + 5 * i + j;
if (*pmin > *(p + 5 * i + j)) pmin = p + 5 * i + j;
}
temp = *(p + 12);
*(p + 12) = *pmax;
*pmax = temp;
temp = *p;
*p = *pmin;
*pmin = temp;
pmin = p + 1;
for (i = 0; i < 5; i++)
for (j = 0; j < 5; j++)
if (((p + 5 * i + j) != p) && (*pmin > *(p + 5 * i + j))) pmin = p + 5 * i + j;
temp = *pmin;
*pmin = *(p + 4);
*(p + 4) = temp;
pmin = p + 1;
for (i = 0; i < 5; i++)
for (j = 0; j < 5; j++)
if (((p + 5 * i + j) != (p + 4)) && ((p + 5 * i + j) != p) && (*pmin > *(p + 5 * i + j)))pmin = p + 5 * i + j;
temp = *pmin;
*pmin = *(p + 20);
*(p + 20) = temp;
pmin = p + 1;
for (i = 0; i < 5; i++)
for (j = 0; j < 5; j++)
if (((p + 5 * i + j) != p) && ((p + 5 * i + j) != (p + 4)) && ((p + 5 * i + j) != (p + 20)) && (*pmin > *(p + 5 * i + j)))
pmin = p + 5 * i + j;
temp = *pmin;
*pmin = *(p + 24);
*(p + 24) = temp;
}
//8.10.2
#include <stdio.h>
int main()
{
void change(int *p);
int a[5][5], *p, i, j;
printf("input matrix:\n");
for (i = 0; i < 5; i++)
for (j = 0; j < 5; j++)
scanf("%d", &a[i][j]);
p = &a[0][0];
change(p);
printf("Now,matrix:\n");
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
printf("%d ", a[i][j]);
printf("\n");
}
return 0;
}
void change(int *p) //交换函数
{
int i, j, temp;
int *pmax, *pmin;
pmax = p;
pmin = p;
for (i = 0; i < 5; i++) //找最大值和最小值的地址,并赋给 pmax,pmin
for (j = i; j < 5; j++)
{
if (*pmax < *(p + 5 * i + j)) pmax = p + 5 * i + j;
if (*pmin > *(p + 5 * i + j)) pmin = p + 5 * i + j;
}
temp = *(p + 12); //将最大值与中心元素互换
*(p + 12) = *pmax;
*pmax = temp;
temp = *p; //将最小值与左上角元素互换
*p = *pmin;
*pmin = temp;
pmin = p + 1;
//将a[0][1]的地址赋给pmin,从该位置开始找最小的元素
for (i = 0; i < 5; i++) //找第二最小值的地址赋给 pmin
for (j = 0; j < 5; j++)
{
if (i == 0 && j == 0) continue;
if (*pmin > *(p + 5 * i + j)) pmin = p + 5 * i + j;
}
temp = *pmin; //将第二最小值与右上角元素互换
*pmin = *(p + 4);
*(p + 4) = temp;
pmin = p + 1;
for (i = 0; i < 5; i++) //找第三最小值的地址赋给pmin
for (j = 0; j < 5; j++)
{
if ((i == 0 && j == 0) || (i == 0 && j == 4)) continue;
if (*pmin > *(p + 5 * i + j)) pmin = p + 5 * i + j;
}
temp = *pmin; //将第三最小值与左下角元素互换
*pmin = *(p + 20);
*(p + 20) = temp;
pmin = p + 1;
for (i = 0; i < 5; i++) //找第四最小值的地址赋给pmin
for (j = 0; j < 5; j++)
{
if ((i == 0 && j == 0) || (i == 0 && j == 4) || (i == 4 && j == 0)) continue;
if (*pmin > *(p + 5 * i + j)) pmin = p + 5 * i + j;
}
temp = *pmin; //将第四最小值与右下角元素互换
*pmin = *(p + 24);
*(p + 24) = temp;
}
11. 在主函数中输入10个等长的字符串。用另一函数对它们排序。然后在主函数输出这10个已排好序的字符串。
//8.11.1
#include <stdio.h>
#include <string.h>
int main()
{
void sort(char s[][6]);
int i;
char str[10][6];
printf("input 10 strings:\n");
for (i = 0; i < 10; i++)
scanf("%s", str[i]);
sort(str);
printf("Now,the sequence is:\n");
for (i = 0; i < 10; i++)
printf("%s\n", str[i]);
return 0;
}
void sort(char s[10][6])
{
int i, j;
char *p, temp[10];
p = temp;
for (i = 0; i < 9; i++)
for (j = 0; j < 9 - i; j++)
if (strcmp(s[j], s[j + 1]) > 0)
{
strcpy(p, s[j]);
strcpy(s[j], s[+j + 1]);
strcpy(s[j + 1], p);
}
}
//8.11.2
#include <stdio.h>
#include <string.h>
int main()
{
void sort(char(*p)[6]);
int i;
char str[10][6];
char(*p)[6];
printf("input 10 strings:\n");
for (i = 0; i < 10; i++)
scanf("%s", str[i]);
p = str;
sort(p);
printf("Now,the sequence is:\n");
for (i = 0; i < 10; i++)
printf("%s\n", str[i]);
return 0;
}
void sort(char(*s)[6])
{
int i, j;
char temp[6], *t = temp;
for (i = 0; i < 9; i++)
for (j = 0; j < 9 - i; j++)
if (strcmp(s[j], s[j + 1]) > 0)
{
strcpy(t, s[j]);
strcpy(s[j], s[+j + 1]);
strcpy(s[j + 1], t);
}
}
12. 用指针数组处理上一题目,字符串不等长。
#include <stdio.h>
#include <string.h>
int main()
{
void sort(char *[]);
int i;
char *p[10], str[10][20];
for (i = 0; i < 10; i++)
p[i] = str[i];
printf("input 10 strings:\n");
for (i = 0; i < 10; i++)
scanf("%s", p[i]);
sort(p);
printf("Now,the sequence is:\n");
for (i = 0; i < 10; i++)
printf("%s\n", p[i]);
return 0;
}
void sort(char *s[])
{
int i, j;
char *temp;
for (i = 0; i < 9; i++)
for (j = 0; j < 9 - i; j++)
if (strcmp(*(s + j), *(s + j + 1)) > 0)
{
temp = *(s + j);
*(s + j) = *(s + j + 1);
*(s + j + 1) = temp;
}
}
13. 写一个用矩形法求定积分的通用函数,分别求∫01sinxdx,∫01cosxdx,∫01exdx
#include<stdio.h>
#include<math.h>
int main()
{
float integral(float(*)(float), float, float, int);//对integarl函数的声明
float fsin(float); //对fsin函数的声明
float fcos(float); //对fcos函数的声明
float fexp(float); //对fexp函数的声明
float a1, b1, a2, b2, a3, b3, c, (*p)(float);
int n = 20;
printf("input a1,b1:");
scanf("%f,%f", &a1, &b1);
printf("input a2,b2:");
scanf("%f,%f", &a2, &b2);
printf("input a3,b3:");
scanf("%f,%f", &a3, &b3);
p = fsin;
c = integral(p, a1, b1, n);
printf("The integral of sin(x) is:%f\n", c);
p = fcos;
c = integral(p, a2, b2, n);
printf("The integral of cos(x) is:%f\n", c);
p = fexp;
c = integral(p, a3, b3, n);
printf("The integral of exp(x) is:%f\n", c);
return 0;
}
float integral(float(*p)(float), float a, float b, int n)
{
int i;
float x, h, s;
h = (b - a) / n;
x = a;
s = 0;
for (i = 1; i <= n; i++)
{
x = x + h;
s = s + (*p)(x)*h;
}
return(s);
}
float fsin(float x)
{
return sin(x);
}
float fcos(float x)
{
return cos(x);
}
float fexp(float x)
{
return exp(x);
}
14. 将n个数按输入时顺序的逆序排列,用函数实现。
#include <stdio.h>
int main()
{
void sort(char *p, int m);
int i, n;
char *p, num[20];
printf("input n:");
scanf("%d", &n);
printf("please input these numbers:\n");
for (i = 0; i < n; i++)
scanf("%d", &num[i]);
p = &num[0];
sort(p, n);
printf("Now,the sequence is:\n");
for (i = 0; i < n; i++)
printf("%d ", num[i]);
printf("\n");
return 0;
}
void sort(char *p, int m) //将n个数逆序排列函数
{
int i;
char temp, *p1, *p2;
for (i = 0; i < m / 2; i++)
{
p1 = p + i;
p2 = p + (m - 1 - i);
temp = *p1;
*p1 = *p2;
*p2 = temp;
}
}
15. 有一个班4个学生,5门课程。①求第1门课程的平均分;②找出有两门以上课程不及格的学生,输出他们的学号和全部课程成绩及平均成绩;③找出平均成绩在90分以上或全部课程成绩在85分以上的学生。分别编3个函数实现以上3个要求。
#include <stdio.h>
int main()
{
void avsco(float *, float *);
void avcour1(char(*)[10], float *);
void fali2(char course[5][10], int num[], float *pscore, float aver[4]);
void good(char course[5][10], int num[4], float *pscore, float aver[4]);
int i, j, *pnum, num[4];
float score[4][5], aver[4], *pscore, *paver;
char course[5][10], (*pcourse)[10];
printf("input course:\n");
pcourse = course;
for (i = 0; i < 5; i++)
scanf("%s", course[i]);
printf("input NO. and scores:\n");
printf("NO.");
for (i = 0; i < 5; i++)
printf(",%s", course[i]);
printf("\n");
pscore = &score[0][0];
pnum = &num[0];
for (i = 0; i < 4; i++)
{
scanf("%d", pnum + i);
for (j = 0; j < 5; j++)
scanf("%f", pscore + 5 * i + j);
}
paver = &aver[0];
printf("\n\n");
avsco(pscore, paver); //求出每个学生的平均成绩
avcour1(pcourse, pscore); //求出第一门课的平均成绩
printf("\n\n");
fali2(pcourse, pnum, pscore, paver); //找出2门课不及格的学生
printf("\n\n");
good(pcourse, pnum, pscore, paver); //找出成绩好的学生
return 0;
}
void avsco(float *pscore, float *paver) //求每个学生的平均成绩的函数
{
int i, j;
float sum, average;
for (i = 0; i < 4; i++)
{
sum = 0.0;
for (j = 0; j < 5; j++)
sum = sum + (*(pscore + 5 * i + j)); //累计每个学生的各科成绩
average = sum / 5; //计算平均成绩
*(paver + i) = average;
}
}
void avcour1(char(*pcourse)[10], float *pscore) //求第一课程的平均成绩的函数
{
int i;
float sum, average1;
sum = 0.0;
for (i = 0; i < 4; i++)
sum = sum + (*(pscore + 5 * i)); //累计每个学生的得分
average1 = sum / 4; //计算平均成绩
printf("course 1:%s average score:%7.2f\n", *pcourse, average1);
}
void fali2(char course[5][10], int num[], float *pscore, float aver[4])
//找两门以上课程不及格的学生的函数
{
int i, j, k, labe1;
printf(" ==========Student who is fail in two courses======= \n");
printf("NO. ");
for (i = 0; i < 5; i++)
printf("%11s", course[i]);
printf(" average\n");
for (i = 0; i < 4; i++)
{
labe1 = 0;
for (j = 0; j < 5; j++)
if (*(pscore + 5 * i + j) < 60.0) labe1++;
if (labe1 >= 2)
{
printf("%d", num[i]);
for (k = 0; k < 5; k++)
printf("%11.2f", *(pscore + 5 * i + k));
printf("%11.2f\n", aver[i]);
}
}
}
void good(char course[5][10], int num[4], float *pscore, float aver[4])
//找成绩优秀学生(各门85以上或平均90分以上)的函数
{
int i, j, k, n;
printf(" ======Students whose score is good======\n");
printf("NO. ");
for (i = 0; i < 5; i++)
printf("%11s", course[i]);
printf(" average\n");
for (i = 0; i < 4; i++)
{
n = 0;
for (j = 0; j < 5; j++)
if (*(pscore + 5 * i + j) > 85.0) n++;
if ((n == 5) || (aver[i] >= 90))
{
printf("%d", num[i]);
for (k = 0; k < 5; k++)
printf("%11.2f", *(pscore + 5 * i + k));
printf("%11.2f\n", aver[i]);
}
}
}
16. 输入一个字符串,内有数字和非数字字符,例如:A123x456 17960? ,302tab5876,将其中连续的数字作为一个整数,依次存放到一数组a中。例如,123放在a[0],456放在a1[1]…统计共有多少个整数,并输出这些数。
#include <stdio.h>
int main()
{
char str[50], *pstr;
int i, j, k, m, e10, digit, ndigit, a[10], *pa;
printf("input a string:\n");
gets(str);
pstr = &str[0]; //字符指针pstr置于数组str 首地址
pa = &a[0]; //指针pa置于a数组首地址
ndigit = 0; //ndigit代表有多少个整数
i = 0; //代表字符串中的第几个字符
j = 0;
while (*(pstr + i) != '\0')
{
if ((*(pstr + i) >= '0') && (*(pstr + i) <= '9'))
j++;
else
{
if (j > 0)
{
digit = *(pstr + i - 1) - 48; //将个数位赋予digit
k = 1;
while (k < j) //将含有两位以上数的其它位的数值累计于digit
{
e10 = 1;
for (m = 1; m <= k; m++)
e10 = e10 * 10; //e10代表该位数所应乘的因子
digit = digit + (*(pstr + i - 1 - k) - 48)*e10; //将该位数的数值\累加于digit
k++; //位数K自增
}
*pa = digit; //将数值赋予数组a
ndigit++;
pa++; //指针pa指向a数组下一元素
j = 0;
}
}
i++;
}
if (j > 0) //以数字结尾字符串的最后一个数据
{
digit = *(pstr + i - 1) - 48; //将个数位赋予digit
k = 1;
while (k < j) //将含有两位以上数的其它位的数值累加于digit
{
e10 = 1;
for (m = 1; m <= k; m++)
e10 = e10 * 10; //e10代表位数所应乘的因子
digit = digit + (*(pstr + i - 1 - k) - 48)*e10; //将该位数的数值累加于digit
k++; //位数K自增
}
*pa = digit; //将数值赋予数组a
ndigit++;
j = 0;
}
printf("There are %d numbers in this line, they are:\n", ndigit);
j = 0;
pa = &a[0];
for (j = 0; j < ndigit; j++) //打印数据
printf("%d ", *(pa + j));
printf("\n");
return 0;
}
17. 写一函数,实现两个字符串的比较。即自己写一个strcmp函数,函数原型为int strcmp(char * p1 ,char * p2); 设p1指向字符串s1, p2指向字符串s2。要求当s1=s2时,返回值为0;若s1≠s2,返回它们二者第1个不同字符的ASCII码差值(如"BOY"与"BAD" ,第2个字母不同,0与A之差为79- 65=14)。如果s1>s2,则输出正值;如果s1<s2,则输出负值。
#include<stdio.h>
int main()
{
int strcmp(char *p1, char *p2);
int m;
char str1[20], str2[20], *p1, *p2;
printf("input two strings:\n");
scanf("%s", str1);
scanf("%s", str2);
p1 = &str1[0];
p2 = &str2[0];
m = strcmp(p1, p2);
printf("result:%d,\n", m);
return 0;
}
int strcmp(char *p1, char *p2) //两个字符串比较函数
{
int i;
i = 0;
while (*(p1 + i) == *(p2 + i))
if (*(p1 + i++) == '\0') return(0); //相等时返回结果0
return(*(p1 + i) - *(p2 + i)); //不等时返回结果为第一个不等字符ASCII码的差值
}
18. 编一程序,输入月份号,输出该月的英文月名。例如,输人3,则输出"March" ,要求用指针数组处理。
#include <stdio.h>
int main()
{
char *month_name[13] = { "illegal month","January","February","March","April",
"May","June","july","August","September","October", "November","December" };
int n;
printf("input month:\n");
scanf("%d", &n);
if ((n <= 12) && (n >= 1))
printf("It is %s.\n", *(month_name + n));
else
printf("It is wrong.\n");
return 0;
}
19.(1) 编写一个函数new,对n个字符开辟连续的存储空间,此函数应返回一个指针(地址),指向字符串开始的空间。new(n)表示分配n个字节的内存空间。(2)写一函数free,将前面用new函数占用的空间释放。free§表示将p(地址)指向的单元以后的内存段释放。
//8.19.1
#include <stdio.h>
#define NEWSIZE 1000 //指定开辟存区的最大容量
char newbuf[NEWSIZE]; //定义字符数组newbuf
char *newp = newbuf; //定义指针变量newp,指向可存区的始端
char *new(int n) //定义开辟存区的函数new,开辟存储区后返回指针
{
if (newp + n <= newbuf + NEWSIZE) //开辟区未超过newbuf数组的大小
{
newp += n; //newp指向存储区的末尾
return(newp - n); //返回一个指针,它指向存区的开始位置
}
else
return(NULL); //当存区不够分配时,返回一个空指针
}
//8.19.2
#include <stdio.h>
#define NEWSIZE 1000
char newbuf[NEWSIZE];
char *newp = newbuf;
void free(char *p) //释放存区函数
{
if (p >= newbuf && p < newbuf + NEWSIZE)
newp = p;
}
20. 用指向指针的指针的方法对5个字符串排序并输出。
#define LINEMAX 20 //定义字符串的最大长度
int main()
{
int i;
char **p, *pstr[5], str[5][LINEMAX];
for (i = 0; i < 5; i++)
pstr[i] = str[i]; //将第i个字符串的首地址赋予指针数组 pstr 的第i个元素
printf("input 5 strings:\n");
for (i = 0; i < 5; i++)
scanf("%s", pstr[i]);
p = pstr;
sort(p);
printf("strings sorted:\n");
for (i = 0; i < 5; i++)
printf("%s\n", pstr[i]);
}
sort(char **p) //冒泡法对5个字符串排序函数
{
int i, j;
char *temp;
for (i = 0; i < 5; i++)
{
for (j = i + 1; j < 5; j++)
{
if (strcmp(*(p + i), *(p + j)) > 0) //比较后交换字符串地址
{
temp = *(p + i);
*(p + i) = *(p + j);
*(p + j) = temp;
}
}
}
return 0;
}
21. 用指向指针的指针的方法对n个整数排序并输出。要求将排序单独写成一个函数。n个整数在主函数中输入,最后在主函数中输出。
#include<stdio.h>
int main()
{
void sort(int **p, int n);
int i, n, data[20], **p, *pstr[20];
printf("input n:\n");
scanf("%d", &n);
for (i = 0; i < n; i++)
pstr[i] = &data[i]; //将第i个整数的地址赋予指针数组 pstr 的第i个元素
printf("input %d integer numbers:", n);
for (i = 0; i < n; i++)
scanf("%d", pstr[i]);
p = pstr;
sort(p, n);
printf("Now,the sequence is:\n");
for (i = 0; i < n; i++)
printf("%d ", *pstr[i]);
printf("\n");
return 0;
}
void sort(int **p, int n)
{
int i, j, *temp;
for (i = 0; i < n - 1; i++)
{
for (j = i + 1; j < n; j++)
{
if (**(p + i) > **(p + j)) //比较后交换整数地址
{
temp = *(p + i);
*(p + i) = *(p + j);
*(p + j) = temp;
}
}
}
}