C和指針 第十六章 習題

16.8 計算平均年齡數組

#include <stdlib.h>
#include <stdio.h>
#define MAX_LEN 512

int main()
{
    int age;
    int totalAge;
    float avgAge;
    int peopleNum;
    FILE *file;
    char info[MAX_LEN];
    char *infoPtr;
    file = fopen("D:/family.txt", "r");

    //按行讀取文件
    while(fgets(info, MAX_LEN, file)){
        infoPtr = info;
        peopleNum = 0;
        totalAge = 0;
        //strtol轉換字符到數字
        while((age = strtol(infoPtr, &infoPtr, 10)) > 0){
            totalAge += age;
            peopleNum++;
        }
        //類型轉換爲float,而後計算平均年齡
        avgAge = (float)totalAge / peopleNum;
        printf("%savg: %5.2f\n", info, avgAge);
    }

    return 0;
}

運行:函數

16.9 計算相同生日機率blog

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

//比較元素
int compare(void const *birth1, void const *birth2){
    return *(int  *)(birth1) - *(int*)(birth2);
}

//打印數組
void print_arr(int *array, int len){
    int idx = 0;
    while(idx <= len){
        printf("%d ", array[idx]);
        idx++;
    }
}

//數組中是否有兩個相同數
int count_same(int *array, int len){
    int same = 0;
    while(len > 0){
        if(array[len] == array[len - 1]){
            return 1;
        }
        len--;
    }
    return 0;
}
int main()
{
    int times = 0;
    int randBirthday[30];
    int peopleCount;
    int sameCount = 0;
    srand((unsigned int)time(0));

    while(times < 100000){
        peopleCount = 29;
        while(peopleCount >= 0){
            randBirthday[peopleCount] = rand() % 365;
            peopleCount--;
        }
        qsort(randBirthday, 30, sizeof(int), compare);
        sameCount += count_same(randBirthday, 29);
        times++;
    }
    printf("%f", (float)(sameCount) / 100000);
    return 0;
}

運行:排序

16.10 插入排序element

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//插入排序
void insert_sort(void *array, unsigned int length, unsigned int size, int (*handle)(void const *num1, void const *num2));
//整型移動函數

//整型比較函數
int int_compare(void const *num1, void const *num2)
{
    return (*(int *)num1 - *(int *)(num2));
}

//打印數組
void print_arr(int *arr , int len){
    for(int idx = 0 ; idx < len; idx++){
        printf("%d ", arr[idx]);
    }
}

int main()
{
    int array[10] = {4, 1, 17, 2, 8 , 9, 22, 12, 7, 5};
    insert_sort(array, 10, sizeof(int), int_compare);
    print_arr(array, 10);
    return 0;
}

void insert_sort(void *array, size_t n_elements, size_t n_size, int (*handle)(void const *num1, void const *num2))
{
    //存放臨時須要位移的元素
    char *temp = malloc(n_size);
    //已排序的元素下標,從0開始,拿第二個元素和第一個對比
    unsigned int sortIdx = 1;
    //元素遊標和已排序元素遊標
    unsigned int idx, idy;
    //位移元素下面
    unsigned int mov;
    //從第二位開始,依次拿出元素和以前的已排序元素比較
    for(idx = 1; idx < n_elements; idx++){
        //開始比較
        for(idy = 0; idy < sortIdx; idy++){
            if(handle(array + idy * n_size, array + idx * n_size) > 0){
                //將元素和已排序元素依次比較,當遇到已排序元素,比當前元素大時,當前元素應該插入到該已排序元素位置,已排序元素應該後移一位
                //位移會覆蓋後面的值,因此須要先保存須要插入的值
                memcpy(temp, array + idx * n_size, n_size);
                for(mov = sortIdx; mov > idy; mov--){
                    memmove(array + mov * n_size, array + (mov - 1) * n_size, n_size);
                }
                //元素插入
                memcpy(array + idy * n_size , temp, n_size);
            }
        }
        //若是須要插入的值,正比如已排序的值中最大的還要大,那麼不須要移動,只要增長已排序的值得下標便可
        sortIdx++;
    }
}

運行:get

相關文章
相關標籤/搜索