Skip to content

Added Linux build system and compatibility for v6.1 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bin/
obj/

history.txt
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"files.associations": {
"fstream": "cpp"
}
}
166 changes: 166 additions & 0 deletions 6.1/Calculate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
//
// Created by quinc on 2021-04-22.
//

#include <string>
#include <sstream>
#include <cmath>
#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;
}
}

}

64 changes: 64 additions & 0 deletions 6.1/Calculate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// Created by quinc on 2021-04-22.
//

#ifndef CALCULATOR_CALCULATE_H
#define CALCULATOR_CALCULATE_H

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>

#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<std::string> 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
Loading