#include <bits/stdc++.h>
using namespace std;
bool isExp(char c)
{
if (c >= '0' && c <= '9')
{
return true;
}
return false;
}
int charToInt(char c) { return (c - '0'); }
int evaluate(char *exp)
{
if (*exp == '\0')
return -1;
int ans = charToInt(exp[0]);
for (int i = 1; exp[i]; i += 2)
{
char opr = exp[i], opd = exp[i + 1];
if (!isExp(opd))
return -1;
if (opr == '+')
ans += charToInt(opd);
else if (opr == '-')
ans -= charToInt(opd);
else if (opr == '*')
ans *= charToInt(opd);
else if (opr == '/')
ans /= charToInt(opd);
else
return -1;
}
return ans;
}
Comments
Leave a comment