用C++设计一个简单的计算器1、设计一个简单的计算器类(类型),要求:(1) 从键盘读入算式.(2) 可以进行加、减、乘

1个回答

  • #include

    int main(void)

    {

    int data1,data2;

    char op;

    printf("please input data1 op data2:");

    scanf("%d %c %d",&data1 ,&op ,&data2);

    switch (op)

    {

    case '+':

    printf("%d + %d = %.0fn",data1,data2,(double)data1 + (double)data2);

    break;

    case '-':

    printf("%d - %d = %.0fn",data1,data2,(double)data1 - (double)data2);

    break;

    case '*':

    printf("%d * %d = %.0fn",data1,data2,(double)data1 * (double)data2);

    break;

    case '/':

    if (data2 == 0)

    {

    printf("Error!chu shu wei 0.");

    }

    else

    {

    printf("%d / %d = %.0fn",data1,data2,(double)data1 / (double)data2);

    }

    break;

    case '%':

    if (data2 == 0)

    {

    printf("Error!chu shu wei 0.");

    }

    else

    {

    printf("%d %% %d=%d3n",data1,data2,data1 % data2);

    }

    break;

    default:

    printf("Unknown operatorn");

    }

    return 0;

    }如果要求是浮点数的话换一下数据类型就可以了