Skip to content

Commit

Permalink
Reformating for style consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
eChadwick committed Oct 16, 2017
1 parent 9578b90 commit 11cfe39
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 74 deletions.
4 changes: 1 addition & 3 deletions Parse Infix/ExpressionException.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#pragma once
#include <string>
#include <sstream>
#include <stdexcept>

// A wrapper class around standard exception. Adds the index of the error to the error message.
class ExpressionException : public std::runtime_error
{
class ExpressionException : public std::runtime_error {
public:
// Constructor for expression_exception
// @param index is the index in the string where the error was detected.
Expand Down
71 changes: 23 additions & 48 deletions Parse Infix/InfixEvaluator.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
#include "InfixEvaluator.h"

void InfixEvaluator::eval_stack(int precedence)
{

void InfixEvaluator::eval_stack(int precedence) {
if (operators.top() == ")") {
operators.pop();
while (!operators.empty() && operators.top() != "("){
while (!operators.empty() && operators.top() != "(") {
eval_operator(operators.top());
operators.pop();
}
if (operators.empty())
cout << "Unbalanced parentheses";
throw runtime_error("Error in expression: Unbalanced parentheses, missing an opening parenthesis");
if (operators.top() == "(")
operators.pop();
}
else {

while (!operators.empty())
{
} else {
while (!operators.empty()) {
if (operators.top() == "(")
if (precedence == 0)
throw ExpressionException(0, "");
throw runtime_error("Error in expression: Unbalanced parentheses, missing a closing parenthesis");
else
break;
else if (precedence > precedences.at(operators.top()))
Expand All @@ -32,17 +27,15 @@ void InfixEvaluator::eval_stack(int precedence)
}
}

void InfixEvaluator::eval_operator(string op)
{
void InfixEvaluator::eval_operator(string op) {
int right = operands.top();
operands.pop();

if (op == "!") operands.push(!right);
else if (op == "++") operands.push(right + 1);
else if (op == "--") operands.push(right - 1);
else if (op == "-1") operands.push(right * -1);
else
{
else {
int left = operands.top();
operands.pop();

Expand All @@ -63,23 +56,18 @@ void InfixEvaluator::eval_operator(string op)
}
}

int InfixEvaluator::evaluate(string input)
{
int InfixEvaluator::evaluate(string input) {
string::iterator iter = input.begin();
unsigned int index = 0;
while (iter != input.end())
{
// Unary Search
while (iter != input.end()) {
unsigned int start = index;
unsigned int length = 0;
char token = '?';
while (!isdigit(*iter))
{
while (!isdigit(*iter)) {
if (*iter == ')')
throw ExpressionException(index, "Unexpected closing parenthesis");

if (*iter == '(')
{
if (*iter == '(') {
operators.push("(");
++iter;
++index;
Expand All @@ -89,8 +77,7 @@ int InfixEvaluator::evaluate(string input)
}

if (length == 0)
switch (*iter)
{
switch (*iter) {
case '+': case '-': case '!':
token = *iter;
length = 1;
Expand All @@ -103,14 +90,11 @@ int InfixEvaluator::evaluate(string input)

if (*(iter + 1) == token)
++length;
else
{
else {
if (length % 2)
switch (token)
{
switch (token) {
case '-':
if (isdigit(*(iter + 1)) || *(iter + 1) == '(')
{
if (isdigit(*(iter + 1)) || *(iter + 1) == '(') {
operators.push("-1");
length--;
break;
Expand All @@ -125,12 +109,10 @@ int InfixEvaluator::evaluate(string input)
operators.push("!");
}
if (!(length % 2))
if (token == '!')
{
if (token == '!') {
operators.push("!");
operators.push("!");
}
else
} else
for (int i = 0; i < length / 2; i++)
operators.push(string(new char[3] { token, token, '\0' }));

Expand All @@ -144,24 +126,19 @@ int InfixEvaluator::evaluate(string input)
}

string s_operand;
while (iter != input.end() && isdigit(*iter))
{
while (iter != input.end() && isdigit(*iter)) {
s_operand += *iter;
++iter;
++index;
}

operands.push(stoul(s_operand));

while (iter != input.end())
{
if (*iter == ' ')
{
while (iter != input.end()) {
if (*iter == ' ') {
++iter;
++index;
}
else if (*iter == ')')
{
} else if (*iter == ')') {
operators.push(")");
eval_stack(0);
++iter;
Expand All @@ -175,9 +152,7 @@ int InfixEvaluator::evaluate(string input)
break;
}

// The only valid place to end evaluation -- after an operand.
if (iter == input.end())
{
if (iter == input.end()) {
if (!operators.empty())
eval_stack(0);

Expand Down
1 change: 0 additions & 1 deletion Parse Infix/InfixEvaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <string>
#include <stack>
#include <map>
#include <iostream>
using namespace std;

// A class for evaluating an infix expression and returning its value as an integer.
Expand Down
3 changes: 0 additions & 3 deletions Parse Infix/Parse Infix.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,6 @@
<ClInclude Include="ExpressionException.h" />
<ClInclude Include="InfixEvaluator.h" />
</ItemGroup>
<ItemGroup>
<Text Include="thoughts for consideration.txt" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
Expand Down
5 changes: 0 additions & 5 deletions Parse Infix/Parse Infix.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,4 @@
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Text Include="thoughts for consideration.txt">
<Filter>Resource Files</Filter>
</Text>
</ItemGroup>
</Project>
19 changes: 5 additions & 14 deletions Parse Infix/Source.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
#include <cstdlib>
#include <iostream>
#include "InfixEvaluator.h"
using namespace std;

void main() {
InfixEvaluator test;
try
{
cout << endl << test.evaluate("10-! !! ++ ++ ---- 5");
cout << endl << test.evaluate("5*7+5-2/2+10+++7");
cout << endl << test.evaluate("(4==5==0==1)*80==80");
cout << endl << test.evaluate("(--(7^(2+1))+(!!!(!!0))*4)");
cout << endl << test.evaluate("(3>2)||(2==1)&&1");
cout << endl << test.evaluate("(!(3^2)&&!0)>=1");
cout << endl << test.evaluate("1+!!!!!((((((((0))))))))*7^10");
cout << endl << test.evaluate("-(72-(-5))<5||0&&!1");
try {
//The string to evaluate. Insert your string between the quotes.
string INPUT_STRING = "";
int output_value = test.evaluate(INPUT_STRING);
}
catch(ExpressionException ee)
{
catch(ExpressionException ee) {
cout << ee.what();
}
getchar();
}

0 comments on commit 11cfe39

Please sign in to comment.