#include <stdio.h>
#include <stdlib.h>
char symbol[] = {'+', '-', '*', '/', '='};
enum {
Add = 0,
Dec,
Mul,
Div,
Equ,
Max
};
int Judge(char c)
{
switch (c)
{
case '+':
return Add;
case '-':
return Dec;
case '*':
return Mul;
case '/':
return Div;
case '=':
return Equ;
default:
return -1;
}
}
double Calculate(double a, double b, int flag)
{
switch (flag)
{
case Add:
return a+b;
case Dec:
return a-b;
case Mul:
return a*b;
case Div:
return a/b;
default:
printf("Error!\n");
exit(1);
}
}
int main(void)
{
double a, b, answer;
char c;
int flag;
printf("Input format: a <symbol(+,-,*,/)> b <enter>\n");
while (1)
{
printf(">>> ");
scanf("%lf", &a);
while ((flag = Judge(c = getchar())) == -1);
scanf("%lf", &b);
while ((c = getchar()) != '\n');
answer = Calculate(a, b, flag);
printf(">>> %lf\n", answer);
}
}
随手写了个,功能不是很强,没有加纠错