Initial commit
This commit is contained in:
62
P1/Calculator.cpp
Normal file
62
P1/Calculator.cpp
Normal file
@@ -0,0 +1,62 @@
|
||||
#include "Calculator.h"
|
||||
#include "VariableNode.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
Calculator::Calculator(int cap)
|
||||
{
|
||||
this->data = new VariableLinkedList(cap);
|
||||
}
|
||||
|
||||
Calculator::~Calculator()
|
||||
{
|
||||
delete this->data;
|
||||
}
|
||||
|
||||
/// @brief Define a new variable
|
||||
/// @param name The name of the new variable
|
||||
/// @param value The initial value to store in the variable
|
||||
/// @return True if the variable was added, false otherwise
|
||||
bool Calculator::define(string name, double value)
|
||||
{
|
||||
return this->data->insert(name, value);
|
||||
}
|
||||
|
||||
/// @brief Perform an operation on two variables, and store the result in a thrid
|
||||
/// @param v1 The first variable to perform the operation on
|
||||
/// @param v2 The second variable to perform the opetation on
|
||||
/// @param v3 The variable to store the output result in
|
||||
/// @param operation A lambda function to apply on the two variables
|
||||
/// @return True if successful, false otherwise
|
||||
bool Calculator::performOperation(string v1, string v2, string out, function<double(double, double)> operation)
|
||||
{
|
||||
VariableNode *vn1 = this->data->search(v1);
|
||||
VariableNode *vn2 = this->data->search(v2);
|
||||
VariableNode *output = this->data->search(out);
|
||||
|
||||
// If a single one of the variables is not found, we cannot continue.
|
||||
if (vn1 == nullptr || vn2 == nullptr || output == nullptr)
|
||||
return false;
|
||||
|
||||
output->setValue(operation(vn1->getValue(), vn2->getValue()));
|
||||
return true;
|
||||
}
|
||||
|
||||
/// @brief Remove the given variable from storage
|
||||
/// @param name The name of the variable to remove
|
||||
/// @return True if the variable was successfully removed, false otherwise
|
||||
bool Calculator::remove(string name)
|
||||
{
|
||||
return this->data->remove(name);
|
||||
}
|
||||
|
||||
/// @brief Print out the value of the given variable
|
||||
/// @param name The name of the variable to print out
|
||||
void Calculator::print(string name)
|
||||
{
|
||||
VariableNode *temp = this->data->search(name);
|
||||
if (temp == nullptr)
|
||||
cout << "variable " << name << " not found" << endl;
|
||||
else
|
||||
cout << temp->getValue() << endl;
|
||||
}
|
||||
22
P1/Calculator.h
Normal file
22
P1/Calculator.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef CALCULATOR_H
|
||||
#define CALCULATOR_H
|
||||
|
||||
#include <iostream>
|
||||
#include <functional>
|
||||
#include "VariableNode.h"
|
||||
#include "VariableLinkedList.h"
|
||||
|
||||
class Calculator
|
||||
{
|
||||
public:
|
||||
bool define(std::string name, double value);
|
||||
bool performOperation(std::string v1, std::string v2, std::string output, std::function<double(double, double)> operation);
|
||||
bool remove(std::string name);
|
||||
void print(std::string name);
|
||||
Calculator(int cap);
|
||||
~Calculator();
|
||||
|
||||
private:
|
||||
VariableLinkedList *data;
|
||||
};
|
||||
#endif
|
||||
2
P1/Makefile
Normal file
2
P1/Makefile
Normal file
@@ -0,0 +1,2 @@
|
||||
all: test.cpp Calculator.cpp VariableNode.cpp VariableLinkedList.cpp
|
||||
g++ -std=c++11 test.cpp Calculator.cpp VariableNode.cpp VariableLinkedList.cpp
|
||||
88
P1/VariableLinkedList.cpp
Normal file
88
P1/VariableLinkedList.cpp
Normal file
@@ -0,0 +1,88 @@
|
||||
#include "VariableLinkedList.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
VariableLinkedList::VariableLinkedList(int cap)
|
||||
{
|
||||
this->maxCount = cap;
|
||||
this->count = 0;
|
||||
this->head = nullptr;
|
||||
}
|
||||
|
||||
VariableLinkedList::~VariableLinkedList()
|
||||
{
|
||||
if (head != nullptr)
|
||||
head->DestroyAll();
|
||||
}
|
||||
|
||||
/// @brief Insert a variable into the head of the linked list
|
||||
/// @param name The name of the variable to insert
|
||||
/// @param value The double value to initialize the variable with
|
||||
/// @return True if the variable was added, false otherwise
|
||||
bool VariableLinkedList::insert(string name, double value)
|
||||
{
|
||||
// Ensure there is space
|
||||
if (count == maxCount)
|
||||
return false;
|
||||
|
||||
// Ensure the variable does not exist
|
||||
VariableNode *s = this->search(name);
|
||||
if (s != nullptr)
|
||||
return false;
|
||||
|
||||
// Insert a new VariableNode into the head.
|
||||
head = new VariableNode(name, value, head);
|
||||
|
||||
count++;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// @brief Remove the variable with name from the linked list
|
||||
/// @param name The name of the variable to remove
|
||||
/// @return True if removed, false otherwise
|
||||
bool VariableLinkedList::remove(string name)
|
||||
{
|
||||
// Root node must be handled in a seperate case
|
||||
if (head != nullptr && head->getName() == name)
|
||||
{
|
||||
VariableNode *temp = head->getNext();
|
||||
delete head;
|
||||
head = temp;
|
||||
count --;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Search for the node before the one we want to remove.
|
||||
VariableNode *currentIndex = this->head;
|
||||
while (currentIndex != nullptr && currentIndex->getNext() != nullptr &&
|
||||
currentIndex->getNext()->getName() != name)
|
||||
currentIndex = currentIndex->getNext();
|
||||
|
||||
// currentIndex will be null if the index does not exist.
|
||||
if (currentIndex == nullptr || currentIndex->getNext() == nullptr)
|
||||
return false;
|
||||
|
||||
VariableNode *temp = currentIndex->getNext()->getNext();
|
||||
delete currentIndex->getNext();
|
||||
count--;
|
||||
currentIndex->setNext(temp);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// @brief Search for the variable with the given name
|
||||
/// @param name The name of the variable to search for
|
||||
/// @return A pointer to a valid VariableNode if the node exists, nullptr otherwise
|
||||
VariableNode *VariableLinkedList::search(string name)
|
||||
{
|
||||
VariableNode *currentIndex = this->head;
|
||||
while (currentIndex != nullptr)
|
||||
{
|
||||
if (currentIndex->getName() == name)
|
||||
{
|
||||
return currentIndex;
|
||||
}
|
||||
currentIndex = currentIndex->getNext();
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
22
P1/VariableLinkedList.h
Normal file
22
P1/VariableLinkedList.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef VARIABLELINKEDLIST_H
|
||||
#define VARIABLELINKEDLIST_H
|
||||
|
||||
#include <iostream>
|
||||
#include <functional>
|
||||
#include "VariableNode.h"
|
||||
|
||||
class VariableLinkedList
|
||||
{
|
||||
public:
|
||||
bool insert(std::string name, double value);
|
||||
VariableNode *search(std::string name);
|
||||
bool remove(std::string name);
|
||||
VariableLinkedList(int cap);
|
||||
~VariableLinkedList();
|
||||
|
||||
private:
|
||||
VariableNode *head;
|
||||
int count;
|
||||
int maxCount;
|
||||
};
|
||||
#endif
|
||||
44
P1/VariableNode.cpp
Normal file
44
P1/VariableNode.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#include "VariableNode.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
VariableNode::VariableNode(std::string name, double value, VariableNode *next)
|
||||
{
|
||||
this->name = name;
|
||||
this->value = value;
|
||||
this->next = next;
|
||||
}
|
||||
|
||||
string VariableNode::getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
double VariableNode::getValue()
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
void VariableNode::setValue(double value)
|
||||
{
|
||||
this->value = value;
|
||||
}
|
||||
|
||||
void VariableNode::setNext(VariableNode *next)
|
||||
{
|
||||
this->next = next; // Be careful when calling setNext, or we could have a memory leak.
|
||||
}
|
||||
|
||||
VariableNode *VariableNode::getNext()
|
||||
{
|
||||
return next;
|
||||
}
|
||||
|
||||
/// @brief Delete all children nodes of this element
|
||||
void VariableNode::DestroyAll()
|
||||
{
|
||||
if (this->next != nullptr)
|
||||
this->next->DestroyAll();
|
||||
|
||||
delete this;
|
||||
}
|
||||
26
P1/VariableNode.h
Normal file
26
P1/VariableNode.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef VARIABLENODE_H
|
||||
#define VARIABLENODE_H
|
||||
|
||||
#include <iostream>
|
||||
|
||||
class VariableNode
|
||||
{
|
||||
public:
|
||||
std::string getName();
|
||||
|
||||
double getValue();
|
||||
void setValue(double value);
|
||||
|
||||
VariableNode *getNext();
|
||||
void setNext(VariableNode *next);
|
||||
|
||||
VariableNode(std::string name, double value, VariableNode *next);
|
||||
|
||||
void DestroyAll();
|
||||
|
||||
private:
|
||||
std::string name;
|
||||
double value;
|
||||
VariableNode *next;
|
||||
};
|
||||
#endif
|
||||
92
P1/test.cpp
Normal file
92
P1/test.cpp
Normal file
@@ -0,0 +1,92 @@
|
||||
#include <iostream>
|
||||
#include "Calculator.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main ()
|
||||
{
|
||||
string cmd;
|
||||
Calculator *calc = nullptr;
|
||||
|
||||
while (cin >> cmd)
|
||||
{
|
||||
if (cmd == "CRT")
|
||||
{
|
||||
int N;
|
||||
cin >> N;
|
||||
|
||||
if (calc != nullptr)
|
||||
delete calc;
|
||||
calc = new Calculator(N);
|
||||
|
||||
cout << "success" << endl;
|
||||
}
|
||||
else if (cmd == "DEF")
|
||||
{
|
||||
string name;
|
||||
double val;
|
||||
|
||||
cin >> name;
|
||||
cin >> val;
|
||||
|
||||
if (calc->define(name, val))
|
||||
{
|
||||
cout << "success" << endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "failure" << endl;
|
||||
}
|
||||
}
|
||||
else if (cmd == "ADD" || cmd == "SUB")
|
||||
{
|
||||
string v1;
|
||||
string v2;
|
||||
string out;
|
||||
|
||||
cin >> v1;
|
||||
cin >> v2;
|
||||
cin >> out;
|
||||
|
||||
function<double(double, double)> operation;
|
||||
if (cmd == "ADD")
|
||||
operation = [](double d1, double d2) {return d1 + d2;};
|
||||
else
|
||||
operation = [](double d1, double d2) {return d1 - d2;};
|
||||
|
||||
if (calc->performOperation(v1, v2, out, operation))
|
||||
{
|
||||
cout << "success" << endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "failure" << endl;
|
||||
}
|
||||
}
|
||||
else if (cmd == "REM")
|
||||
{
|
||||
string name;
|
||||
|
||||
cin >> name;
|
||||
|
||||
if (calc->remove(name))
|
||||
cout << "success" << endl;
|
||||
else
|
||||
cout << "failure" << endl;
|
||||
}
|
||||
else if (cmd == "PRT")
|
||||
{
|
||||
string name;
|
||||
|
||||
cin >> name;
|
||||
|
||||
calc->print(name);
|
||||
}
|
||||
else if (cmd == "END")
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
delete calc;
|
||||
}
|
||||
Reference in New Issue
Block a user