#include
#include
#include
#include
using namespace std;
bool IsNumber(char str)//判断输入的x,y是不是符合要求的数
{
int nLen = strlen(str);
int k = 0;
int nCount = 0;//记录小数点个数
for (int i = 0; i
{
if ((str[i] > 47 && str[i] < 58) || str[i] == '.')
{
if (str[i] > 47 && str[i] < 58)
{
k = k + 1;
}
if (str[i] == '.'&&nCount == 0)
{
k = k + 1;
nCount++;
}
}
}
if (k == nLen)
{
return true;
}
else
{
return false;
}
}
void check(char input, double&x, double&y, int&op, char&err)//输出等式
{
if (input == '+')
op = 1;
if (input == '-')
op = 2;
if (input == '*')
op = 3;
if (input == '/')
op = 4;
if (input != '+'&input != '-'&input != '*'&input != '/')
op = -3;
switch (op)
{
case 1:
cout << x + y << endl;
break;
case 2:
cout << x - y << endl;
break;
case 3:
cout << x*y << endl;
break;
case 4:
cout << x / y << endl;
break;
case -3:
cout << err << endl;
}
}
int main()
{
double x, y;
int c, d,op;
char input;
char*err;
err = "操作符错误";
cin >> x;
cin >> input;
cin >> y;
char str1[100], str2[100];
sprintf(str1, "%d", x);//将x,y转化为字符串,输入检查数组
sprintf(str2, "%d", y);
c = IsNumber(str1);
cout << c << endl;
d = IsNumber(str2);
cout << d << endl;
if (c != 1 && d == 1)
{
cout << "左操作数错误" << endl;
}
if (c == 1 && d != 1)
{
cout << "右操作数错误" << endl;
}
if (c == 0 && d == 0)
{
cout << "左右操作数都错误" << endl;
}
if (c == 1 && d == 1)
{
check(input, x, y, op, err);
}
system("pause");
}