diff --git a/AVL.cpp b/AVL.cpp new file mode 100644 index 0000000..e3084a7 --- /dev/null +++ b/AVL.cpp @@ -0,0 +1,306 @@ +#include +#include +#include +#include +#include "matrix.h" +#include + +using namespace std; + +struct AVLNode +{ + AVLNode *parent, *left, *right; + int height; + matrix* value; +}; + +class AVL +{ + + public: + AVLNode *root; + + AVL(){ + this->root = NULL; + }; + + AVLNode* left_rotate(AVLNode *node_x) + { + AVLNode *node_y, *temp; + node_y = node_x->right; + node_y->parent = node_x->parent; + node_x->parent = node_y; + temp = node_y->left; + node_y->left = node_x; + node_x->right = temp; + if(temp != NULL){ + temp->parent = node_x; + } + if(node_y->parent != NULL) + { + if(node_y->parent->left == node_x){ + node_y->parent->left = node_y; + }else{ + node_y->parent->right = node_y; + } + }else{ + this->root = node_y; + } + node_x->height = this->height(node_x); + node_y->height = this->height(node_y); + return node_x; + }; + + AVLNode* right_rotate(AVLNode *node_y) + { + AVLNode *node_x, *temp; + node_x = node_y->left; + node_x->parent = node_y->parent; + node_y->parent = node_x; + temp = node_x->right; + node_x->right = node_y; + node_y->left = temp; + if(temp != NULL){ + temp->parent = node_y; + } + if(node_x->parent != NULL) + { + if(node_x->parent->left == node_y){ + node_x->parent->left = node_x; + }else{ + node_x->parent->right = node_x; + } + }else{ + this->root = node_x; + } + node_y->height = this->height(node_y); + node_x->height = this->height(node_x); + return node_y; + }; + + bool check_balance(AVLNode *node) + { + if(node == NULL)return true; + int leftHeight, rightHeigh; + if(node->right != NULL) + { + rightHeigh = node->right->height; + }else{ + rightHeigh = -1; + } + if(node->left != NULL) + { + leftHeight = node->left->height; + }else{ + leftHeight = -1; + } + if(leftHeight >= rightHeigh) + { + return (leftHeight-rightHeigh > 1) ? false : true; + }else{ + return (rightHeigh-leftHeight > 1) ? false : true; + } + }; + + void update_heights(AVLNode *node) + { + if(node == NULL)return; + node->height = this->height(node); + this->update_heights(node->parent); + } + + int height(AVLNode *node) + { + if(node == NULL)return -1; + int leftHeight, rightHeigh; + if(node->right != NULL) + { + rightHeigh = node->right->height; + }else{ + rightHeigh = -1; + } + if(node->left != NULL) + { + leftHeight = node->left->height; + }else{ + leftHeight = -1; + } + return (leftHeight >= rightHeigh) ? leftHeight+1 : rightHeigh+1; + }; + + void insert(matrix* value) + { + AVLNode *node = new AVLNode; + node->value = value; + node->parent = NULL; + node->left = NULL; + node->right = NULL; + AVLNode *current = this->root; + if(this->root != NULL) + { + while(true){ + if(node->value->get_name() <= current->value->get_name()) + { + if(current->left != NULL){ + current = current->left; + }else{ + current->left = node; + node->parent = current; + break; + } + }else{ + if(current->right != NULL){ + current = current->right; + }else{ + current->right = node; + node->parent = current; + break; + } + } + } + current = node; + while(current->parent != NULL){ + current = current->parent; + current->height = this->height(current); + } + }else{ + this->root = node; + node->height = 0; + } + current = node; + bool is_next_left = true /* left*/, is_prev_left = true /*left*/; + while(current->parent != NULL) + { + if(current->parent->left == current) + { + is_prev_left = is_next_left; + is_next_left = true; + }else + { + is_prev_left = is_next_left; + is_next_left = false; + } + bool flag = false; + if(!this->check_balance(current->parent) && false) + { + /* case 0 : left left*/ + if(is_prev_left && is_next_left) + { + current = this->right_rotate(current->parent); + flag = true; + current = current->parent; + } + /* case 1 : left right*/ + if(!is_prev_left && is_next_left) + { + current = this->left_rotate(current); + current = current->parent; + current = this->right_rotate(current->parent); + current = current->parent; + flag = true; + } + /* case 2 : right right*/ + if(!is_prev_left && !is_next_left) + { + current = this->left_rotate(current->parent); + flag = true; + current = current->parent; + } + /* case 3 : right left*/ + if(is_prev_left && !is_next_left) + { + current = this->right_rotate(current); + current = current->parent; + current = this->left_rotate(current->parent); + current = current->parent; + flag = true; + } + } + if(flag) + { + this->update_heights(current); + }else{ + current = current->parent; + } + } + }; + + AVLNode* min(AVLNode *node) + { + AVLNode *current = node; + bool flag = true; + while(current->left != NULL){ + current = current->left; + flag = false; + } + return flag ? node : current; + }; + + AVLNode* max(AVLNode *node) + { + AVLNode *current = node; + bool flag = true; + while(current->right != NULL){ + current = current->right; + flag = false; + } + return flag ? node : current; + }; + + AVLNode* next_max(AVLNode *node) + { + if(node->right != NULL) + { + return this->min(node->right); + }if(this->root == node){ + return NULL; + } + else if(node->parent->left == node){ + return node->parent; + }else{ + AVLNode *current = node; + while(current->parent != NULL && current->parent->right == current){ + current = current->parent; + } + if(current->parent != NULL){ + return current->parent; + } + return NULL; + } + }; + + matrix* find(string name) + { + AVLNode* current = this->root; + while(current != NULL) + { + string currentName = current->value->get_name(); + if(name == currentName) + { + return current->value; + }else if(name < currentName){ + current = current->left; + }else{ + current = current->right; + } + } + return NULL; + } + + void print() + { + AVLNode *current = NULL; + if(this->root != NULL){ + current = this->min(this->root); + }else{ + cout<value->print_matrix(); + //cout<height<next_max(current); + } + cout< A = [1.4 2.2 3.2; 4.4 5.4 6.4; 3.3 4.2 2.2]; ++ > B = [1.5 4.1 5.4; 3.1 4.2 1.2; 3.2 4.3 2.2]; ++ > C = A + B ++ > D = A - B ++ > E = A * B ++ > F = A / B ++ > G = A’ + + + + +## Phase 2: Advanced Operations and Tuning + ++ A = 5.5 + 12 * sin(0.4) + 2.2^4; ++ B = [1.2 2.3 A;[1.3 2.4;4.6 1.3],[3.2;7.8]]; ++ C = [[B [3.4; 2.1; 3.5+9.1]] ++ 1.2^3 3+1.2 15/(2.1+10*sin(0.12)) 1.2] ++ D = rand(4,4) ++ E = eye(4, 4) ++ F = zeros(2, 3) ++ G = ones(3, 6) ++ L = (1.2 + 3.4 - 5.6)/(2.1*3.2 + 4.6) - ++ 12.1*3.1 + (1.2 + 5.2)^(4/(3.2+5.6)) ++ X = ((C*D .+ 4)./2.1 + sqrt(D))./C.^2 ++ Y = (C^3 * sin(1./D))^(0.1) + + + + + + + + + ## TEAM ++ Youmna Ebrahim ++ Youmna Jehad ++ Youmna Mahmoud ++ Younan Nagy ++ Youssef Mohamed ++ Youssef Ahmed ++ Nader Yasser ++ Waleed Emad + + + + +# Project Title + +One Paragraph of project description goes here + +## Getting Started + +These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system. + +### Prerequisites + +What things you need to install the software and how to install them + +``` +Give examples +``` + +### Installing + +A step by step series of examples that tell you have to get a development env running + +Say what the step will be + +``` +Give the example +``` + +And repeat + +``` +until finished +``` + +End with an example of getting some data out of the system or using it for a little demo + +## Running the tests + +Explain how to run the automated tests for this system + +### Break down into end to end tests + +Explain what these tests test and why + +``` +Give an example +``` + +### And coding style tests + +Explain what these tests test and why + +``` +Give an example +``` + +## Deployment + +Add additional notes about how to deploy this on a live system + +## Built With + +* [Dropwizard](http://www.dropwizard.io/1.0.2/docs/) - The web framework used +* [Maven](https://maven.apache.org/) - Dependency Management +* [ROME](https://rometools.github.io/rome/) - Used to generate RSS Feeds + +## Contributing + +Please read [CONTRIBUTING.md](https://gist.github.com/PurpleBooth/b24679402957c63ec426) for details on our code of conduct, and the process for submitting pull requests to us. + +## Versioning + +We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/your/project/tags). + +## Authors + +* **Billie Thompson** - *Initial work* - [PurpleBooth](https://github.com/PurpleBooth) + +See also the list of [contributors](https://github.com/your/project/contributors) who participated in this project. + +## License + +This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details + +## Acknowledgments + +* Hat tip to anyone who's code was used +* Inspiration +* etc diff --git a/app b/app index 5c51a9d..49c0df4 100644 Binary files a/app and b/app differ diff --git a/main.cpp b/main.cpp index 2e7c398..bb8b83d 100644 --- a/main.cpp +++ b/main.cpp @@ -1,4 +1,3 @@ - #include #include #include @@ -6,46 +5,51 @@ #include "matrix.h" #include "dynamic_array_matrix.h" #define MATRIX array_matrix.get_matrix() -using namespace std; +using namespace std; -int main() +int main(int argc, char* argv[]) { - math_lab our_math_lab; - our_math_lab.load_file("D:\\projects\\nadertest\\bin\\Debug\\matrix_file.txt"); +try{ + math_lab our_math_lab; + if(argc>1) + our_math_lab.load_file(argv[1]); + else + our_math_lab.open_command(); +} +catch(int error){ + + if(error==0) cout<<"error: rows or columns is a negative value."< #include "math_lab.h" +#include +#include + +#include + #define MATRIX array_matrix.get_matrix() using namespace std; +//---------------------------------------------------------------------------------------------//trim spaces & \r +string trim_r(string right_string) +{ + if(right_string.find('\r')==-1)//recursion base case + { + return right_string; + } + + string left_string=""; + + int i=right_string.find('\r')+1; + + left_string=right_string.substr(0,i-1); + + return left_string+trim_spaces(right_string.substr(i,right_string.length()-1)); + +} +// --------------------------------------------------------------------------------------------------------------------------------- // +/*string trim_n(string right_string) +{ + if(right_string.find('\n')==-1)//recursion base case + { + return right_string; + } + + string left_string=""; + + int i=right_string.find('\n')+1; + + left_string=right_string.substr(0,i-1); + + return left_string+trim_spaces(right_string.substr(i,right_string.length()-1)); + +}*/ //----------------------------------------------------------------------------------------------//trim spaces fun->public fun string trim_spaces(string right_string) { @@ -59,7 +98,6 @@ void math_lab::decode(string operation) st= array_matrix.find_matrix(first_operand); nd=array_matrix.find_matrix(second_operand); th=array_matrix.find_matrix(third_operand); - // cout< -#include -#include - -using namespace std; -/*attach your libraries here*/ - -/* - attach your function here and make sure - you use the inputs from the class variables - in case you needed to attach a new variable - inside the class feel free to add it in "matrix.h" -*/ - -//example function -//void matrix::doNull(); - - -//default constructor -matrix::matrix() -{ - elements =NULL; - rows = 0; - columns =0; - name = ""; -} - - -//constructor -matrix::matrix(int rows, int columns) -{ - - this->rows = rows; - this->columns = columns; - - if ((rows*columns) == 0) { elements = NULL; return; } - - elements = new double*[rows]; - for(int i=0;i rows = p.rows; - this -> columns = p.columns; - - - if ((rows*columns) == 0) { elements = NULL; return; } - - - //create the matrix - elements = new double*[rows]; - for(int i=0;i name = name; -} - -//resize matrix -void matrix::resize_matrix(int rows, int columns) -{ - - if ((rows*columns) == 0) { rows = columns = 0; elements = NULL; return; } - - double** newElements = new double*[rows]; - for(int i=0;irows) - { - for(int k=0; kcolumns; k++) - { - if((k+1)<=this->columns) - { - newElements[i][k] = this->elements[i][k]; - } - } - } - } - this->destroy_matrix(); - this->rows = rows; - this->columns = columns; - this->elements = newElements; -} - - -//destructor -matrix::~matrix() -{ - destroy_matrix(); -} - -//destroy matrix -void matrix::destroy_matrix() -{ - - if(elements) - { - - - - for (int i = 0; i < rows; i++) - { - delete[] elements [i]; //deletes an inner array of integer; - } - delete[] elements; //delete pointer holding array of pointers; - - elements =NULL; - rows = 0; - columns =0; - name = ""; - } -} -//fill matrix from cin -void matrix::fill_matrix_cl() -{ - cout << "Please fill the matrix with its elements" << endl; - for (int i = 0; i < rows; i++) - { - cout << "values in row: " << i+1 << endl; - for (int j = 0; j < columns; j++) - { - cin >> elements[i][j]; - } - cout << endl; - } -} - -void matrix::fill_matrix(string inputString) -{ -string newString = space_trimer(inputString); // remove beginning spaces -// getting the name of the matrix -string name = newString.substr(0,1); - -// to remove brackets -int bracketFinder = newString.find("[",0); -string newString2 = newString.substr(bracketFinder+1, newString.length()-bracketFinder-3); - - -int rows; -int columns; -rows = number_of(newString2.length(),newString2, ";") + 1; - -// substring the string into row -int beginRow = 0 ; // position of a row starting -int endRow; // position of a row ending -string row ; -for(int i = 0 ; i < rows ; i ++) -{ - endRow = newString2.find(";",beginRow); - row = newString2.substr(beginRow , endRow - beginRow); - string newRow = space_trimer(row); // to remove beginning spaces - beginRow = endRow+1; // update the begin of next find - - // now to get the number of columns - if(i == 0)// to create the matrix once since i now can get number of columns - { - columns = number_of(newRow.length(),newRow," ") + 1; - this -> reset_matrix(rows,columns); - } - - // substring the row into elements - int beginElement = 0; - int endElement; - string elementString; - float elementFloat; - for(int j = 0 ; j < columns ; j++) - { - endElement = newRow.find(" " ,beginElement); - elementString = newRow.substr(beginElement, endElement - beginElement); - beginElement = endElement +1; - elementFloat = strtof((elementString).c_str(),0); // string to float - // save the element to the matrix - this -> elements[i][j] = elementFloat; - } - -} - - this -> name = name; - - - //To print or not - - - if( inputString[inputString.length()-1] != ';' ) - print_matrix(); - -} - -//empty matrix -void matrix::empty_matrix() -{ - - for (int i = 0; i < rows; i++) - { - for (int j = 0 ; j < columns ; j++) - elements[i][j]=0; - } - -} - -void matrix::copy_matrix(matrix & p) -{ - destroy_matrix(); - - - this -> rows = p.rows; - this -> columns = p.columns; - - - if ((rows*columns) == 0) { elements = NULL; return; } - - - //create the matrix - elements = new double*[rows]; - for(int i=0;i rows = rows; - this -> columns = columns; - - if ((rows*columns) == 0) { elements = NULL; return; } - - this -> elements = new double*[rows]; - for(int i=0;i elements[i] = new double[columns]; - } - - empty_matrix(); - -} - - - -void matrix::print_matrix() -{ - if(this->elements == NULL) //to prevent crash - cout << "this matrix is not created" <name << " = " << endl; - for(int i=0;irows)-1,(this->columns)-1); - for(int i=0;i<(temp->rows);i++) - { - - int flagRows = 0; - if(i >= row){ - flagRows = 1; - } - for(int k=0;k<(temp->columns);k++) - { - int flagColumns = 0; - if(k >= column){ - flagColumns = 1; - } - temp->elements[i][k] = this->elements[i+flagRows][k+flagColumns]; - - } - } - return *temp; -} - -// measure the determinant of the matrix, it will crash if the number of rows != num of colums -double matrix::determinant() -{ - double result = 0.0; - if(this->rows == 2) - { - result = ((this->elements[0][0])*(this->elements[1][1])) - - ((this->elements[0][1])*(this->elements[1][0])); - return result; - } - for(int i=0;i<(this->rows);i++) - { - matrix temp = this->new_sub_matrix(i,0); - result += (temp.determinant())*pow(-1,i)*this->elements[i][0]; - } - return result; -} - -//flips the rows and columns , it won't crash -void matrix::flip_matrix() -{ - matrix temp(this->columns, this->rows); - for(int i=0;i< temp.rows;i++) - { - for(int k=0;k< temp.columns;k++) - { - temp.elements[i][k] = this->elements[k][i]; - } - } - std::string name = this->name; - this->copy_matrix(temp); - this->name = name; -} - -// divide matrix A over B , it won't crash if the rows != columns so make sure you operate with the right data -matrix matrix::inverse() -{ - matrix *temp = new matrix; - temp->copy_matrix(*this); - double det = temp->determinant(); - int flag = 1; - for (int i = 0; i < temp->columns; i++) - { - flag = pow(-1,i); - for (int k = 0; k < temp->rows; k++) - { - temp->elements[k][i] = (this->new_sub_matrix(k,i)).determinant() * flag; - flag *= -1; - } - } - temp->flip_matrix(); - multiply_num(*temp, (1/det), *temp); - return *temp; -} - -// divide number B over matrix A , it will crash if the number of rows != num of colums -void divide_num_over_matrix(matrix &A, double B , matrix &C) -{ - matrix a = A.inverse(); - multiply_num(a, B , C); -} - -// divide matrix A over B , it will crash if the number of rows != num of colums or if the 2 matrix don't match -void divide_matrix(matrix &A, matrix &B , matrix &C) -{ - matrix b = B.inverse(); - multiply_matrix(A, b , C); -} - - //sum of two matrix - void sum_matrix(matrix &A, matrix &B , matrix &C) - { - if (A.rows != B.rows || A.columns != B.columns)cout << "error sizing" << endl; - - else { - matrix result(A.rows,A.columns); - C = result; - for (int i = 0; i < A.rows; i++) - { - for (int j = 0; j < A.columns; j++) - C.elements[i][j] = A.elements[i][j] + B.elements[i][j]; - } - } - - } - - - -// sub of two matrix -void sub_matrix(matrix &A, matrix &B , matrix &C) - { - - if (A.rows != B.rows || A.columns != B.columns)cout << "error sizing" << endl; - - else { - - matrix result(A.rows,A.columns); - C = result; - for (int i = 0; i < A.rows; i++) - { - for (int j = 0; j < A.columns; j++) - C.elements[i][j] = A.elements[i][j] - B.elements[i][j]; - } - } - - } - - - - // matrix matrix:: operator - (matrix & p) // A - B = C - //{ - //return sub_matrix(*this, p); - //} - - - - //sum of matrix and number - void sum_num(matrix &A, double B, matrix &C) - { - matrix result(A.rows, A.columns); - - C = result; - - for (int i = 0; i < A.rows; i++) - { - for (int j = 0; j < A.columns; j++) - C.elements[i][j] = A.elements[i][j] + B; - } - - } - - - - //multiply of matrix and number - void multiply_num(matrix &A, double B, matrix &C) -{ - matrix result(A.rows, A.columns); - - for (int i = 0; i < A.rows; i++) - { - for (int j = 0; j < A.columns; j++) - result.elements[i][j] = A.elements[i][j] * B; - } - C = result; - -} - - - //sub of matrix and number - void sub_num(matrix &A, double B, matrix &C) - { - matrix result(A.rows, A.columns); - - C = result; - - for (int i = 0; i < A.rows; i++) - { - for (int j = 0; j < A.columns; j++) - C.elements[i][j] = A.elements[i][j] - B; - } - - } - - - -void multiply_matrix(matrix &A, matrix &B , matrix &C) -{ - - if (A.columns!=B.rows) - cout << "error columns of first is not equal rows of the second" << endl; - //Or whatever the doctor says - - else - { - - matrix result(A.rows,B.columns); - C = result; - - // Multiplying and store in r - for (int i=0;irows,this -> columns); - sum_matrix((*this),p , result); - return result; - } - - - matrix matrix:: operator - (matrix p) // A + B = C - { - matrix result(this->rows,this -> columns); - sub_matrix((*this),p , result); - return result; - } - -matrix matrix :: operator + (double p)// A + number = C -{ - matrix result(this->rows,this -> columns); - sum_num((*this),p , result); - return result; - -} - - - -matrix matrix :: operator - (double p)// A + number = C -{ - matrix result(this->rows,this -> columns); - sub_num((*this),p , result); - return result; - -} - - - -matrix operator + (double a, matrix p) -{ - - matrix result(p.rows, p.columns); - sum_num(p,a , result); - return result; - - -} - - -matrix operator - (double a, matrix p) -{ - - matrix result(p.rows, p.columns); - p=-p; - sum_num(p,a , result); - return result; - -} - -matrix operator - (matrix p) -{ - matrix result(p.rows,p.columns); - - multiply_num(p,-1,result); - return result; -} - - -matrix matrix :: operator * (matrix p) //C=A*B -{ - matrix result(this -> rows , p.columns); - multiply_matrix((*this), p , result); - - return result; - -} - -matrix matrix :: operator * (double p) //C=A*B -{ - matrix result(this->rows , this-> columns); - multiply_num(*this,p,result); - return result; -} - - - -matrix operator * (double a, matrix p) // A = double * A -{ - matrix result(p.rows , p.columns); - multiply_num(p,a,result); - return result; -} - -//C=A/B it will crash if the number of rows != num of colums or if the 2 matrix don't match -matrix matrix :: operator / (matrix p) -{ - matrix result(this -> rows , p.columns); - divide_matrix((*this), p , result); - - return result; - -} - -matrix matrix :: operator / (double p) //C=A/p -{ - matrix result(this->rows , this-> columns); - multiply_num(*this,(1/p),result); - return result; -} - -//it will crash if the number of rows != num of colums -matrix operator / (double a, matrix p) // C = a / p -{ - matrix result(p.rows , p.columns); - divide_num_over_matrix(p,a,result); - return result; -} - - - - - -// Global Functions -int number_of(int e, string s,string c) -{ -int N=0; - -for (int i=0;i<=e;i++) - { - if(s.substr(i,1)==c) - N++; - } - return N; - -} - - -string space_trimer(string text) -{ - int spaceCounter = 0; - - while(1) - { - if(text.substr(spaceCounter,1) == " ") - { - spaceCounter ++; - } - else break; - } - - return text.substr(spaceCounter,text.length() - spaceCounter); - -} - - -/*matrix matrix :: sum_matrix(matrix &A) -{ - matrix result(this->rows , this -> columns); - if (A.rows != this ->rows || A.columns != this ->columns)cout << "error sizing" << endl; - - else { - for (int i = 0; i < A.rows; i++) - { - for (int j = 0; j < A.columns; j++) - result.elements[i][j] = this ->elements[i][j] + A.elements[i][j]; - } - } - return result; -} -void matrix:: operator += (matrix& A) -{ - sum_matrix(A); -} - - - -// sub_num operator - matrix matrix:: operator - (int p) // A - number = C - { - return sub_num(*this, p); - } - - matrix operator - (int a, matrix &p) // number - A = C - { - return sub_num(p, a); - } - -// sum_num operator - matrix matrix:: operator + (int p) // A + B = C - { - return sum_num(*this, p); - } - - matrix operator + (int a , matrix &p) // A + B = C - { - return sum_num(p,a); - } - -*/ +#include "matrix.h" +#include +#include +#include +#include +#include +#include +#include +using namespace std; +/*attach your libraries here*/ + + +/* + attach your function here and make sure + you use the inputs from the class variables + in case you needed to attach a new variable + inside the class feel free to add it in "matrix.h" +*/ + +//example function +//void matrix::doNull(); + + +//default constructor +matrix::matrix() +{ + elements =NULL; + rows = 0; + columns =0; + name = ""; +} + + +//constructor +matrix::matrix(int rows, int columns) +{ + if(rows<0 || columns <0) throw(0); + this->rows = rows; + this->columns = columns; + + if ((rows*columns) == 0) { + elements = NULL; + cout<<"warning: rows or columns equal zero, NULL Matrix created."< rows = p.rows; + this -> columns = p.columns; + + + + if ((rows*columns) == 0) { + elements = NULL; + cout<<"warning: input is a NULL matrix."< name = name; +} + +//resize matrix +void matrix::resize_matrix(int rows, int columns) +{ + if(rows<0 || columns<0) throw(0); + + if ((rows*columns) == 0) { + rows = columns = 0; + elements = NULL; + cout<<"warning: input rows or columns =0, NULL matrix created."<rows) + { + for(int k=0; kcolumns; k++) + { + if((k+1)<=this->columns) + { + newElements[i][k] = this->elements[i][k]; + } + } + } + } + this->destroy_matrix(); + this->rows = rows; + this->columns = columns; + this->elements = newElements; +} + + +//destructor +matrix::~matrix() +{ + destroy_matrix(); +} + +//destroy matrix +void matrix::destroy_matrix() +{ + + if(elements) + { + + + for (int i = 0; i < rows; i++) + { + delete[] elements [i]; //deletes an inner array of integer; + } + delete[] elements; //delete pointer holding array of pointers; + + + elements =NULL; + rows = 0; + columns =0; + name = ""; + } +} +//fill matrix from cin +void matrix::fill_matrix_cl() +{ + cout << "Please fill the matrix with its elements" << endl; + for (int i = 0; i < rows; i++) + { + cout << "values in row: " << i+1 << endl; + for (int j = 0; j < columns; j++) + { + cin >> elements[i][j]; + if(cin.fail()) throw(1); + } + cout << endl; + } +} + +void matrix::fill_matrix(string inputString) +{ + + +string newString = space_trimer(inputString); // remove spaces + + +// getting the name of the matrix +string name = newString.substr(0,1); + +// to remove brackets +int bracketFinder = newString.find("[",0); +string newString2; +if( newString[newString.length()-1] != ';' ) +newString2 = newString.substr(bracketFinder+1, newString.length()-bracketFinder-2); +else +newString2 = newString.substr(bracketFinder+1, newString.length()-bracketFinder-3); + + +int rows; +int columns; +rows = number_of(newString2.length(),newString2, ";") + 1; + +// new Edit ... +if( newString[newString.length()-3] == ';' ) + rows --; + + + +// substring the string into row +int beginRow = 0 ; // position of a row starting +int endRow; // position of a row ending +string row ; +for(int i = 0 ; i < rows ; i ++) +{ + endRow = newString2.find(";",beginRow); + row = newString2.substr(beginRow , endRow - beginRow); + string newRow = space_trimer(row); // to remove beginning spaces + beginRow = endRow+1; // update the begin of next find + + // now to get the number of columns + if(i == 0)// to create the matrix once since i now can get number of columns + { + columns = number_of(newRow.length(),newRow," ") + 1; + this -> reset_matrix(rows,columns); + } + else{ + int newcolumn=number_of(newRow.length(),newRow," ") + 1; + if(newcolumn!=columns) throw(2); + } + // substring the row into elements + int beginElement = 0; + int endElement; + string elementString; + float elementFloat; + for(int j = 0 ; j < columns ; j++) + { + endElement = newRow.find(" " ,beginElement); + elementString = newRow.substr(beginElement, endElement - beginElement); + beginElement = endElement +1; + elementFloat = strtof((elementString).c_str(),0); // string to float + // save the element to the matrix + this -> elements[i][j] = elementFloat; + } + +} + + this -> name = name; + + + //To print or not + if( newString[newString.length()-1] != ';' ) + print_matrix(); + +} + +//empty matrix +void matrix::empty_matrix() +{ + + for (int i = 0; i < rows; i++) + { + for (int j = 0 ; j < columns ; j++) + elements[i][j]=0; + } + +} + +void matrix::copy_matrix(matrix & p) +{ + string name = this->name; + destroy_matrix(); + + + this -> rows = p.rows; + this -> columns = p.columns; + this -> name = name; + + if ((rows*columns) == 0) { + elements = NULL; + cout<<"warning: input rows or columns =0, NULL matrix created."< rows = rows; + this -> columns = columns; + + if ((rows*columns) == 0) { + elements = NULL; + cout<<"warning: input rows or columns =0, NULL matrix created."< elements = new double*[rows]; + for(int i=0;i elements[i] = new double[columns]; + } + + + empty_matrix(); + + +} + + + +void matrix::print_matrix() +{ + if(this->elements == NULL) //to prevent crash + cout << "This matrix is not created." <name << " = " << endl; + for(int i=0;i(rows-1) || column>(columns-1)) throw(4); + + matrix *temp = new matrix((this->rows)-1,(this->columns)-1); + for(int i=0;i<(temp->rows);i++) + { + int flagRows = 0; + if(i >= row){ + flagRows = 1; + } + for(int k=0;k<(temp->columns);k++) + { + int flagColumns = 0; + if(k >= column){ + flagColumns = 1; + } + temp->elements[i][k] = this->elements[i+flagRows][k+flagColumns]; + } + } + return *temp; +} + +// measure the determinant of the matrix, it will crash if the number of rows != num of colums +double matrix::determinant() +{ + if(rows!=columns) throw(5); + + double result = 0.0; + if(this->rows == 2) + { + result = ((this->elements[0][0])*(this->elements[1][1])) + - ((this->elements[0][1])*(this->elements[1][0])); + return result; + } + for(int i=0;i<(this->rows);i++) + { + matrix temp = this->new_sub_matrix(i,0); + result += (temp.determinant())*pow(-1,i)*this->elements[i][0]; + } + return result; +} + +//flips the rows and columns , it won't crash +void matrix::flip_matrix() +{ + matrix temp(this->columns, this->rows); + for(int i=0;i< temp.rows;i++) + { + for(int k=0;k< temp.columns;k++) + { + temp.elements[i][k] = this->elements[k][i]; + } + } + std::string name = this->name; + this->copy_matrix(temp); + this->name = name; +} + +// divide matrix A over B , it won't crash if the rows != columns so make sure you operate with the right data + + + +matrix matrix::inverse() +{ + + + if(rows != columns) throw(6); + + matrix* temp = new matrix; + *temp = *this; + matrix u(rows,columns); + u.unity_matrix(); + int n = rows; + int i,j,k; + float d; + for(i=n-1;i>0;i--) + { + //if(a[i-1][1]<=a[i][1]) + if (temp -> elements[i-1][i-1]==0) + for(j=0;j elements[i][j]; + temp -> elements[i][j]=temp -> elements[i-1][j]; + temp -> elements[i-1][j]=d; + d=u.elements[i][j]; + u.elements[i][j]=u.elements[i-1][j]; + u.elements[i-1][j]=d; + } + } + for(i=0;i elements[j][i]/temp -> elements[i][i]; + for(k=0;k elements[j][k]-=temp -> elements[i][k]*d; + u.elements[j][k]-=u.elements[i][k]*d; + } + } + } + for(i=0;i elements[i][i]; + if(d==0) throw(7); + for(j=0;j elements[i][j]=temp -> elements[i][j]/d; + u.elements[i][j]=u.elements[i][j]/d; + } + } + return u; + + + + /*matrix *temp = new matrix; + temp->copy_matrix(*this); + double det = temp->get_determinant(); + //determinant(); + int flag = 1; + for (int i = 0; i < temp->columns; i++) + { + flag = pow(-1,i); + for (int k = 0; k < temp->rows; k++) + { + temp->elements[k][i] = (this->new_sub_matrix(k,i)).get_determinant() * flag; + flag *= -1; + } + } + temp->flip_matrix(); + multiply_num(*temp, (1/det), *temp); + return *temp;*/ +} + +void matrix:: unity_matrix() +{ + // if ( rows != columns || rows ==0 || columns==0 ) + // return; +if(rows != columns) throw(8); +if(rows==0 || columns==0) throw(9); +for(int i = 0 ; i < rows ; i++) + for(int j=0 ; j < columns ; j++) + if(i == j) + elements[i][j] = 1; + else + elements[i][j] = 0; +} + +bool is_equal(matrix &A , matrix &B) +{ + + if (A.rows != B.rows || A.columns!= B.columns) + return false; + + for(int i = 0 ; i < A.rows ; i++ ) + for(int j = 0 ; j < A.columns ; j++) + if(A.elements[i][j]!=B.elements[i][j]) + return false; + + return true; + +} + + + +// divide matrix A over B , it will crash if the number of rows != num of colums or if the 2 matrix don't match +void divide_matrix(matrix &A, matrix &B , matrix &C) +{ + if(B.get_columns() != B.get_rows()) throw(10); + if(A.get_columns() != B.get_rows()) throw(11); + + matrix b = B.inverse(); + multiply_matrix(A, b , C); +} + + //sum of two matrix + void sum_matrix(matrix &A, matrix &B , matrix &C) + { + //if (A.rows != B.rows || A.columns != B.columns)cout << "error sizing" << endl; + if(A.rows !=B.rows || A.columns != B.columns) throw(12); + for (int i = 0; i < A.rows; i++) + { + for (int j = 0; j < A.columns; j++) + C.elements[i][j] = A.elements[i][j] + B.elements[i][j]; + } + + + } + + +// sub of two matrix +void sub_matrix(matrix &A, matrix &B , matrix &C) + { + if(A.rows !=B.rows || A.columns != B.columns) throw(13); + + for (int i = 0; i < A.rows; i++) + { + for (int j = 0; j < A.columns; j++) + C.elements[i][j] = A.elements[i][j] - B.elements[i][j]; + } + + } + + + + //sum of matrix and number + void sum_num(matrix &A, double B, matrix &C) + { + + for (int i = 0; i < A.rows; i++) + { + for (int j = 0; j < A.columns; j++) + C.elements[i][j] = A.elements[i][j] + B; + } + + } + + //multiply of matrix and number + void multiply_num(matrix &A, double B, matrix &C) +{ + for (int i = 0; i < A.rows; i++) + { + for (int j = 0; j < A.columns; j++) + C.elements[i][j] = A.elements[i][j] * B; + } + +} + + + //sub of matrix and number + void sub_num(matrix &A, double B, matrix &C) + { + + for (int i = 0; i < A.rows; i++) + { + for (int j = 0; j < A.columns; j++) + C.elements[i][j] = A.elements[i][j] - B; + } + + } + + +void div_num(matrix &A, double B , matrix &C) // to divide matrix and number + +{ + for (int i = 0; i < A.rows; i++) + { + for (int j = 0; j < A.columns; j++){ + if(A.elements[i][j]==0) throw(14); + C.elements[i][j] = B / A.elements[i][j]; + } + } +} + +void multiply_matrix(matrix &A, matrix &B , matrix &C) +{ + + if (A.columns!=B.rows) throw(15); + + // Multiplying and store in r + for (int i=0;irows,this -> columns); + sum_matrix((*this),m , result); + return result; + } + + + matrix matrix:: operator - (matrix &m) // A - B = C + { + matrix result(this->rows,this -> columns); + sub_matrix((*this),m , result); + return result; + } + +matrix matrix :: operator + (double a)// C = m + a +{ + matrix result(this->rows,this -> columns); + sum_num((*this),a , result); + return result; + +} + + + +matrix matrix :: operator - (double a)// C = m -a +{ + matrix result(this->rows,this -> columns); + sub_num((*this), a , result); + return result; +} + + + +matrix operator + (double a, matrix &m) //a+m +{ + + matrix result(m.rows, m.columns); + sum_num(m,a , result); + return result; + + +} + + +matrix operator - (double a, matrix &m) // a-m +{ + + matrix result(m.rows, m.columns); + m=-m; + sum_num(m,a , result); + return result; + +} + +matrix operator - (matrix &p) // A = -A +{ + matrix result(p.rows,p.columns); + + multiply_num(p,-1,result); + return result; +} + + +matrix matrix :: operator * (matrix &m) //C=A*B +{ + matrix result(rows , m.columns); + multiply_matrix((*this),m, result); + return result; + +} + +matrix matrix :: operator * (double a) //C=m*a +{ + matrix result(this->rows , this-> columns); + multiply_num(*this,a,result); + return result; +} + + + +matrix operator * (double a, matrix &m) // C = a * m +{ + matrix result(m.rows , m.columns); + multiply_num(m,a,result); + return result; +} + + +matrix matrix :: operator / (matrix &m) +{ + + + matrix result(rows , m.columns); + if(is_equal((*this) , m)) + result.unity_matrix(); + + + // else if (m.get_determinant() == 0 || isnan(m.get_determinant())) + //{ + // cout << "Error determinant = 0" <rows , this-> columns); + multiply_num(*this,(1/a),result); + return result; +} + +matrix operator / (double a, matrix &m) // C = a / m +{ + matrix result(m.rows,m.columns); + div_num(m,a,result); + return result; +} + +//WALEED ... +matrix matrix::get_cofactor(int r,int c) +{ + if(rows<=1 || columns<=1) throw(16); + if(r>rows || c>columns) throw(17); + matrix m(rows-1, columns-1); + for(int iR=0;iR0;i--) + { + //if(a[i-1][1]<=a[i][1]) + if (temp -> elements[i-1][i-1]==0) + for(j=0;j elements[i][j]; + temp -> elements[i][j]=temp -> elements[i-1][j]; + temp -> elements[i-1][j]=d; + } + } + for(i=0;i elements[j][i]/temp -> elements[i][i]; + for(k=0;k elements[j][k]-=temp -> elements[i][k]*d; + } + } + } + for (i = 0 ; i < n ; i++) + { + result *= temp -> elements[i][i]; + } + + delete temp; // $$$$$$ + return result; + + + + + /* if(rows!=columns);//throw("Invalid matrix dimension"); + if(rows==1&&columns==1)return elements[0][0]; + double value = 0, m = 1; + for(int iR=0;iRrows , this->columns); + power(*this,n,result); + return result; +} + +//Function to calculate the power to each element this is for the operator ".^" and can have a fraction power + +void power_elements(matrix& a, double n, matrix& result){ + for(int i=0; i=0){ + result.elements[i][j]=sqrt(a.elements[i][j]); + } + else { + throw(19); + } + } + } +} + +//Function for filling the matrix with zeros +matrix zeros(int rows, int columns){ + matrix result(rows,columns); + for(int i=0; icolumns; + +for(int i=0;irows; +} + + + matrix *newMatrix = new matrix(actual_rows,cols); +int k=0; + for(int i= 0 ;i < rows ; i++) + { + for(int actual_rows2=0; actual_rows2rows;actual_rows2++) + { + for(int j = 0 ; j < cols ; j++ ) + { + newMatrix -> elements[k][j] = input[i]->elements[actual_rows2][j]; + + } + + k++; + } + } + + return newMatrix; + +} + + + + + +matrix *add_horz(matrix** input , int cols) +{ + int actual_cols=0; + + int rows = input[0]->rows; + +for(int i=0;icolumns; +} + + matrix *newMatrix = new matrix(rows,actual_cols); +int k=0; + for(int i= 0 ;i < cols ; i++) + { + for(int actual_cols2=0; actual_cols2columns;actual_cols2++) + { + for(int j = 0 ; j < rows ; j++ ) + { + newMatrix -> elements[j][k] = input[i]->elements[j][actual_cols2]; + + } + + k++; + } + } + + return newMatrix; + +} + + + + + + + +// Global Functions + +//count number of chars in a string +int number_of(int e, string s,string c) +{ +int N=0; + +for (int i=0;i<=e;i++) + { + if(s.substr(i,1)==c) + N++; + } + return N; + +} + + +//to trim spaces from the begin and end of a text +string space_trimer(string text) +{ + + // to trim extra start spaces + int spaceStarter = 0; + + while(1) + { + if(text.substr(spaceStarter,1) == " ") + { + spaceStarter++; + } + else break; + } + + string start_trimed = text.substr(spaceStarter,text.length() - spaceStarter); + + // to trim extra ending spaces + spaceStarter = start_trimed.length()-1; + int counter = 0; + while(1) + { + if(start_trimed.substr(spaceStarter,1) == " ") + { + counter ++; + spaceStarter--; + } + else break; + } + string all_trimed = start_trimed.substr(0, start_trimed.length() - counter); + + return all_trimed ; + +} + +//to find a number of digits in a float digits ( 1322.12 returns 4 ) max 6 digits +int number_digits(float input) +{ +if (input < 10) return 1; +if (input < 100) return 2; +if (input < 1000) return 3; +if (input < 10000) return 4; +if (input < 100000) return 5; +//if (input < 1000000) return 6; +else return 6; +} + + diff --git a/matrix.h b/matrix.h index 6bde0e2..43699c5 100644 --- a/matrix.h +++ b/matrix.h @@ -1,84 +1,119 @@ -#ifndef MATRIX_H -#define MATRIX_H -#include - - -class matrix -{ - double** elements; - int rows,columns; - std::string name; - // bool valid; to be added.. - - /*attach any further variables here*/ - -public: - - /*attach your header function here*/ - - matrix(); //default constructor does not create a matrix - matrix(const matrix &p);//( matrix A = B ) - matrix(int rows, int columns); //constructor create a matrix (rows*columns) and initialize them with zeros - ~matrix(); //destructor - - - - //gets - int get_rows(); - int get_columns(); - std::string get_name(); - - //sets - void set_name(std::string name); - void resize_matrix(int rows, int columns);//updates num of rows and columns and updates the elements array - - //algorithms and functions - void fill_matrix_cl();//fill matrix with cin - void fill_matrix(std::string inputString); //fill from a string - void empty_matrix(); // make all the elements of the array = 0; - void destroy_matrix(); - void print_matrix(); - void reset_matrix(int rows, int columns); //reset dimentions - void copy_matrix(matrix &p); - - matrix new_sub_matrix(int row, int column); // generate a sub matrix, it won't crash - double determinant(); // measure the determinant of the matrix, it will crash if the number of rows != num of colums - void flip_matrix(); //flips the rows and columns , it won't crash - matrix inverse(); // divide matrix A over B , it won't crash if the rows != columns so make sure you operate on the right data - - //Friend functions - friend void sum_matrix(matrix &A, matrix &B , matrix &C); // to sum two matrix C = A+B - friend void sub_matrix(matrix &A, matrix &B , matrix &C); // to sub two matrix C = A-B - friend void multiply_matrix(matrix &A, matrix &B , matrix &C); // to multiply two matrix C = A*B - friend void divide_matrix(matrix &A, matrix &B , matrix &C); // divide matrix A over B , it will crash if the number of rows != num of colums or if the 2 matrix don't match - friend void sum_num(matrix &A, double B , matrix &C); // to sum matrix and number - friend void sub_num(matrix &A, double B , matrix &C); // to sub matrix and number - friend void multiply_num(matrix &A, double B , matrix &C); // to sub matrix and number - friend void divide_num_over_matrix(matrix &A, double B , matrix &C); // divide number B over matrix A , it will crash if the number of rows != num of colums - - //Operators - matrix operator = (matrix p); // A = B = C - matrix operator + (matrix p);// A + B = C - matrix operator - (matrix p);// A - B = C - matrix operator + (double p);// A + number = C - friend matrix operator + (double a, matrix p);// number + A = C - matrix operator - (double p);// A - number = C - friend matrix operator - (double a, matrix p);// number + A = C - matrix operator * (matrix p); - matrix operator * (double p); // C= A * number - friend matrix operator * (double a, matrix p); // A = number * A - friend matrix operator - (matrix p); // A = -A - matrix operator / (matrix p); //C=A/B it will crash if the number of rows != num of colums or if the 2 matrix don't match - matrix operator / (double p); //C=A/p - friend matrix operator / (double a, matrix p); //it will crash if the number of rows != num of colums - - - -}; - -//global functions -std::string space_trimer(std::string text); // remove extra spaces from beginning of a text -int number_of(int e, std::string s,std::string c); // count the number of special char in a text - - -#endif // MATRIX_H +#ifndef MATRIX_H +#define MATRIX_H +#include + + +class matrix +{ + double** elements; + int rows,columns; + // bool valid; to be added.. + + /*attach any further variables here*/ + +public: + std::string name; + + /*attach your header function here*/ + + matrix(); //default constructor does not create a matrix + matrix(const matrix &p);//( matrix A = B ) + matrix(int rows, int columns); //constructor create a matrix (rows*columns) and initialize them with zeros + ~matrix(); //destructor + + + + //gets + int get_rows(); + int get_columns(); + std::string get_name(); + + //sets + void set_name(std::string name); + void resize_matrix(int rows, int columns);//updates num of rows and columns and updates the elements array + + //algorithms and functions + void fill_matrix_cl();//fill matrix with cin + void fill_matrix(std::string inputString); //fill from a string + void empty_matrix(); // make all the elements of the array = 0; + void destroy_matrix(); + void print_matrix(); + void reset_matrix(int rows, int columns); //reset dimentions + void sum(matrix *A , matrix *B); + void copy_matrix(matrix &p); + matrix new_sub_matrix(int row, int column); // generate a sub matrix, it won't crash + double determinant(); // measure the determinant of the matrix, it will crash if the number of rows != num of colums + void flip_matrix(); //flips the rows and columns , it won't crash + matrix inverse(); // divide matrix A over B , it won't crash if the rows != columns so make sure you operate on the right data + void unity_matrix(); + + //TEST WALEED ... + double get_determinant(); + matrix get_cofactor(int r, int c); + + + //Friend functions + friend void sum_matrix(matrix &A, matrix &B , matrix &C); // to sum two matrix C = A+B + friend void sub_matrix(matrix &A, matrix &B , matrix &C); // to sub two matrix C = A-B + friend void multiply_matrix(matrix &A, matrix &B , matrix &C); // to multiply two matrix C = A*B + friend void divide_matrix(matrix &A, matrix &B , matrix &C); // divide matrix A over B , it will crash if the number of rows != num of colums or if the 2 matrix don't match + friend void sum_num(matrix &A, double B , matrix &C); // to sum matrix and number + friend void sub_num(matrix &A, double B , matrix &C); // to sub matrix and number + friend void multiply_num(matrix &A, double B , matrix &C); // to multiply matrix and number + friend void div_num(matrix &A, double B , matrix &C); // to divide matrix and number + friend bool is_equal(matrix &A , matrix &B); + + + + //Operators + matrix operator = (matrix m); // A = B = C + friend matrix operator - (matrix &m); // C = -A + matrix operator + (matrix &m);// C = A + B + matrix operator + (double a);// C = A + number + friend matrix operator + (double a, matrix &m);// C = number + A + matrix operator - (matrix &m);// C = A - B + matrix operator - (double a);// C = A - number + friend matrix operator - (double a, matrix &m); // C = number - A + matrix operator * (matrix &m); // C = A + B + matrix operator * (double a); // C= A * number + friend matrix operator * (double a, matrix &m); // C = number * A + matrix operator / (matrix &m); //C= A/B it will crash if the number of rows != num of colums or if the 2 matrix don't match + matrix operator / (double a); //C= A/number + friend matrix operator / (double a, matrix &m); //C=number/A + + +//Phase two Functions + friend matrix zeros(int rows, int columns);//func. to fill matrix with zeros + friend matrix random(int rows, int columns);// func.to fill matrix with random no. + friend matrix ones(int rows, int columns); //Function for filling the matrix with ones +//matrix kobry (int r, int c); + friend void power(matrix& a,int n,matrix& result);//matrix power int/ shouldn't take 1x1 matrix power + matrix operator ^ (int n); + + friend void power_elements(matrix& a, double n, matrix& result); //power raised to every element/ can be double + + friend void squareroot ( matrix& a, matrix& result);// square root to each element + +//trigonometric and logarithmic functions + + friend matrix sin_elements(matrix& a); + friend matrix cos_elements(matrix& a); + friend matrix tan_elements(matrix& a); + + friend matrix log_elements(matrix& a); + friend matrix ln_elements(matrix& a); + + +friend matrix * add_ver(matrix** input ,int rows); +friend matrix *add_horz(matrix** input , int cols); + + +}; + +//global functions +std::string space_trimer(std::string text); // remove extra spaces from beginning of a text +int number_of(int e, std::string s,std::string c); // count the number of special char in a text +int number_digits(float input); //count number of digits + + +#endif // MATRIX_H diff --git a/our_string.cpp b/our_string.cpp new file mode 100644 index 0000000..65b004c --- /dev/null +++ b/our_string.cpp @@ -0,0 +1,66 @@ +#include "our_string.h" +using namespace std; + +//trim char +string our_string:: trim(string right_string,char c) +{ + if(right_string.find(c)==-1) + { + return right_string; + } + string left_string=""; + + int i=right_string.find(c)+1; + + left_string=right_string.substr(0,i-1); + + return left_string+trim(right_string.substr(i,right_string.length()-1),c); + +} + +//if number return 1 +bool our_string :: is_number (string c , double & number ){ + for(int i=0; i=48 && int(c[i])<=57)|| int(c[i])==46) + continue; + else return 0; + } + number=atof(c.c_str());; + return 1; +} + +//find one char +int our_string:: find_str (string input ,char a,int& howmany,int start) +{ + howmany=0; + int x=0; + int index; + for(int i=start;i +using namespace std; +class our_string : public string +{ +public: + //our_string Trim(char a); + //bool Is_it_num(our_string input, double &num); + //int find_str(our_string a, int &how_many,int start); + //int find_2str(our_string a, our_string b,int start); + + string trim(string right_string,char c); + int find_str (string input ,char a,int& howmany,int start); + int find_2str(string input,char a,char b,int start); + bool is_number (string c , double & number ); +}; diff --git a/shuffleYard.cpp b/shuffleYard.cpp new file mode 100644 index 0000000..8a6fdff --- /dev/null +++ b/shuffleYard.cpp @@ -0,0 +1,1109 @@ +#include +#include +#include +#include +#include "matrix.h" +#include +#include "AVL.cpp" + +#include +#include +#include + +using namespace std; + +#define PI 3.14159265 + +void sighandler(int signum) +{ + //printf("Process %d got signal %d\n", getpid(), signum); + throw "----------------- INVALID NUMBER OF OPERATORS -------------------"; +} + + +AVL matrixTree; + +class Node +{ + public: + Node *child; + virtual int type() = 0; +}; + +class CharNode : public Node +{ + public: + char value; + int type() + { + return 0; + } + CharNode(char value) + { + this->value = value; + } +}; + +class FloatNode : public Node +{ + public: + float value; + int type() + { + return 1; + } + FloatNode(float value) + { + this->value = value; + } + +}; + +class MatrixNode : public Node +{ + public: + matrix* value; + bool important; + int type() + { + return 2; + } + MatrixNode(matrix *value) + { + this->value = value; + this->important = false; + } + MatrixNode(matrix *value, bool important) + { + this->value = value; + this->important = important; + } + ~MatrixNode() + { + if(!important)delete value; + } +}; + +class TempMatrixNode : public Node +{ + public: + matrix* value; + bool important; + int type() + { + return 3; + } + TempMatrixNode(matrix *value) + { + this->value = value; + this->important = false; + } + TempMatrixNode(matrix *value, bool important) + { + this->value = value; + this->important = important; + } + ~TempMatrixNode() + { + if(!important)delete value; + } +}; + +Node* do_operation(string str); + +int get_operator_order(char cur) +{ + if(cur == '=')return 1; + if(cur == '+' || cur == '-')return 2; + // " for ./ + if(cur == '*' || cur == '/' || cur == '"')return 3; + //minus operator + if(cur == '%')return 4; + if(cur == '^')return 5; + //sin operator + //cos operator + //tan operator + if(cur == '<' || cur == '!' || cur == '_')return 6; + // rand(4,4) + // eye(4, 4) + // zeros(2, 3) + // ones(3, 6) + // sqrt() + if(cur == '@' || cur == '#' || cur == '?' || cur == ':' || cur == '`')return 7; + //print operator + if(cur == '>')return 8; + if(cur == '(')return 0; + if(cur == ')')return 0; +}; + +bool is_higher_order_than(char cur, CharNode* node) +{ + if(node->value == '(' && cur == ')')return true; + //minus operator + if(node->value == '%' && cur == '%')return true; + if(get_operator_order(cur) > get_operator_order(node->value)) + { + return true; + } + return false; +}; + +template +class Stack +{ + public: + T *top; + Stack() + { + this->top = NULL; + } + void add(T *node) + { + node->child = this->top; + this->top = node; + } + T* pop() + { + T * temp = this->top; + this->top = this->top->child; + return temp; + } + void print() + { + T *current = this->top; + while(current != NULL) + { + switch(current->type()) + { + case 2: + //((MatrixNode*)(current))->value->print_matrix(); + break; + case 1: + cout<<((FloatNode*)(current))->value<value<type() == 2) + { + cout<<((FloatNode*)(current))->value<value<child; + } + } +}; + +char* infix_to_reverse_polish(char *infix) +{ + int countAll = 0, countVirtual = 0; + for (int i = 0; infix[i] != '\0'; ++i) + { + if(infix[i] == '[') + { + while(infix[i] != ']') + { + countAll++; + i++; + } + countVirtual+=2; + countAll++; + } + else if(infix[i] != ' ') + { + countAll++; + if(infix[i] == '.' || infix[i] == '*' || infix[i] == '/' || infix[i] == '^' || infix[i] == '+' || infix[i] == '-' || infix[i] == '(' || infix[i] == ')' || infix[i] == ']' || infix[i] == '[' || infix[i] == '=') + { + countVirtual++; + } + } + } + char *temp = new char[countAll + countVirtual + 1]; + Stack* temp_stack = new Stack(); + int j = 0; + bool flag = true, flagNegative = false; + for (int i = 0; infix[i] != '\0'; ++i) + { + if(infix[i] != ' ') + { + //./ operation + if(infix[i] == '.' && infix[i+1] == '/'){ + i += 1; + flag = false; + CharNode *x = new CharNode('"'); + temp_stack->add(x); + flagNegative = true; + } + //sin operation + else if(infix[i] == 's' && infix[i+1] == 'i' && infix[i+2] == 'n' && infix[i+3] == '('){ + i += 2; + flag = false; + CharNode *x = new CharNode('<'); + temp_stack->add(x); + } + //cos operation + else if(infix[i] == 'c' && infix[i+1] == 'o' && infix[i+2] == 's' && infix[i+3] == '('){ + i += 2; + flag = false; + CharNode *x = new CharNode('!'); + temp_stack->add(x); + } + //tan operation + else if(infix[i] == 't' && infix[i+1] == 'a' && infix[i+2] == 'n' && infix[i+3] == '('){ + i += 2; + flag = false; + CharNode *x = new CharNode('_'); + temp_stack->add(x); + } + //sqrt operation + else if(infix[i] == 's' && infix[i+1] == 'q' && infix[i+2] == 'r' && infix[i+3] == 't' && infix[i+4] == '('){ + i += 3; + flag = false; + CharNode *x = new CharNode('`'); + temp_stack->add(x); + } + //rand operation + else if(infix[i] == 'r' && infix[i+1] == 'a' && infix[i+2] == 'n' && infix[i+3] == 'd' && infix[i+4] == '('){ + i += 5; + flag = false; + temp[j] = '@'; + j++; + int bracketCount = 1; + while(true) + { + if(infix[i] == '(')bracketCount++; + if(infix[i] == ')')bracketCount--; + if(bracketCount == 0)break; + temp[j] = infix[i]; + i++; + j++; + } + temp[j] = '@'; + j++; + flagNegative = true; + } + //eye operation + else if(infix[i] == 'e' && infix[i+1] == 'y' && infix[i+2] == 'e' && infix[i+3] == '('){ + i += 4; + flag = false; + temp[j] = '#'; + j++; + int bracketCount = 1; + while(true) + { + if(infix[i] == '(')bracketCount++; + if(infix[i] == ')')bracketCount--; + if(bracketCount == 0)break; + temp[j] = infix[i]; + i++; + j++; + } + temp[j] = '#'; + j++; + flagNegative = true; + } + //zeros operation + else if(infix[i] == 'z' && infix[i+1] == 'e' && infix[i+2] == 'r' && infix[i+3] == 'o' && infix[i+4] == 's' && infix[i+5] == '('){ + i += 6; + flag = false; + temp[j] = '?'; + j++; + j++; + int bracketCount = 1; + while(true) + { + if(infix[i] == '(')bracketCount++; + if(infix[i] == ')')bracketCount--; + if(bracketCount == 0)break; + temp[j] = infix[i]; + i++; + j++; + } + temp[j] = '?'; + j++; + flagNegative = true; + } + //ones operation + else if(infix[i] == 'o' && infix[i+1] == 'n' && infix[i+2] == 'e' && infix[i+3] == 's' && infix[i+4] == '('){ + i += 5; + flag = false; + temp[j] = ':'; + j++; + int bracketCount = 1; + while(true) + { + if(infix[i] == '(')bracketCount++; + if(infix[i] == ')')bracketCount--; + if(bracketCount == 0)break; + temp[j] = infix[i]; + i++; + j++; + } + temp[j] = ':'; + j++; + flagNegative = true; + } + else if(infix[i] == '[') + { + flag = false; + while(infix[i] != ']') + { + temp[j] = infix[i]; + i++; + j++; + } + temp[j] = ']'; + j++; + flagNegative = true; + } + else if(infix[i] == ';'){ + bool equal_flag = false; + while(temp_stack->top != NULL) + { + if(((CharNode*)(temp_stack->top))->value == '='){ + equal_flag = true; + temp[j] = '>'; + j++; + } + CharNode *x = (CharNode*)temp_stack->pop(); + temp[j] = x->value; + j++; + delete x; + } + flag = false; + //print + if(!equal_flag){ + temp[j] = '>'; + j++; + } + flagNegative = false; + } + else if( infix[i] == '*' || infix[i] == '/' || infix[i] == '^' || infix[i] == '+' || infix[i] == '-' || infix[i] == '(' || infix[i] == ')' || infix[i] == '%' || infix[i] == '=') + { + if(infix[i] == '-' && !flagNegative)infix[i]= '%'; + if(temp_stack->top != NULL) + { + //flagNegative = false; + if((infix[i] == '(' || is_higher_order_than(infix[i], (CharNode*)temp_stack->top))&& infix[i] != ')'){ + flag = false; + CharNode *x = new CharNode(infix[i]); + flagNegative = false; + temp_stack->add(x); + }else{ + flag = false; + if(infix[i] == ')')flagNegative = true; + CharNode *x = (CharNode*)temp_stack->pop(); + if(x->value != '(') + { + temp[j] = x->value; + j++; + i--; + } + delete x; + } + }else{ + flag = false; + CharNode *x = new CharNode(infix[i]); + flagNegative = false; + temp_stack->add(x); + } + }else{ + if(!flag) + { + temp[j] = ';'; + j++; + } + temp[j] = infix[i]; + j++; + flag = true; + flagNegative = true; + } + } + } + while(temp_stack->top != NULL){ + CharNode *x = (CharNode*)temp_stack->pop(); + temp[j] = x->value; + j++; + } + + temp[j] = '\0'; + + return temp; +} + +Node* reverse_polish_to_float(char *reverse_polish) +{ + Stack* temp_stack = new Stack(); + bool echo_flag = false; + for (int i = 0; reverse_polish[i] != '\0'; ++i) + { + if(reverse_polish[i] == ';'); + else if(reverse_polish[i] == '='){ + Node *right = temp_stack->pop(); + Node *left = temp_stack->pop(); + if(right->type() == 3){ + throw "--------------- UNDEFINED VARIABLE \"" + (((TempMatrixNode*)right)->value)->name + "\" AFTER \"" + "=" + "\" ----------------"; + } + if(right->type() == 1){ + if(echo_flag){ + cout<<(((FloatNode*)right)->value)<important = false; + }else{ + *(((MatrixNode*)left)->value) = *(((MatrixNode*)right)->value); + if(echo_flag){ + (((MatrixNode*)right)->value)->print_matrix(); + cout<value)); + } + echo_flag = false; + delete left; + delete right; + } + else if(reverse_polish[i] == '[') + { + int count = 0; + for (int k = i+1; reverse_polish[k] != ']'; ++k) + { + count++; + } + char *temp_string = new char[count+4]; + int j = 2; + temp_string[0]='='; + temp_string[1]='['; + i++; + while(reverse_polish[i] != ']') + { + temp_string[j] = reverse_polish[i]; + j++; + i++; + } + temp_string[count+2]=']'; + temp_string[count+3]=';'; + temp_string[count+4]='\0'; + //cout<fill_matrix(temp_string); + Node *tempNode = new MatrixNode(tempMatrix); + /*temp value*/ + temp_stack->add(tempNode); + delete temp_string; + } + //rand operation + else if(reverse_polish[i] == '@') + { + int count = 0; + for (int k = i+1; reverse_polish[k] != ','; ++k) + { + count++; + } + char *temp_string = new char[count + 1]; + int j = 0; + i++; + while(reverse_polish[i] != ',') + { + temp_string[j] = reverse_polish[i]; + j++; + i++; + } + temp_string[count]='\0'; + Node *left = do_operation(temp_string); + if(left->type() == 2) + { + throw "---- EXPECT 1ST ARG. OF RAND TO BE FLOAT BUT MATRIX WAS FOUND ----"; + } + i++; + count = 0; + for (int k = i+1; reverse_polish[k] != '@'; ++k) + { + count++; + } + temp_string = new char[count + 1]; + j = 0; + i++; + while(reverse_polish[i] != '@') + { + temp_string[j] = reverse_polish[i]; + j++; + i++; + } + temp_string[count]='\0'; + Node *right = do_operation(temp_string); + if(right->type() == 2) + { + throw "---- EXPECT 2ND ARG. OF RAND TO BE FLOAT BUT MATRIX WAS FOUND ----"; + } + //calculations part + Node *tempNode; + matrix* y = new matrix; + { + matrix x; + *y = x.random((int)(((FloatNode*)left)->value), (int)(((FloatNode*)right)->value)); + } + tempNode = new MatrixNode(y); + temp_stack->add(tempNode); + delete left; + delete right; + } + //eye operation + else if(reverse_polish[i] == '#') + { + int count = 0; + for (int k = i+1; reverse_polish[k] != ','; ++k) + { + count++; + } + char *temp_string = new char[count + 1]; + int j = 0; + i++; + while(reverse_polish[i] != ',') + { + temp_string[j] = reverse_polish[i]; + j++; + i++; + } + temp_string[count]='\0'; + Node *left = do_operation(temp_string); + if(left->type() == 2) + { + throw "---- EXPECT 1ST ARG. OF EYE TO BE FLOAT BUT MATRIX WAS FOUND -----"; + } + i++; + count = 0; + for (int k = i+1; reverse_polish[k] != '#'; ++k) + { + count++; + } + temp_string = new char[count + 1]; + j = 0; + i++; + while(reverse_polish[i] != '#') + { + temp_string[j] = reverse_polish[i]; + j++; + i++; + } + temp_string[count]='\0'; + Node *right = do_operation(temp_string); + if(right->type() == 2) + { + throw "---- EXPECT 2ND ARG. OF EYE TO BE FLOAT BUT MATRIX WAS FOUND -----"; + } + //calculations part + Node *tempNode; + matrix* y = new matrix; + { + matrix x; + *y = x.ones((int)(((FloatNode*)left)->value), (int)(((FloatNode*)right)->value)); + } + tempNode = new MatrixNode(y); + temp_stack->add(tempNode); + delete left; + delete right; + } + //zeros operation + else if(reverse_polish[i] == '?') + { + int count = 0; + for (int k = i+1; reverse_polish[k] != ','; ++k) + { + count++; + } + char *temp_string = new char[count + 1]; + int j = 0; + i++; + while(reverse_polish[i] != ',') + { + temp_string[j] = reverse_polish[i]; + j++; + i++; + } + temp_string[count]='\0'; + Node *left = do_operation(temp_string); + if(left->type() == 2) + { + throw "---- EXPECT 1ST ARG. OF ZEROS TO BE FLOAT BUT MATRIX WAS FOUND ---"; + } + i++; + count = 0; + for (int k = i+1; reverse_polish[k] != '?'; ++k) + { + count++; + } + temp_string = new char[count + 1]; + j = 0; + i++; + while(reverse_polish[i] != '?') + { + temp_string[j] = reverse_polish[i]; + j++; + i++; + } + temp_string[count]='\0'; + Node *right = do_operation(temp_string); + if(right->type() == 2) + { + throw "---- EXPECT 2ND ARG. OF ZEROS TO BE FLOAT BUT MATRIX WAS FOUND ---"; + } + //calculations part + Node *tempNode; + matrix* y = new matrix; + { + matrix x; + *y = x.zeros((int)(((FloatNode*)left)->value), (int)(((FloatNode*)right)->value)); + } + tempNode = new MatrixNode(y); + temp_stack->add(tempNode); + delete left; + delete right; + } + //ones operation + else if(reverse_polish[i] == ':') + { + int count = 0; + for (int k = i+1; reverse_polish[k] != ','; ++k) + { + count++; + } + char *temp_string = new char[count + 1]; + int j = 0; + i++; + while(reverse_polish[i] != ',') + { + temp_string[j] = reverse_polish[i]; + j++; + i++; + } + temp_string[count]='\0'; + Node *left = do_operation(temp_string); + if(left->type() == 2) + { + throw "---- EXPECT 1ST ARG. OF ONES TO BE FLOAT BUT MATRIX WAS FOUND ---"; + } + i++; + count = 0; + for (int k = i+1; reverse_polish[k] != ':'; ++k) + { + count++; + } + temp_string = new char[count + 1]; + j = 0; + i++; + while(reverse_polish[i] != ':') + { + temp_string[j] = reverse_polish[i]; + j++; + i++; + } + temp_string[count]='\0'; + Node *right = do_operation(temp_string); + if(right->type() == 2) + { + throw "---- EXPECT 2ND ARG. OF ONES TO BE FLOAT BUT MATRIX WAS FOUND ---"; + } + //calculations part + Node *tempNode; + matrix* y = new matrix; + { + matrix x; + *y = x.ones((int)(((FloatNode*)left)->value), (int)(((FloatNode*)right)->value)); + } + tempNode = new MatrixNode(y); + temp_stack->add(tempNode); + delete left; + delete right; + } + //sin operator + else if(reverse_polish[i] == '<') + { + Node *left = temp_stack->pop(); + Node *tempNode; + if(left->type() == 3){ + throw "-------------- UNDEFINED VARIABLE \"" + (((TempMatrixNode*)left)->value)->name + "\" INSIDE \"" + "sin" + "\" --------------"; + } + if(left->type() == 1) + { + tempNode = new FloatNode(sin ( ((FloatNode*)(left))->value * PI/180) ); + }else{ + matrix *tempMatrix = new matrix; + *tempMatrix = ( *(((MatrixNode*)left)->value) * -1); + tempNode = new MatrixNode(tempMatrix); + } + /*temp value*/ + temp_stack->add(tempNode); + delete left; + } + //cos operator + else if(reverse_polish[i] == '!') + { + Node *left = temp_stack->pop(); + Node *tempNode; + if(left->type() == 3){ + throw "-------------- UNDEFINED VARIABLE \"" + (((TempMatrixNode*)left)->value)->name + "\" INSIDE \"" + "cos" + "\" --------------"; + } + if(left->type() == 1) + { + tempNode = new FloatNode(cos ( ((FloatNode*)(left))->value * PI/180) ); + }else{ + matrix *tempMatrix = new matrix; + *tempMatrix = ( *(((MatrixNode*)left)->value) * -1); + tempNode = new MatrixNode(tempMatrix); + } + /*temp value*/ + temp_stack->add(tempNode); + delete left; + } + //tan operator + else if(reverse_polish[i] == '_') + { + Node *left = temp_stack->pop(); + Node *tempNode; + if(left->type() == 3){ + throw "-------------- UNDEFINED VARIABLE \"" + (((TempMatrixNode*)left)->value)->name + "\" INSIDE \"" + "tan" + "\" --------------"; + } + if(left->type() == 1) + { + tempNode = new FloatNode(tan ( ((FloatNode*)(left))->value * PI/180) ); + }else{ + matrix *tempMatrix = new matrix; + *tempMatrix = ( *(((MatrixNode*)left)->value) * -1); + tempNode = new MatrixNode(tempMatrix); + } + /*temp value*/ + temp_stack->add(tempNode); + delete left; + } + //sqrt operator + else if(reverse_polish[i] == '`') + { + Node *left = temp_stack->pop(); + Node *tempNode; + if(left->type() == 3){ + throw "-------------- UNDEFINED VARIABLE \"" + (((TempMatrixNode*)left)->value)->name + "\" INSIDE \"" + "sqrt" + "\" -------------"; + } + if(left->type() == 1) + { + tempNode = new FloatNode(sqrt ( ((FloatNode*)(left))->value ) ); + }else{ + matrix *tempMatrix = new matrix; + *tempMatrix = ( *(((MatrixNode*)left)->value) * -1); + tempNode = new MatrixNode(tempMatrix); + } + /*temp value*/ + temp_stack->add(tempNode); + delete left; + } + //print operator + else if(reverse_polish[i] == '>') + { + if(reverse_polish[i+1] != '='){ + Node *left = temp_stack->pop(); + if(left->type() == 1) + { + cout<<((FloatNode*)(left))->value; + /*the third print if the output was float*/ + }else{ + ((MatrixNode*)(left))->value->print_matrix(); + } + cout<pop(); + Node *tempNode; + if(left->type() == 3){ + throw "---------------- UNDEFINED VARIABLE \"" + (((TempMatrixNode*)left)->value)->name + "\" AFTER \"" + '-' + "\" ---------------"; + } + if(left->type() == 1) + { + tempNode = new FloatNode(((FloatNode*)(left))->value * -1); + }else{ + matrix *tempMatrix = new matrix; + *tempMatrix = ( *(((MatrixNode*)left)->value) * -1); + tempNode = new MatrixNode(tempMatrix); + } + /*temp value*/ + temp_stack->add(tempNode); + delete left; + } + else if(reverse_polish[i] == '*' || reverse_polish[i] == '/' || reverse_polish[i] == '^' || reverse_polish[i] == '+' || reverse_polish[i] == '-' || reverse_polish[i] == '"') + { + Node *right = temp_stack->pop(); + Node *left = temp_stack->pop(); + if(left->type() == 3){ + throw "----------------- UNDEFINED VARIABLE \"" + (((TempMatrixNode*)left)->value)->name + "\" BEFORE " + reverse_polish[i] + " ---------------"; + }else if(right->type() == 3){ + throw "----------------- UNDEFINED VARIABLE \"" + (((TempMatrixNode*)right)->value)->name + "\" AFTER " + reverse_polish[i] + " ----------------"; + } + Node *tempNode = NULL; + switch(reverse_polish[i]){ + case '+': + if(left->type() == 1) + { + if(right->type() == 1) + { + tempNode = new FloatNode(( (((FloatNode*)left)->value) + (((FloatNode*)right)->value) )); + }else{ + matrix *tempMatrix = new matrix; + *tempMatrix = ( (((FloatNode*)left)->value) + *(((MatrixNode*)right)->value) ); + tempNode = new MatrixNode(tempMatrix); + } + }else{ + if(right->type() == 1) + { + matrix *tempMatrix = new matrix; + *tempMatrix = ( *(((MatrixNode*)left)->value) + (((FloatNode*)right)->value) ); + tempNode = new MatrixNode(tempMatrix); + }else{ + matrix *tempMatrix = new matrix; + *tempMatrix = ( *(((MatrixNode*)left)->value) + *(((MatrixNode*)right)->value) ); + tempNode = new MatrixNode(tempMatrix); + } + } + /*temp values*/ + break; + case '-': + if(left->type() == 1) + { + if(right->type() == 1) + { + tempNode = new FloatNode(( (((FloatNode*)left)->value) - (((FloatNode*)right)->value) )); + }else{ + matrix *tempMatrix = new matrix; + *tempMatrix = ( (((FloatNode*)left)->value) - *(((MatrixNode*)right)->value) ); + tempNode = new MatrixNode(tempMatrix); + } + }else{ + if(right->type() == 1) + { + matrix *tempMatrix = new matrix; + *tempMatrix = ( *(((MatrixNode*)left)->value) - (((FloatNode*)right)->value) ); + tempNode = new MatrixNode(tempMatrix); + }else{ + matrix *tempMatrix = new matrix; + *tempMatrix = ( *(((MatrixNode*)left)->value) - *(((MatrixNode*)right)->value) ); + tempNode = new MatrixNode(tempMatrix); + } + } + /*temp values*/ + break; + case '*': + if(left->type() == 1) + { + if(right->type() == 1) + { + tempNode = new FloatNode(( (((FloatNode*)left)->value) * (((FloatNode*)right)->value) )); + }else{ + matrix *tempMatrix = new matrix; + *tempMatrix = ( (((FloatNode*)left)->value) * *(((MatrixNode*)right)->value) ); + tempNode = new MatrixNode(tempMatrix); + } + }else{ + if(right->type() == 1) + { + matrix *tempMatrix = new matrix; + *tempMatrix = ( *(((MatrixNode*)left)->value) * (((FloatNode*)right)->value) ); + tempNode = new MatrixNode(tempMatrix); + }else{ + matrix *tempMatrix = new matrix; + *tempMatrix = ( *(((MatrixNode*)left)->value) * *(((MatrixNode*)right)->value) ); + tempNode = new MatrixNode(tempMatrix); + } + } + /*temp values*/ + break; + case '/': + if(left->type() == 1) + { + if(right->type() == 1) + { + tempNode = new FloatNode(( (((FloatNode*)left)->value) / (((FloatNode*)right)->value) )); + }else{ + // matrix *tempMatrix = new matrix; + // *tempMatrix = ( (((FloatNode*)left)->value) / *(((MatrixNode*)right)->value) ); + // tempNode = new MatrixNode(tempMatrix); + throw "---- EXPECT 2ND OPERAND OF / TO BE FLOAT BUT MATRIX WAS FOUND ----"; + } + }else{ + if(right->type() == 1) + { + // matrix *tempMatrix = new matrix; + // *tempMatrix = ( *(((MatrixNode*)left)->value) / (((FloatNode*)right)->value) ); + // tempNode = new MatrixNode(tempMatrix); + throw "---- EXPECT 2ND OPERAND OF / TO BE MATRIX BUT FLOAT WAS FOUND ----"; + }else{ + matrix *tempMatrix = new matrix; + *tempMatrix = ( *(((MatrixNode*)left)->value) / *(((MatrixNode*)right)->value) ); + tempNode = new MatrixNode(tempMatrix); + } + } + /*temp values*/ + break; + case '"': + if(left->type() == 1) + { + if(right->type() == 1) + { + //tempNode = new FloatNode(( (((FloatNode*)left)->value) / (((FloatNode*)right)->value) )); + throw "---- EXPECT 2ND OPERAND OF ./ TO BE MATRIX BUT FLOAT WAS FOUND ---"; + }else{ + matrix *tempMatrix = new matrix; + *tempMatrix = ( (((FloatNode*)left)->value) / *(((MatrixNode*)right)->value) ); + tempNode = new MatrixNode(tempMatrix); + } + }else{ + if(right->type() == 1) + { + matrix *tempMatrix = new matrix; + *tempMatrix = ( *(((MatrixNode*)left)->value) / (((FloatNode*)right)->value) ); + tempNode = new MatrixNode(tempMatrix); + }else{ + // matrix *tempMatrix = new matrix; + // *tempMatrix = ( *(((MatrixNode*)left)->value) / *(((MatrixNode*)right)->value) ); + // tempNode = new MatrixNode(tempMatrix); + throw "---- EXPECT 2ND OPERAND OF ./ TO BE FLOAT BUT MATRIX WAS FOUND ----"; + } + } + /*temp values*/ + break; + case '^': + if(left->type() == 1) + { + if(right->type() == 1) + { + tempNode = new FloatNode( pow( (((FloatNode*)left)->value), (((FloatNode*)right)->value) ) ); + }else{ + //throw error + } + }else{ + if(right->type() == 1) + { + matrix *tempMatrix = new matrix; + *tempMatrix = ( *(((MatrixNode*)left)->value) ^ (((FloatNode*)right)->value) ); + tempNode = new MatrixNode(tempMatrix); + }else{ + //throw error + } + } + /*temp values*/ + break; + } + temp_stack->add(tempNode); + delete right; + delete left; + }else{ + int temp = i; + while(true) + { + i++; + if(reverse_polish[i] != '\0') + { + if(reverse_polish[i] == ';' || reverse_polish[i] == '*' || reverse_polish[i] == '/' || reverse_polish[i] == '^' || reverse_polish[i] == '+' || reverse_polish[i] == '-' || reverse_polish[i] == '(' || reverse_polish[i] == ')' || reverse_polish[i] == '[' || reverse_polish[i] == '`' || reverse_polish[i] == '"' + || reverse_polish[i] == '%' || reverse_polish[i] == '=' || reverse_polish[i] == '<' || reverse_polish[i] == '!' || reverse_polish[i] == '_' || reverse_polish[i] == '@' || reverse_polish[i] == '#' || reverse_polish[i] == '?' || reverse_polish[i] == ':' || reverse_polish[i] == '>') + { + i--; + break; + } + }else{ + i--; + break; + } + } + int j = 0; + bool is_Matrix = false; + char * word = new char[i-temp+1]; + for (int k = temp; k <= i; ++k) + { + if (reverse_polish[k] != ' ' && reverse_polish[k] != '\0') + { + if((48 <= ((int)reverse_polish[k]) && ((int)reverse_polish[k]) <= 57)||reverse_polish[k] == '.'); + else{ + is_Matrix = true; + } + word[j] = reverse_polish[k]; + j++; + } + } + word[j] = '\0'; + //cout<<"size:"<unity_matrix(); + tempMatrix->set_name(word); + //matrixTree.insert(tempMatrix); + tempNode = new TempMatrixNode(tempMatrix, true); + } + }else{ + tempNode = new FloatNode(atof(word)); + } + temp_stack->add(tempNode); + } + } + if(temp_stack->top != NULL) + { + return temp_stack->pop(); + }else{ + return NULL; + } +} + +Node* do_operation(string str){ + signal(SIGSEGV, sighandler); + int length = str.size(); + char *infix = new char[length+1]; + int i = 0; + for (; i < length; ++i) + { + infix[i]=str[i]; + } + infix[i] = '\0'; + char *temp = infix_to_reverse_polish(infix); + //cout< "; + getline(cin, str); + if(str == "exit")break; + if(str == "all") + { + cout<