Skip to content

Commit

Permalink
add eval_unaries function
Browse files Browse the repository at this point in the history
  • Loading branch information
User authored and User committed Oct 10, 2017
1 parent cdaaa71 commit f9c8e14
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,4 @@ ModelManifest.xml

# FAKE - F# Make
.fake/
/Parse Infix/thoughts for consideration.txt
7 changes: 3 additions & 4 deletions Parse Infix/Parse Infix.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,13 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="infix_evaluator.cpp" />
<ClCompile Include="infix_to_postfix.cpp" />
<ClCompile Include="postfix_evaluator.cpp" />
<ClCompile Include="Source.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="infix_evaluator.h" />
<ClInclude Include="infix_to_postfix.h" />
<ClInclude Include="postfix_evaluator.h" />
</ItemGroup>
<ItemGroup>
<Text Include="thoughts for consideration.txt" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
17 changes: 5 additions & 12 deletions Parse Infix/Parse Infix.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,18 @@
<ClCompile Include="Source.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="postfix_evaluator.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="infix_to_postfix.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="infix_evaluator.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="infix_to_postfix.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="postfix_evaluator.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="infix_evaluator.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Text Include="thoughts for consideration.txt">
<Filter>Resource Files</Filter>
</Text>
</ItemGroup>
</Project>
6 changes: 5 additions & 1 deletion Parse Infix/Source.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#include <cstdlib>
#include <iostream>
#include "infix_evaluator.h"
using namespace std;

void main() {

infix_evaluator test;
string test_string = "+!!----";
test.eval_unaries(0, 7, 5, test_string);
getchar();
}
45 changes: 45 additions & 0 deletions Parse Infix/infix_evaluator.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,46 @@
#include "infix_evaluator.h"

void infix_evaluator::eval_stack(int precedence)
{
}
//infix_string&, int start, length, operand. Can strip spaces
int infix_evaluator::eval_unaries(int start, int length, int operand, string& infix_string)
{
if (length == 0)
return operand;
else {
int index = start + length - 1;
char token = infix_string[index];
int count = 1;
while (count < length && infix_string[index] == infix_string[index -1]) {
count++;
index--;
}
if (token == '!') {
if (count % 2 == 0)
eval_unaries(start, length - count, !!operand, infix_string);
else
eval_unaries(start, length - count, !operand, infix_string);
}else if (token == '+') {
if (count % 2 == 0) {
for (int i = 0; i < count / 2; i++) {
operand++;
}
eval_unaries(start, length - count, operand, infix_string);
}
else {
cout << "Binary operator with single operand at index " << index;
}
}else if (token == '-') {
if (count % 2 == 0) {
for (int i = 0; i < count / 2; i++) {
operand--;
}
eval_unaries(start, length - count, operand, infix_string);
}
else {
cout << "Binary operator with single operand at index " << index;
}
}
}
}
18 changes: 10 additions & 8 deletions Parse Infix/infix_evaluator.h
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
#pragma once

#include "infix_to_postfix.h"
#include<string>
#include<stack>
#include<iostream>
using namespace std;

class infix_to_postfix {
class infix_evaluator {
public:
// @param infix_string is the infix expression to evaluate.
// @return is the value of the string as a single integer
int eval(string infix_string);
private:
//private:
stack<int>operators;
stack<char>operands;

// evaluates the stacks as far as possible and pushes the result to operators stack.
// @param precedence is the precedence of the next operator to be added to the stack.
// evaluates the stacks as far as possible and pushes the result to operand stack.
// @param precedence is the precedence of the next operator to be added to the operator stack.
void eval_stack(int precedence);

// evaluates one or many repeated +, -, or ! in a substring of infix_string
// @param start is the first index of the substring
// evaluates one or many repeated +, -, or ! in a substring of infix_string. Whitespace between
// consecutive operators is ignored.
// @param start is an interator to the first index of the substring in the infix_string
// @param length is the length of the substring
// @param operand is the operand which the unary operators will be applied to
// @param operator_string is the string of one or many repeated + and/or - and/or !
// @return is the value of operand after unary operations have been applied.
int eval_unaries(int start, int length, int operand);
int eval_unaries(int start, int length, int operand, string& infix_string);
};
1 change: 0 additions & 1 deletion Parse Infix/infix_to_postfix.cpp

This file was deleted.

1 change: 0 additions & 1 deletion Parse Infix/infix_to_postfix.h

This file was deleted.

1 change: 0 additions & 1 deletion Parse Infix/postfix_evaluator.cpp

This file was deleted.

1 change: 0 additions & 1 deletion Parse Infix/postfix_evaluator.h

This file was deleted.

0 comments on commit f9c8e14

Please sign in to comment.