指针编程题

来源:互联网 发布:淘宝卖图书好卖吗 编辑:程序博客网 时间:2024/06/06 08:25

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(temp, 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[]);//实现strcpyvoid 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;            }        }    }}//实现strcpyvoid strcpy1(char *p1, char *p2){    while(((*(p1++)) = (*(p2++)))){            }}


0 0
原创粉丝点击