fix: do calcul in right order
This commit is contained in:
parent
a37a07615b
commit
481de18bbd
@ -16,10 +16,10 @@ int calculate(std::stack<int>& nums, std::stack<char>& operators)
|
|||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
int ret;
|
int ret;
|
||||||
int a = nums.top();
|
|
||||||
nums.pop();
|
|
||||||
int b = nums.top();
|
int b = nums.top();
|
||||||
nums.pop();
|
nums.pop();
|
||||||
|
int a = nums.top();
|
||||||
|
nums.pop();
|
||||||
|
|
||||||
switch (operators.top()) {
|
switch (operators.top()) {
|
||||||
case '+':
|
case '+':
|
||||||
@ -60,18 +60,20 @@ int main(int ac, char** av)
|
|||||||
if (std::isdigit(av[1][i]))
|
if (std::isdigit(av[1][i]))
|
||||||
nums.push(av[1][i] - '0');
|
nums.push(av[1][i] - '0');
|
||||||
else if (std::strchr("/+-*", av[1][i]) != NULL)
|
else if (std::strchr("/+-*", av[1][i]) != NULL)
|
||||||
operators.push(av[1][i]);
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
std::cout << "error: '" << av[1][i] << "' unknown char" << std::endl;
|
operators.push(av[1][i]);
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
if (calculate(nums, operators))
|
if (calculate(nums, operators))
|
||||||
{
|
{
|
||||||
std::cout << "error: division by 0" << std::endl;
|
std::cout << "error: division by 0" << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::cout << "error: '" << av[1][i] << "' unknown char" << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
if (nums.size() != 1)
|
if (nums.size() != 1)
|
||||||
{
|
{
|
||||||
std::cout << "error: too many numbers" << std::endl;
|
std::cout << "error: too many numbers" << std::endl;
|
||||||
@ -79,7 +81,7 @@ int main(int ac, char** av)
|
|||||||
}
|
}
|
||||||
if (operators.size() != 0)
|
if (operators.size() != 0)
|
||||||
{
|
{
|
||||||
std::cout << "error: too manu operators" << std::endl;
|
std::cout << "error: too many operators" << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
std::cout << nums.top();
|
std::cout << nums.top();
|
||||||
|
Loading…
Reference in New Issue
Block a user