C/C++ self-defined simple Map mapping, Reduce convergence function, first-acquaintance function application

C/C++ custom simple Map mapping, Reduce convergence function, simple application of first-acquainted functions

Program application goals

Recently I learned the C/C++ language, corresponding to the application of pointers and pointer functions, as well as declaring a new type of typedef or using function calls.

Procedure requirements

To customize a map function, it is required to be able to pass in a custom operation function: perform addition or subtraction operations.
Customize a reduce function, which requires that each number in the array can be aggregated into one content as required. Here is only one for the convenience of expression Accumulation function: addall (int *arr, int n) .

Program example

    #include <stdio.h>
    #include <malloc.h>
    
    typedef int (*opt)(int,int);//自定义函数指针类型
    typedef int (*opt2)(int *, int);
    
    
    int *map(opt opt1,int *arr,int n,int *temp);//函数声明
    int reduce(opt2 opt2,int *arr,int n);
    int add(int m,int n);
    int sub(int m,int n);
    int addall(int *arr,int n);
    
    
    int main() {
    
    
        int n;//定义数组长度
        printf("please enter arr[number]--number:");
        scanf("%d",&n);
        int a[n];
        int *p=a;
        printf("please enter arr[number]--arr[]:");
        for(;p<a+n;p++){
    
    
            scanf("%d",p);
        }
        p=a;
        printf("The original arr[number] is:");
        for(;p<a+n;p++){
    
    
            printf("%3d",*p);
        }
        printf("\n");
        printf("please enter 0-->map or 1-->reduce .");
        int x;
        scanf("%d",&x);
       

The add function and sub function pointers can be passed into the map function as actual parameters for selective operations. The advantage of writing in this way is that the function is well encapsulated and flexible, and other operations can be added as needed.

        if(x==0){
    
    
            int (*temp)(int,int);
            printf("please enter 01-->add or 02-->sub");
            int y;
            scanf("%d",&y);
            if(y==01){
    
    
                printf("arr[number] is changed by using add from map.");
                temp=add;
            }
            if(y==02){
    
    
                printf("arr[number] is changed by using sub from map.");
                temp=sub;
            }
            int *p0=(int *)malloc(sizeof(int)*n);
            int *p2=map(temp,a,n,p0);
            for(int i=0;i<n;i++,p2++){
    
    
                printf("%3d",*p2);
            }
            printf("\n");
            free(p0);
        }
        if(x==1){
    
    
            printf("arr[number] is changed by using addall from reduce.");
            printf("%3d",reduce(addall,a,n));
            printf("\n");
        }
        return 0;
    }
   
函数部分
    int *map(opt opt1,int *arr,int n,int *temp){
    
    
        int *temp1=temp;
        for(int *p=arr;p<arr+n;p++){
    
    
            *temp1=opt1(*p,1);
            *temp1++;
        }
        return temp;
    }
    
    int add(int m,int n){
    
    
        return m+n;
    }
    
    int sub(int m,int n){
    
    
        return m-n;
    }
    int reduce(opt2 opt2,int *arr,int n){
    
    
        return opt2(arr,n);
    }
    
    int addall(int *arr,int n){
    
    
        int all;
        for(int *p=arr;p<arr+n;p++){
    
    
            all+=*p;
        }
        return all;
 

to sum up

When you first learn programming, you should not only meet the requirements and output the correct results, but more importantly, improve the readability, encapsulation, and robustness of the code, and think more about how you can use your own code style freely.

ps:
   留作总结,希望各位大神指正。

Guess you like

Origin blog.csdn.net/weixin_44290157/article/details/90302192