结构体和函数指针实现加减乘除

构思:
创建结构体,结构体的成员中有一个成员为函数指针,赋值后可指向各个算法的地址;
当输入add 1 2时,指针数组分别指向add,1和2;
随后进行字符串比较,判断进入哪一个算法;
实现:

char optr;
int(*pf)(int, int) = NULL;      //函数指针
int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }
int mul(int a, int b) { return a * b; }
 struct operation
{
    char *name;           
    int(*pf)(int, int);   //结构体的成员为一个函数指针
    char c;             
}ops[] = {
    { "add", add, '+' },
    { "sub", sub, '-' },
    { "mul", mul, 'X' }
};
void math_man(char *buf[], int num)
{
    int a = atoi(buf[1]);
    int b = atoi(buf[2]);
    int r = pf(a, b);
    printf("%s%c%s=%d\n", buf[1], optr,buf[2], r);
}
void do_action(char *pbuff[], int num)
{
    int i = 0;
    for (i = 0; i < sizeof(ops) / sizeof(ops[0]); i++)
    {
        if (strcmp(pbuff[0], ops[i].name) == 0)    //判断字符串
        {
            pf = ops[i].pf;    //函数指针指向
            optr = ops[i].c;   //optr指向算术符
            break;
        }
    }
    math_man(pbuff,num);
}
void parse(char *buff1)
{
    int num = 0;;
    int i = 0;
    char *str[3];      //数组指针 指向三个字符串的地址
    int status = 0;
    for (i = 0; buff1[i] != '\0'; i++)
    {
        if (!isspace(buff1[i])&&(status == 0))   //记录字符串的位置
        {
            status = 1;      //可以让buff1[0] 指向add  buff1[1] buff1[2]分别指向后面的两个数字
            str[num++] = buff1+i;
        }
        else if(isspace(buff1[i]))    //遇到空格则下一个位置是下一个字符串的起始位置,并截断上一个字符串的末尾
        {
            status = 0;
            buff1[i] = '\0';  
        }
    }
    do_action(str, num);
}
int main()
{
    char buff[200] ;
    while (1)
    {
        printf("input>:");
        scanf("%[^\n]%*c",buff);
        parse(buff);
        memset(buff, 0x00, sizeof(buff));//每次读取后进行初始化以免发生冲突
    }
    system("pause");
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮您解答这个问题。 首先,我们在C语言中可以通过结构体来表示一个复数,代码如下: ```c typedef struct Complex { float real; // 实部 float imag; // 虚部 } Complex; ``` 接下来,我们需要在另一个c文件中编写四个函数,分别代表两个复数的加减乘除。代码如下: ```c #include "complex.h" Complex addComplex(Complex *c1, Complex *c2) { Complex result; result.real = c1->real + c2->real; result.imag = c1->imag + c2->imag; return result; } Complex subComplex(Complex *c1, Complex *c2) { Complex result; result.real = c1->real - c2->real; result.imag = c1->imag - c2->imag; return result; } Complex mulComplex(Complex *c1, Complex *c2) { Complex result; result.real = c1->real * c2->real - c1->imag * c2->imag; result.imag = c1->real * c2->imag + c1->imag * c2->real; return result; } Complex divComplex(Complex *c1, Complex *c2) { Complex result; float denominator = c2->real * c2->real + c2->imag * c2->imag; result.real = (c1->real * c2->real + c1->imag * c2->imag) / denominator; result.imag = (c1->imag * c2->real - c1->real * c2->imag) / denominator; return result; } ``` 以上代码实现了四个函数,分别为addComplex、subComplex、mulComplex和divComplex。这些函数的输入均为两个结构体指针,输出为一个结构体。 最后,我们需要将这些函数通过头文件的形式引入主程序。我们可以创建一个名为complex.h的头文件,并将以上四个函数的声明写入其中,代码如下: ```c #ifndef COMPLEX_H #define COMPLEX_H typedef struct Complex { float real; // 实部 float imag; // 虚部 } Complex; Complex addComplex(Complex *c1, Complex *c2); Complex subComplex(Complex *c1, Complex *c2); Complex mulComplex(Complex *c1, Complex *c2); Complex divComplex(Complex *c1, Complex *c2); #endif ``` 最后,我们可以在主程序中调用这些函数,进行测试。代码如下: ```c #include <stdio.h> #include "complex.h" int main() { Complex c1 = {1.0, 2.0}; Complex c2 = {3.0, 4.0}; Complex addResult = addComplex(&c1, &c2); Complex subResult = subComplex(&c1, &c2); Complex mulResult = mulComplex(&c1, &c2); Complex divResult = divComplex(&c1, &c2); printf("c1 + c2 = %.2f + %.2fi\n", addResult.real, addResult.imag); printf("c1 - c2 = %.2f + %.2fi\n", subResult.real, subResult.imag); printf("c1 * c2 = %.2f + %.2fi\n", mulResult.real, mulResult.imag); printf("c1 / c2 = %.2f + %.2fi\n", divResult.real, divResult.imag); return 0; } ``` 以上代码将输出以下内容: ``` c1 + c2 = 4.00 + 6.00i c1 - c2 = -2.00 - 2.00i c1 * c2 = -5.00 + 10.00i c1 / c2 = 0.44 - 0.08i ``` 这表明,我们成功地实现了一个复数结构体以及四个函数,用于对两个复数进行加减乘除运算。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值