c语言四则运算函数题

描述
  与简单的求和函数类似,但是这次不单单是20以内的求和了。我们定义了四个函数,add(a,b)=a+b; sub(a,b)=a-b; mul(a,b)=a*b; div(a,b)=a/b,输出它们的值。
a b 都是整数

格式
输入格式
  输入数据有多组。
  第一行输入n,接下来n行输入n个函数。(保证是单层运算,不会出现如:add(add(a,b),c)这样的情况)

输出格式
  顺序输出对应函数的值。
 每个输出占一行。
 除法运算中,除数为0时输出“error”,得到的商不是整数的保留两位小数。

样例
样例输入 
6
add(1,5)
mul(7,8)
div(5,3)
sub(1,2)
sub(3,4)
add(11,1)
样例输出 
6
56
1.67
-1
-1
12

代码如下:

#include <stdio.h>
#include <string.h>
double add(double a,double b)
{
    return (a+b); 
}
double sub(double a,double b)
{
    return (a-b);
}
double mul(double a,double b)
{
    return a*b;
}
double div(double a,double b)
{
    return a/b;
};

int main()
{
    int i,n;
    char c1,c2;
    double a,b,t;
    scanf("%d",&n);
    getchar();
    for (i=0;i<n;i++)
    {
        scanf("%c%c%c%c%lf,%lf%c",&c1,&c2,&c2,&c2,&a,&b,&c2);
        getchar();
        if(c1 == 'a')
        {
            t = add(a,b);
            if(t == (int)t)
                printf("%d\n",(int)t);
            else
                printf("%.2lf\n",t);
        }
        else if(c1 == 'm')
        {
            t = mul(a,b);
            if(t == (int)t)
                printf("%d\n",(int)t);
            else
                printf("%.2lf\n",t);
        }
        else if(c1 == 's')
        {
            t=sub(a,b);
            if(t == (int)t)
                printf("%d\n",(int)t);
            else
                printf("%.2lf\n",t);
        }
        else if (c1 == 'd')
        {
            if(b==0)
                printf("error\n");
            else
            {
                t = div(a,b);
                if(t == (int)t)
                    printf("%d\n",(int)t);
                else
                    printf("%.2lf\n",t);
            }
        }
    }
    return 0;
}


img


就是这个。

#include <stdio.h>
int main()
{
    char c[4] = {0};
    int a,b;
    double d = 0;
    scanf("%[^(](%d,%d)",c,&a,&b);
    if(strcmp(c,"add") == 0)
        printf("%d",a+b);
    else if(strcmp(c,"sub") == 0)
        printf("%d",a-b);
    if(strcmp(c,"mul") == 0)
        printf("%d",a*b);
    if(strcmp(c,"dev") == 0)
    {
        if(b==0)
            printf("error");
        else
        {
            d = a*1.0/b;
            if(d == (int)d)
                printf("%d",(int)d);
            else
                printf("%.2f",d);
        }
    }
    return 0;
}