算数运算求解(命令行参数)

img

#include <stdio.h>
#include <stdlib.h>

int main(int argc,char *argv[])
{
    float x,y;
    char ch;
    float result;
    if(argc !=4 )
    {
        printf("Input error,end.\n");
        exit(1);
    }

    x=atof(argv[1]);
    y=atof(argv[3]);
    ch=argv[2][0];
    switch (ch)
    {
    case '+':
        result = x+y;
        break;
    case '-':
        result = x-y;
        break;
    case '*':
        result = x*y;
        break;
    case '/':
        if(y==0)
        {
            printf("ERROR:Zero donominstor.\n");
            exit(-1);
        }
        result = x/y;
        break;    
    default:
        printf("ERROR:Operator over bound.\n");
        exit(-2);
    }
    printf("%f%c%f=%f\n",x,ch,y,result);

    return 0;
}