我只会一种方法,求完整代码!希望方法尽可能多

请用多种方法定义一个函数,该函数的功能是:根据提供的两个原始整数,计算出二者的和以及乘积并返回。主函数中调用该函数,实现求两个整数的和与积。
请用尽可能多的方法进行函数的定义及在主函数中对应的调用,给出每种方法下的完整程序

列举了三种比较常用方法,有注释说明。仅供参考!谢谢!

img

#include <stdio.h>

//第一种方法用数组返回 数组的索引0是和
//索引1是积
int* add_mul1(int a, int b)
{
    static int arr[2];
    arr[0]=a+b;
    arr[1]=a*b;
    return arr;
}

//第二种方法直接用参数指针的方式
int add_mul2(int a,int b,int *sum,int *mul)
{
    *sum=a+b;
    *mul=a*b;
    return 0;
}

//第三种返回结构体方式
struct add_mul{
    int sum;
    int mul;
}
;
struct add_mul add_mul3(int a,int b)
{
    struct add_mul addmul;
    addmul.sum=a+b;
    addmul.mul=a*b;
    return addmul;
}

int main(int argc, char** argv){
    int a=3;
    int b=20;

    //第一种引用方式
    int *sumandmul=add_mul1(a, b);
    printf("第一种:\n%d+%d=%d\n%dX%d=%d\n\n",a,b,sumandmul[0],a,b,sumandmul[1]);
 
   //第二种引用方式
    int sum,mul;
    add_mul2( a, b,&sum,&mul);
    printf("第二种:\n%d+%d=%d\n%dX%d=%d\n\n",a,b,sum,a,b,mul);
 
   //第三种引用方式
    struct add_mul add_mul;
    add_mul=add_mul3( a, b);
    printf("第三种:\n%d+%d=%d\n%dX%d=%d\n\n",a,b,add_mul.sum,a,b,add_mul.mul);
 
   return 0;
}


#include <stdio.h> 

void addmul(int i,int j, int *sum, int *mul)
{
    *sum = i+j;
    *mul = i*j;
}

int main()
{ 
    int a = 3, b = 4, sum, mul;
    addmul(a,b, &sum, &mul);
    printf("%d %d",sum,mul); 
    
    return 0; 
}
#include <stdio.h> 

struct sm
{
    int x,y;
};

sm summul(int x,int y)
{
    sm a;
    a.x = x+y;
    a.y = x*y;
    return a;
}

int main()
{ 
    int a = 3, b = 4;
    sm c = summul(a,b);
    printf("%d %d",c.x,c.y); 
    
    return 0; 
}
#include <stdio.h> 

class SumMul{
public:
    int x,y;
    SumMul(int a,int b)
    {
        x = a+b;
        y = a*b;
    }
};

int main()
{ 
    int a = 3, b = 4;
    SumMul sm(a,b);
    printf("%d %d",sm.x,sm.y); 
    
    return 0; 
}

传递参数的方式有多种、以及可以用指针的方式。返回数据类型可以是数组,或者指针。


#include<stdio.h>
//1、结构体方法
struct data
{
    int x=0, y=0;
    int sum = 0;//和
    int m = 0;  //积
};
//2、分别写和与积
int add(int a, int b)
{
    return a + b;
}
int mul(int a, int b)
{
    return a * b;
}
//结构体类型的函数,可返回和与积
void mulity(data &d)
{
    d.sum = d.x + d.y;
    d.m = d.x * d.y;
}
int main()
{
    int a = 6, b = 6;
    int sum=add(a, b);
    int m= mul(a, b);
    printf("和为: %d,乘积为:%d",sum,m);
    data d;
    d.x = 5, d.y = 5;
    mulity(d);
    printf("\n和为: %d,乘积为:%d",d.sum,d.m);
}

运行效果:

img

位运算方法求和

int addAndMultiply(int a, int b) {
    int* arr = (int*)malloc(2 * sizeof(int));
    while (b != 0) { // 当进位为 0 时跳出
        int c = (a & b) << 1;  // c = 进位
        a ^= b; // a = 非进位和
        b = c; // b = 进位
    }
    return a;
}

3个function

img

#include <stdio.h>

int function1 (int a,int b,int *add)
{
    int mul=a*b;
    *add=a+b;
    return mul;
}
void function2 (int a,int b,int *add ,int *mul)
{
    *mul=a*b;
    *add=a+b;
    return;
}
void function3 (int a,int b,int *anw)
{
    anw[0]=a+b;
    anw[1]=a*b;
    return;
}
int main()
{
    int a,b;
    scanf("%d%d",&a,&b);
    int add1;
    int mul1=function1(a,b,&add1);
    printf("function1: 和:%d  积%d\n",add1,mul1);
    
    int add2;
    int mul2;
    function2(a,b,&add2,&mul2);
    printf("function2: 和:%d  积%d\n",add2,mul2);
    
    int awn[2];
    function3(a,b,awn);
    printf("function3: 和:%d  积%d\n",awn[0],awn[1]);
    return 0;
}

3种方法如下:

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
//方法1
void fun1(int a, int b, int* add, int* mul)
{
    *add = a + b;
    *mul = a * b;
}
//方法2
struct Result
{
    int add;
    int mul;
};
void fun2(int a, int b, struct Result* st)
{
    st->add = a + b;
    st->mul = a * b;
}

//方法3
int fun3(int a, int b, int* add)
{
    int mul = a * b;
    *add = a + b;
    return mul;
}

int main()
{
    int a, b, add, mul;
    struct Result st;
    scanf("%d %d", &a, &b);
    //调用方法1
    fun1(a, b, &add, &mul);
    printf("方法1:和=%d,乘积=%d\n", add, mul);
    //调用方法2
    fun2(a, b, &st);
    printf("方法2:和=%d,乘积=%d\n", st.add, st.mul);
    //调用方法3
    mul = fun3(a, b, &add);
    printf("方法3:和=%d,乘积=%d\n", add, mul);

    return 0;
}