垃圾代码1

作者在 2011-08-29 12:56:03 发布以下内容
#include <stdio.h>
float calc(float x,char op,float y);
int main()
{
float num1=0,num2=0,num3=0,temp=0;
char op[2][2]={0};
printf("Input expression:" );
scanf("%f",&num1);

scanf("%c",&op[0][0]);
scanf("%f",&num2);
scanf("%c",&op[0][1]);
scanf("%f",&num3);

if(op[0][0]=='+'||op[0][0]=='-')op[1][0]='1';
if(op[0][0]=='*'||op[0][0]=='/')op[1][0]='0';
if(op[0][1]=='+'||op[0][1]=='-')op[1][1]='1';
if(op[0][1]=='*'||op[0][1]=='/')op[1][1]='0';
if(op[1][1]<op[1][0])
     {
         temp=calc(num2,op[0][1],num3);
         temp=calc(num1,op[0][0],temp);
        
     }
else
     {
         temp=calc(num1,op[0][0],num2);
         temp=calc(temp,op[0][1],num3);
        
     }
     printf("=%lf\n",temp);
return 0;    
}
float calc(float x,char op,float y)
{
    float temp=0;
    
    switch(op)
    {
        case '+':
         temp=x+y;
         break;
         case '-':
         temp=x-y;
         break;
         case '*':
         temp=x*y;
         break;
         case '/':
         if(y==0)//这里需要防除数为0,没有给出真正的解决方案!!!
         {
         printf("error!");
         temp=0xfffff;
         }
         else
         temp=x/y;
         break;
    }

    return temp;
}

运行效果:
Input expression:34-23*2
=-12.000000
请按任意键继续. . .
运行效果:
Input expression:3*2-17
=-11.000000
请按任意键继续. . .
 
无用代码 | 阅读 740 次
文章评论,共0条
游客请输入验证码
浏览1313次
文章归档
最新评论