算法01 C语言设计

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;
}

 

转载于:https://www.cnblogs.com/paprikatree/p/10425655.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值