当前位置: 代码迷 >> C语言 >> 我写的简单计算器代码,请高手看看不好之处!谢谢了!
  详细解决方案

我写的简单计算器代码,请高手看看不好之处!谢谢了!

热度:188   发布时间:2008-04-16 21:05:54.0
我写的简单计算器代码,请高手看看不好之处!谢谢了!
void main()
{
    double operand1,operand2,result;
    char ch;
    printf("请输入计算式:\n");
    do
    {
        scanf("%lf%c",&operand1,&ch);
        while(ch!='=')
        {
            scanf("%lf",&operand2);
            switch(ch)
            {
            case '+':
                result=operand1+operand2;
                operand1=result;
                scanf("%c",&ch);
                break;
            case '-':
                result=operand1-operand2;
                operand1=result;
                scanf("%c",&ch);
                break;
            case '*':
                result=operand1*operand2;
                operand1=result;
                scanf("%c",&ch);
                break;
            case '/':
                if(operand2!=0)
                     {
                         result=operand1/operand2;
                           operand1=result;
                        scanf("%c",&ch);
                     }
                    else
                         ch='=';    
                 break;
            case '%':
                if(operand2!=0)
                 {
                         result=(int)operand1%(int)operand2;
                        operand1=result;
                        scanf("%c",&ch);
                 }
                    else
                         ch='=';    
                break;
            default :
                printf("你的运算符错误,请重新编译输入计算式:\n");
                
                break;

            }
        }
        if(operand2!=0)
            printf("%f\n",result);
        else
            printf("运算数为零,请重新编译输入计算式:\n");
    
    }while(operand2!=0);
    
    
}
搜索更多相关的解决方案: 计算器  代码  

----------------解决方案--------------------------------------------------------
  相关解决方案