diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5d1f8ed --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +bin/ +obj/ + +history.txt \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..d1fe325 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "fstream": "cpp" + } +} \ No newline at end of file diff --git a/6.1/Calculate.cpp b/6.1/Calculate.cpp new file mode 100644 index 0000000..840ddda --- /dev/null +++ b/6.1/Calculate.cpp @@ -0,0 +1,166 @@ +// +// Created by quinc on 2021-04-22. +// + +#include +#include +#include +#include "Calculate.h" + +//TODO Remake calculate struct completely. +std::string calculate::add(double numA, double numB) { + const double add = numA + numB; + return writetoVector("The sum of the two numbers are ", add, numA, numB); +} + +std::string calculate::subtract(double numA, double numB) { + const double subtract = numA - numB; + return writetoVector("The difference of the two numbers are ", subtract, numA, numB); +} + +std::string calculate::multiply(double numA, double numB) { + const double multiply = numA * numB; + return writetoVector("The product of the two numbers you put in are ", multiply, numA, numB); +} + +std::string calculate::divide(double numA, double numB) { + const double divide = numA / numB; + return writetoVector("The dividend of the two numbers you put in are ", divide, numA, numB); +} + +std::string calculate::power(double numA, double numB) { + const double power = pow(numA, numB); + return writetoVector(" to the power of ", power, numA, numB); +} + +std::string calculate::square(double numA, double numB) { + const double square = sqrt(numA); + return writetoVector("The square root of ", square, numA, numB); +} + +std::string calculate::writetoVector(std::string st, const double value, double numA, double numB) { + std::stringstream outputStream; + std::stringstream writeStream; + if (oper == 's') { + outputStream << st; + outputStream << numA; + outputStream << " is "; + outputStream << value; + outputStream << std::endl; + } else if (oper == '^') { + outputStream << numA; + outputStream << st; + outputStream << numB; + outputStream << " is "; + outputStream << value; + outputStream << std::endl; + } else { + outputStream << st; + outputStream << value; + outputStream << std::endl; + } + + if (oper == 's') { + writeStream << "Square root of: " << numA << " = " << value; + } else { + writeStream << numA << " " << oper << " " << numB << " = " << value; + } + + vt.push_back(writeStream.str()); + + return outputStream.str(); +} + +void calculate::setOperator(char oper) { + this->oper = oper; +} + +//TODO +void calculate::openCalculator() { + writeHistory.open(fileName, std::ios::app); + readHistory.open(fileName); + while (!readHistory.eof()) { + std::string readHistoryto; + std::getline(readHistory, readHistoryto); + vt.push_back(readHistoryto); + } + std::reverse(vt.begin(), vt.end()); + +} + +void calculate::closeCalculator() { + writeToFile(); + if (writeHistory.is_open()) { + writeHistory.close(); + + } + if (readHistory.is_open()) { + readHistory.close(); + } + +} + +[[deprecated("Try not to use this")]] +std::ofstream &calculate::getWriteHistory() { + return writeHistory; +} + +void calculate::readHistoryandShow() { + for (int i = vt.size() - 1; i >= 0; i--) { + std::cout << vt[i] << std::endl; + } +} + +void calculate::deleteHistory() { + if (writeHistory.is_open()) { + writeHistory.close(); + writeHistory.open(fileName, std::ios::out | std::ios::trunc); + vt.clear(); + } else { + writeHistory.open(fileName, std::ios::out | std::ios::trunc); + vt.clear(); + } + std::cout << "\nHistory Deleted!!\n"; + system("pause"); + CLEAR; +} + +//TODO +void calculate::writeToFile() { + if (writeHistory.is_open()) { + writeHistory.close(); + writeHistory.open(fileName, std::ios::out | std::ios::trunc); + for (int i = vt.size() - 1; i >= 0; i--) { + if (i != 0) { + writeHistory << vt[i] << std::endl; + + } else { + writeHistory << vt[i]; + } + + } + } else if (!writeHistory.is_open()) { + writeHistory.open(fileName, std::ios::out | std::ios::trunc); + for (int i = vt.size() - 1; i >= 0; i--) { + if (i != 0) { + writeHistory << vt[i] << std::endl; + + } else { + writeHistory << vt[i]; + } + + } + } else { + try { + throw "ERROR WRITING HISTORY TO FILE"; + } + catch ( + const char *e + ) { + CLEAR; + std::cout << e << std::endl; + } + } + +} + diff --git a/6.1/Calculate.h b/6.1/Calculate.h new file mode 100644 index 0000000..ff366a3 --- /dev/null +++ b/6.1/Calculate.h @@ -0,0 +1,64 @@ +// +// Created by quinc on 2021-04-22. +// + +#ifndef CALCULATOR_CALCULATE_H +#define CALCULATOR_CALCULATE_H + +#include +#include +#include +#include +#include + +#pragma once + +#ifdef TARGET_OS_MAC + #define CLEAR system("clear") +#elif defined __linux__ + #define CLEAR system("clear") +#elif defined _WIN32 || defined _WIN64 + #define CLEAR system("cls") +#else +#error "unknown platform" +#endif + +struct calculate { +private: + std::vector vt; + std::ifstream readHistory; + std::ofstream writeHistory; + char oper; + + std::string writetoVector(std::string st, const double value, double numA, double numB); +public: + std::string fileName = "history.txt"; +public: + void openCalculator(); + + void writeToFile(); + + void readHistoryandShow(); + + void deleteHistory(); + + std::string add(double numA, double numB); + + std::string subtract(double numA, double numB); + + std::string multiply(double numA, double numB); + + std::string divide(double numA, double numB); + + std::string power(double numA, double numB); + + std::string square(double numA, double numB); + + void setOperator(char oper); + + std::ofstream& getWriteHistory(); + + void closeCalculator(); +}; + +#endif //CALCULATOR_CALCULATE_H diff --git a/6.1/main.cpp b/6.1/main.cpp new file mode 100644 index 0000000..69358fc --- /dev/null +++ b/6.1/main.cpp @@ -0,0 +1,177 @@ +#include +#include +#include +#include +#include "Calculate.h" + +double calculation(double numA, double numB, char oper); + +static double question(); + +calculate cal; + +int HistoryShow(); + +double IntroductionInput(); + +int mainMenu() { + int choice; + std::cout << "\tMAIN MENU\n\n" << "1. Use Calculator.\n" << "2. Show History.\n" << "3. Exit\n"; + while (true) { + std::cout << "What is your choice? (1 - 3) "; + std::cin >> choice; + switch (choice) { + case 1: + CLEAR; + IntroductionInput(); + break; + case 2: + CLEAR; + HistoryShow(); + break; + case 3: + cal.closeCalculator(); + exit(0); + default: + while (std::cin.fail()) { + if (std::cin.fail()) { + std::cin.clear(); + std::cin.ignore(std::numeric_limits::max(), '\n'); + } else { + break; + } + break; + } + } + } +} + +int HistoryShow() { + int choiceHistory; + std::string readHistoryTo; + cal.readHistoryandShow(); + std::cout << "\n1. Clear History\n" << "2. Return to main menu\n\n"; + while (true) { + std::cout << "Your Choice? (1 - 2) "; + std::cin >> choiceHistory; + switch (choiceHistory) { + case 1: + cal.deleteHistory(); + return mainMenu(); + case 2: + CLEAR; + return mainMenu(); + default: + while (std::cin.fail()) { + if (std::cin.fail()) { + std::cin.clear(); + std::cin.ignore(std::numeric_limits::max(), '\n'); + } else { + break; + } + std::cout << "Your Choice? (1 - 2) "; + std::cin >> choiceHistory; + } + break; + } + } +} + +double IntroductionInput() { + double numA; + double numB; + char oper; + std::cout << "\n\tHow to use the calculator: \n\n" + << "Just simply enter the first number of your problem then the operator and then the second number of your problem." + << std::endl; + std::cout + << "Operators are: \"+ for adding, - for subtracting, * for multiplying, / for dividing, ^ for power, s for square root.\"" + << std::endl << "\n"; + std::cout << "Enter the first number: "; + std::cin >> numA; + while (std::cin.fail()) { + if (std::cin.fail()) { + std::cin.clear(); + std::cin.ignore(std::numeric_limits::max(), '\n'); + } else { + break; + } + std::cout << "Enter the first number: "; + std::cin >> numA; + } + std::cout << std::endl; + std::cout << "Enter the operator: "; + std::cin >> oper; + cal.setOperator(oper); + std::cin.clear(); + std::cout << std::endl; + if (oper == 's') { + std::cin.ignore(); + return calculation(numA, 0, oper); + } else { + std::cout << "Enter the second number: "; + std::cin >> numB; + while (std::cin.fail()) { + if (std::cin.fail()) { + std::cin.clear(); + std::cin.ignore(std::numeric_limits::max(), '\n'); + } else { + break; + } + std::cout << "Enter the second number: "; + std::cin >> numB; + } + } + std::cout << std::endl; + return calculation(numA, numB, oper); +} + +int main() { + cal.openCalculator(); + mainMenu(); +} + +double calculation(double numA, double numB, char oper) { + if (oper == '+' || oper == '-' || oper == '*' || oper == '/' || oper == '^' || oper == 's') { + if (oper == '+') { + std::cout << cal.add(numA, numB); + } else if (oper == '-') { + std::cout << cal.subtract(numA, numB); + } else if (oper == '*') { + std::cout << cal.multiply(numA, numB); + } else if (oper == '/') { + std::cout << cal.divide(numA, numB); + + } else if (oper == '^') { + std::cout << cal.power(numA, numB); + } else if (oper == 's') { + std::cout << cal.square(numA, numB); + } + return question(); + } else { + std::cout << "\nYou have put in an invalid operator/number!\n\a"; + return question(); + } +} + +double question() { + while (true) { + std::string yn; + std::cout << "\nDo you want to use the calculator again? "; + std::cin >> yn; + std::cout << std::endl; + if (yn == "Yes" || yn == "No" || yn == "YES" || yn == "NO" || yn == "YEs" || yn == "nO" || yn == "yes" || + yn == "no" || yn == "y" || yn == "n") { + if (yn == "Yes" || yn == "YES" || yn == "YEs" || yn == "yes" || yn == "y") { + return IntroductionInput(); + } else if (yn == "No" || yn == "NO" || yn == "nO" || yn == "no" || yn == "n") { + cal.getWriteHistory().close(); + cal.getWriteHistory().open(cal.fileName, std::ios::app); + CLEAR; + mainMenu(); + } + } else { + std::cout << "Invalid answer, try again.\a\n"; + } + } +} diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..bd4da2e --- /dev/null +++ b/Makefile @@ -0,0 +1,20 @@ + +CC = g++ +CFLAGS = -Wall +BIN_DIR = ./bin +OBJ_DIR = ./obj + +calculator_6_1: $(OBJ_DIR)/main_6_1.o $(OBJ_DIR)/Calculate_6_1.o + $(CC) $(CFLAGS) -o $(BIN_DIR)/$@ $^ + +$(OBJ_DIR)/main_6_1.o: ./6.1/main.cpp $(OBJ_DIR) $(BIN_DIR) + $(CC) $(CFLAGS) -c $< -o $@ + +$(OBJ_DIR)/Calculate_6_1.o: ./6.1/Calculate.cpp $(OBJ_DIR) ./6.1/Calculate.h + $(CC) $(CFLAGS) -c $< -o $@ + +$(BIN_DIR): + mkdir -p $@ + +$(OBJ_DIR): + mkdir -p $@ \ No newline at end of file