8.21
#include <stdio.h>
void bubbleSort(int **p, int n);
int main(void){
int a[100];
int *b[100];
int n, i, **p;
printf("Input your n(n < 100)\n");
scanf("%d", &n);
printf("Input your a[n]\n");
for(i = 0; i < n; i++)
scanf("%d", a + i);
for(i = 0; i < n; i++)
b[i] = a + i;
p = b;
bubbleSort(p, n);
for(i = 0; i < n; i++)
printf("%d ", *b[i]);
printf("\n");
return 0;
}
void bubbleSort(int **p, int n){
int i, j, flag = 1;
int *temp;
for(i = 0; flag; i++){
flag = 0;
for(j = n - 1; j > i; j--){
flag = 1;
if(**(p + i) > **(p + j)){
temp = *(p + i);
*(p + i) = *(p + j);
*(p + j) = temp;
}
}
}
}
8.20
#include <stdio.h>
#include <string.h>
void selectionSort(char **p);
int main(void)
{
char a[5][30], *b[5], **p;
int i;
for(i = 0; i < 5; i++)
b[i] = a[i];
for(i = 0; i < 5; i++)
scanf("%s", b[i]);
p = b;
selectionSort(p);
printf("\n");
for(i = 0; i < 5; i++)
printf("%s\n", b[i]);
return 0;
}
void selectionSort(char **p)
{
int i, j, minj, n = 5;
char *temp;
for(i = 0; i < n - 1; i++)
{
minj = i;
for(j = i; j < n; j++)
{
if(strcmp(*(p + j), *(p + minj)) < 0)
minj = j;
}
temp = *(p + i);
*(p + i) = *(p + minj);
*(p + minj) = temp;
}
}
8.16
#include <stdio.h> int getnum(char str[], int a[]); int main(void) { char str[100] = {}, *pstr; int a[100] = {}, total, *p, i; p = a; pstr = str; printf("Input your string\n"); gets(pstr); total = getnum(str, a); for(i = 0; a[i] != '\0'; i++) printf("%d ", a[i]); printf("total = %d\n", total); return 0; } int getnum(char str[], int a[]) { int count = 0, temp = 0; int i = 0, j = 0, k = 0, m = 0, e10; char *p; int *pa; p = str; pa = a; while(*(p + i) != '\0') { if((*(p + i) >= '0') && (*(p + i) <= '9')) { j++; } else { if(j > 0) { temp = *(p + i -1) - '0'; k = 1; while(k < j) { e10 = 1; for(m = 1; m <= k; m++) e10 = e10 * 10; temp = temp + (*(p + i - 1 - k) - '0') * e10; k++; printf("2th while e10 temp = %d\n", temp); } //while *pa = temp; printf("pa++ = %d\n", *pa); pa++; j = 0; count++; } //if } //else i++; } // while if(j > 0) { temp = *(p + i -1) - '0'; k = 1; while(k < j) { e10 = 1; for(m = 1; m <= k; m++) e10 = e10 * 10; temp = temp + (*(p + i - 1 - k) - '0') * e10; k++; printf("2th while e10 temp = %d\n", temp); } //while *pa = temp; printf("pa++ = %d\n", *pa); j = 0; pa++; count++; } //if *pa = '\0'; return count; }