Skip to content

Commit

Permalink
Completed basic functionality of calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio Kim committed Apr 4, 2020
1 parent fc384d5 commit b6ffb9e
Show file tree
Hide file tree
Showing 23 changed files with 237 additions and 35 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ MathEngine/libMathEngine.a
Tests/run
CalcUI/CalcUI
.includes/
.libs/
.sandbox/*/sandbox
.vscode/
2 changes: 1 addition & 1 deletion .sandbox/.info
Original file line number Diff line number Diff line change
@@ -1 +1 @@
py_ncurses_test
basic_evaluation
15 changes: 15 additions & 0 deletions .sandbox/basic_evaluation/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CXX = g++
CXXFLAGS = -Ofast -std=c++17 -Wall -MMD -Werror=vla -DDEBUG
EXEC = sandbox

OBJECTS = sandbox.o
DEPENDS = ${OBJECTS:.o=.d}

${EXEC}: ${OBJECTS}
${CXX} ${CXXFLAGS} ${OBJECTS} -o ${EXEC}

-include ${DEPENDS}

clean:
rm ${OBJECTS} ${EXEC} ${DEPENDS}
.PHONY: clean
12 changes: 12 additions & 0 deletions .sandbox/basic_evaluation/sandbox.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cmath>

using namespace std;

int main(){
cout << std::setprecision(16) << sin(3) << endl;
}

9 changes: 9 additions & 0 deletions .sandbox/basic_evaluation/sandbox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import torch
import numpy as np

def main():
pass


if __name__ == "__main__":
main()
14 changes: 12 additions & 2 deletions .utils/build_ui
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
#!/bin/bash

cd CalcUI/
qmake CalcUI.pro -spec linux-g++ CONFIG+=debug CONFIG+=qml_debug
make

if [ ! -f Makefile ]; then
qmake CalcUI.pro -spec linux-g++ CONFIG+=debug CONFIG+=qml_debug
fi

make 2> calcui.cerr
if [ $? -ne 0 ]; then
vi -R calcui.cerr
exit 1
else
rm -f calcui.cerr
fi
12 changes: 8 additions & 4 deletions .utils/install
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
#!/bin/bash

root_dir=$(pwd)

rm -rf .libs/
mkdir -p .libs/
rm -rf .includes/
mkdir -p .includes/
cd .includes/
curr_dir=$(pwd)
curr_dir="$root_dir/.includes"

# Install GNU Scientific Library
wget ftp://ftp.gnu.org/gnu/gsl/gsl-latest.tar.gz
tar -xzf gsl-latest.tar.gz
rm -f gsl-latest.tar.gz
gsl_dir=$curr_dir/$(ls | grep "gsl")
gsl_dir=$curr_dir/$(ls | grep "gsl-")
cd $gsl_dir
./configure && make
ln -s $gsl_dir/gsl $curr_dir/gsl
ln -s $gsl_dir/.libs $curr_dir/.gsl_libs
ln -s $gsl_dir/cblas/.libs $curr_dir/.cblas_libs
ln -s $gsl_dir/.libs/libgsl.a $root_dir/.libs/libgsl.a
ln -s $gsl_dir/cblas/.libs/libgslcblas.a $root_dir/.libs/libgslcblas.a

# Install QT5
cd $curr_dir
Expand Down
30 changes: 30 additions & 0 deletions CalcUI/calcwindow.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,45 @@
#include <iostream>
#include <string>
#include <sstream>

#include "calcwindow.h"
#include "ui_calcwindow.h"
#include <QTextEdit>
#include <QString>

#include <Expressions/InvalidExpression.h>
#include <Utils/exceptions.h>

using namespace std;

CalcWindow::CalcWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::CalcWindow)
{
ui->setupUi(this);

connect(ui->input1, &QTextEdit::textChanged, this, &CalcWindow::on_textChanged);
}

CalcWindow::~CalcWindow()
{
delete ui;
}


void CalcWindow::on_textChanged()
{
string input = ui->input1->toPlainText().toStdString();

try {
auto expr = engine.evaluate(input);
if (!dynamic_cast<InvalidExpression*>(expr.get())){
ostringstream out;
out << expr;
string output = out.str();
ui->output1->setText(QString::fromUtf8(output.c_str()));
}
} catch(const Exception& e){
cerr << e.what() << endl;
} catch(...){}
}
5 changes: 5 additions & 0 deletions CalcUI/calcwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define CALCWINDOW_H

#include <QMainWindow>
#include <MathEngine.h>

QT_BEGIN_NAMESPACE
namespace Ui { class CalcWindow; }
Expand All @@ -15,7 +16,11 @@ class CalcWindow : public QMainWindow
CalcWindow(QWidget *parent = nullptr);
~CalcWindow();

private slots:
void on_textChanged();

private:
Ui::CalcWindow *ui;
MathEngine engine;
};
#endif // CALCWINDOW_H
6 changes: 6 additions & 0 deletions CalcUI/calcwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
</item>
<item>
<widget class="QTextEdit" name="output1">
<property name="font">
<font>
<family>DejaVu Math TeX Gyre</family>
<pointsize>18</pointsize>
</font>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
Expand Down
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ test: build
ui:
.utils/build_ui

run: ui
run: build ui
./CalcUI/CalcUI

debug: build ui
gdb -q -ex="run" ./CalcUI/CalcUI

sandbox:
python3 -u Sandboxer/sandboxer.py --create --name $(name) --lang $(lang)

Expand All @@ -28,5 +31,5 @@ bp: # breakpoints
edit Tests/.gdbinit

clean:
rm -f MathEngine/libMathEngine.a Tests/run
rm -f MathEngine/libMathEngine.a Tests/run CalcUI/CalcUI
.PHONY: clean
3 changes: 2 additions & 1 deletion MathEngine/Expressions/Expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
struct Expression;

typedef std::unique_ptr<Expression> expression;
typedef std::unordered_map<std::string, double> Variables;
typedef std::unordered_map<std::string, expression> Variables;

double gsl_expression_function(double x, void* params);

Expand All @@ -21,6 +21,7 @@ struct Expression {

virtual bool evaluable() = 0;
virtual expression evaluate();
virtual expression evaluate(const Variables& vars);

virtual gsl_function function() {
gsl_function F;
Expand Down
39 changes: 39 additions & 0 deletions MathEngine/Expressions/Expression/InvalidExpression.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

#include <sstream>
#include <gsl/gsl_math.h>

#include "../InvalidExpression.h"

using namespace std;

InvalidExpression::InvalidExpression(const Exception& e): message{e.msg} {}

expression InvalidExpression::simplify() {
return make_unique<InvalidExpression>(message);
}
expression InvalidExpression::derivative(const std::string& var) {
return make_unique<InvalidExpression>(message);
}
expression InvalidExpression::integrate(const std::string& var) {
return make_unique<InvalidExpression>(message);
}

bool InvalidExpression::evaluable(){ return false; }

double InvalidExpression::value() { return GSL_NAN; }

double InvalidExpression::value(const Variables& vars) { return GSL_NAN; }

bool InvalidExpression::complex(){ return false; }

expression InvalidExpression::copy() {
return make_unique<InvalidExpression>(message);
}

std::ostream& InvalidExpression::print(std::ostream& out) {
return out << message;
}

std::ostream& InvalidExpression::postfix(std::ostream& out) {
return out << message;
}
8 changes: 7 additions & 1 deletion MathEngine/Expressions/Expression/expression.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
#include "../Expression.h"
#include "../NumericalExpression.h"

double gsl_expression_function(double x, void* params){
return ((Expression *) params)->value({{"x", x}});
Variables vars;
vars["x"] = std::make_unique<NumExpression>(x);
return ((Expression *) params)->value(vars);
}

expression Expression::evaluate(){ return std::make_unique<NumExpression>(this->value()); }
expression Expression::evaluate(const Variables& vars){ return std::make_unique<NumExpression>(this->value(vars)); }

std::ostream& operator<<(std::ostream& out, expression& e){
return out << e.get();
}
Expand Down
18 changes: 18 additions & 0 deletions MathEngine/Expressions/InvalidExpression.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <string>

#include "Expression.h"
#include "../Utils/exceptions.h"

class InvalidExpression: public Expression {

std::string message;

public:
InvalidExpression(const Exception&);

EXPRESSION_OVERRIDES

};

15 changes: 7 additions & 8 deletions MathEngine/Expressions/NumericalExpressions/NumExpression.cc
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@

#include <iomanip>
#include <sstream>

#include "../NumericalExpression.h"
#include "../../Utils/exceptions.h"

using namespace std;

expression Expression::evaluate(){ return std::make_unique<NumExpression>(this->value()); }

NumExpression::NumExpression(double real, double imag): real{real}, imag{imag} {}
NumExpression::NumExpression(const std::string& num): real{0}, imag{0} {
istringstream iss{num};
Expand Down Expand Up @@ -44,23 +43,23 @@ expression NumExpression::copy() {
}

std::ostream& NumExpression::print(std::ostream& out) {
out << real;
out << std::setprecision(16) << real;
if (imag > 0){
out << '+' << imag << "i";
out << '+' << std::setprecision(16) << imag << "i";
}
else if (imag < 0){
out << imag << "i";
out << std::setprecision(16) << imag << "i";
}
return out;
}

std::ostream& NumExpression::postfix(std::ostream& out) {
out << real;
out << std::setprecision(16) << real;
if (imag > 0){
out << imag << "i" << " +";
out << std::setprecision(16) << imag << "i" << " +";
}
else if (imag < 0){
out << -imag << "i" << " -";
out << std::setprecision(16) << -imag << "i" << " -";
}
return out;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ double VariableExpression::value() { return num; }

double VariableExpression::value(const Variables& vars) {
if (vars.count(name) > 0){
return vars.at(name);
return vars.at(name)->value();
}
return num;
}
Expand Down
2 changes: 1 addition & 1 deletion MathEngine/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CXX = g++
CXXFLAGS = -Ofast -g -std=c++17 -fPIC -Wall -MMD -Werror=vla -I../.includes/gsl
CXXFLAGS = -Ofast -g -std=c++17 -fPIC -Wall -MMD -Werror=vla -I../.includes/
TARGET_LIB = libMathEngine.a # target lib

MATHENGINE = $(wildcard MathEngine/*.cc)
Expand Down
4 changes: 4 additions & 0 deletions MathEngine/MathEngine.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <list>
#include <map>
#include <memory>
#include <string>
#include <gsl/gsl_math.h>
Expand All @@ -13,6 +14,7 @@
class MathEngine {

std::unique_ptr<Parser> parser;
Variables variables;

public:
MathEngine();
Expand All @@ -21,5 +23,7 @@ class MathEngine {

expression parse(const std::string& input);
expression operator()(const std::string& input);

expression evaluate(const std::string& input);
};

Loading

0 comments on commit b6ffb9e

Please sign in to comment.