实现输入两个实数和一个四则运算符(+,―,*,/)
#include <stdio.h>
void main()
{
double x,y,result;
char op;
int flag;
printf("Input x,y,op:\n");
scanf("%lf%lf%c",&x,&y,&op);
flag=1;
switch (op)
{
case '+' :result=x+y;break;
case '-' :result=x-y;break;
case '*' :result=x*y;break;
case '/' :
if (y!=0)
result=x/y;
else
flag=0;
break;
default: flag=1;
break;
printf("result=%f\n",result);
else
printf("invalid input y or op\n");
}
#include <stdio.h>
void main()
{
int i,j,k,1=1;
for(i=1;i<50;i++)
for(j=0;j<50;j++)
{k=result;
if(flag=1)
{
printf("%2d%2d%2d",i,j,k);
break;
if(1%5==0)printf("\n");
}
}
}
----------------解决方案--------------------------------------------------------
#include"stdio.h"
#include"stdlib.h"
void main()
{
double x,y,result;
char op;
int flag;
printf("Input x,y,op:\n");
scanf("%lf",&x);
fflush(stdin);
scanf("%lf",&y);
fflush(stdin);
scanf("%c",&op);
fflush(stdin);
flag=1;
switch (op)
{
case '+' :result=x+y;break;
case '-' :result=x-y;break;
case '*' :result=x*y;break;
case '/' :
if (y!=0)
{
result=x/y;
}
else
{
flag=0;
} break;
default: flag=0;
}
if(flag==1)
printf("result=%f\n",result);
else
printf("invalid input y or op\n");
}
----------------解决方案--------------------------------------------------------