指針編程題

main.m文件

//1.輸入10個整數,將其中最小的數與第一個數交換,把最大的數和最後一個數對換,寫三個函數1.輸入10個數,2.進行處理,3.輸出10個數  
    int a[10] = {0};  
    int *p = NULL;  
    p = a;  
    //1.輸入  
    inputArr(a, 10);  
    outputArr(a, 10);  
    printf("\n");  
    //2.處理函數  
    handleArr(a, 10);  
      
    //3.輸出函數  
    printf("輸出函數爲:\n");  
    outputArr(a, 10);  
   printf("\n");  
      
    //2.在主函數中輸入10個等長的字符串.用另一個函數對他們排序,然後主函數輸出10個已經排好的字符串  
    char str[5][20] = {0};  
    char *p[5] = {0};  
       printf("請輸入5個等長的字符串:\n");  
    for (int i = 0; i < 5; i++) {  
        p[i] = str[i];  
    }  
    for (int i = 0; i < 5; i++) {  
        scanf("%s", p[i]);  
    }  
    orderArr(p);  
        for (int i = 0; i < 5; i++) {  
       printf("%s ", *(p+i));  
    }  
      
    //3.有一個字符串,包含數字與字母,編程去除數字,要求:1.在原字符串操作2.使用指針處理  
    char str[] = "2a45abcd123sdafasdfasfasfasfasdfasfd9";  
    char *p = str;  
    int i = 0;  
//    char temp[255] = {0};  
    while (*(p + i) != '\0') {  
        if (*(p + i) >= '0' && *(p + i) <= '9') {  
            //判斷當前的字符是否是數字,如果是數字,就做剔除操作  
            //(剔除,覆蓋(使用後面的字符,將前面的字符覆蓋掉))  
            //覆蓋的開始位置,以及要使用的覆蓋內容的開始位置  
            //p + i.   p + i + 1.  
            strcpy(p + i, temp);  
            strcpy1(p + i, p + i + 1);  
        }else{  
            i++;  
        }  
    }  
    printf("%s", p);

.h文件

//(1)輸入函數  

void inputArr(int *p, int count);  

//(2)處理函數  

void handleArr(int *p, int count);  

//(3)輸出函數  

void outputArr(int *p, int count);  

//(4)查找數組中最大值的下標  

int maxIndexOfArr(int *p, int count);  

//(5)查找數組中最小值的下標  

int minIndexOfArr(int *p, int count);  

//(6.交換兩個數的值  

void swap(int *x, int *y);  

  

  

//2.字符串冒泡排序  

void orderArr(char *p[]);  

  

//實現strcpy  

void strcpy1(char *p1, char *p2); 

.m文件

//(1)輸入函數  

void inputArr(int *p, int count)  

{  

    for (int i = 0; i< count; i++) {  

        *(p + i) = arc4random() % (20 - 10 + 1) + 10;  

    }  

}  

//(2)處理函數  

void handleArr(int *p, int count)  

{  

    int minIndex = minIndexOfArr(p, count);//存儲最下值下標  

    int maxIndex = maxIndexOfArr(p, count);//存儲最大值下標  

    //交換  

    if (minIndex != 0) {  

        if (maxIndex == 0) {  

            maxIndex = minIndex;  

        }  

        swap(p, (p + minIndex));  

    }  

    if(maxIndex != count - 1){  

        swap((p + count - 1), (p + maxIndex));  

    }  

}  

//(3)輸出函數  

void outputArr(int *p, int count)  

{  

    for (int i = 0; i < 10; i++) {  

        printf("%d  ", *(p + i));  

    }  

}  

//(4)查找數組中最大值的下標  

int maxIndexOfArr(int *p, int count)  

{  

    int max = *p;//存儲最大值  

    int maxIndex = 0;//記錄最大值的下標  

    for (int i = 1; i < count; i++) {  

        if (max < *(p + i)) {  

            max = *(p + i);//存儲最大值  

            maxIndex = i;//存儲最大值的位置  

        }  

    }  

    return maxIndex;  

}  

//(5)查找數組中最小值的下標  

int minIndexOfArr(int *p, int count)  

{  

    int min = *p;//存儲最小值  

    int minIndex = 0;//記錄最小值的下標  

    for (int i = 1; i < count; i++) {  

        if (min > *(p + i)) {  

            min = *(p + i);//存儲最小值  

            minIndex = i;//存儲最小值的位置  

        }  

    }  

    return minIndex;  

}  

//(6)交換兩個數的值  實參傳地址  

void swap(int *x, int *y)  

{  

    int temp = *x;  

    *x = *y;  

    *y = temp;  

}  

  

//2.字符串冒泡排序  

void orderArr(char *p[])  

{  

    for (int i = 0; i < 5-1; i++) {  

        for (int j = 0; j < 5-1-i; j++) {  

            if (strcmp(*(p +j), *(p +j+1))>0) {  

                char *temp =NULL;  

                temp= *(p +j);  

                *(p +j)= *(p +j+1);  

                *(p +j+1)= temp;  

            }  

        }  

    }  

}  

  

//實現strcpy  

void strcpy1(char *p1, char *p2)  

{  

    while(((*(p1++)) = (*(p2++)))){  

          

    }  



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章