四则运算

来源:互联网 发布:ios更新时网络中断 编辑:程序博客网 时间:2024/04/29 06:39

#include<stdio.h>

#include <string.h>

float  getNum()
{
float num;
scanf("%f",&num);
return num;
}
char getOpt()
{
return getchar();
}
float caculate(float op1 ,float op2 ,char opt)
{
if(opt=='+')return op1+op2;
if(opt=='-')return op1-op2;
if(opt=='*')return op1*op2;
if(opt=='/')return op1/op2;
return 0;
}
void main()
{
float op1,op2;
char opt;
op1 = getNum();
do
{
opt = getOpt();
if ( opt == '=' ) break;
op2 = getNum();
op1 = caculate(op1,op2,opt);
}while(op1!='');
printf("%f\n",op1);
}

0 0
原创粉丝点击