Created
March 7, 2019 21:26
-
-
Save hazelweakly/140ba798e2a2ef56e757e8892b907085 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "arithmetic_utilities.h" | |
#include <cstdlib> | |
// Honestly kinda not seeing why most of these helper functions are a thing if | |
// they're all one liners. Most C++ codebases avoid that since the overhead of | |
// calling functions is huge compared to just writing the oneliner. | |
/** | |
* convert char array into a number using the atoi | |
* @param char* arr | |
* @return int | |
*/ | |
int Helper::getNum(char* arr){ | |
int num = atoi(arr); | |
return num; | |
} | |
/** | |
* Checks to see if the current char is a valid Digit or not | |
* @param char c | |
* @return bool | |
*/ | |
bool Helper::isValidDig(char c){ | |
return (c >= '0' && c <= '9') | |
} | |
/** | |
* | |
* The only time that we will have a negative number when parsing + and * is when | |
* - is the first element after +, * or (, otherwise it will be negative | |
*/ | |
bool Helper::isNeg(std::vector<char>& vec, int pos){ // why pos instead of idx? | |
// Many programmers stylistically prefer to do early returns to keep indenting levels low | |
if (vec[pos] != '-') { | |
return false; | |
} | |
//Check to see if we are looking at the first element of the char array | |
// (less than equal instead of == to make sure you can't pass in a negative number?) | |
if(pos <= 0) { | |
return true; | |
} | |
auto token = vec[pos-1]; | |
return (token == '*' || token == '+' || token == '(' | |
} | |
//check to see that the section of the vector we are working is a multiplication | |
bool Helper::isMult(std::vector<char>& vec, int pos){ | |
return vec[pos] == '*'; | |
} | |
bool Helper::isAdd(std::vector<char>& vec, int pos){ | |
return vec[pos] == '+'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment