From b85bfcd5b6a78967c512cb46761f84b4521dc6e7 Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Tue, 3 Jan 2017 19:47:32 +0900 Subject: [PATCH 01/52] Add calculator class --- calculator.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ mathcalc.cpp | 2 ++ types.h | 8 -------- 3 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 calculator.cpp diff --git a/calculator.cpp b/calculator.cpp new file mode 100644 index 0000000..0f548a9 --- /dev/null +++ b/calculator.cpp @@ -0,0 +1,44 @@ +#include +#include +#include "types.h" + +struct PROGRESSION_FLAGS { + list expressions; + list::iterator it; + bool exists_mul_and_div; + bool exists_pls_and_mns; + bool go_through; +}; + +class Calculator { + private: + struct PROGRESSION_FLAGS pflags; + + public: + void Calculator(); + void ~Calculator(); + + void buildExpressions(); + void processBrackets(); + void execCalculation(); +}; + +void Calculator::Calculator(string expression) { + +} + +void Calculator::~Calculater() { + +} + +void Calculator::buildExpressions() { + +} + +void Calculator::processBrackets() { + +} + +void Calculator::execCalculation() { + +} diff --git a/mathcalc.cpp b/mathcalc.cpp index bd8804c..c551c61 100644 --- a/mathcalc.cpp +++ b/mathcalc.cpp @@ -12,6 +12,8 @@ using namespace std; #include "bracket.h" #include "calcurate.h" +#include "calculator.cpp" + int main(int argc, char *argv[]) { struct PROGRESSION_FLAGS pflags; diff --git a/types.h b/types.h index f2d65b9..c83830a 100644 --- a/types.h +++ b/types.h @@ -6,12 +6,4 @@ struct EXP_DIVIDER_RESULT { int right_value; }; -struct PROGRESSION_FLAGS { - list expressions; - list::iterator it; - bool exists_mul_and_div; - bool exists_pls_and_mns; - bool go_through; -}; - #endif From 0b467857fa51ad318ea3f382b6ff85c4060697a6 Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Tue, 3 Jan 2017 22:33:02 +0900 Subject: [PATCH 02/52] Modify Makefile --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 58f54e8..41e4ddb 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ DBG_OPTS = -O0 -g3 -Wall -SOURCE = mathcalc.cpp +SOURCE = mathcalc.cpp calculator.cpp TARGET = mathcalc mathcalc: $(SOURCE) From 681d929adaba8d87c8a6b1c6855187fc24bb3731 Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Tue, 3 Jan 2017 22:52:30 +0900 Subject: [PATCH 03/52] Revert --- types.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/types.h b/types.h index c83830a..f2d65b9 100644 --- a/types.h +++ b/types.h @@ -6,4 +6,12 @@ struct EXP_DIVIDER_RESULT { int right_value; }; +struct PROGRESSION_FLAGS { + list expressions; + list::iterator it; + bool exists_mul_and_div; + bool exists_pls_and_mns; + bool go_through; +}; + #endif From 735222f23b178e57b7d50ee90f79ab429206934b Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Tue, 3 Jan 2017 22:56:48 +0900 Subject: [PATCH 04/52] Add namespace prefix --- types.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types.h b/types.h index f2d65b9..8ebf3a4 100644 --- a/types.h +++ b/types.h @@ -7,8 +7,8 @@ struct EXP_DIVIDER_RESULT { }; struct PROGRESSION_FLAGS { - list expressions; - list::iterator it; + std::list expressions; + std::list::iterator it; bool exists_mul_and_div; bool exists_pls_and_mns; bool go_through; From 457d1f5d7c8938375c579ba64afb5557285d4560 Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Tue, 3 Jan 2017 22:57:02 +0900 Subject: [PATCH 05/52] Add calculator.h --- calculator.cpp | 37 ++++++++----------------------------- calculator.h | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 29 deletions(-) create mode 100644 calculator.h diff --git a/calculator.cpp b/calculator.cpp index 0f548a9..0d49919 100644 --- a/calculator.cpp +++ b/calculator.cpp @@ -1,44 +1,23 @@ #include #include -#include "types.h" - -struct PROGRESSION_FLAGS { - list expressions; - list::iterator it; - bool exists_mul_and_div; - bool exists_pls_and_mns; - bool go_through; -}; - -class Calculator { - private: - struct PROGRESSION_FLAGS pflags; - - public: - void Calculator(); - void ~Calculator(); - - void buildExpressions(); - void processBrackets(); - void execCalculation(); -}; - -void Calculator::Calculator(string expression) { +#include "calculator.h" +Calculator::Calculator(std::string expression) { + return; } -void Calculator::~Calculater() { - +Calculator::~Calculator() { + return; } void Calculator::buildExpressions() { - + return; } void Calculator::processBrackets() { - + return; } void Calculator::execCalculation() { - + return; } diff --git a/calculator.h b/calculator.h new file mode 100644 index 0000000..0eca918 --- /dev/null +++ b/calculator.h @@ -0,0 +1,22 @@ +#ifndef __CALCULATOR_H__ +#define __CALCULATOR_H__ + +#include +#include +#include "types.h" + +class Calculator { + private: + struct PROGRESSION_FLAGS pflags; + + public: + Calculator(std::string expressions); + ~Calculator(); + + void buildExpressions(); + void processBrackets(); + void execCalculation(); +}; + +#endif // __CALCULATOR_H__ + From 735c54d626dcb2c929ced9c53d0a897e68520e9c Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Tue, 3 Jan 2017 22:58:33 +0900 Subject: [PATCH 06/52] Remove a duplicated -Wall option --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 41e4ddb..7e3afa7 100644 --- a/Makefile +++ b/Makefile @@ -14,4 +14,4 @@ run: $(TARGET) ./mathcalc release: mathcalc.cpp - g++ -Wall -o $(TARGET) $(SOURCE) + g++ -o $(TARGET) $(SOURCE) From 9c726e3f650e1a6fcb707d2ac067e04140dcf2c3 Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Tue, 3 Jan 2017 22:59:11 +0900 Subject: [PATCH 07/52] Modify --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 7e3afa7..e84023d 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ SOURCE = mathcalc.cpp calculator.cpp TARGET = mathcalc mathcalc: $(SOURCE) - g++ $(DBG_OPTS) -Wall -o $(TARGET) $(SOURCE) + g++ $(DBG_OPTS) -o $(TARGET) $(SOURCE) .PHONY: clean run debug release clean: From eaa79ea3ab8edca15f607ac30191bf2523553334 Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Tue, 3 Jan 2017 23:00:47 +0900 Subject: [PATCH 08/52] Fix --- mathcalc.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mathcalc.cpp b/mathcalc.cpp index c551c61..e329a92 100644 --- a/mathcalc.cpp +++ b/mathcalc.cpp @@ -11,8 +11,7 @@ using namespace std; #include "utils.h" #include "bracket.h" #include "calcurate.h" - -#include "calculator.cpp" +#include "calculator.h" int main(int argc, char *argv[]) { From 3776023c3ef17967728159891d9988ecba96414b Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Tue, 3 Jan 2017 23:08:08 +0900 Subject: [PATCH 09/52] Create Calculator instance --- mathcalc.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/mathcalc.cpp b/mathcalc.cpp index e329a92..931b73d 100644 --- a/mathcalc.cpp +++ b/mathcalc.cpp @@ -15,14 +15,15 @@ using namespace std; int main(int argc, char *argv[]) { - struct PROGRESSION_FLAGS pflags; - string expressions; - if (argc == 1) { cout << "usage: mathcalc [expression]" << endl; - return 0; + return 1; } + struct PROGRESSION_FLAGS pflags; + string expressions = argv[1]; + Calculator calculator(expressions); + // Build expression array from string given from command-line expressions = argv[1]; build_expressions(expressions, &pflags); From afe68d8c5c15b20fb7da69120c0e8bea0d01e8ce Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Wed, 4 Jan 2017 02:37:41 +0900 Subject: [PATCH 10/52] Implement StringExpression class --- Makefile | 2 +- calculator.cpp | 26 ++++++++++++- calculator.h | 9 ++++- mathcalc.cpp | 9 ++++- stringExpression.cpp | 87 ++++++++++++++++++++++++++++++++++++++++++++ stringExpression.h | 34 +++++++++++++++++ types.h | 24 ++++++++++++ 7 files changed, 184 insertions(+), 7 deletions(-) create mode 100644 stringExpression.cpp create mode 100644 stringExpression.h diff --git a/Makefile b/Makefile index e84023d..308f129 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ DBG_OPTS = -O0 -g3 -Wall -SOURCE = mathcalc.cpp calculator.cpp +SOURCE = mathcalc.cpp calculator.cpp stringExpression.cpp TARGET = mathcalc mathcalc: $(SOURCE) diff --git a/calculator.cpp b/calculator.cpp index 0d49919..c97d78a 100644 --- a/calculator.cpp +++ b/calculator.cpp @@ -1,8 +1,12 @@ #include #include +#include +#include #include "calculator.h" +#include "stringExpression.h" -Calculator::Calculator(std::string expression) { +Calculator::Calculator(std::string expressions) { + this->stringExpressions = expressions; return; } @@ -10,7 +14,25 @@ Calculator::~Calculator() { return; } -void Calculator::buildExpressions() { +void Calculator::parseStringExpressions() { + StringExpression stringExpression(this->stringExpressions); + + while (1) { + stringExpression.begin(); + stringExpression.parseNumberValue(); + if (stringExpression.hasNextExpression()) { + stringExpression.parseExpression(); + stringExpression.next(); + } else { + break; + } + } + + this->expressionList = stringExpression.getExpressionsList(); + return; +} + +void Calculator::printExpressions() { return; } diff --git a/calculator.h b/calculator.h index 0eca918..aa2c0c4 100644 --- a/calculator.h +++ b/calculator.h @@ -7,13 +7,18 @@ class Calculator { private: - struct PROGRESSION_FLAGS pflags; + // struct PROGRESSION_FLAGS pflags; + + std::string stringExpressions; + struct ExpressionList expressionList; public: Calculator(std::string expressions); ~Calculator(); - void buildExpressions(); + void parseStringExpressions(); + void printExpressions(); + void processBrackets(); void execCalculation(); }; diff --git a/mathcalc.cpp b/mathcalc.cpp index 931b73d..cd51ba1 100644 --- a/mathcalc.cpp +++ b/mathcalc.cpp @@ -20,10 +20,14 @@ int main(int argc, char *argv[]) return 1; } - struct PROGRESSION_FLAGS pflags; + // struct PROGRESSION_FLAGS pflags; string expressions = argv[1]; - Calculator calculator(expressions); + Calculator calc(expressions); + calc.parseStringExpressions(); + calc.printExpressions(); + + /* // Build expression array from string given from command-line expressions = argv[1]; build_expressions(expressions, &pflags); @@ -33,6 +37,7 @@ int main(int argc, char *argv[]) // Last calcuration after cleaning up of brackets cout << calcurate(pflags.expressions.begin(), pflags.expressions.end()) << endl; + */ return 0; } diff --git a/stringExpression.cpp b/stringExpression.cpp new file mode 100644 index 0000000..30d042b --- /dev/null +++ b/stringExpression.cpp @@ -0,0 +1,87 @@ +#include +#include +#include +#include "stringExpression.h" +#include "types.h" + +void StringExpression::setSign(std::string sign) { + this->expressionSigns.push_back(sign); +} + +void StringExpression::defineExpressionSings() { + this->setSign("+"); + this->setSign("-"); + this->setSign("*"); + this->setSign("/"); + this->setSign("("); + this->setSign(")"); +} + +EXPRESSION_TYPE StringExpression::getExpressionType(std::string sign) { + if (sign == "+") return PLUS; + else if (sign == "-") return MINUS; + else if (sign == "/") return DIVIDE; + else if (sign == "*") return MULTIPLE; + else if (sign == "(") return BRACKET_BEGIN; + else if (sign == ")") return BRACKET_END; + else return OTHER; +} + +std::string::size_type StringExpression::getNextExpressionPos() { + std::vector expressionPositionsIndex; + std::vector::iterator it = this->expressionSigns.begin(); + std::vector::iterator end = this->expressionSigns.end(); + + for (; it != end; it++) { + expressionPositionsIndex.push_back(this->expressions.find(*it, this->currentPos)); + } + + return *std::min_element(expressionPositionsIndex.begin(), expressionPositionsIndex.end()); +} + +StringExpression::StringExpression(std::string expressions) { + this->defineExpressionSings(); + this->expressions = expressions; + this->currentPos = 0; +} + +struct ExpressionList StringExpression::getExpressionsList() { + return this->expressionList; +} + +void StringExpression::begin() { + this->nextExpressionPos = this->getNextExpressionPos(); +} + +void StringExpression::next() { + this->currentPos = (nextExpressionPos + 1); +} + +bool StringExpression::hasNextExpression() { + nextExpressionPos = this->getNextExpressionPos(); + if (this->nextExpressionPos == std::string::npos) { + return false; + } else { + return true; + } +} + +void StringExpression::parseNumberValue() { + struct EXPRESSION expression; + std::string valuePart = + this->expressions.substr(this->currentPos, this->nextExpressionPos - this->currentPos); + + expression.type = VALUE; + expression.value = valuePart; + this->expressionList.expressions.push_back(expression); +} + +void StringExpression::parseExpression() { + struct EXPRESSION expression; + std::string expressionPart = this->expressions.substr(nextExpressionPos, 1); + + expression.type = this->getExpressionType(expressionPart); + expression.value = expressionPart; + this->expressionList.expressions.push_back(expression); +} + diff --git a/stringExpression.h b/stringExpression.h new file mode 100644 index 0000000..bfc3a12 --- /dev/null +++ b/stringExpression.h @@ -0,0 +1,34 @@ +#ifndef __STRING_EXPRESSION_H__ +#define __STRING_EXPRESSION_H__ + +#include +#include +#include "types.h" + +typedef std::string EXPRESSION_SIGN; + +class StringExpression { + private: + struct ExpressionList expressionList; + std::vector expressionSigns; + std::string::size_type currentPos; + std::string::size_type nextExpressionPos; + std::string expressions; + + void setSign(std::string sign); + void defineExpressionSings(); + EXPRESSION_TYPE getExpressionType(std::string sign); + std::string::size_type getNextExpressionPos(); + + public: + StringExpression(std::string expressions); + struct ExpressionList getExpressionsList(); + + bool hasNextExpression(); + void parseNumberValue(); + void parseExpression(); + void begin(); + void next(); +}; + +#endif diff --git a/types.h b/types.h index 8ebf3a4..1faf6a5 100644 --- a/types.h +++ b/types.h @@ -1,6 +1,30 @@ #ifndef TYPES_H #define TYPES_H +#include +#include + +enum EXPRESSION_TYPE { + PLUS = 0, + MINUS, + DIVIDE, + MULTIPLE, + BRACKET_BEGIN, + BRACKET_END, + + VALUE, + OTHER +}; + +struct EXPRESSION { + EXPRESSION_TYPE type; + std::string value; +}; + +struct ExpressionList { + std::list expressions; +}; + struct EXP_DIVIDER_RESULT { int left_value; int right_value; From 71cad096f7f8b0e657339cdc94e5b7319dfdc45e Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Wed, 4 Jan 2017 02:58:15 +0900 Subject: [PATCH 11/52] Lots of modifications --- stringExpression.cpp | 39 ++++++++++++++++++--------------------- stringExpression.h | 9 ++++----- types.h | 14 +++++++++++--- 3 files changed, 33 insertions(+), 29 deletions(-) diff --git a/stringExpression.cpp b/stringExpression.cpp index 30d042b..fe7644b 100644 --- a/stringExpression.cpp +++ b/stringExpression.cpp @@ -4,36 +4,33 @@ #include "stringExpression.h" #include "types.h" -void StringExpression::setSign(std::string sign) { - this->expressionSigns.push_back(sign); +struct SIGN StringExpression::makeSign(std::string sign, SIGN_TYPE type) { + struct SIGN expressionSign; + expressionSign.string = sign; + expressionSign.type = type; + return expressionSign; } -void StringExpression::defineExpressionSings() { - this->setSign("+"); - this->setSign("-"); - this->setSign("*"); - this->setSign("/"); - this->setSign("("); - this->setSign(")"); +void StringExpression::addSign(struct SIGN sign) { + this->expressionSigns.push_back(sign); } -EXPRESSION_TYPE StringExpression::getExpressionType(std::string sign) { - if (sign == "+") return PLUS; - else if (sign == "-") return MINUS; - else if (sign == "/") return DIVIDE; - else if (sign == "*") return MULTIPLE; - else if (sign == "(") return BRACKET_BEGIN; - else if (sign == ")") return BRACKET_END; - else return OTHER; +void StringExpression::defineExpressionSings() { + this->addSign(this->makeSign("+", PLUS)); + this->addSign(this->makeSign("-", MINUS)); + this->addSign(this->makeSign("*", MULTIPLY)); + this->addSign(this->makeSign("/", DIVIDE)); + this->addSign(this->makeSign("(", BRACKET_BEGIN)); + this->addSign(this->makeSign(")", BRACKET_END)); } std::string::size_type StringExpression::getNextExpressionPos() { std::vector expressionPositionsIndex; - std::vector::iterator it = this->expressionSigns.begin(); - std::vector::iterator end = this->expressionSigns.end(); + std::vector::iterator it = this->expressionSigns.begin(); + std::vector::iterator end = this->expressionSigns.end(); for (; it != end; it++) { - expressionPositionsIndex.push_back(this->expressions.find(*it, this->currentPos)); + expressionPositionsIndex.push_back(this->expressions.find(it->string, this->currentPos)); } return *std::min_element(expressionPositionsIndex.begin(), expressionPositionsIndex.end()); @@ -80,7 +77,7 @@ void StringExpression::parseExpression() { struct EXPRESSION expression; std::string expressionPart = this->expressions.substr(nextExpressionPos, 1); - expression.type = this->getExpressionType(expressionPart); + expression.type = SIGN; expression.value = expressionPart; this->expressionList.expressions.push_back(expression); } diff --git a/stringExpression.h b/stringExpression.h index bfc3a12..0bf6856 100644 --- a/stringExpression.h +++ b/stringExpression.h @@ -5,19 +5,18 @@ #include #include "types.h" -typedef std::string EXPRESSION_SIGN; - class StringExpression { private: struct ExpressionList expressionList; - std::vector expressionSigns; + std::vector expressionSigns; std::string::size_type currentPos; std::string::size_type nextExpressionPos; std::string expressions; - void setSign(std::string sign); + struct SIGN makeSign(std::string sign, SIGN_TYPE type); + void addSign(struct SIGN sign); void defineExpressionSings(); - EXPRESSION_TYPE getExpressionType(std::string sign); + SIGN_TYPE getExpressionType(std::string sign); std::string::size_type getNextExpressionPos(); public: diff --git a/types.h b/types.h index 1faf6a5..eb42f7c 100644 --- a/types.h +++ b/types.h @@ -4,16 +4,24 @@ #include #include -enum EXPRESSION_TYPE { +enum SIGN_TYPE { PLUS = 0, MINUS, DIVIDE, - MULTIPLE, + MULTIPLY, BRACKET_BEGIN, BRACKET_END, + OTHER +}; +enum EXPRESSION_TYPE { VALUE, - OTHER + SIGN +}; + +struct SIGN { + SIGN_TYPE type; + std::string string; }; struct EXPRESSION { From 99b8bbd9c5d041dc29cc0dd374d6c77906a28edd Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Wed, 4 Jan 2017 09:25:24 +0900 Subject: [PATCH 12/52] Rename SIGN -> SYMBOL --- stringExpression.cpp | 12 ++++++------ stringExpression.h | 8 ++++---- types.h | 14 +++++++------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/stringExpression.cpp b/stringExpression.cpp index fe7644b..9f3035a 100644 --- a/stringExpression.cpp +++ b/stringExpression.cpp @@ -4,14 +4,14 @@ #include "stringExpression.h" #include "types.h" -struct SIGN StringExpression::makeSign(std::string sign, SIGN_TYPE type) { - struct SIGN expressionSign; +struct SYMBOL StringExpression::makeSign(std::string sign, SYMBOL_TYPE type) { + struct SYMBOL expressionSign; expressionSign.string = sign; expressionSign.type = type; return expressionSign; } -void StringExpression::addSign(struct SIGN sign) { +void StringExpression::addSign(struct SYMBOL sign) { this->expressionSigns.push_back(sign); } @@ -26,8 +26,8 @@ void StringExpression::defineExpressionSings() { std::string::size_type StringExpression::getNextExpressionPos() { std::vector expressionPositionsIndex; - std::vector::iterator it = this->expressionSigns.begin(); - std::vector::iterator end = this->expressionSigns.end(); + std::vector::iterator it = this->expressionSigns.begin(); + std::vector::iterator end = this->expressionSigns.end(); for (; it != end; it++) { expressionPositionsIndex.push_back(this->expressions.find(it->string, this->currentPos)); @@ -77,7 +77,7 @@ void StringExpression::parseExpression() { struct EXPRESSION expression; std::string expressionPart = this->expressions.substr(nextExpressionPos, 1); - expression.type = SIGN; + expression.type = SYMBOL; expression.value = expressionPart; this->expressionList.expressions.push_back(expression); } diff --git a/stringExpression.h b/stringExpression.h index 0bf6856..4710f35 100644 --- a/stringExpression.h +++ b/stringExpression.h @@ -8,15 +8,15 @@ class StringExpression { private: struct ExpressionList expressionList; - std::vector expressionSigns; + std::vector expressionSigns; std::string::size_type currentPos; std::string::size_type nextExpressionPos; std::string expressions; - struct SIGN makeSign(std::string sign, SIGN_TYPE type); - void addSign(struct SIGN sign); + struct SYMBOL makeSign(std::string sign, SYMBOL_TYPE type); + void addSign(struct SYMBOL sign); void defineExpressionSings(); - SIGN_TYPE getExpressionType(std::string sign); + SYMBOL_TYPE getExpressionType(std::string sign); std::string::size_type getNextExpressionPos(); public: diff --git a/types.h b/types.h index eb42f7c..f1a33d3 100644 --- a/types.h +++ b/types.h @@ -4,7 +4,7 @@ #include #include -enum SIGN_TYPE { +enum SYMBOL_TYPE { PLUS = 0, MINUS, DIVIDE, @@ -14,14 +14,14 @@ enum SIGN_TYPE { OTHER }; -enum EXPRESSION_TYPE { - VALUE, - SIGN +struct SYMBOL { + SYMBOL_TYPE type; + std::string string; }; -struct SIGN { - SIGN_TYPE type; - std::string string; +enum EXPRESSION_TYPE { + VALUE, + SYMBOL }; struct EXPRESSION { From 4b1b0547ef74801933b4107b6944c606d4adf487 Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Wed, 4 Jan 2017 09:29:45 +0900 Subject: [PATCH 13/52] makeSign is a member function of SYMBOL --- stringExpression.cpp | 19 ++++++------------- types.h | 7 +++++++ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/stringExpression.cpp b/stringExpression.cpp index 9f3035a..70791e1 100644 --- a/stringExpression.cpp +++ b/stringExpression.cpp @@ -4,24 +4,17 @@ #include "stringExpression.h" #include "types.h" -struct SYMBOL StringExpression::makeSign(std::string sign, SYMBOL_TYPE type) { - struct SYMBOL expressionSign; - expressionSign.string = sign; - expressionSign.type = type; - return expressionSign; -} - void StringExpression::addSign(struct SYMBOL sign) { this->expressionSigns.push_back(sign); } void StringExpression::defineExpressionSings() { - this->addSign(this->makeSign("+", PLUS)); - this->addSign(this->makeSign("-", MINUS)); - this->addSign(this->makeSign("*", MULTIPLY)); - this->addSign(this->makeSign("/", DIVIDE)); - this->addSign(this->makeSign("(", BRACKET_BEGIN)); - this->addSign(this->makeSign(")", BRACKET_END)); + this->addSign(SYMBOL::makeSign("+", PLUS)); + this->addSign(SYMBOL::makeSign("-", MINUS)); + this->addSign(SYMBOL::makeSign("*", MULTIPLY)); + this->addSign(SYMBOL::makeSign("/", DIVIDE)); + this->addSign(SYMBOL::makeSign("(", BRACKET_BEGIN)); + this->addSign(SYMBOL::makeSign(")", BRACKET_END)); } std::string::size_type StringExpression::getNextExpressionPos() { diff --git a/types.h b/types.h index f1a33d3..16ee0cc 100644 --- a/types.h +++ b/types.h @@ -17,6 +17,13 @@ enum SYMBOL_TYPE { struct SYMBOL { SYMBOL_TYPE type; std::string string; + + static SYMBOL makeSign(std::string sign, SYMBOL_TYPE type) { + struct SYMBOL symbol; + symbol.string = sign; + symbol.type = type; + return symbol; + } }; enum EXPRESSION_TYPE { From 9b70c301408c3b2bc2d7de23aa89d69dcaeeb06e Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Wed, 4 Jan 2017 09:36:13 +0900 Subject: [PATCH 14/52] SYMBOL struct has more members --- stringExpression.cpp | 14 +++++++------- stringExpression.h | 3 +-- types.h | 30 +++++++++++++++--------------- 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/stringExpression.cpp b/stringExpression.cpp index 70791e1..9b6e325 100644 --- a/stringExpression.cpp +++ b/stringExpression.cpp @@ -9,12 +9,12 @@ void StringExpression::addSign(struct SYMBOL sign) { } void StringExpression::defineExpressionSings() { - this->addSign(SYMBOL::makeSign("+", PLUS)); - this->addSign(SYMBOL::makeSign("-", MINUS)); - this->addSign(SYMBOL::makeSign("*", MULTIPLY)); - this->addSign(SYMBOL::makeSign("/", DIVIDE)); - this->addSign(SYMBOL::makeSign("(", BRACKET_BEGIN)); - this->addSign(SYMBOL::makeSign(")", BRACKET_END)); + this->addSign(SYMBOL::makeSymbol("+", SYMBOL::PLUS)); + this->addSign(SYMBOL::makeSymbol("-", SYMBOL::MINUS)); + this->addSign(SYMBOL::makeSymbol("*", SYMBOL::MULTIPLY)); + this->addSign(SYMBOL::makeSymbol("/", SYMBOL::DIVIDE)); + this->addSign(SYMBOL::makeSymbol("(", SYMBOL::BRACKET_BEGIN)); + this->addSign(SYMBOL::makeSymbol(")", SYMBOL::BRACKET_END)); } std::string::size_type StringExpression::getNextExpressionPos() { @@ -23,7 +23,7 @@ std::string::size_type StringExpression::getNextExpressionPos() { std::vector::iterator end = this->expressionSigns.end(); for (; it != end; it++) { - expressionPositionsIndex.push_back(this->expressions.find(it->string, this->currentPos)); + expressionPositionsIndex.push_back(this->expressions.find(it->character, this->currentPos)); } return *std::min_element(expressionPositionsIndex.begin(), expressionPositionsIndex.end()); diff --git a/stringExpression.h b/stringExpression.h index 4710f35..1b9aff0 100644 --- a/stringExpression.h +++ b/stringExpression.h @@ -13,10 +13,9 @@ class StringExpression { std::string::size_type nextExpressionPos; std::string expressions; - struct SYMBOL makeSign(std::string sign, SYMBOL_TYPE type); void addSign(struct SYMBOL sign); void defineExpressionSings(); - SYMBOL_TYPE getExpressionType(std::string sign); + SYMBOL::TYPES getExpressionType(std::string sign); std::string::size_type getNextExpressionPos(); public: diff --git a/types.h b/types.h index 16ee0cc..1d17c6c 100644 --- a/types.h +++ b/types.h @@ -4,23 +4,23 @@ #include #include -enum SYMBOL_TYPE { - PLUS = 0, - MINUS, - DIVIDE, - MULTIPLY, - BRACKET_BEGIN, - BRACKET_END, - OTHER -}; - struct SYMBOL { - SYMBOL_TYPE type; - std::string string; - - static SYMBOL makeSign(std::string sign, SYMBOL_TYPE type) { + enum TYPES { + PLUS = 0, + MINUS, + DIVIDE, + MULTIPLY, + BRACKET_BEGIN, + BRACKET_END, + OTHER + }; + + SYMBOL::TYPES type; + std::string character; + + static SYMBOL makeSymbol(std::string character, SYMBOL::TYPES type) { struct SYMBOL symbol; - symbol.string = sign; + symbol.character = character; symbol.type = type; return symbol; } From 7c729135511fad4245381d3a79605c1651a5a6a7 Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Wed, 4 Jan 2017 09:39:31 +0900 Subject: [PATCH 15/52] Rename functions --- stringExpression.cpp | 24 ++++++++++++------------ stringExpression.h | 6 +++--- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/stringExpression.cpp b/stringExpression.cpp index 9b6e325..69e177c 100644 --- a/stringExpression.cpp +++ b/stringExpression.cpp @@ -4,23 +4,23 @@ #include "stringExpression.h" #include "types.h" -void StringExpression::addSign(struct SYMBOL sign) { - this->expressionSigns.push_back(sign); +void StringExpression::addSymbol(struct SYMBOL symbol) { + this->symbols.push_back(symbol); } -void StringExpression::defineExpressionSings() { - this->addSign(SYMBOL::makeSymbol("+", SYMBOL::PLUS)); - this->addSign(SYMBOL::makeSymbol("-", SYMBOL::MINUS)); - this->addSign(SYMBOL::makeSymbol("*", SYMBOL::MULTIPLY)); - this->addSign(SYMBOL::makeSymbol("/", SYMBOL::DIVIDE)); - this->addSign(SYMBOL::makeSymbol("(", SYMBOL::BRACKET_BEGIN)); - this->addSign(SYMBOL::makeSymbol(")", SYMBOL::BRACKET_END)); +void StringExpression::defineSymbols() { + this->addSymbol(SYMBOL::makeSymbol("+", SYMBOL::PLUS)); + this->addSymbol(SYMBOL::makeSymbol("-", SYMBOL::MINUS)); + this->addSymbol(SYMBOL::makeSymbol("*", SYMBOL::MULTIPLY)); + this->addSymbol(SYMBOL::makeSymbol("/", SYMBOL::DIVIDE)); + this->addSymbol(SYMBOL::makeSymbol("(", SYMBOL::BRACKET_BEGIN)); + this->addSymbol(SYMBOL::makeSymbol(")", SYMBOL::BRACKET_END)); } std::string::size_type StringExpression::getNextExpressionPos() { std::vector expressionPositionsIndex; - std::vector::iterator it = this->expressionSigns.begin(); - std::vector::iterator end = this->expressionSigns.end(); + std::vector::iterator it = this->symbols.begin(); + std::vector::iterator end = this->symbols.end(); for (; it != end; it++) { expressionPositionsIndex.push_back(this->expressions.find(it->character, this->currentPos)); @@ -30,7 +30,7 @@ std::string::size_type StringExpression::getNextExpressionPos() { } StringExpression::StringExpression(std::string expressions) { - this->defineExpressionSings(); + this->defineSymbols(); this->expressions = expressions; this->currentPos = 0; } diff --git a/stringExpression.h b/stringExpression.h index 1b9aff0..1782365 100644 --- a/stringExpression.h +++ b/stringExpression.h @@ -8,13 +8,13 @@ class StringExpression { private: struct ExpressionList expressionList; - std::vector expressionSigns; + std::vector symbols; std::string::size_type currentPos; std::string::size_type nextExpressionPos; std::string expressions; - void addSign(struct SYMBOL sign); - void defineExpressionSings(); + void addSymbol(struct SYMBOL sign); + void defineSymbols(); SYMBOL::TYPES getExpressionType(std::string sign); std::string::size_type getNextExpressionPos(); From 926abb3071a38c792a0bddd36006c2a0818c5cf7 Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Wed, 4 Jan 2017 09:40:45 +0900 Subject: [PATCH 16/52] Add this --- stringExpression.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stringExpression.cpp b/stringExpression.cpp index 69e177c..9b74142 100644 --- a/stringExpression.cpp +++ b/stringExpression.cpp @@ -48,7 +48,7 @@ void StringExpression::next() { } bool StringExpression::hasNextExpression() { - nextExpressionPos = this->getNextExpressionPos(); + this->nextExpressionPos = this->getNextExpressionPos(); if (this->nextExpressionPos == std::string::npos) { return false; } else { From ffeaa6c662a1f7e6dfcddb9afcd5694bf22e2a19 Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Wed, 4 Jan 2017 10:09:32 +0900 Subject: [PATCH 17/52] Add ExpressionList class --- Makefile | 2 +- calculator.cpp | 3 +++ calculator.h | 3 ++- calcurate.h | 2 ++ expressionList.cpp | 21 +++++++++++++++++++++ expressionList.h | 17 +++++++++++++++++ stringExpression.cpp | 17 +++++------------ stringExpression.h | 3 ++- types.h | 25 ++++++++++++++----------- 9 files changed, 67 insertions(+), 26 deletions(-) create mode 100644 expressionList.cpp create mode 100644 expressionList.h diff --git a/Makefile b/Makefile index 308f129..be6f96b 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ DBG_OPTS = -O0 -g3 -Wall -SOURCE = mathcalc.cpp calculator.cpp stringExpression.cpp +SOURCE = mathcalc.cpp calculator.cpp stringExpression.cpp expressionList.cpp TARGET = mathcalc mathcalc: $(SOURCE) diff --git a/calculator.cpp b/calculator.cpp index c97d78a..5042ad8 100644 --- a/calculator.cpp +++ b/calculator.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "calculator.h" #include "stringExpression.h" @@ -33,6 +34,8 @@ void Calculator::parseStringExpressions() { } void Calculator::printExpressions() { + std::string stringifiedExpression = this->expressionList.getStringified(); + std::cout << stringifiedExpression << std::endl; return; } diff --git a/calculator.h b/calculator.h index aa2c0c4..93983e5 100644 --- a/calculator.h +++ b/calculator.h @@ -4,13 +4,14 @@ #include #include #include "types.h" +#include "expressionList.h" class Calculator { private: // struct PROGRESSION_FLAGS pflags; std::string stringExpressions; - struct ExpressionList expressionList; + ExpressionList expressionList; public: Calculator(std::string expressions); diff --git a/calcurate.h b/calcurate.h index 5cbe7c0..50523d1 100644 --- a/calcurate.h +++ b/calcurate.h @@ -152,6 +152,7 @@ int calcurate(list::iterator begin, list::iterator end) return _stoi(*pflags.expressions.begin()); } +/* void build_expressions(string expressions, struct PROGRESSION_FLAGS *pflags) { string::size_type current, prev, pos_plus, pos_minus, pos_multi, pos_div; @@ -194,5 +195,6 @@ void build_expressions(string expressions, struct PROGRESSION_FLAGS *pflags) return; } +*/ #endif diff --git a/expressionList.cpp b/expressionList.cpp new file mode 100644 index 0000000..5da356f --- /dev/null +++ b/expressionList.cpp @@ -0,0 +1,21 @@ +#include +#include +#include "types.h" +#include "expressionList.h" + +void ExpressionList::add(struct EXPRESSION expression) { + this->expressions.push_back(expression); +} + +std::string ExpressionList::getStringified() { + std::string buffer; + std::list::iterator it = this->expressions.begin(); + std::list::iterator end = this->expressions.end(); + + for (; it != end; it++) { + buffer += it->value; + } + + return buffer; +} + diff --git a/expressionList.h b/expressionList.h new file mode 100644 index 0000000..c3c9f69 --- /dev/null +++ b/expressionList.h @@ -0,0 +1,17 @@ +#ifndef __EXPRESSION_LIST_H__ +#define __EXPRESSION_LIST_H__ + +#include +#include +#include "types.h" + +class ExpressionList { + private: + std::list expressions; + + public: + void add(struct EXPRESSION expression); + std::string getStringified(); +}; + +#endif // __EXPRESSION_LIST_H__ diff --git a/stringExpression.cpp b/stringExpression.cpp index 9b74142..f613859 100644 --- a/stringExpression.cpp +++ b/stringExpression.cpp @@ -1,8 +1,9 @@ #include #include #include -#include "stringExpression.h" #include "types.h" +#include "stringExpression.h" +#include "expressionList.h" void StringExpression::addSymbol(struct SYMBOL symbol) { this->symbols.push_back(symbol); @@ -57,21 +58,13 @@ bool StringExpression::hasNextExpression() { } void StringExpression::parseNumberValue() { - struct EXPRESSION expression; std::string valuePart = this->expressions.substr(this->currentPos, this->nextExpressionPos - this->currentPos); - - expression.type = VALUE; - expression.value = valuePart; - this->expressionList.expressions.push_back(expression); + this->expressionList.add(EXPRESSION::makeExpression(EXPRESSION::VALUE, valuePart)); } void StringExpression::parseExpression() { - struct EXPRESSION expression; - std::string expressionPart = this->expressions.substr(nextExpressionPos, 1); - - expression.type = SYMBOL; - expression.value = expressionPart; - this->expressionList.expressions.push_back(expression); + std::string symbolPart = this->expressions.substr(nextExpressionPos, 1); + this->expressionList.add(EXPRESSION::makeExpression(EXPRESSION::SYMBOL, symbolPart)); } diff --git a/stringExpression.h b/stringExpression.h index 1782365..a95cefc 100644 --- a/stringExpression.h +++ b/stringExpression.h @@ -4,10 +4,11 @@ #include #include #include "types.h" +#include "expressionList.h" class StringExpression { private: - struct ExpressionList expressionList; + ExpressionList expressionList; std::vector symbols; std::string::size_type currentPos; std::string::size_type nextExpressionPos; diff --git a/types.h b/types.h index 1d17c6c..c61bc75 100644 --- a/types.h +++ b/types.h @@ -18,26 +18,29 @@ struct SYMBOL { SYMBOL::TYPES type; std::string character; - static SYMBOL makeSymbol(std::string character, SYMBOL::TYPES type) { + static struct SYMBOL makeSymbol(std::string character, SYMBOL::TYPES type) { struct SYMBOL symbol; symbol.character = character; symbol.type = type; return symbol; - } -}; - -enum EXPRESSION_TYPE { - VALUE, - SYMBOL + }; }; struct EXPRESSION { - EXPRESSION_TYPE type; + enum TYPES { + VALUE, + SYMBOL + }; + + EXPRESSION::TYPES type; std::string value; -}; -struct ExpressionList { - std::list expressions; + static struct EXPRESSION makeExpression(EXPRESSION::TYPES type, std::string value) { + struct EXPRESSION expression; + expression.type = type; + expression.value = value; + return expression; + }; }; struct EXP_DIVIDER_RESULT { From a7cb7e08cca6ca0088d6eba69c1f8519b77f03f8 Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Wed, 4 Jan 2017 10:13:21 +0900 Subject: [PATCH 18/52] Remove processBrackets --- calculator.cpp | 4 ---- calculator.h | 2 -- mathcalc.cpp | 1 + 3 files changed, 1 insertion(+), 6 deletions(-) diff --git a/calculator.cpp b/calculator.cpp index 5042ad8..1122313 100644 --- a/calculator.cpp +++ b/calculator.cpp @@ -39,10 +39,6 @@ void Calculator::printExpressions() { return; } -void Calculator::processBrackets() { - return; -} - void Calculator::execCalculation() { return; } diff --git a/calculator.h b/calculator.h index 93983e5..5521394 100644 --- a/calculator.h +++ b/calculator.h @@ -19,8 +19,6 @@ class Calculator { void parseStringExpressions(); void printExpressions(); - - void processBrackets(); void execCalculation(); }; diff --git a/mathcalc.cpp b/mathcalc.cpp index cd51ba1..ede0eee 100644 --- a/mathcalc.cpp +++ b/mathcalc.cpp @@ -11,6 +11,7 @@ using namespace std; #include "utils.h" #include "bracket.h" #include "calcurate.h" + #include "calculator.h" int main(int argc, char *argv[]) From 9d8c9dc2e05accc25273cb65ed1c9a6f124656c2 Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Wed, 4 Jan 2017 10:22:27 +0900 Subject: [PATCH 19/52] Implement skeltons of validators --- calculator.cpp | 8 ++++++++ calculator.h | 1 + expressionList.cpp | 12 ++++++++++++ expressionList.h | 3 +++ 4 files changed, 24 insertions(+) diff --git a/calculator.cpp b/calculator.cpp index 1122313..d5c880e 100644 --- a/calculator.cpp +++ b/calculator.cpp @@ -39,6 +39,14 @@ void Calculator::printExpressions() { return; } +bool Calculator::validateExpressions() { + return ( + this->expressionList.validateBracketsParing() && + this->expressionList.validateDuplicatedSymbol() && + this->expressionList.validateLonelySymbol() + ); +} + void Calculator::execCalculation() { return; } diff --git a/calculator.h b/calculator.h index 5521394..f059222 100644 --- a/calculator.h +++ b/calculator.h @@ -19,6 +19,7 @@ class Calculator { void parseStringExpressions(); void printExpressions(); + bool validateExpressions(); void execCalculation(); }; diff --git a/expressionList.cpp b/expressionList.cpp index 5da356f..0f124e5 100644 --- a/expressionList.cpp +++ b/expressionList.cpp @@ -19,3 +19,15 @@ std::string ExpressionList::getStringified() { return buffer; } +bool ExpressionList::validateBracketsParing() { + return true; +} + +bool ExpressionList::validateDuplicatedSymbol() { + return true; +} + +bool ExpressionList::validateLonelySymbol() { + return true; +} + diff --git a/expressionList.h b/expressionList.h index c3c9f69..d61e087 100644 --- a/expressionList.h +++ b/expressionList.h @@ -11,6 +11,9 @@ class ExpressionList { public: void add(struct EXPRESSION expression); + bool validateBracketsParing(); + bool validateDuplicatedSymbol(); + bool validateLonelySymbol(); std::string getStringified(); }; From 8a93cb4899927ef3c07b7b7cae20442cece85d0e Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Wed, 4 Jan 2017 10:28:34 +0900 Subject: [PATCH 20/52] Implement equal operator --- types.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/types.h b/types.h index c61bc75..918592d 100644 --- a/types.h +++ b/types.h @@ -24,6 +24,10 @@ struct SYMBOL { symbol.type = type; return symbol; }; + + bool operator==(std::string character) { + return (this->character == character); + } }; struct EXPRESSION { @@ -41,6 +45,10 @@ struct EXPRESSION { expression.value = value; return expression; }; + + bool operator==(std::string value) { + return (this->value == value); + } }; struct EXP_DIVIDER_RESULT { From 28120d26cc1168dac3f4fe0b4d33c7ea274194cd Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Wed, 4 Jan 2017 10:49:48 +0900 Subject: [PATCH 21/52] Add Exception struct --- types.h | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/types.h b/types.h index 918592d..83db427 100644 --- a/types.h +++ b/types.h @@ -51,17 +51,31 @@ struct EXPRESSION { } }; +struct Exception { + std::string message; + + static struct Exception make(std::string message) { + struct Exception e; + e.message = message; + return e; + }; + + const char *msg() { + return this->message.c_str(); + }; +}; + struct EXP_DIVIDER_RESULT { - int left_value; - int right_value; + int left_value; + int right_value; }; struct PROGRESSION_FLAGS { - std::list expressions; - std::list::iterator it; - bool exists_mul_and_div; - bool exists_pls_and_mns; - bool go_through; + std::list expressions; + std::list::iterator it; + bool exists_mul_and_div; + bool exists_pls_and_mns; + bool go_through; }; #endif From a91f58dc399bbef1692a6c4949688360d62837fc Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Wed, 4 Jan 2017 10:50:15 +0900 Subject: [PATCH 22/52] Change return variable type --- calculator.cpp | 11 +++++------ calculator.h | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/calculator.cpp b/calculator.cpp index d5c880e..762081e 100644 --- a/calculator.cpp +++ b/calculator.cpp @@ -39,12 +39,11 @@ void Calculator::printExpressions() { return; } -bool Calculator::validateExpressions() { - return ( - this->expressionList.validateBracketsParing() && - this->expressionList.validateDuplicatedSymbol() && - this->expressionList.validateLonelySymbol() - ); +void Calculator::validateExpressions() { + this->expressionList.validateBracketsParing(); + this->expressionList.validateDuplicatedSymbol(); + this->expressionList.validateLonelySymbol(); + return; } void Calculator::execCalculation() { diff --git a/calculator.h b/calculator.h index f059222..d7b19bb 100644 --- a/calculator.h +++ b/calculator.h @@ -19,7 +19,7 @@ class Calculator { void parseStringExpressions(); void printExpressions(); - bool validateExpressions(); + void validateExpressions(); void execCalculation(); }; From 68bcbfa7995e01115f98405d7f42a0d2f8482cff Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Wed, 4 Jan 2017 11:19:44 +0900 Subject: [PATCH 23/52] Update return variale types --- expressionList.cpp | 15 +++++++++------ expressionList.h | 6 +++--- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/expressionList.cpp b/expressionList.cpp index 0f124e5..0b3fead 100644 --- a/expressionList.cpp +++ b/expressionList.cpp @@ -19,15 +19,18 @@ std::string ExpressionList::getStringified() { return buffer; } -bool ExpressionList::validateBracketsParing() { - return true; +void ExpressionList::validateBracketsParing() { + // throw Exception::make("Brackets unmatched."); + return; } -bool ExpressionList::validateDuplicatedSymbol() { - return true; +void ExpressionList::validateDuplicatedSymbol() { + // throw Exception::make("Symbols duplicated."); + return; } -bool ExpressionList::validateLonelySymbol() { - return true; +void ExpressionList::validateLonelySymbol() { + // throw Exception::make("Lonely symbol."); + return; } diff --git a/expressionList.h b/expressionList.h index d61e087..cf0c4a9 100644 --- a/expressionList.h +++ b/expressionList.h @@ -11,9 +11,9 @@ class ExpressionList { public: void add(struct EXPRESSION expression); - bool validateBracketsParing(); - bool validateDuplicatedSymbol(); - bool validateLonelySymbol(); + void validateBracketsParing(); + void validateDuplicatedSymbol(); + void validateLonelySymbol(); std::string getStringified(); }; From 2e3b06266ff6ee68d078ca1f0e2ba7746f9a79f6 Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Wed, 4 Jan 2017 11:19:50 +0900 Subject: [PATCH 24/52] Use try block --- mathcalc.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/mathcalc.cpp b/mathcalc.cpp index ede0eee..f0a0c64 100644 --- a/mathcalc.cpp +++ b/mathcalc.cpp @@ -18,15 +18,21 @@ int main(int argc, char *argv[]) { if (argc == 1) { cout << "usage: mathcalc [expression]" << endl; - return 1; + exit(1); } // struct PROGRESSION_FLAGS pflags; string expressions = argv[1]; Calculator calc(expressions); - calc.parseStringExpressions(); - calc.printExpressions(); + try { + calc.validateExpressions(); + calc.parseStringExpressions(); + calc.printExpressions(); + } catch (struct Exception e) { + std::cerr << "Error: " << e.msg() << std::endl; + exit(1); + } /* // Build expression array from string given from command-line From 7326aaf43bed700ca1fb8aa97a04727d8f9b5a6c Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Wed, 4 Jan 2017 11:33:42 +0900 Subject: [PATCH 25/52] Update clean task --- Makefile | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index be6f96b..92e1bfe 100644 --- a/Makefile +++ b/Makefile @@ -7,8 +7,10 @@ mathcalc: $(SOURCE) .PHONY: clean run debug release clean: - rm $(TARGET) - rm -r mathcalc.dSYM + $(shell [ -e $(TARGET) ] && rm $(TARGET)) + $(shell [ -e core ] && rm core) + $(shell [ -d mathcalc.dSYM ] && rm -r mathcalc.dSYM) + @echo Done run: $(TARGET) ./mathcalc From 1bbe877f13d42edec8e9af959559b2abead2f326 Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Wed, 4 Jan 2017 12:31:38 +0900 Subject: [PATCH 26/52] Implemt cleanupJunks sand validateDuplicatedSymbol --- calculator.cpp | 1 + expressionList.cpp | 28 ++++++++++++++++++++++++++-- expressionList.h | 5 +++++ mathcalc.cpp | 2 +- types.h | 2 +- 5 files changed, 34 insertions(+), 4 deletions(-) diff --git a/calculator.cpp b/calculator.cpp index 762081e..fc7b203 100644 --- a/calculator.cpp +++ b/calculator.cpp @@ -30,6 +30,7 @@ void Calculator::parseStringExpressions() { } this->expressionList = stringExpression.getExpressionsList(); + this->expressionList.cleanupJunks(); return; } diff --git a/expressionList.cpp b/expressionList.cpp index 0b3fead..1e8c288 100644 --- a/expressionList.cpp +++ b/expressionList.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "types.h" #include "expressionList.h" @@ -7,6 +8,10 @@ void ExpressionList::add(struct EXPRESSION expression) { this->expressions.push_back(expression); } +void ExpressionList::cleanupJunks() { + this->expressions.remove_if(ExpressionList::emptyExpression); +} + std::string ExpressionList::getStringified() { std::string buffer; std::list::iterator it = this->expressions.begin(); @@ -20,12 +25,31 @@ std::string ExpressionList::getStringified() { } void ExpressionList::validateBracketsParing() { - // throw Exception::make("Brackets unmatched."); + size_t beginBracketCount = std::count(this->expressions.begin(), this->expressions.end(), std::string("(")); + size_t endBracketCount = std::count(this->expressions.begin(), this->expressions.end(), std::string(")")); + + if (beginBracketCount != endBracketCount) { + throw Exception::make("Brackets unmatched."); + } + return; } void ExpressionList::validateDuplicatedSymbol() { - // throw Exception::make("Symbols duplicated."); + std::list::iterator it = this->expressions.begin(); + std::list::iterator end = this->expressions.end(); + std::list::iterator temp; + + for (; it != end; it++) { + if (it->type == EXPRESSION::SYMBOL) { + temp = it; + temp++; + if (temp->type == EXPRESSION::SYMBOL && temp->value != std::string("(") && temp->value != ")") { + throw Exception::make("Duplicated symbol."); + } + } + } + return; } diff --git a/expressionList.h b/expressionList.h index cf0c4a9..ee61e41 100644 --- a/expressionList.h +++ b/expressionList.h @@ -9,8 +9,13 @@ class ExpressionList { private: std::list expressions; + static bool emptyExpression(const struct EXPRESSION &expression) { + return (expression.type == EXPRESSION::VALUE && expression.value == ""); + }; + public: void add(struct EXPRESSION expression); + void cleanupJunks(); void validateBracketsParing(); void validateDuplicatedSymbol(); void validateLonelySymbol(); diff --git a/mathcalc.cpp b/mathcalc.cpp index f0a0c64..c899bae 100644 --- a/mathcalc.cpp +++ b/mathcalc.cpp @@ -26,8 +26,8 @@ int main(int argc, char *argv[]) Calculator calc(expressions); try { - calc.validateExpressions(); calc.parseStringExpressions(); + calc.validateExpressions(); calc.printExpressions(); } catch (struct Exception e) { std::cerr << "Error: " << e.msg() << std::endl; diff --git a/types.h b/types.h index 83db427..f8a538b 100644 --- a/types.h +++ b/types.h @@ -32,7 +32,7 @@ struct SYMBOL { struct EXPRESSION { enum TYPES { - VALUE, + VALUE = 0, SYMBOL }; From 1df5a0fe1b61070cdf9c630eae05b310ba0cc0b1 Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Wed, 4 Jan 2017 12:57:37 +0900 Subject: [PATCH 27/52] Implement validateLonelySymbol --- expressionList.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/expressionList.cpp b/expressionList.cpp index 1e8c288..14c5bbf 100644 --- a/expressionList.cpp +++ b/expressionList.cpp @@ -44,7 +44,9 @@ void ExpressionList::validateDuplicatedSymbol() { if (it->type == EXPRESSION::SYMBOL) { temp = it; temp++; - if (temp->type == EXPRESSION::SYMBOL && temp->value != std::string("(") && temp->value != ")") { + if ((it->value != std::string(")")) && + (temp->type == EXPRESSION::SYMBOL) && + (temp->value != std::string("("))) { throw Exception::make("Duplicated symbol."); } } @@ -54,7 +56,15 @@ void ExpressionList::validateDuplicatedSymbol() { } void ExpressionList::validateLonelySymbol() { - // throw Exception::make("Lonely symbol."); + std::list::iterator head = this->expressions.begin(); + std::list::iterator tail = this->expressions.end(); + + tail--; + if ((head->type == EXPRESSION::SYMBOL && head->value != std::string("(")) || + (tail->type == EXPRESSION::SYMBOL && tail->value != std::string(")"))) { + throw Exception::make("Lonely symbol."); + } + return; } From 71f8924d6ffeb776de23da46ce69013abef8fe6f Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Wed, 4 Jan 2017 13:00:49 +0900 Subject: [PATCH 28/52] Fix spacing --- expressionList.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/expressionList.cpp b/expressionList.cpp index 14c5bbf..eba2664 100644 --- a/expressionList.cpp +++ b/expressionList.cpp @@ -18,7 +18,7 @@ std::string ExpressionList::getStringified() { std::list::iterator end = this->expressions.end(); for (; it != end; it++) { - buffer += it->value; + buffer += it->value; } return buffer; From 41f1cea2d995dabdb4d522b497b092d5373f2d8a Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Mon, 8 May 2017 13:02:34 +0900 Subject: [PATCH 29/52] Remove bracket.h and calcurate.h --- bracket.h | 139 ----------------------------------- calcurate.h | 200 --------------------------------------------------- mathcalc.cpp | 14 ---- 3 files changed, 353 deletions(-) delete mode 100644 bracket.h delete mode 100644 calcurate.h diff --git a/bracket.h b/bracket.h deleted file mode 100644 index d59b252..0000000 --- a/bracket.h +++ /dev/null @@ -1,139 +0,0 @@ -#ifndef BRACKET_H -#define BRACKET_H - -#include "types.h" -#include "calcurate.h" - -#define BEGIN_BRACKET 1 -#define END_BRACKET 2 - -#define NO_MORE_BRACKETS -1 -#define BRACKET_MISMATCH -2 - -int bracket_count(list expressions, int type) -{ - list::iterator it = expressions.begin(); - int result = 0; - - while (it != expressions.end()) { - switch (type) { - case BEGIN_BRACKET: - if (*it == "(") { - result++; - } - break; - case END_BRACKET: - if (*it == ")") { - result++; - } - break; - default: - ; - } - it++; - } - - return result; -} - -int process_brackets(struct PROGRESSION_FLAGS *pflags) -{ - list partial_expressions; - list::iterator it, pair_match_seek_it; - list::iterator last_begin_bracket_it, - pair_end_bracket_it; - list::iterator statement_begin, - statement_end; - list::iterator delete_back_it; - list::iterator insert_position; - bool begin_bracket_found, - end_bracket_found; - int begin_bracket_counter = 0, - end_bracket_counter = 0; - int bracket_value = 0; - - begin_bracket_counter = bracket_count(pflags->expressions, BEGIN_BRACKET); - end_bracket_counter = bracket_count(pflags->expressions, END_BRACKET); - if (begin_bracket_counter != end_bracket_counter) { - return BRACKET_MISMATCH; - } - if (!begin_bracket_counter || !end_bracket_counter) { - return NO_MORE_BRACKETS; - } - - it = pflags->expressions.begin(); - while (it != pflags->expressions.end()) { - begin_bracket_found = false; - end_bracket_found = false; - - if (*it == "(") { - begin_bracket_found = true; - last_begin_bracket_it = it; - - pair_match_seek_it = it; - while (pair_match_seek_it != pflags->expressions.end()) { - pair_match_seek_it++; - if (*pair_match_seek_it == "(") { - break; - } - if (*pair_match_seek_it == ")") { - end_bracket_found = true; - pair_match_seek_it++; - pair_end_bracket_it = pair_match_seek_it; - break; - } - } - } - if (begin_bracket_found && end_bracket_found) { - break; - } - - it++; - } - - statement_begin = last_begin_bracket_it; - statement_begin++; - statement_end = pair_end_bracket_it; - statement_end--; - bracket_value = calcurate(statement_begin, statement_end); - - delete_back_it = pair_end_bracket_it; - delete_back_it++; - - insert_position = pflags->expressions.erase(last_begin_bracket_it, delete_back_it); - insert_position = pflags->expressions.insert(insert_position, _itos(bracket_value)); - - // Clean up waste element that is added after inserting bracket_value - insert_position--; - pflags->expressions.erase(insert_position); - - cout << stringify_list(pflags->expressions) << endl; - - return 0; -} - -void exec_bracket_processing(struct PROGRESSION_FLAGS *pflags) -{ - bool exit_bracket_processing = false; - - while (1) { - cout << "------" << endl; - switch (process_brackets(pflags)) { - case NO_MORE_BRACKETS: - exit_bracket_processing = true; - break; - case BRACKET_MISMATCH: - cout << "Bracket mis-match" << endl; - return; - default: - break; - } - if (exit_bracket_processing) { - break; - } - } - - return; -} - -#endif diff --git a/calcurate.h b/calcurate.h deleted file mode 100644 index 50523d1..0000000 --- a/calcurate.h +++ /dev/null @@ -1,200 +0,0 @@ -#ifndef _CALCURATE_H_ -#define _CALCURATE_H_ - -#define DIV 1 -#define MUL 2 -#define PLS 3 -#define MNS 4 - -#define NO_EXP 0 - -// Process expressions -int expression_processor( - string it, - bool *exists_mul_and_div, - bool *exists_pls_and_mns, - bool go_through ) -{ - int exptype = NO_EXP; - - if (it == "*") { - exptype = MUL; - *exists_mul_and_div = true; - } else if (it == "/") { - exptype = DIV; - *exists_mul_and_div = true; - } - if (!*exists_mul_and_div && go_through) { - if (it == "+") { - exptype = PLS; - *exists_pls_and_mns = true; - } else if (it == "-") { - exptype = MNS; - *exists_pls_and_mns = true; - } - } - - return exptype; -} - -void exp_divider( - list *exp_array, - list::iterator *it, - struct EXP_DIVIDER_RESULT *exp_result ) -{ - string buf; - - *it = exp_array->erase(*it); - buf = *(*it); - exp_result->right_value = _stoi(buf); - - *it = exp_array->erase(*it); - *(*it)--; - buf = *(*it); - exp_result->left_value = _stoi(buf); -} - -int exec_calcurate(int exptype, struct EXP_DIVIDER_RESULT divider_result) -{ - int let = 0; - int right = divider_result.right_value; - int left = divider_result.left_value; - - switch (exptype) { - case MUL: let = right * left; break; - case DIV: let = left / right; break; - case PLS: let = left + right; break; - case MNS: let = left - right; break; - default: let = (int)NULL; break; - } - - return let; -} - -int sub_calcurate(struct PROGRESSION_FLAGS *pflags) -{ - struct EXP_DIVIDER_RESULT edr; - int right, left, let; - int exptype; - - exptype = expression_processor( - *pflags->it, &pflags->exists_mul_and_div, - &pflags->exists_pls_and_mns, pflags->go_through - ); - - if (exptype != NO_EXP) { - exp_divider(&pflags->expressions, &pflags->it, &edr); - right = edr.right_value; - left = edr.left_value; - - if (right == -1 || left == -1) { - return -1; - } - - let = exec_calcurate(exptype, edr); - - if (let == (int)NULL) { - return -2; - } - - pflags->expressions.insert(pflags->it, _itos(let)); - pflags->it = pflags->expressions.erase(pflags->it); - - return 1; - } - - return 0; -} - -int calcurate(list::iterator begin, list::iterator end) -{ - struct PROGRESSION_FLAGS pflags; - int result; - - pflags.it = begin; - for (;pflags.it != end;pflags.it++) { - pflags.expressions.push_back(*pflags.it); - } - - pflags.go_through = false; - - cout << "(" << stringify_list(pflags.expressions) << ")" << endl; - - while (1) { - pflags.exists_mul_and_div = false; - pflags.exists_pls_and_mns = false; - - pflags.it = pflags.expressions.begin(); - while (pflags.it != pflags.expressions.end()) { - result = sub_calcurate(&pflags); - - if (result == 1) { - cout << "= (" << stringify_list(pflags.expressions) << ")" << endl; - } else if (result < 0) { - cout << "Error(" << result << ")" << endl; - } - - pflags.it++; - } - - if (pflags.go_through && - !pflags.exists_mul_and_div && - !pflags.exists_pls_and_mns) { - break; - } - - // processing for subtraction and addition is not going - // to be triggerd off before going-through expression - // processing has not completed once. - pflags.go_through = true; - } - - return _stoi(*pflags.expressions.begin()); -} - -/* -void build_expressions(string expressions, struct PROGRESSION_FLAGS *pflags) -{ - string::size_type current, prev, pos_plus, pos_minus, pos_multi, pos_div; - string::size_type pos_bracket_begin, pos_bracket_end; - string substr_buffer; - bool noexp; - - current = 0; - noexp = false; - - while (1) { - pos_plus = expressions.find("+", current); - pos_minus = expressions.find("-", current); - pos_multi = expressions.find("*", current); - pos_div = expressions.find("/", current); - pos_bracket_begin = expressions.find("(", current); - pos_bracket_end = expressions.find(")", current); - if (pos_plus == string::npos && - pos_minus == string::npos && - pos_multi == string::npos && - pos_div == string::npos && - pos_bracket_begin == string::npos && - pos_bracket_end == string::npos) { - noexp = true; - } - - // number - prev = current; - current = min(pos_plus, min(pos_minus, min(pos_multi, - min(pos_div, min(pos_bracket_begin, pos_bracket_end))))); - pflags->expressions.push_back(expressions.substr(prev, current - prev)); - - // expression - if (noexp) break; - pflags->expressions.push_back(expressions.substr(current, 1)); - current++; - } - - cout << "> " << stringify_list(pflags->expressions) << endl; - - return; -} -*/ - -#endif diff --git a/mathcalc.cpp b/mathcalc.cpp index c899bae..8cf6810 100644 --- a/mathcalc.cpp +++ b/mathcalc.cpp @@ -9,8 +9,6 @@ using namespace std; #include "types.h" #include "utils.h" -#include "bracket.h" -#include "calcurate.h" #include "calculator.h" @@ -34,18 +32,6 @@ int main(int argc, char *argv[]) exit(1); } - /* - // Build expression array from string given from command-line - expressions = argv[1]; - build_expressions(expressions, &pflags); - - // Clean up bracket-wrapped expressions - exec_bracket_processing(&pflags); - - // Last calcuration after cleaning up of brackets - cout << calcurate(pflags.expressions.begin(), pflags.expressions.end()) << endl; - */ - return 0; } From 965d8b4b6ba90d2561f5dbee6c48935aa67739f2 Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Mon, 8 May 2017 18:33:21 +0900 Subject: [PATCH 30/52] Remove utils.h --- mathcalc.cpp | 4 +--- utils.h | 31 ------------------------------- 2 files changed, 1 insertion(+), 34 deletions(-) delete mode 100644 utils.h diff --git a/mathcalc.cpp b/mathcalc.cpp index 8cf6810..04480b8 100644 --- a/mathcalc.cpp +++ b/mathcalc.cpp @@ -7,10 +7,8 @@ #include using namespace std; -#include "types.h" -#include "utils.h" - #include "calculator.h" +#include "types.h" int main(int argc, char *argv[]) { diff --git a/utils.h b/utils.h deleted file mode 100644 index 7a44d2b..0000000 --- a/utils.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef UTILS_H -#define UTILS_H - -string _itos(int n) -{ - stringstream s; - s << n; - return s.str(); -} - -int _stoi(string s) { - if (s.empty()) { - return -1; - } - return atoi(s.c_str()); -} - -string stringify_list(list n) -{ - string buf; - list::iterator begin = n.begin(); - list::iterator end = n.end(); - - for (;begin != end;begin++) { - buf += *begin; - } - - return buf; -} - -#endif From 8efcbd71b4ddcc3107bc9d636272a3d1f1117b88 Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Sat, 15 Jul 2017 13:08:38 +0900 Subject: [PATCH 31/52] Remove unnecessary include --- mathcalc.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/mathcalc.cpp b/mathcalc.cpp index c899bae..bcde18c 100644 --- a/mathcalc.cpp +++ b/mathcalc.cpp @@ -10,7 +10,6 @@ using namespace std; #include "types.h" #include "utils.h" #include "bracket.h" -#include "calcurate.h" #include "calculator.h" From 5de8367279f4405614bb1f093e2a75bda9f5ea83 Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Thu, 18 Apr 2019 18:34:47 +0900 Subject: [PATCH 32/52] wip --- calculator.cpp | 13 +++++-------- calculator.h | 8 ++++---- mathcalc.cpp | 6 +++--- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/calculator.cpp b/calculator.cpp index fc7b203..6c3f66a 100644 --- a/calculator.cpp +++ b/calculator.cpp @@ -11,11 +11,7 @@ Calculator::Calculator(std::string expressions) { return; } -Calculator::~Calculator() { - return; -} - -void Calculator::parseStringExpressions() { +void Calculator::parse() { StringExpression stringExpression(this->stringExpressions); while (1) { @@ -34,19 +30,20 @@ void Calculator::parseStringExpressions() { return; } -void Calculator::printExpressions() { +void Calculator::print() { std::string stringifiedExpression = this->expressionList.getStringified(); std::cout << stringifiedExpression << std::endl; return; } -void Calculator::validateExpressions() { +void Calculator::validate() { this->expressionList.validateBracketsParing(); this->expressionList.validateDuplicatedSymbol(); this->expressionList.validateLonelySymbol(); return; } -void Calculator::execCalculation() { +void Calculator::run() { + // TODO: あとでつくる return; } diff --git a/calculator.h b/calculator.h index d7b19bb..e2aaaa0 100644 --- a/calculator.h +++ b/calculator.h @@ -17,10 +17,10 @@ class Calculator { Calculator(std::string expressions); ~Calculator(); - void parseStringExpressions(); - void printExpressions(); - void validateExpressions(); - void execCalculation(); + void parse(); + void print(); + void validate(); + void run(); }; #endif // __CALCULATOR_H__ diff --git a/mathcalc.cpp b/mathcalc.cpp index 04480b8..d032251 100644 --- a/mathcalc.cpp +++ b/mathcalc.cpp @@ -22,9 +22,9 @@ int main(int argc, char *argv[]) Calculator calc(expressions); try { - calc.parseStringExpressions(); - calc.validateExpressions(); - calc.printExpressions(); + calc.parse(); + calc.validate(); + calc.print(); } catch (struct Exception e) { std::cerr << "Error: " << e.msg() << std::endl; exit(1); From 9631b7160076239ddee09433ef727e588359c1bd Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Thu, 18 Apr 2019 18:39:59 +0900 Subject: [PATCH 33/52] wip --- calculator.cpp | 9 ++------- calculator.h | 6 +----- mathcalc.cpp | 4 ++-- stringExpression.cpp | 2 +- stringExpression.h | 2 +- types.h | 2 ++ 6 files changed, 9 insertions(+), 16 deletions(-) diff --git a/calculator.cpp b/calculator.cpp index 6c3f66a..4e0800b 100644 --- a/calculator.cpp +++ b/calculator.cpp @@ -6,13 +6,8 @@ #include "calculator.h" #include "stringExpression.h" -Calculator::Calculator(std::string expressions) { - this->stringExpressions = expressions; - return; -} - -void Calculator::parse() { - StringExpression stringExpression(this->stringExpressions); +void Calculator::parse(std::string expressions) { + StringExpression stringExpression(expressions); while (1) { stringExpression.begin(); diff --git a/calculator.h b/calculator.h index e2aaaa0..6b56877 100644 --- a/calculator.h +++ b/calculator.h @@ -10,14 +10,10 @@ class Calculator { private: // struct PROGRESSION_FLAGS pflags; - std::string stringExpressions; ExpressionList expressionList; public: - Calculator(std::string expressions); - ~Calculator(); - - void parse(); + void parse(std::string expressions); void print(); void validate(); void run(); diff --git a/mathcalc.cpp b/mathcalc.cpp index d032251..e392d49 100644 --- a/mathcalc.cpp +++ b/mathcalc.cpp @@ -20,9 +20,9 @@ int main(int argc, char *argv[]) // struct PROGRESSION_FLAGS pflags; string expressions = argv[1]; - Calculator calc(expressions); + Calculator calc; try { - calc.parse(); + calc.parse(expressions); calc.validate(); calc.print(); } catch (struct Exception e) { diff --git a/stringExpression.cpp b/stringExpression.cpp index f613859..75dcd4d 100644 --- a/stringExpression.cpp +++ b/stringExpression.cpp @@ -36,7 +36,7 @@ StringExpression::StringExpression(std::string expressions) { this->currentPos = 0; } -struct ExpressionList StringExpression::getExpressionsList() { +ExpressionList StringExpression::getExpressionsList() { return this->expressionList; } diff --git a/stringExpression.h b/stringExpression.h index a95cefc..37039f5 100644 --- a/stringExpression.h +++ b/stringExpression.h @@ -21,7 +21,7 @@ class StringExpression { public: StringExpression(std::string expressions); - struct ExpressionList getExpressionsList(); + ExpressionList getExpressionsList(); bool hasNextExpression(); void parseNumberValue(); diff --git a/types.h b/types.h index f8a538b..abc5dfb 100644 --- a/types.h +++ b/types.h @@ -65,6 +65,7 @@ struct Exception { }; }; +/* struct EXP_DIVIDER_RESULT { int left_value; int right_value; @@ -77,5 +78,6 @@ struct PROGRESSION_FLAGS { bool exists_pls_and_mns; bool go_through; }; +*/ #endif From 66218bddfa5249bf67af53f11235dd5131095ffe Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Fri, 19 Apr 2019 11:42:13 +0900 Subject: [PATCH 34/52] Introduced minunit --- .gitignore | 1 + Makefile | 6 +- test/Makefile | 15 ++ test/test | Bin 0 -> 10644 bytes test/test.cpp | 26 ++++ vendors/minunit.h | 386 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 431 insertions(+), 3 deletions(-) create mode 100644 test/Makefile create mode 100755 test/test create mode 100644 test/test.cpp create mode 100644 vendors/minunit.h diff --git a/.gitignore b/.gitignore index 6e20617..2572c09 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ mathcalc core /*.dSYM +/test/*.dSYM diff --git a/Makefile b/Makefile index 92e1bfe..abc8815 100644 --- a/Makefile +++ b/Makefile @@ -5,15 +5,15 @@ TARGET = mathcalc mathcalc: $(SOURCE) g++ $(DBG_OPTS) -o $(TARGET) $(SOURCE) -.PHONY: clean run debug release +.PHONY: clean run release clean: $(shell [ -e $(TARGET) ] && rm $(TARGET)) + $(shell [ -d $(TARGET).dSYM ] && rm -r $(TARGET).dSYM) $(shell [ -e core ] && rm core) - $(shell [ -d mathcalc.dSYM ] && rm -r mathcalc.dSYM) @echo Done run: $(TARGET) ./mathcalc release: mathcalc.cpp - g++ -o $(TARGET) $(SOURCE) + g++ -o $(TARGET) $(SOURCE) \ No newline at end of file diff --git a/test/Makefile b/test/Makefile new file mode 100644 index 0000000..d2ce4c8 --- /dev/null +++ b/test/Makefile @@ -0,0 +1,15 @@ +DBG_OPTS = -O0 -g3 -Wall +TEST_SOURCE = test.cpp +TARGET = test + +test: $(TEST_SOURCE) + g++ $(DBG_OPTS) -o $(TARGET) $(TEST_SOURCE) -I ../vendors + +.PHONY: clean +clean: $(TARGET) + $(shell [ -e $(TARGET) ] && rm $(TARGET)) + $(shell [ -d $(TARGET).dSYM ] && rm -r $(TARGET).dSYM) + @echo Done + +run: $(TARGET) + ./$(TARGET) \ No newline at end of file diff --git a/test/test b/test/test new file mode 100755 index 0000000000000000000000000000000000000000..397e041061dfa2b24a09625c515bacf10f1a6c3f GIT binary patch literal 10644 zcmeHNe{5UT6~1Zftqp|dPHolwU^P`;(w5mt+LW}&;IwryBkjs)w1Qe*oY=0-{3G?V zL&I8Ba5~NPtYuR{Y#LRE##Rlcb*w`m(Y36MWYd6x&|p)Aq6v^Ab*#$%AZ&>2`|i8X zes<{8{k1>lE8Tm~cg{WMo^$Sf*Ynhh|i)1 zQFVPs_pO1u17H0LxwWOK7Jee4omNbs>pKIxcGeD&`kdHa6ozjC?ubt*nXYFe`?J-7 z_4Xz(koNATA`0<5lQi3T;*cHcx{=NG8x>Z)y^YtH_SUNr7Pb`TD|=P{Xitwt5`&Qp z2G!deRQB#w28fyWN}1)~Ksseq1Fp9>q3rpU0pgl?D~3%%*9VMjCZ5`>`1ST&ni=m= zB`3C*8ebvM^<3({cq**NQ_-}b7TI2 zHETTW9yk&2mwt8G#?R|`2g_AE{?1_|8%cULDHV(sVkIj5n~(glU!^ef`TtmYgN1j4 z`HOvHfsYt|;ox+zkh`RP?d+81Y|*i4qnc-2JIpPm9qD9iih3#{mlS;tZCS$-Olb=p z*F#u-T!G~P!Mu;1&_=lIGmDo1j5!vOg;M@0KEaBP7Qp=3rh;Q03Ezi2Dzp(Uai*Tvp1Yv)ZBlEsja0Ws0ycPBt;8|hc51q?|y|*HA(K|Ej)i{?J_9h6+rxoyV;=GSt zCG2*9F^88dl=4H^7)3`bpbUE%3Ev`0D`EdaW@dpkgkS0q-fK|g6Ck7S{FhwgXTg6C z{6gRq!rse_AnfmOWZw5Co*zg~T49d^SHp&>?VP9cF(!HOuOKY8JPaBpqtfJ4<*hJ) z7z5?<1yIn~0u3@Ia4ZDMQ`&CFW@rFx17~;3%~YRLzFw-IC_0FP`4{{4oKxqEOFdfK zFq@g|AOk(V zzOZ+CvPCI=Xzt|z7Qq>q4nFuIld|Vrl*Ln%ulz-)+)vmT{V-W{q^u`30s{N!i!fCP ze1udxO#_$JUoQ1MmcHB6e;YGjnf_lf6FwKLN9JAgkvVq7`cGm0XZfwT@KUg}_z@&KdWE=tpBPDJI{;BF4B*lNU(w%h%-Vq;4QJ zPHHQu-;nAfb&6Dq)XzvgKhhDci5GU7+K>67pRb7L^9bpewj%a4aF0=OoZdM##3=4774rJDVN*R6?GXA z{D2D^uGUN>lvq{A8_c8!Ry8*Zk8m3lIU)|>bx7lkw*+N+`Il-lH035n{OL(s+gh$E^$s`_K9KcV`k zRsX!|UsC;f)z_JAz(xid8E9mnk%2}A8X0J0ppk(_1{xV?WT26OMg|%g_#b3o;qs+- zaoW%Lb-oG@r*qjsaYx2E>gUTa`Ep$E$NOEp%sy1ct%@v7qaw2u8Uu;+z}E$U3m_Uz zEBCdQHbfkg@-|}#OstkT#r)rJ%wWmKdJDb!exeDED2v6YY23S0T`DQx9qmxWclX@#wPj3{j71Jetw z3CToZT9%+{Agt$+_oRRvCd>6`?buU?`F|g~yt@wf*5P;^-dBgeQHS{~+wB+Y@Xf%t zp!!ic_a@ZMs9R8ZSU$+$Nh88W=dP&PIYN(^{;3GS_4f_sNT8E-V5 zz*%m1*L8Y_V(Y{E_pjZsKHRyXEw(><|Bd5`=V{eKJQMc|D~?BuJ(=&Pg+>v z-rdu_Hkp&|WOUxm4+)RQI}}NU)3BS2r*f%y))PZwFtVdEa$qnwBzc{5tKCL-*yy!3 zdYwtPcPX24GIL(jctyas0d~P!OT&m{bA#sKbroJVl+76?v)y85a$HI2uo-~PmkN^A z(Pj-wgp90?&r12}EJv@mMA=9v6Hec!j8x__Uw6>V?5qXa+qw9g)2Si65;145Pi!NH KS^dAr;lBZe-~i|V literal 0 HcmV?d00001 diff --git a/test/test.cpp b/test/test.cpp new file mode 100644 index 0000000..b40c5cb --- /dev/null +++ b/test/test.cpp @@ -0,0 +1,26 @@ +#include "minunit.h" + +MU_TEST(test1) { + mu_check(1 == 1); +} + +MU_TEST(test2) { + mu_check(2 == 2); +} + +MU_TEST(test3) { + mu_check(3 == 3); +} + +MU_TEST(test4) { + mu_check(2 == 1); +} + +int main(int argc, char *argv[]) { + MU_RUN_TEST(test1); + MU_RUN_TEST(test2); + MU_RUN_TEST(test3); + MU_RUN_TEST(test4); + MU_REPORT(); + return minunit_status; +} \ No newline at end of file diff --git a/vendors/minunit.h b/vendors/minunit.h new file mode 100644 index 0000000..aaa83b6 --- /dev/null +++ b/vendors/minunit.h @@ -0,0 +1,386 @@ +/* + * Copyright (c) 2012 David Siñuela Pastor, siu.4coders@gmail.com + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#ifndef MINUNIT_MINUNIT_H +#define MINUNIT_MINUNIT_H + +#ifdef __cplusplus + extern "C" { +#endif + +#if defined(_WIN32) +#include +#if defined(_MSC_VER) && _MSC_VER < 1900 + #define snprintf _snprintf + #define __func__ __FUNCTION__ +#endif + +#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__)) + +/* Change POSIX C SOURCE version for pure c99 compilers */ +#if !defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 200112L +#undef _POSIX_C_SOURCE +#define _POSIX_C_SOURCE 200112L +#endif + +#include /* POSIX flags */ +#include /* clock_gettime(), time() */ +#include /* gethrtime(), gettimeofday() */ +#include +#include +#include + +#if defined(__MACH__) && defined(__APPLE__) +#include +#include +#endif + +#else +#error "Unable to define timers for an unknown OS." +#endif + +#include +#include + +/* Maximum length of last message */ +#define MINUNIT_MESSAGE_LEN 1024 +/* Accuracy with which floats are compared */ +#define MINUNIT_EPSILON 1E-12 + +/* Misc. counters */ +static int minunit_run = 0; +static int minunit_assert = 0; +static int minunit_fail = 0; +static int minunit_status = 0; + +/* Timers */ +static double minunit_real_timer = 0; +static double minunit_proc_timer = 0; + +/* Last message */ +static char minunit_last_message[MINUNIT_MESSAGE_LEN]; + +/* Test setup and teardown function pointers */ +static void (*minunit_setup)(void) = NULL; +static void (*minunit_teardown)(void) = NULL; + +/* Definitions */ +#define MU_TEST(method_name) static void method_name(void) +#define MU_TEST_SUITE(suite_name) static void suite_name(void) + +#define MU__SAFE_BLOCK(block) do {\ + block\ +} while(0) + +/* Run test suite and unset setup and teardown functions */ +#define MU_RUN_SUITE(suite_name) MU__SAFE_BLOCK(\ + suite_name();\ + minunit_setup = NULL;\ + minunit_teardown = NULL;\ +) + +/* Configure setup and teardown functions */ +#define MU_SUITE_CONFIGURE(setup_fun, teardown_fun) MU__SAFE_BLOCK(\ + minunit_setup = setup_fun;\ + minunit_teardown = teardown_fun;\ +) + +/* Test runner */ +#define MU_RUN_TEST(test) MU__SAFE_BLOCK(\ + if (minunit_real_timer==0 && minunit_proc_timer==0) {\ + minunit_real_timer = mu_timer_real();\ + minunit_proc_timer = mu_timer_cpu();\ + }\ + if (minunit_setup) (*minunit_setup)();\ + minunit_status = 0;\ + test();\ + minunit_run++;\ + if (minunit_status) {\ + minunit_fail++;\ + printf("F");\ + printf("\n%s\n", minunit_last_message);\ + }\ + fflush(stdout);\ + if (minunit_teardown) (*minunit_teardown)();\ +) + +/* Report */ +#define MU_REPORT() MU__SAFE_BLOCK(\ + double minunit_end_real_timer;\ + double minunit_end_proc_timer;\ + printf("\n\n%d tests, %d assertions, %d failures\n", minunit_run, minunit_assert, minunit_fail);\ + minunit_end_real_timer = mu_timer_real();\ + minunit_end_proc_timer = mu_timer_cpu();\ + printf("\nFinished in %.8f seconds (real) %.8f seconds (proc)\n\n",\ + minunit_end_real_timer - minunit_real_timer,\ + minunit_end_proc_timer - minunit_proc_timer);\ +) + +/* Assertions */ +#define mu_check(test) MU__SAFE_BLOCK(\ + minunit_assert++;\ + if (!(test)) {\ + snprintf(minunit_last_message, MINUNIT_MESSAGE_LEN, "%s failed:\n\t%s:%d: %s", __func__, __FILE__, __LINE__, #test);\ + minunit_status = 1;\ + return;\ + } else {\ + printf(".");\ + }\ +) + +#define mu_fail(message) MU__SAFE_BLOCK(\ + minunit_assert++;\ + snprintf(minunit_last_message, MINUNIT_MESSAGE_LEN, "%s failed:\n\t%s:%d: %s", __func__, __FILE__, __LINE__, message);\ + minunit_status = 1;\ + return;\ +) + +#define mu_assert(test, message) MU__SAFE_BLOCK(\ + minunit_assert++;\ + if (!(test)) {\ + snprintf(minunit_last_message, MINUNIT_MESSAGE_LEN, "%s failed:\n\t%s:%d: %s", __func__, __FILE__, __LINE__, message);\ + minunit_status = 1;\ + return;\ + } else {\ + printf(".");\ + }\ +) + +#define mu_assert_int_eq(expected, result) MU__SAFE_BLOCK(\ + int minunit_tmp_e;\ + int minunit_tmp_r;\ + minunit_assert++;\ + minunit_tmp_e = (expected);\ + minunit_tmp_r = (result);\ + if (minunit_tmp_e != minunit_tmp_r) {\ + snprintf(minunit_last_message, MINUNIT_MESSAGE_LEN, "%s failed:\n\t%s:%d: %d expected but was %d", __func__, __FILE__, __LINE__, minunit_tmp_e, minunit_tmp_r);\ + minunit_status = 1;\ + return;\ + } else {\ + printf(".");\ + }\ +) + +#define mu_assert_double_eq(expected, result) MU__SAFE_BLOCK(\ + double minunit_tmp_e;\ + double minunit_tmp_r;\ + minunit_assert++;\ + minunit_tmp_e = (expected);\ + minunit_tmp_r = (result);\ + if (fabs(minunit_tmp_e-minunit_tmp_r) > MINUNIT_EPSILON) {\ + int minunit_significant_figures = 1 - log10(MINUNIT_EPSILON);\ + snprintf(minunit_last_message, MINUNIT_MESSAGE_LEN, "%s failed:\n\t%s:%d: %.*g expected but was %.*g", __func__, __FILE__, __LINE__, minunit_significant_figures, minunit_tmp_e, minunit_significant_figures, minunit_tmp_r);\ + minunit_status = 1;\ + return;\ + } else {\ + printf(".");\ + }\ +) + +#define mu_assert_string_eq(expected, result) MU__SAFE_BLOCK(\ + const char* minunit_tmp_e = expected;\ + const char* minunit_tmp_r = result;\ + minunit_assert++;\ + if (!minunit_tmp_e) {\ + minunit_tmp_e = "";\ + }\ + if (!minunit_tmp_r) {\ + minunit_tmp_r = "";\ + }\ + if(strcmp(minunit_tmp_e, minunit_tmp_r)) {\ + snprintf(minunit_last_message, MINUNIT_MESSAGE_LEN, "%s failed:\n\t%s:%d: '%s' expected but was '%s'", __func__, __FILE__, __LINE__, minunit_tmp_e, minunit_tmp_r);\ + minunit_status = 1;\ + return;\ + } else {\ + printf(".");\ + }\ +) + +/* + * The following two functions were written by David Robert Nadeau + * from http://NadeauSoftware.com/ and distributed under the + * Creative Commons Attribution 3.0 Unported License + */ + +/** + * Returns the real time, in seconds, or -1.0 if an error occurred. + * + * Time is measured since an arbitrary and OS-dependent start time. + * The returned real time is only useful for computing an elapsed time + * between two calls to this function. + */ +static double mu_timer_real(void) +{ +#if defined(_WIN32) + /* Windows 2000 and later. ---------------------------------- */ + LARGE_INTEGER Time; + LARGE_INTEGER Frequency; + + QueryPerformanceFrequency(&Frequency); + QueryPerformanceCounter(&Time); + + Time.QuadPart *= 1000000; + Time.QuadPart /= Frequency.QuadPart; + + return (double)Time.QuadPart / 1000000.0; + +#elif (defined(__hpux) || defined(hpux)) || ((defined(__sun__) || defined(__sun) || defined(sun)) && (defined(__SVR4) || defined(__svr4__))) + /* HP-UX, Solaris. ------------------------------------------ */ + return (double)gethrtime( ) / 1000000000.0; + +#elif defined(__MACH__) && defined(__APPLE__) + /* OSX. ----------------------------------------------------- */ + static double timeConvert = 0.0; + if ( timeConvert == 0.0 ) + { + mach_timebase_info_data_t timeBase; + (void)mach_timebase_info( &timeBase ); + timeConvert = (double)timeBase.numer / + (double)timeBase.denom / + 1000000000.0; + } + return (double)mach_absolute_time( ) * timeConvert; + +#elif defined(_POSIX_VERSION) + /* POSIX. --------------------------------------------------- */ + struct timeval tm; +#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) + { + struct timespec ts; +#if defined(CLOCK_MONOTONIC_PRECISE) + /* BSD. --------------------------------------------- */ + const clockid_t id = CLOCK_MONOTONIC_PRECISE; +#elif defined(CLOCK_MONOTONIC_RAW) + /* Linux. ------------------------------------------- */ + const clockid_t id = CLOCK_MONOTONIC_RAW; +#elif defined(CLOCK_HIGHRES) + /* Solaris. ----------------------------------------- */ + const clockid_t id = CLOCK_HIGHRES; +#elif defined(CLOCK_MONOTONIC) + /* AIX, BSD, Linux, POSIX, Solaris. ----------------- */ + const clockid_t id = CLOCK_MONOTONIC; +#elif defined(CLOCK_REALTIME) + /* AIX, BSD, HP-UX, Linux, POSIX. ------------------- */ + const clockid_t id = CLOCK_REALTIME; +#else + const clockid_t id = (clockid_t)-1; /* Unknown. */ +#endif /* CLOCK_* */ + if ( id != (clockid_t)-1 && clock_gettime( id, &ts ) != -1 ) + return (double)ts.tv_sec + + (double)ts.tv_nsec / 1000000000.0; + /* Fall thru. */ + } +#endif /* _POSIX_TIMERS */ + + /* AIX, BSD, Cygwin, HP-UX, Linux, OSX, POSIX, Solaris. ----- */ + gettimeofday( &tm, NULL ); + return (double)tm.tv_sec + (double)tm.tv_usec / 1000000.0; +#else + return -1.0; /* Failed. */ +#endif +} + +/** + * Returns the amount of CPU time used by the current process, + * in seconds, or -1.0 if an error occurred. + */ +static double mu_timer_cpu(void) +{ +#if defined(_WIN32) + /* Windows -------------------------------------------------- */ + FILETIME createTime; + FILETIME exitTime; + FILETIME kernelTime; + FILETIME userTime; + + /* This approach has a resolution of 1/64 second. Unfortunately, Windows' API does not offer better */ + if ( GetProcessTimes( GetCurrentProcess( ), + &createTime, &exitTime, &kernelTime, &userTime ) != 0 ) + { + ULARGE_INTEGER userSystemTime; + memcpy(&userSystemTime, &userTime, sizeof(ULARGE_INTEGER)); + return (double)userSystemTime.QuadPart / 10000000.0; + } + +#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__)) + /* AIX, BSD, Cygwin, HP-UX, Linux, OSX, and Solaris --------- */ + +#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) + /* Prefer high-res POSIX timers, when available. */ + { + clockid_t id; + struct timespec ts; +#if _POSIX_CPUTIME > 0 + /* Clock ids vary by OS. Query the id, if possible. */ + if ( clock_getcpuclockid( 0, &id ) == -1 ) +#endif +#if defined(CLOCK_PROCESS_CPUTIME_ID) + /* Use known clock id for AIX, Linux, or Solaris. */ + id = CLOCK_PROCESS_CPUTIME_ID; +#elif defined(CLOCK_VIRTUAL) + /* Use known clock id for BSD or HP-UX. */ + id = CLOCK_VIRTUAL; +#else + id = (clockid_t)-1; +#endif + if ( id != (clockid_t)-1 && clock_gettime( id, &ts ) != -1 ) + return (double)ts.tv_sec + + (double)ts.tv_nsec / 1000000000.0; + } +#endif + +#if defined(RUSAGE_SELF) + { + struct rusage rusage; + if ( getrusage( RUSAGE_SELF, &rusage ) != -1 ) + return (double)rusage.ru_utime.tv_sec + + (double)rusage.ru_utime.tv_usec / 1000000.0; + } +#endif + +#if defined(_SC_CLK_TCK) + { + const double ticks = (double)sysconf( _SC_CLK_TCK ); + struct tms tms; + if ( times( &tms ) != (clock_t)-1 ) + return (double)tms.tms_utime / ticks; + } +#endif + +#if defined(CLOCKS_PER_SEC) + { + clock_t cl = clock( ); + if ( cl != (clock_t)-1 ) + return (double)cl / (double)CLOCKS_PER_SEC; + } +#endif + +#endif + + return -1; /* Failed. */ +} + +#ifdef __cplusplus +} +#endif + +#endif /* MINUNIT_MINUNIT_H */ From 4b65fef4c8b2c4d884d73bfabfe4185f2c9b9270 Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Fri, 19 Apr 2019 11:56:26 +0900 Subject: [PATCH 35/52] Update gitignore --- .gitignore | 1 + test/test | Bin 10644 -> 0 bytes 2 files changed, 1 insertion(+) delete mode 100755 test/test diff --git a/.gitignore b/.gitignore index 2572c09..1b52725 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ mathcalc core /*.dSYM /test/*.dSYM +/test/test diff --git a/test/test b/test/test deleted file mode 100755 index 397e041061dfa2b24a09625c515bacf10f1a6c3f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 10644 zcmeHNe{5UT6~1Zftqp|dPHolwU^P`;(w5mt+LW}&;IwryBkjs)w1Qe*oY=0-{3G?V zL&I8Ba5~NPtYuR{Y#LRE##Rlcb*w`m(Y36MWYd6x&|p)Aq6v^Ab*#$%AZ&>2`|i8X zes<{8{k1>lE8Tm~cg{WMo^$Sf*Ynhh|i)1 zQFVPs_pO1u17H0LxwWOK7Jee4omNbs>pKIxcGeD&`kdHa6ozjC?ubt*nXYFe`?J-7 z_4Xz(koNATA`0<5lQi3T;*cHcx{=NG8x>Z)y^YtH_SUNr7Pb`TD|=P{Xitwt5`&Qp z2G!deRQB#w28fyWN}1)~Ksseq1Fp9>q3rpU0pgl?D~3%%*9VMjCZ5`>`1ST&ni=m= zB`3C*8ebvM^<3({cq**NQ_-}b7TI2 zHETTW9yk&2mwt8G#?R|`2g_AE{?1_|8%cULDHV(sVkIj5n~(glU!^ef`TtmYgN1j4 z`HOvHfsYt|;ox+zkh`RP?d+81Y|*i4qnc-2JIpPm9qD9iih3#{mlS;tZCS$-Olb=p z*F#u-T!G~P!Mu;1&_=lIGmDo1j5!vOg;M@0KEaBP7Qp=3rh;Q03Ezi2Dzp(Uai*Tvp1Yv)ZBlEsja0Ws0ycPBt;8|hc51q?|y|*HA(K|Ej)i{?J_9h6+rxoyV;=GSt zCG2*9F^88dl=4H^7)3`bpbUE%3Ev`0D`EdaW@dpkgkS0q-fK|g6Ck7S{FhwgXTg6C z{6gRq!rse_AnfmOWZw5Co*zg~T49d^SHp&>?VP9cF(!HOuOKY8JPaBpqtfJ4<*hJ) z7z5?<1yIn~0u3@Ia4ZDMQ`&CFW@rFx17~;3%~YRLzFw-IC_0FP`4{{4oKxqEOFdfK zFq@g|AOk(V zzOZ+CvPCI=Xzt|z7Qq>q4nFuIld|Vrl*Ln%ulz-)+)vmT{V-W{q^u`30s{N!i!fCP ze1udxO#_$JUoQ1MmcHB6e;YGjnf_lf6FwKLN9JAgkvVq7`cGm0XZfwT@KUg}_z@&KdWE=tpBPDJI{;BF4B*lNU(w%h%-Vq;4QJ zPHHQu-;nAfb&6Dq)XzvgKhhDci5GU7+K>67pRb7L^9bpewj%a4aF0=OoZdM##3=4774rJDVN*R6?GXA z{D2D^uGUN>lvq{A8_c8!Ry8*Zk8m3lIU)|>bx7lkw*+N+`Il-lH035n{OL(s+gh$E^$s`_K9KcV`k zRsX!|UsC;f)z_JAz(xid8E9mnk%2}A8X0J0ppk(_1{xV?WT26OMg|%g_#b3o;qs+- zaoW%Lb-oG@r*qjsaYx2E>gUTa`Ep$E$NOEp%sy1ct%@v7qaw2u8Uu;+z}E$U3m_Uz zEBCdQHbfkg@-|}#OstkT#r)rJ%wWmKdJDb!exeDED2v6YY23S0T`DQx9qmxWclX@#wPj3{j71Jetw z3CToZT9%+{Agt$+_oRRvCd>6`?buU?`F|g~yt@wf*5P;^-dBgeQHS{~+wB+Y@Xf%t zp!!ic_a@ZMs9R8ZSU$+$Nh88W=dP&PIYN(^{;3GS_4f_sNT8E-V5 zz*%m1*L8Y_V(Y{E_pjZsKHRyXEw(><|Bd5`=V{eKJQMc|D~?BuJ(=&Pg+>v z-rdu_Hkp&|WOUxm4+)RQI}}NU)3BS2r*f%y))PZwFtVdEa$qnwBzc{5tKCL-*yy!3 zdYwtPcPX24GIL(jctyas0d~P!OT&m{bA#sKbroJVl+76?v)y85a$HI2uo-~PmkN^A z(Pj-wgp90?&r12}EJv@mMA=9v6Hec!j8x__Uw6>V?5qXa+qw9g)2Si65;145Pi!NH KS^dAr;lBZe-~i|V From 846e4683fad419ebfd3dc760c4fd9e277e912ea2 Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Fri, 19 Apr 2019 12:23:01 +0900 Subject: [PATCH 36/52] wip --- .gitignore | 1 + calculator.cpp | 6 ++++ calculator.h | 3 -- exception.h | 20 ++++++++++++++ expression.h | 27 ++++++++++++++++++ expressionList.cpp | 3 +- expressionList.h | 2 +- mathcalc.cpp | 2 +- stringExpression.cpp | 18 ++++++------ stringExpression.h | 2 +- symbol.h | 32 +++++++++++++++++++++ types.h | 66 +------------------------------------------- 12 files changed, 101 insertions(+), 81 deletions(-) create mode 100644 exception.h create mode 100644 expression.h create mode 100644 symbol.h diff --git a/.gitignore b/.gitignore index 1b52725..481f66e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.vscode mathcalc core /*.dSYM diff --git a/calculator.cpp b/calculator.cpp index 4e0800b..6993407 100644 --- a/calculator.cpp +++ b/calculator.cpp @@ -6,6 +6,7 @@ #include "calculator.h" #include "stringExpression.h" +// Parses string expressions of std::string into ExpressionList void Calculator::parse(std::string expressions) { StringExpression stringExpression(expressions); @@ -25,12 +26,15 @@ void Calculator::parse(std::string expressions) { return; } +// Prints stringified ExpressionList into STDOUT void Calculator::print() { std::string stringifiedExpression = this->expressionList.getStringified(); std::cout << stringifiedExpression << std::endl; return; } +// Checks out if this->expressionList has any invalid expression, and throws exceptions. +// his method expects to be called after Calculator::parse because this->expressionList is populated by it. void Calculator::validate() { this->expressionList.validateBracketsParing(); this->expressionList.validateDuplicatedSymbol(); @@ -39,6 +43,8 @@ void Calculator::validate() { } void Calculator::run() { + + // TODO: あとでつくる return; } diff --git a/calculator.h b/calculator.h index 6b56877..cac97e2 100644 --- a/calculator.h +++ b/calculator.h @@ -3,13 +3,10 @@ #include #include -#include "types.h" #include "expressionList.h" class Calculator { private: - // struct PROGRESSION_FLAGS pflags; - ExpressionList expressionList; public: diff --git a/exception.h b/exception.h new file mode 100644 index 0000000..15a4d99 --- /dev/null +++ b/exception.h @@ -0,0 +1,20 @@ +#ifndef EXCEPTION_H +#define EXCEPTION_H + +#include + +struct Exception { + std::string message; + + static struct Exception make(std::string message) { + struct Exception e; + e.message = message; + return e; + }; + + const char *msg() { + return this->message.c_str(); + }; +}; + +#endif \ No newline at end of file diff --git a/expression.h b/expression.h new file mode 100644 index 0000000..6f2c105 --- /dev/null +++ b/expression.h @@ -0,0 +1,27 @@ +#ifndef EXPRESSION_H +#define EXPRESSION_H + +#include + +struct EXPRESSION { + enum TYPES { + VALUE = 0, + SYMBOL + }; + + EXPRESSION::TYPES type; + std::string value; + + static struct EXPRESSION make(EXPRESSION::TYPES type, std::string value) { + struct EXPRESSION expression; + expression.type = type; + expression.value = value; + return expression; + }; + + bool operator==(std::string value) { + return (this->value == value); + } +}; + +#endif \ No newline at end of file diff --git a/expressionList.cpp b/expressionList.cpp index eba2664..9cd3ccd 100644 --- a/expressionList.cpp +++ b/expressionList.cpp @@ -1,7 +1,8 @@ #include #include #include -#include "types.h" +#include "expression.h" +#include "exception.h" #include "expressionList.h" void ExpressionList::add(struct EXPRESSION expression) { diff --git a/expressionList.h b/expressionList.h index ee61e41..9207c50 100644 --- a/expressionList.h +++ b/expressionList.h @@ -3,7 +3,7 @@ #include #include -#include "types.h" +#include "expression.h" class ExpressionList { private: diff --git a/mathcalc.cpp b/mathcalc.cpp index e392d49..4df89db 100644 --- a/mathcalc.cpp +++ b/mathcalc.cpp @@ -5,10 +5,10 @@ #include #include #include +#include "exception.h" using namespace std; #include "calculator.h" -#include "types.h" int main(int argc, char *argv[]) { diff --git a/stringExpression.cpp b/stringExpression.cpp index 75dcd4d..e44e723 100644 --- a/stringExpression.cpp +++ b/stringExpression.cpp @@ -1,7 +1,7 @@ #include #include #include -#include "types.h" +#include "expression.h" #include "stringExpression.h" #include "expressionList.h" @@ -10,12 +10,12 @@ void StringExpression::addSymbol(struct SYMBOL symbol) { } void StringExpression::defineSymbols() { - this->addSymbol(SYMBOL::makeSymbol("+", SYMBOL::PLUS)); - this->addSymbol(SYMBOL::makeSymbol("-", SYMBOL::MINUS)); - this->addSymbol(SYMBOL::makeSymbol("*", SYMBOL::MULTIPLY)); - this->addSymbol(SYMBOL::makeSymbol("/", SYMBOL::DIVIDE)); - this->addSymbol(SYMBOL::makeSymbol("(", SYMBOL::BRACKET_BEGIN)); - this->addSymbol(SYMBOL::makeSymbol(")", SYMBOL::BRACKET_END)); + this->addSymbol(SYMBOL::make("+", SYMBOL::PLUS)); + this->addSymbol(SYMBOL::make("-", SYMBOL::MINUS)); + this->addSymbol(SYMBOL::make("*", SYMBOL::MULTIPLY)); + this->addSymbol(SYMBOL::make("/", SYMBOL::DIVIDE)); + this->addSymbol(SYMBOL::make("(", SYMBOL::BRACKET_BEGIN)); + this->addSymbol(SYMBOL::make(")", SYMBOL::BRACKET_END)); } std::string::size_type StringExpression::getNextExpressionPos() { @@ -60,11 +60,11 @@ bool StringExpression::hasNextExpression() { void StringExpression::parseNumberValue() { std::string valuePart = this->expressions.substr(this->currentPos, this->nextExpressionPos - this->currentPos); - this->expressionList.add(EXPRESSION::makeExpression(EXPRESSION::VALUE, valuePart)); + this->expressionList.add(EXPRESSION::make(EXPRESSION::VALUE, valuePart)); } void StringExpression::parseExpression() { std::string symbolPart = this->expressions.substr(nextExpressionPos, 1); - this->expressionList.add(EXPRESSION::makeExpression(EXPRESSION::SYMBOL, symbolPart)); + this->expressionList.add(EXPRESSION::make(EXPRESSION::SYMBOL, symbolPart)); } diff --git a/stringExpression.h b/stringExpression.h index 37039f5..18d6641 100644 --- a/stringExpression.h +++ b/stringExpression.h @@ -3,7 +3,7 @@ #include #include -#include "types.h" +#include "symbol.h" #include "expressionList.h" class StringExpression { diff --git a/symbol.h b/symbol.h new file mode 100644 index 0000000..b85a78b --- /dev/null +++ b/symbol.h @@ -0,0 +1,32 @@ +#ifndef SYMBOL_H +#define SYMBOL_H + +#include + +struct SYMBOL { + enum TYPES { + PLUS = 0, + MINUS, + DIVIDE, + MULTIPLY, + BRACKET_BEGIN, + BRACKET_END, + OTHER + }; + + SYMBOL::TYPES type; + std::string character; + + static struct SYMBOL make(std::string character, SYMBOL::TYPES type) { + struct SYMBOL symbol; + symbol.character = character; + symbol.type = type; + return symbol; + }; + + bool operator==(std::string character) { + return (this->character == character); + } +}; + +#endif // SYMBOL_H \ No newline at end of file diff --git a/types.h b/types.h index abc5dfb..3f787ec 100644 --- a/types.h +++ b/types.h @@ -1,70 +1,6 @@ #ifndef TYPES_H #define TYPES_H -#include -#include - -struct SYMBOL { - enum TYPES { - PLUS = 0, - MINUS, - DIVIDE, - MULTIPLY, - BRACKET_BEGIN, - BRACKET_END, - OTHER - }; - - SYMBOL::TYPES type; - std::string character; - - static struct SYMBOL makeSymbol(std::string character, SYMBOL::TYPES type) { - struct SYMBOL symbol; - symbol.character = character; - symbol.type = type; - return symbol; - }; - - bool operator==(std::string character) { - return (this->character == character); - } -}; - -struct EXPRESSION { - enum TYPES { - VALUE = 0, - SYMBOL - }; - - EXPRESSION::TYPES type; - std::string value; - - static struct EXPRESSION makeExpression(EXPRESSION::TYPES type, std::string value) { - struct EXPRESSION expression; - expression.type = type; - expression.value = value; - return expression; - }; - - bool operator==(std::string value) { - return (this->value == value); - } -}; - -struct Exception { - std::string message; - - static struct Exception make(std::string message) { - struct Exception e; - e.message = message; - return e; - }; - - const char *msg() { - return this->message.c_str(); - }; -}; - /* struct EXP_DIVIDER_RESULT { int left_value; @@ -80,4 +16,4 @@ struct PROGRESSION_FLAGS { }; */ -#endif +#endif // TYPES_H \ No newline at end of file From 49ed88abc1abff41ec704f057e0380b147d40ff8 Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Mon, 29 Apr 2019 17:19:00 +0900 Subject: [PATCH 37/52] Added calling run() --- mathcalc.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/mathcalc.cpp b/mathcalc.cpp index 4df89db..8c56c59 100644 --- a/mathcalc.cpp +++ b/mathcalc.cpp @@ -25,6 +25,7 @@ int main(int argc, char *argv[]) calc.parse(expressions); calc.validate(); calc.print(); + calc.run(); } catch (struct Exception e) { std::cerr << "Error: " << e.msg() << std::endl; exit(1); From 5c9667b327b86815c4c3fea07814726ff3a6dc1d Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Mon, 29 Apr 2019 23:05:53 +0900 Subject: [PATCH 38/52] Added unit test for RPN calc --- .circleci/config.yml | 10 +++++ Makefile | 10 +++-- calculator.cpp | 9 ++-- expression.h | 19 +++++++- expressionList.cpp | 31 +++++++------ expressionList.h | 4 +- rpn.cpp | 42 ++++++++++++++++++ rpn.h | 17 ++++++++ test/Makefile | 8 ++-- test/test.cpp | 67 +++++++++++++++++++++++------ {vendors => test/vendors}/minunit.h | 0 11 files changed, 173 insertions(+), 44 deletions(-) create mode 100644 .circleci/config.yml create mode 100644 rpn.cpp create mode 100644 rpn.h rename {vendors => test/vendors}/minunit.h (100%) diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..3b0e7e6 --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,10 @@ +version: 2 +jobs: + build: + docker: + - image: debian:stretch + + steps: + - checkout + - cd test && make run + diff --git a/Makefile b/Makefile index abc8815..6ed4686 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,13 @@ DBG_OPTS = -O0 -g3 -Wall -SOURCE = mathcalc.cpp calculator.cpp stringExpression.cpp expressionList.cpp TARGET = mathcalc +SOURCE = mathcalc.cpp \ + calculator.cpp \ + stringExpression.cpp \ + expressionList.cpp \ + rpn.cpp mathcalc: $(SOURCE) - g++ $(DBG_OPTS) -o $(TARGET) $(SOURCE) + g++ $(DBG_OPTS) -std=c++11 -o $(TARGET) $(SOURCE) .PHONY: clean run release clean: @@ -16,4 +20,4 @@ run: $(TARGET) ./mathcalc release: mathcalc.cpp - g++ -o $(TARGET) $(SOURCE) \ No newline at end of file + g++ -o $(TARGET) $(SOURCE) diff --git a/calculator.cpp b/calculator.cpp index 6993407..e1e6330 100644 --- a/calculator.cpp +++ b/calculator.cpp @@ -28,13 +28,12 @@ void Calculator::parse(std::string expressions) { // Prints stringified ExpressionList into STDOUT void Calculator::print() { - std::string stringifiedExpression = this->expressionList.getStringified(); - std::cout << stringifiedExpression << std::endl; + std::cout << this->expressionList.stringify() << std::endl; return; } -// Checks out if this->expressionList has any invalid expression, and throws exceptions. -// his method expects to be called after Calculator::parse because this->expressionList is populated by it. +// Checks out if this->expressionList has any invalid expression, and throws exceptions. +// his method expects to be called after Calculator::parse because this->expressionList is populated by it. void Calculator::validate() { this->expressionList.validateBracketsParing(); this->expressionList.validateDuplicatedSymbol(); @@ -43,8 +42,6 @@ void Calculator::validate() { } void Calculator::run() { - - // TODO: あとでつくる return; } diff --git a/expression.h b/expression.h index 6f2c105..11c6cf9 100644 --- a/expression.h +++ b/expression.h @@ -3,6 +3,9 @@ #include +// TODO: EXPRESSIONは単なるインターフェースにしてこれを満たす +// 数値クラス, 記号クラスを実装するほうがよい. 例えばtoLongメソッド +// などは記号に対してはどのように作用するか予測できないためよくない。 struct EXPRESSION { enum TYPES { VALUE = 0, @@ -19,9 +22,21 @@ struct EXPRESSION { return expression; }; - bool operator==(std::string value) { + bool operator==(std::string value) const { return (this->value == value); } + + inline bool isSymbol() const { + return this->type == SYMBOL; + } + + inline long toLong() const { + if (this->isSymbol()) { + return 0; + } else { + return std::stol(this->value); + } + } }; -#endif \ No newline at end of file +#endif // EXPRESSION_H diff --git a/expressionList.cpp b/expressionList.cpp index 9cd3ccd..5feee82 100644 --- a/expressionList.cpp +++ b/expressionList.cpp @@ -13,18 +13,6 @@ void ExpressionList::cleanupJunks() { this->expressions.remove_if(ExpressionList::emptyExpression); } -std::string ExpressionList::getStringified() { - std::string buffer; - std::list::iterator it = this->expressions.begin(); - std::list::iterator end = this->expressions.end(); - - for (; it != end; it++) { - buffer += it->value; - } - - return buffer; -} - void ExpressionList::validateBracketsParing() { size_t beginBracketCount = std::count(this->expressions.begin(), this->expressions.end(), std::string("(")); size_t endBracketCount = std::count(this->expressions.begin(), this->expressions.end(), std::string(")")); @@ -57,8 +45,8 @@ void ExpressionList::validateDuplicatedSymbol() { } void ExpressionList::validateLonelySymbol() { - std::list::iterator head = this->expressions.begin(); - std::list::iterator tail = this->expressions.end(); + std::list::const_iterator head = this->expressions.begin(); + std::list::const_iterator tail = this->expressions.end(); tail--; if ((head->type == EXPRESSION::SYMBOL && head->value != std::string("(")) || @@ -69,3 +57,18 @@ void ExpressionList::validateLonelySymbol() { return; } +std::string ExpressionList::stringify() const { + std::string buffer; + std::list::const_iterator it = this->expressions.begin(); + std::list::const_iterator end = this->expressions.end(); + + for (; it != end; it++) { + buffer += it->value; + } + + return buffer; +} + +std::list ExpressionList::toList() const { + return this->expressions; +} diff --git a/expressionList.h b/expressionList.h index 9207c50..43a8ccb 100644 --- a/expressionList.h +++ b/expressionList.h @@ -19,7 +19,9 @@ class ExpressionList { void validateBracketsParing(); void validateDuplicatedSymbol(); void validateLonelySymbol(); - std::string getStringified(); + + std::string stringify() const; + std::list toList() const; }; #endif // __EXPRESSION_LIST_H__ diff --git a/rpn.cpp b/rpn.cpp new file mode 100644 index 0000000..5c17f02 --- /dev/null +++ b/rpn.cpp @@ -0,0 +1,42 @@ +#include "rpn.h" +#include "expression.h" +#include +#include +#include + +RPN::RPN(std::list expressions) { + this->expressions = expressions; +} + +long RPN::result() const { + std::stack calcStack; + std::list::const_iterator it = this->expressions.begin(); + std::list::const_iterator end = this->expressions.end(); + + for (; it != end; it++) { + if ((*it).isSymbol()) { + const long left = calcStack.top(); + calcStack.pop(); + const long right = calcStack.top(); + calcStack.pop(); + calcStack.push(this->operate(left, right, (*it).value)); + } else { + calcStack.push((*it).toLong()); + } + } + + return calcStack.top(); +} + +// TODO: 予期しない記号が来た場合の処理が微妙なのでどうにかしたい +long RPN::operate(const long left, const long right, const std::string symbol) const { + if (symbol == "+") { + return left + right; + } else if (symbol == "-") { + return left - right; + } else if (symbol == "*") { + return left * right; + } else { // (symbol == "/") + return left / right; + } +} diff --git a/rpn.h b/rpn.h new file mode 100644 index 0000000..76a001b --- /dev/null +++ b/rpn.h @@ -0,0 +1,17 @@ +#ifndef RPN_H +#define RPN_H + +#include +#include + +class RPN { + private: + std::list expressions; + long operate(const long left, const long right, const std::string symbol) const; + + public: + RPN(std::list expressions); + long result() const; +}; + +#endif // RPN_H diff --git a/test/Makefile b/test/Makefile index d2ce4c8..aafaa37 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,15 +1,15 @@ DBG_OPTS = -O0 -g3 -Wall -TEST_SOURCE = test.cpp TARGET = test +TEST_SOURCE = test.cpp ../rpn.cpp test: $(TEST_SOURCE) - g++ $(DBG_OPTS) -o $(TARGET) $(TEST_SOURCE) -I ../vendors + g++ $(DBG_OPTS) -std=c++11 -o $(TARGET) $(TEST_SOURCE) -I vendors -.PHONY: clean +.PHONY: clean clean: $(TARGET) $(shell [ -e $(TARGET) ] && rm $(TARGET)) $(shell [ -d $(TARGET).dSYM ] && rm -r $(TARGET).dSYM) @echo Done run: $(TARGET) - ./$(TARGET) \ No newline at end of file + ./$(TARGET) diff --git a/test/test.cpp b/test/test.cpp index b40c5cb..4005995 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -1,26 +1,65 @@ -#include "minunit.h" +#include "vendors/minunit.h" +#include "../expression.h" +#include "../rpn.h" +#include -MU_TEST(test1) { - mu_check(1 == 1); +MU_TEST(rpnPlus) { + const struct EXPRESSION plus = + EXPRESSION::make(EXPRESSION::TYPES::SYMBOL, "+"); + + std::list expressions; + expressions.push_back(EXPRESSION::make(EXPRESSION::TYPES::VALUE, "10")); + expressions.push_back(EXPRESSION::make(EXPRESSION::TYPES::VALUE, "5")); + expressions.push_back(plus); + + const RPN rpnPlus(expressions); + mu_check(rpnPlus.result() == 15); } -MU_TEST(test2) { - mu_check(2 == 2); +MU_TEST(rpnMinus) { + const struct EXPRESSION minus = + EXPRESSION::make(EXPRESSION::TYPES::SYMBOL, "-"); + + std::list expressions; + expressions.push_back(EXPRESSION::make(EXPRESSION::TYPES::VALUE, "10")); + expressions.push_back(EXPRESSION::make(EXPRESSION::TYPES::VALUE, "20")); + expressions.push_back(minus); + + const RPN rpnMinus(expressions); + mu_check(rpnMinus.result() == 10); } -MU_TEST(test3) { - mu_check(3 == 3); +MU_TEST(rpnMulti) { + const struct EXPRESSION minus = + EXPRESSION::make(EXPRESSION::TYPES::SYMBOL, "*"); + + std::list expressions; + expressions.push_back(EXPRESSION::make(EXPRESSION::TYPES::VALUE, "20")); + expressions.push_back(EXPRESSION::make(EXPRESSION::TYPES::VALUE, "3")); + expressions.push_back(minus); + + const RPN rpnMinus(expressions); + mu_check(rpnMinus.result() == 60); } -MU_TEST(test4) { - mu_check(2 == 1); +MU_TEST(rpnDivision) { + const struct EXPRESSION minus = + EXPRESSION::make(EXPRESSION::TYPES::SYMBOL, "/"); + + std::list expressions; + expressions.push_back(EXPRESSION::make(EXPRESSION::TYPES::VALUE, "3")); + expressions.push_back(EXPRESSION::make(EXPRESSION::TYPES::VALUE, "30")); + expressions.push_back(minus); + + const RPN rpnMinus(expressions); + mu_check(rpnMinus.result() == 10); } int main(int argc, char *argv[]) { - MU_RUN_TEST(test1); - MU_RUN_TEST(test2); - MU_RUN_TEST(test3); - MU_RUN_TEST(test4); + MU_RUN_TEST(rpnPlus); + MU_RUN_TEST(rpnMinus); + MU_RUN_TEST(rpnMulti); + MU_RUN_TEST(rpnDivision); MU_REPORT(); return minunit_status; -} \ No newline at end of file +} diff --git a/vendors/minunit.h b/test/vendors/minunit.h similarity index 100% rename from vendors/minunit.h rename to test/vendors/minunit.h From ade9f5e03f26f7849fdc4b2264bd89cac28aac27 Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Mon, 29 Apr 2019 23:07:32 +0900 Subject: [PATCH 39/52] This commit will fail --- rpn.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rpn.cpp b/rpn.cpp index 5c17f02..a387849 100644 --- a/rpn.cpp +++ b/rpn.cpp @@ -34,7 +34,7 @@ long RPN::operate(const long left, const long right, const std::string symbol) c return left + right; } else if (symbol == "-") { return left - right; - } else if (symbol == "*") { + } else if (symbol == "+") { return left * right; } else { // (symbol == "/") return left / right; From 8dd8aa595b3f56547b1e81ed262fe4875252e4e6 Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Mon, 29 Apr 2019 23:08:36 +0900 Subject: [PATCH 40/52] Fix config.yml --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 3b0e7e6..db5f878 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -6,5 +6,5 @@ jobs: steps: - checkout - - cd test && make run + - cd test && make run From c9bb7a33bbc1fabf29624360f55fd97379e4748b Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Mon, 29 Apr 2019 23:10:04 +0900 Subject: [PATCH 41/52] Fix again --- .circleci/config.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index db5f878..c6fe984 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,10 +1,10 @@ version: 2 jobs: - build: + test: docker: - image: debian:stretch steps: - checkout - - cd test && make run + - run: cd test && make run From b020665c3913e3b6f0bb73bfb8154d28f6f8db7f Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Mon, 29 Apr 2019 23:10:50 +0900 Subject: [PATCH 42/52] Fix again, again --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index c6fe984..8565032 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,6 +1,6 @@ version: 2 jobs: - test: + build: docker: - image: debian:stretch From 6f4113d656dc847044855fca31a8bd47bbf2cd2f Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Mon, 29 Apr 2019 23:14:01 +0900 Subject: [PATCH 43/52] Install build-essential --- .circleci/config.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.circleci/config.yml b/.circleci/config.yml index 8565032..319d124 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -6,5 +6,6 @@ jobs: steps: - checkout + - run: sudo apt -y install build-essential - run: cd test && make run From 779e153773a3cc35d3ed2bb0831658184195259e Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Mon, 29 Apr 2019 23:15:08 +0900 Subject: [PATCH 44/52] Removed sudo --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 319d124..2c14e5d 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -6,6 +6,6 @@ jobs: steps: - checkout - - run: sudo apt -y install build-essential + - run: apt install -y build-essential - run: cd test && make run From ed594eab2c61c6d98296d4eac265bc294d1643f1 Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Mon, 29 Apr 2019 23:16:36 +0900 Subject: [PATCH 45/52] Run "apt update" --- .circleci/config.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.circleci/config.yml b/.circleci/config.yml index 2c14e5d..ebfbfa5 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -6,6 +6,7 @@ jobs: steps: - checkout + - run: apt update -y - run: apt install -y build-essential - run: cd test && make run From b5930348baace0efa7f26b101122a091d344a39b Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Mon, 29 Apr 2019 23:19:00 +0900 Subject: [PATCH 46/52] Gave some names --- .circleci/config.yml | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index ebfbfa5..3add209 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -6,7 +6,15 @@ jobs: steps: - checkout - - run: apt update -y - - run: apt install -y build-essential - - run: cd test && make run + - run: + name: Install deps + command: | + apt update -y + apt install -y build-essential + - run: + name: Test + command: | + cd test + make + ./test From c6859be4f764b0ab1594b055601c1f1ecb67f9d4 Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Mon, 29 Apr 2019 23:20:29 +0900 Subject: [PATCH 47/52] Fix --- rpn.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rpn.cpp b/rpn.cpp index a387849..5c17f02 100644 --- a/rpn.cpp +++ b/rpn.cpp @@ -34,7 +34,7 @@ long RPN::operate(const long left, const long right, const std::string symbol) c return left + right; } else if (symbol == "-") { return left - right; - } else if (symbol == "+") { + } else if (symbol == "*") { return left * right; } else { // (symbol == "/") return left / right; From 35a44dcd5be0d73fcadc05dd2bcc34d4f2e8cbc5 Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Mon, 29 Apr 2019 23:44:14 +0900 Subject: [PATCH 48/52] Fix minunit bug --- test/vendors/minunit.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/vendors/minunit.h b/test/vendors/minunit.h index aaa83b6..263a200 100644 --- a/test/vendors/minunit.h +++ b/test/vendors/minunit.h @@ -1,6 +1,6 @@ /* * Copyright (c) 2012 David Siñuela Pastor, siu.4coders@gmail.com - * + * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including @@ -8,10 +8,10 @@ * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: - * + * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. - * + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND @@ -111,7 +111,7 @@ static void (*minunit_teardown)(void) = NULL; minunit_proc_timer = mu_timer_cpu();\ }\ if (minunit_setup) (*minunit_setup)();\ - minunit_status = 0;\ + /* minunit_status = 0; // here resets an error code (maybe a bug?) */ \ test();\ minunit_run++;\ if (minunit_status) {\ @@ -234,13 +234,13 @@ static double mu_timer_real(void) /* Windows 2000 and later. ---------------------------------- */ LARGE_INTEGER Time; LARGE_INTEGER Frequency; - + QueryPerformanceFrequency(&Frequency); QueryPerformanceCounter(&Time); - + Time.QuadPart *= 1000000; Time.QuadPart /= Frequency.QuadPart; - + return (double)Time.QuadPart / 1000000.0; #elif (defined(__hpux) || defined(hpux)) || ((defined(__sun__) || defined(__sun) || defined(sun)) && (defined(__SVR4) || defined(__svr4__))) From d70d267c6e18acc252d2fb17911a1213a6648ce1 Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Mon, 29 Apr 2019 23:44:23 +0900 Subject: [PATCH 49/52] Makes a bug --- rpn.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rpn.cpp b/rpn.cpp index 5c17f02..a387849 100644 --- a/rpn.cpp +++ b/rpn.cpp @@ -34,7 +34,7 @@ long RPN::operate(const long left, const long right, const std::string symbol) c return left + right; } else if (symbol == "-") { return left - right; - } else if (symbol == "*") { + } else if (symbol == "+") { return left * right; } else { // (symbol == "/") return left / right; From d72807c731d70edf7ac97c5cf31e02b10c492796 Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Mon, 29 Apr 2019 23:45:21 +0900 Subject: [PATCH 50/52] Fix the bug --- rpn.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rpn.cpp b/rpn.cpp index a387849..5c17f02 100644 --- a/rpn.cpp +++ b/rpn.cpp @@ -34,7 +34,7 @@ long RPN::operate(const long left, const long right, const std::string symbol) c return left + right; } else if (symbol == "-") { return left - right; - } else if (symbol == "+") { + } else if (symbol == "*") { return left * right; } else { // (symbol == "/") return left / right; From d6a1effe4e30cd4e329391a51658d6a5441c9cce Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Mon, 29 Apr 2019 23:48:30 +0900 Subject: [PATCH 51/52] Update README --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c058a35..52e2f45 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # mathcalc -Simple arithmetic calculater for command-line. +> Simple arithmetic calculater for command-line. +[![CircleCI](https://circleci.com/gh/IzumiSy/mathcalc.svg?style=svg)](https://circleci.com/gh/IzumiSy/mathcalc) + ```bash $ ./mathcalc "1+2+(10*2+(5*2))+10" > 1+2+(10*2+(5*2))+10 From 7d609aa0f2c9e734c6bc2f35b718f282ca9d9612 Mon Sep 17 00:00:00 2001 From: IzumiSy Date: Mon, 29 Apr 2019 23:49:21 +0900 Subject: [PATCH 52/52] Fixed README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 52e2f45..7359e33 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # mathcalc > Simple arithmetic calculater for command-line. + [![CircleCI](https://circleci.com/gh/IzumiSy/mathcalc.svg?style=svg)](https://circleci.com/gh/IzumiSy/mathcalc) ```bash