From 853413bab8ed3e5621f508acf145c76cfa81a225 Mon Sep 17 00:00:00 2001 From: Kidsadakorn Nuallaoong <121489701+KidsadakornNuallaoong@users.noreply.github.com> Date: Mon, 22 Apr 2024 23:37:22 +0700 Subject: [PATCH] Update LAB add folder lab week4 and week5 --- OOP/LabW4/ArrayList/arrayList.cpp | 442 ++++++++++++++++++++++++ OOP/LabW4/ArrayList/arrayList.h | 43 +++ OOP/LabW4/ArrayList/main.cpp | 99 ++++++ OOP/LabW4/ArrayList/makefile | 19 + OOP/LabW4/ArrayStack/ArrayStack.cpp | 76 ++++ OOP/LabW4/ArrayStack/ArrayStack.h | 30 ++ OOP/LabW4/ArrayStack/main.cpp | 163 +++++++++ OOP/LabW4/ArrayStack/makefile | 22 ++ OOP/LabW4/ChainNode/ChainNode.cpp | 8 + OOP/LabW4/ChainNode/ChainNode.h | 15 + OOP/LabW4/ChainNode/main.cpp | 13 + OOP/LabW4/ChainNode/makefile | 18 + OOP/LabW4/LinkStack/LinkStack.cpp | 68 ++++ OOP/LabW4/LinkStack/LinkStack.h | 25 ++ OOP/LabW4/LinkStack/main.cpp | 163 +++++++++ OOP/LabW4/LinkStack/makefile | 28 ++ OOP/LabW4/LinkedList/LinkedList.cpp | 495 +++++++++++++++++++++++++++ OOP/LabW4/LinkedList/LinkedList.h | 45 +++ OOP/LabW4/LinkedList/main.cpp | 138 ++++++++ OOP/LabW4/LinkedList/makefile | 20 ++ OOP/LabW5/ChainNode/ChainNode.cpp | 8 + OOP/LabW5/ChainNode/ChainNode.h | 15 + OOP/LabW5/ChainNode/main.cpp | 18 + OOP/LabW5/ChainNode/makefile | 18 + OOP/LabW5/LinkStack/LinkStack.cpp | 68 ++++ OOP/LabW5/LinkStack/LinkStack.h | 25 ++ OOP/LabW5/LinkStack/main.cpp | 163 +++++++++ OOP/LabW5/LinkStack/makefile | 28 ++ OOP/LabW5/LinkedList/LinkedList.cpp | 495 +++++++++++++++++++++++++++ OOP/LabW5/LinkedList/LinkedList.h | 45 +++ OOP/LabW5/LinkedList/main.cpp | 138 ++++++++ OOP/LabW5/LinkedList/makefile | 20 ++ OOP/LabW5/StackApplications/main.cpp | 173 ++++++++++ OOP/LabW5/StackApplications/makefile | 25 ++ 34 files changed, 3169 insertions(+) create mode 100644 OOP/LabW4/ArrayList/arrayList.cpp create mode 100644 OOP/LabW4/ArrayList/arrayList.h create mode 100644 OOP/LabW4/ArrayList/main.cpp create mode 100644 OOP/LabW4/ArrayList/makefile create mode 100644 OOP/LabW4/ArrayStack/ArrayStack.cpp create mode 100644 OOP/LabW4/ArrayStack/ArrayStack.h create mode 100644 OOP/LabW4/ArrayStack/main.cpp create mode 100644 OOP/LabW4/ArrayStack/makefile create mode 100644 OOP/LabW4/ChainNode/ChainNode.cpp create mode 100644 OOP/LabW4/ChainNode/ChainNode.h create mode 100644 OOP/LabW4/ChainNode/main.cpp create mode 100644 OOP/LabW4/ChainNode/makefile create mode 100644 OOP/LabW4/LinkStack/LinkStack.cpp create mode 100644 OOP/LabW4/LinkStack/LinkStack.h create mode 100644 OOP/LabW4/LinkStack/main.cpp create mode 100644 OOP/LabW4/LinkStack/makefile create mode 100644 OOP/LabW4/LinkedList/LinkedList.cpp create mode 100644 OOP/LabW4/LinkedList/LinkedList.h create mode 100644 OOP/LabW4/LinkedList/main.cpp create mode 100644 OOP/LabW4/LinkedList/makefile create mode 100644 OOP/LabW5/ChainNode/ChainNode.cpp create mode 100644 OOP/LabW5/ChainNode/ChainNode.h create mode 100644 OOP/LabW5/ChainNode/main.cpp create mode 100644 OOP/LabW5/ChainNode/makefile create mode 100644 OOP/LabW5/LinkStack/LinkStack.cpp create mode 100644 OOP/LabW5/LinkStack/LinkStack.h create mode 100644 OOP/LabW5/LinkStack/main.cpp create mode 100644 OOP/LabW5/LinkStack/makefile create mode 100644 OOP/LabW5/LinkedList/LinkedList.cpp create mode 100644 OOP/LabW5/LinkedList/LinkedList.h create mode 100644 OOP/LabW5/LinkedList/main.cpp create mode 100644 OOP/LabW5/LinkedList/makefile create mode 100644 OOP/LabW5/StackApplications/main.cpp create mode 100644 OOP/LabW5/StackApplications/makefile diff --git a/OOP/LabW4/ArrayList/arrayList.cpp b/OOP/LabW4/ArrayList/arrayList.cpp new file mode 100644 index 0000000..4e53ba6 --- /dev/null +++ b/OOP/LabW4/ArrayList/arrayList.cpp @@ -0,0 +1,442 @@ +#include "arrayList.h" +#include + +using namespace std; + +ArrayList::ArrayList(int maxSize) +{ + // * create array with maxSize element + L = new int[maxSize]; + // * set maximum size to maxSize + this->maxSize = maxSize; + // * set current size to zero + curSize = 0; +} + +ArrayList::~ArrayList() +{ + // * free memory + delete []L; +} + +int ArrayList::size() +{ + // * return curren element size + return curSize; +} + +int ArrayList::indexOf(int e) +{ + int i; + if (curSize <= 0) + { + cout << "[massage error] Empty Stack!!!!!" << endl; + return -1; + } + // * loop before checking element of index + for (i = 0; i <= curSize - 1 && L[i] != e; ++i) + { + this->L[i] = L[i]; + } // * check + if (L[i] == e) + { + cout << "[Massage] : Index of Array : {" << e << "} is [" << i << "]" << endl; + } + else + { + cout << "[Massage Warning] : Element is not found!!!" << endl; + return -1; + } + + return i; +} + +int ArrayList::get(int i) +{ + // * check empty + if (curSize = 0) + { + cout << "[Massage Error] : Stack is empty!" << endl; + return -1; + } + + // * check lower + if (i < 0) + { + cout << "[Massage Warning] : i is lower bound" << endl; + cout << "[Massage] : i is changed to 0" << endl; + i = 0; + } + // * check upper + else if (i > curSize - 1) + { + cout << "[Massage Warning] : i is over bound" << endl; + cout << "[Massage] : i is changed to " << curSize - 1 << endl; + i = curSize - 1; + } + // * get L[i] + return L[i]; +} + +void ArrayList::set(int i, int e) +{ + // * check lower + if (i < 0) + { + cout << "[Massage Warning] : Index is lower bound can't to set this element" << endl; + } + else if (i < curSize) + { + // * set element e to i + cout << "[Massage Warning] : Element " << L[i]; + L[i] = e; + cout << " is changed " << L[i] << endl; + } + else if (i >= curSize) + { + // * check upper + cout << "[Massage Warning] : Index is over bound can't to set this element" << endl; + } +} + +int ArrayList::remove(int i) +{ + // * check empty + if (curSize == 0) + { + cout << "[Massage Error] : Stack is empty!" << endl; + return -1; + } + + // * check lower + if (i < 0) + { + cout << "[Massage Warning] : Can't remove index this lower bound" << endl; + return -1; + } + else if (i > curSize) + { + // * check upper + cout << "[Massage Warning] : Can't remove index this over bound" << endl; + return -1; + } + + int tmp = L[i]; + // * return value to remove + // * loop to index i + for (int j = i; j <= curSize - 1; ++j) + { + L[j] = L[j + 1]; // * remove value index i + } + curSize--; + + return tmp; +} + +void ArrayList::add(int i, int e) +{ + // * check empty Stack + if (curSize == maxSize) + { + cout << "[Massage] : Stack is full !!!!!" << endl; + return; + } + + // * check lower bound + if (i < 0) + { + cout << "[Massage Warning] : Lower bound" << endl; + cout << "[Massage] : Index is changed to 0" << endl; + i = 0; + } + // * check upper bound + else if (i > curSize) + { + cout << "[Massage Warning] : upper bound" << endl; + cout << "[Massage] : Index is changed to " << curSize << endl; + i = curSize; + } + // * index is ok + // * shift right from curSize -1 down to i + + for (int j = curSize - 1; j >= i; j--) + { + // * move element at i to i + 1 + L[j + 1] = L[j]; + } + + // * put e at i index + L[i] = e; + // * increment current size + curSize++; +} + +void ArrayList::clear() +{ + // * clear element in array + /* + while(curSize != 0){ + curSize--; + } + */ + curSize = 0; + cout << "[Massage] : Array is clear" << endl; +} + +int ArrayList::max() +{ + int result = 0; + // * check empty Stack + if (curSize = 0) + { + cout << "[Massage Error] : Array is Empty!!!!" << endl; + cout << "[Massage] Can't find max value" << endl; + return -1; + } + else if (curSize < 2) + { + result = L[curSize - 1]; // * check first index and set index 1 + + cout << "[Massage] : MaxValue is " << result << endl; + return -1; + } + // * find max value in array + for (int i = 0; i < curSize; ++i) + { + if (result < L[i]) + { + result = L[i]; + } + } + + cout << "[Massage] : MaxValue is " << result << endl; + + return result; +} + +int ArrayList::min() +{ + int result; + // * check empty Stack + if (curSize = 0) + { + cout << "[Massage Error] : Array is Empty!!!!" << endl; + cout << "[Massage] Can't find min value" << endl; + return -1; + } + else if (curSize < 2) + { // * check first index and set index 1 + result = L[curSize - 1]; + + cout << "[Massage] : MinValue is " << result << endl; + return -1; + } + // * find min value in array + int i = 0; + result = L[i]; + do + { + if (result > L[i]) + result = L[i]; + ++i; + + } while (i < curSize); + + cout << "[Massage] : MinValue is " << result << endl; + + return result; +} + +int ArrayList::oddCount() +{ + // * check empty Stack + if (curSize == 0) + { + cout << "[Massage] : Stack is empty!!!" << endl; + return -1; + } + + int i = 0; + int count = 0; + + while (i <= curSize) + { + // * checking value divide 2 is false + // * count += p->element % 2; + if (L[i] % 2) + count++; + + // * if(p->element % 2 == true){ + // * cout << p->element << ","; + // * Odd number counting + // * count = p->element; + + // * count += p->element % 2; + // * cout << count << ","; + + // * Odd number counting + i++; + } + return count; +} + +int ArrayList::evenCount() +{ + // * check empty Stack + if (curSize == 0) + { + cout << "[Massage] : Stack is empty!!!" << endl; + return -1; + } + + int cur = curSize; + + return cur -= oddCount(); +} + +float ArrayList::sum() +{ + // * check empty Stack + if (curSize == 0) + { + cout << "[Massage] : Stack is empty!!!" << endl; + return -1; + } + + int i = 0, sum = 0; + + while (i < curSize - 1) + { + // * + value for find sum + sum += L[i]; + ++i; + } + + return sum; +} + +float ArrayList::mean() +{ + // * check empty Stack + if (curSize == 0) + { + cout << "[Massage] : Stack is empty!!!" << endl; + return -1; + } + + return sum() / curSize; +} + +void ArrayList::sort() +{ + // * check empty Stack + if (curSize == 0) + { + cout << "[Massage] : Stack is empty!!!" << endl; + return; + } + + int i = 0; + + // * loop from curSize + while (i < curSize - 1) + { + int j = 0; + // * loop for swap element + while (j < curSize - 1) + { + if (L[j] > L[j + 1]) + { + // * swap element + swap(L[j], L[j + 1]); + } + ++j; + } + ++i; + } +} + +void ArrayList::swap(int &a, int &b) +{ + // * check empty Stack + if (curSize == 0) + { + cout << "[Massage] : Stack is empty!!!" << endl; + return; + } + + // * just swap element function + int t = a; + a = b; + b = t; +} + +void ArrayList::appendRandom(int round) +{ + int i = 0, rand = 0; + // * loop with round + while (i < round && i <= maxSize) + { + // * random number < 101 + rand = random() % 101; + add(curSize, rand); + ++i; + } +} + +/*int ArrayList::max() +{ + // * find max value in array + int result = 0; + for(int i = 0; i < curSize; ++i){ + if(result < L[i]){ + result = L[i]; + } + } + if(result == 0){ + cout << "[Massage] : Value is [0] no Max/min" << endl; + } + cout << "[Massage] : MaxValue is " << result << endl; + return result; +} + +int ArrayList::min() +{ + // * find min value in array + int i = 0; + int result = L[i]; + do + { + if(result > L[i]){ + result = L[i]; + } + i++; + }while(i < curSize); + if(result == 0){ + cout << "[Massage] : Value is 0 no Max/min" << endl; + } + cout << "[Massage] : MinValue is " << result << endl; + return result; +} +*/ +void ArrayList::display() +{ + // * show L to L:(?,?), size: corSize/MaxSize + // * L: {1,2,3} + cout << "[Massage] -> L: {"; + if (curSize <= 0) + { + cout << "}, "; + } + else + for (int i = 0; i < curSize; ++i) + { + cout << L[i]; + if (i < curSize - 1) + cout << ", "; + else + cout << "}, "; + } + + cout << "Size: " << curSize << "/" << maxSize << endl; +} \ No newline at end of file diff --git a/OOP/LabW4/ArrayList/arrayList.h b/OOP/LabW4/ArrayList/arrayList.h new file mode 100644 index 0000000..a204eaf --- /dev/null +++ b/OOP/LabW4/ArrayList/arrayList.h @@ -0,0 +1,43 @@ +#if !defined(_ARRAYLIST_H) +#define _ARRAYLIST_H + +// * #define DEFAULT_MAX_SIZE 5 + +class ArrayList +{ + private: // * data + int *L; // ! List element + int maxSize; // * maximum element + int curSize; // * current number element + + public: // * method + ArrayList(int maxSize); // ! contructor *important (1)* + ~ArrayList(); // ! destructor + // * basic method + int size(); + int indexOf(int e); + int get(int i); + void set(int i, int e); + int remove(int i); + void add(int i,int e); + + void clear(); + int max(); + int min(); + + int oddCount(); + int evenCount(); + float sum(); + float mean(); + void sort(); + + void swap(int &a, int &b); + + void appendRandom(int round); + + // * addition method + void display(); + +}; + +#endif // _ARRAYLIST_H \ No newline at end of file diff --git a/OOP/LabW4/ArrayList/main.cpp b/OOP/LabW4/ArrayList/main.cpp new file mode 100644 index 0000000..a133ed2 --- /dev/null +++ b/OOP/LabW4/ArrayList/main.cpp @@ -0,0 +1,99 @@ +/*************************************************************************************** + * ID: 65037743 + * NAME: Mr.Kidsadakorn Nuallaoong + * DESCRIPTION: ArrayList Testing + ***************************************************************************************/ +#include +#include "arrayList.h" + +using namespace std; + +int main() +{ + ArrayList list(5); + + int choice,index,element; + +do +{ + cout << "----------------- MENU -----------------" << endl << endl; + + cout << "================ DISPLAY ===============" << endl << endl; + + list.display(); + + cout << endl << "========================================" << endl << endl; + + cout << "1: Add element to List, add()" << endl; + cout << "2: Remove element from list, remove()" << endl; + cout << "3: Set element to list, set()" << endl; + cout << "4: Get element from list, get()" << endl; + cout << "5: Index of element, indexOf()" << endl; + cout << "6: List size, size()" << endl; + cout << "7: Clear list, clear()" << endl; + cout << "8: Find maximum number from list, max()" << endl; + cout << "9: Find minimum number from list, min()" << endl; + cout << "0: Exit!!!!" << endl; + + cout << endl; + + cout << "Enter your choice : " ; cin >> choice; + + switch (choice) + { + case 1: // * add + cout << "Enter index : "; cin >> index; + cout << "Enter element : "; cin >> element; + list.add(index, element); + break; + + case 2: // * remove + cout << "Enter index : "; cin >> index; + list.remove(index); + break; + + case 3: // * set + cout << "Enter index : "; cin >> index; + cout << "Enter element : "; cin >> element; + list.set(index, element); + break; + + case 4: // * get + cout << "Enter index : "; cin >> index; + cout << "[Massage] list[" << index << "] is " << list.get(index) << endl; + break; + + case 5: // * index of + cout << "Enter element : "; cin >> element; + list.indexOf(element); + break; + + case 6: // * list size + cout << "[Massage] Current List Size is " << list.size() << endl; + break; + + case 7: // * clear + list.clear(); + break; + + case 8: // * max + list.max(); + break; + + case 9: // * min + list.min(); + break; + + case 0: + cout << "Bye...." << endl << endl; + cout << "-------------- Call method --------------" << endl; + break; + + default: // * Exit + cout << "[Massage] Wrong choice,try again..." << endl; + } + +}while(choice != 0); + + return 0; +} \ No newline at end of file diff --git a/OOP/LabW4/ArrayList/makefile b/OOP/LabW4/ArrayList/makefile new file mode 100644 index 0000000..2a00c56 --- /dev/null +++ b/OOP/LabW4/ArrayList/makefile @@ -0,0 +1,19 @@ +run: all + @clear + @echo Program is running... + + @./program + @rm -f *.o program + +all: main.o arrayList.o + @g++ main.o arraylist.o -o program -g + +main.o: main.cpp + @g++ -c main.cpp -o main.o -g + +arraylist.o: arrayList.cpp + @g++ -c arrayList.cpp -o arrayList.o -g + +clean: + @rm -f *.o program + @echo Cleaning success...... \ No newline at end of file diff --git a/OOP/LabW4/ArrayStack/ArrayStack.cpp b/OOP/LabW4/ArrayStack/ArrayStack.cpp new file mode 100644 index 0000000..b2b5ccc --- /dev/null +++ b/OOP/LabW4/ArrayStack/ArrayStack.cpp @@ -0,0 +1,76 @@ +#include "ArrayStack.h" + +ArrayStack::ArrayStack(int maxSize) : ArrayList(maxSize) +{ + // * set maxSize = maxSize + this->maxSize = maxSize; +} + +void ArrayStack::push(int e) +{ + // * add to element + add(size(), e); +} + +int ArrayStack::pop() +{ + // * remove element index Size - 1 + return remove(size() - 1); +} + +int ArrayStack::top() +{ + // * get element index Size - 1 + return get(size() - 1); +} + +bool ArrayStack::isEmpty() +{ + return size() == 0; +} + +bool ArrayStack::isFull() +{ + return size() == DEFAULT_STACK_SIZE; +} + +void ArrayStack::swapTopTwo() +{ + // * get and remove element + // * We can use pop(); ---> 2 line + int a = remove(size() - 1); + int b = remove(size() - 1); + + // * add element to first index + // * We can use push(element); ---> 2 line + add(size(), a); + add(size(), b); +} + +void ArrayStack::moveMaxToTop() +{ + // * find max value + int a = max(); + // * get and remove max stack + remove(indexOf(max())); + // * add element a to last index + push(a); +} + +void ArrayStack::sortStack() +{ + // * get sorting + sort(); +} + +void ArrayStack::flip() +{ + int j = size() - 1; + + for (j; j > 0; --j) + { + // * get and remove element index[0] + // * add element to j-1/r + add(j, remove(0)); + } +} \ No newline at end of file diff --git a/OOP/LabW4/ArrayStack/ArrayStack.h b/OOP/LabW4/ArrayStack/ArrayStack.h new file mode 100644 index 0000000..5ce6595 --- /dev/null +++ b/OOP/LabW4/ArrayStack/ArrayStack.h @@ -0,0 +1,30 @@ +#if !defined(_ARRAYSTACK_H) +#define _ARRAYSTACK_H + +#include "../ArrayList/arrayList.h" + +#define DEFAULT_STACK_SIZE 5 + +class ArrayStack : public ArrayList +{ + private: + int maxSize; + + public: + ArrayStack(int maxSize = DEFAULT_STACK_SIZE); + // ~ArrayStack(); + + void push(int e); + int pop(); + int top(); + + bool isEmpty(); + bool isFull(); + + void swapTopTwo(); + void moveMaxToTop(); + void sortStack(); + void flip(); +}; + +#endif // _ARRAYSTACK_H diff --git a/OOP/LabW4/ArrayStack/main.cpp b/OOP/LabW4/ArrayStack/main.cpp new file mode 100644 index 0000000..f5542c2 --- /dev/null +++ b/OOP/LabW4/ArrayStack/main.cpp @@ -0,0 +1,163 @@ +/*************************************************************************************** + * ID: 65037743 + * NAME: Mr.Kidsadakorn Nuallaoong + * DESCRIPTION: ArrayStack Testing + ***************************************************************************************/ +#include +#include "ArrayStack.h" + +using namespace std; + +int main() +{ + ArrayStack Stack; + + int choice, index, element, round; + + do + { + cout << "----------------- MENU -----------------" << endl + << endl; + + cout << "================ DISPLAY ===============" << endl + << endl; + + Stack.display(); + + cout << endl + << "========================================" << endl + << endl; + + cout << "1: Add element to last Stack, push()" << endl; + cout << "2: Get last element, pop()" << endl; + cout << "3: Get top element, top()" << endl; + cout << "4: Clear element in Stack, clear()" << endl; + + cout << "5: Check size of Stack is Empty?, isEmpty()" << endl; + cout << "6: Check size of Stack is Full?, isFull()" << endl; + cout << "7: Find maximum number from Stack, max()" << endl; + cout << "8: Find minimum number from Stack, min()" << endl; + + cout << "9. Random and add to Stack, appendRandom()" << endl; + cout << "10. Swap two element on pop, swapToTwo()" << endl; + cout << "11. move element to top Stack, moveToTop()" << endl; + + cout << "12: Find odd number from list, oddcount()" << endl; + cout << "13: Find even number from list, evencount()" << endl; + cout << "14: Find sum value, sum()" << endl; + cout << "15: Calculate average value from list, mean()" << endl; + + cout << "16. Sorting and stacking low to high, sortStack()" << endl; + cout << "17. flip list, flip()" << endl; + + cout << "0: Exit!!!!" << endl; + + cout << endl; + + cout << "Enter your choice : "; + cin >> choice; + + switch (choice) + { + case 1: // * push + cout << "[Massage] Enter element : "; + cin >> element; + Stack.push(element); + break; + + case 2: // * pop + element = Stack.pop(); + cout << "[Massage] get element : " << element << endl; + break; + + case 3: // * top + element = Stack.top(); + cout << "[Massage] Element on top : " << element << endl; + break; + + case 4: // * clear + Stack.clear(); + break; + + case 5: + if(Stack.isEmpty() != false){ + cout << "[Massage] is empty!!!" << endl; + }else{ + cout << "[Massage] Can't add Stack!!!" << endl; + } + break; + + case 6: + if(Stack.isFull() != false){ + cout << "[Massage] is Full!!!" << endl; + }else{ + cout << "[Massage] Can add Stack!!!" << endl; + } + break; + + case 7: // * max + element = Stack.max(); + cout << "[Massage] : MaxValue is : " << element << endl; + break; + + case 8: // * min + element = Stack.min(); + cout << "[Massage] : MinValue is : " << element << endl; + break; + + case 9: // * appendrandom + cout << "Enter number of numbers : "; + cin >> round; + Stack.appendRandom(round); + break; + + case 10: // * swapToTwo + Stack.swapTopTwo(); + break; + + case 11: // * moveToTop + Stack.moveMaxToTop(); + break; + + case 12: // * oddcount + element = Stack.oddCount(); + cout << "[Massage] Odd counting " << element << endl; + break; + + case 13: // * evencount + element = Stack.evenCount(); + cout << "[Massage] Even counting " << element << endl; + break; + + case 14: + element = Stack.sum(); + cout << "[Massage] Summation is " << element << endl; + break; + + case 15: + element = Stack.mean(); + cout << "[Massage] Mean is " << element << endl; + break; + + case 16: + Stack.sortStack(); + break; + + case 17: // * moveToTop + Stack.flip(); + break; + + case 0: + cout << "Bye...." << endl + << endl; + cout << "-------------- Call method --------------" << endl; + break; + + default: // * Exit + cout << "[Massage] Wrong choice,try again..." << endl; + } + + } while (choice != 0); + + return 0; +} \ No newline at end of file diff --git a/OOP/LabW4/ArrayStack/makefile b/OOP/LabW4/ArrayStack/makefile new file mode 100644 index 0000000..79d7534 --- /dev/null +++ b/OOP/LabW4/ArrayStack/makefile @@ -0,0 +1,22 @@ +run: all + @clear + @echo Program is running... + + @./LabW4 + @rm -f *.o ../ArrayList/ArrayList.o LabW4 + +all: main.o ArrayStack.o ../ArrayList/arrayList.o + @g++ main.o ArrayStack.o ../ArrayList/arrayList.o -o LabW4 -g + +main.o: main.cpp + @g++ -c main.cpp -o main.o -g + +ArrayStack.o: ArrayStack.cpp + @g++ -c ArrayStack.cpp -o ArrayStack.o -g + +../ArrayList/arrayList.o: ../ArrayList/arrayList.cpp + @g++ -c ../ArrayList/arrayList.cpp -o ../ArrayList/arrayList.o -g + +clean: + @rm -f *.o ../ArrayList/ArrayList.o LabW4 + @echo Cleaning success...... \ No newline at end of file diff --git a/OOP/LabW4/ChainNode/ChainNode.cpp b/OOP/LabW4/ChainNode/ChainNode.cpp new file mode 100644 index 0000000..09c6e0d --- /dev/null +++ b/OOP/LabW4/ChainNode/ChainNode.cpp @@ -0,0 +1,8 @@ +#include +#include "ChainNode.h" + +ChainNode::ChainNode(int e, ChainNode *p) +{ + element = e; + next = p; +} \ No newline at end of file diff --git a/OOP/LabW4/ChainNode/ChainNode.h b/OOP/LabW4/ChainNode/ChainNode.h new file mode 100644 index 0000000..e85d0fe --- /dev/null +++ b/OOP/LabW4/ChainNode/ChainNode.h @@ -0,0 +1,15 @@ +#if !defined(_CHAINNODE_H) +#define _CHAINNODE_H + +#include + +class ChainNode +{ + public: + int element; + ChainNode *next; + public: + ChainNode(int e = 0, ChainNode *p = NULL); +}; + +#endif // _CHAINNODE_H diff --git a/OOP/LabW4/ChainNode/main.cpp b/OOP/LabW4/ChainNode/main.cpp new file mode 100644 index 0000000..e48ba67 --- /dev/null +++ b/OOP/LabW4/ChainNode/main.cpp @@ -0,0 +1,13 @@ +#include +#include "ChainNode.h" + +using namespace std; + +int main() +{ + ChainNode a; // * ==> e = 0, p = NULL + ChainNode b(10, &a); // * ==> e = 10, p = &a + ChainNode c(20, &b); // * ==> e = 20, p = &b + + return 0; +} diff --git a/OOP/LabW4/ChainNode/makefile b/OOP/LabW4/ChainNode/makefile new file mode 100644 index 0000000..e67dfde --- /dev/null +++ b/OOP/LabW4/ChainNode/makefile @@ -0,0 +1,18 @@ +run: + @g++ -c main.cpp -o main.o -g + @g++ -c ChainNode.cpp -o ChainNode.o -g + @g++ main.o ChainNode.o -o LabW3 -g + + @./LabW3 + + @rm -f *.o LabW3 + +all: main.o ChainNode.o + g++ main.o ChainNode.o -o week4 -g + +main.o: main.cpp ChainNode.cpp + g++ -c main.cpp -o main.o -g + g++ -c ChainNode.cpp -o ChainNode.o -g + +clean: + rm -f *.o LabW3 diff --git a/OOP/LabW4/LinkStack/LinkStack.cpp b/OOP/LabW4/LinkStack/LinkStack.cpp new file mode 100644 index 0000000..69e76d2 --- /dev/null +++ b/OOP/LabW4/LinkStack/LinkStack.cpp @@ -0,0 +1,68 @@ +#include "LinkStack.h" + +void LinkStack::push(int e) +{ + // * add to element + add(size(), e); +} + +int LinkStack::pop() +{ + // * remove element index Size - 1 + return remove(size() - 1); +} + +int LinkStack::top() +{ + // * get element index Size - 1 + return get(size() - 1); +} + +bool LinkStack::isEmpty() +{ + return LinkedList::isEmpty(); +} + +bool LinkStack::isFull() +{ + return LinkedList::isFull(); +} + +void LinkStack::swapTopTwo() +{ + // * get and remove element + int a = remove(size() - 1); + int b = remove(size() - 1); + + // * add element to first index + add(size(), a); + add(size(), b); +} + +void LinkStack::moveMaxToTop() +{ + // * find max value + int a = max(); + // * get and remove max stack + remove(indexOf(max())); + // * add element a to last index + push(a); +} + +void LinkStack::sortStack() +{ + // * get sorting + sort(); +} + +void LinkStack::flip() +{ + int j = size() - 1; + + for (j; j > 0; --j) + { + // * get and remove element index[0] + // * add element to j-1/r + add(j, remove(0)); + } +} \ No newline at end of file diff --git a/OOP/LabW4/LinkStack/LinkStack.h b/OOP/LabW4/LinkStack/LinkStack.h new file mode 100644 index 0000000..53c7d7f --- /dev/null +++ b/OOP/LabW4/LinkStack/LinkStack.h @@ -0,0 +1,25 @@ +#if !defined(_LINKSTACK_H) +#define _LINKSTACK_H + +#include "../LinkedList/LinkedList.h" + +class LinkStack : public LinkedList +{ + private: + int maxSize; + + public: + void push(int e); + int pop(); + int top(); + + bool isEmpty(); + bool isFull(); + + void swapTopTwo(); + void moveMaxToTop(); + void sortStack(); + void flip(); +}; + +#endif // _LINKSTACK_H \ No newline at end of file diff --git a/OOP/LabW4/LinkStack/main.cpp b/OOP/LabW4/LinkStack/main.cpp new file mode 100644 index 0000000..8cb507d --- /dev/null +++ b/OOP/LabW4/LinkStack/main.cpp @@ -0,0 +1,163 @@ +/*************************************************************************************** + * ID: 65037743 + * NAME: Mr.Kidsadakorn Nuallaoong + * DESCRIPTION: LinkStack Testing + ***************************************************************************************/ +#include +#include "LinkStack.h" + +using namespace std; + +int main() +{ + LinkStack Stack; + + int choice, index, element, round; + + do + { + cout << "----------------- MENU -----------------" << endl + << endl; + + cout << "================ DISPLAY ===============" << endl + << endl; + + Stack.display(); + + cout << endl + << "========================================" << endl + << endl; + + cout << "1: Add element to last Stack, push()" << endl; + cout << "2: Get last element, pop()" << endl; + cout << "3: Get top element, top()" << endl; + cout << "4: Clear element in Stack, clear()" << endl; + + cout << "5: Check size of Stack is Empty?, isEmpty()" << endl; + cout << "6: Check size of Stack is Full?, isFull()" << endl; + cout << "7: Find maximum number from Stack, max()" << endl; + cout << "8: Find minimum number from Stack, min()" << endl; + + cout << "9. Random and add to Stack, appendRandom()" << endl; + cout << "10. Swap two element on pop, swapToTwo()" << endl; + cout << "11. move element to top Stack, moveToTop()" << endl; + + cout << "12: Find odd number from list, oddcount()" << endl; + cout << "13: Find even number from list, evencount()" << endl; + cout << "14: Find sum value, sum()" << endl; + cout << "15: Calculate average value from list, mean()" << endl; + + cout << "16. Sorting and stacking low to high, sortStack()" << endl; + cout << "17. flip list, flip()" << endl; + + cout << "0: Exit!!!!" << endl; + + cout << endl; + + cout << "Enter your choice : "; + cin >> choice; + + switch (choice) + { + case 1: // * push + cout << "[Massage] Enter element : "; + cin >> element; + Stack.push(element); + break; + + case 2: // * pop + element = Stack.pop(); + cout << "[Massage] get element : " << element << endl; + break; + + case 3: // * top + element = Stack.top(); + cout << "[Massage] Element on top : " << element << endl; + break; + + case 4: // * clear + Stack.clear(); + break; + + case 5: + if(Stack.isEmpty() != false){ + cout << "[Massage] is empty!!!" << endl; + }else{ + cout << "[Massage] Can't add Stack!!!" << endl; + } + break; + + case 6: + if(Stack.isFull() != false){ + cout << "[Massage] is Full!!!" << endl; + }else{ + cout << "[Massage] Can add Stack!!!" << endl; + } + break; + + case 7: // * max + element = Stack.max(); + cout << "[Massage] : MaxValue is : " << element << endl; + break; + + case 8: // * min + element = Stack.min(); + cout << "[Massage] : MinValue is : " << element << endl; + break; + + case 9: // * appendrandom + cout << "Enter number of numbers : "; + cin >> round; + Stack.appendRandom(round); + break; + + case 10: // * swapToTwo + Stack.swapTopTwo(); + break; + + case 11: // * moveToTop + Stack.moveMaxToTop(); + break; + + case 12: // * oddcount + element = Stack.oddCount(); + cout << "[Massage] Odd counting " << element << endl; + break; + + case 13: // * evencount + element = Stack.evenCount(); + cout << "[Massage] Even counting " << element << endl; + break; + + case 14: + element = Stack.sum(); + cout << "[Massage] Summation is " << element << endl; + break; + + case 15: + element = Stack.mean(); + cout << "[Massage] Mean is " << element << endl; + break; + + case 16: + Stack.sortStack(); + break; + + case 17: // * moveToTop + Stack.flip(); + break; + + case 0: + cout << "Bye...." << endl + << endl; + cout << "-------------- Call method --------------" << endl; + break; + + default: // * Exit + cout << "[Massage] Wrong choice,try again..." << endl; + } + + } while (choice != 0); + + return 0; +} \ No newline at end of file diff --git a/OOP/LabW4/LinkStack/makefile b/OOP/LabW4/LinkStack/makefile new file mode 100644 index 0000000..1cd64b5 --- /dev/null +++ b/OOP/LabW4/LinkStack/makefile @@ -0,0 +1,28 @@ +run: + @g++ -c main.cpp -o main.o -g + @g++ -c LinkStack.cpp -o LinkStack.o -g + @g++ -c ../LinkedList/LinkedList.cpp -o ../LinkedList/LinkedList.o -g + @g++ -c ../ChainNode/ChainNode.cpp -o ../ChainNode/ChainNode.o -g + @g++ main.o ../LinkedList/LinkedList.o ../ChainNode/ChainNode.o LinkStack.o -o LabW4 -g + + @./LabW4 + + @rm -f *.o ../LinkedList/LinkedList.o ../ChainNode/ChainNode.o LabW4 + +all: main.o LinkStack.o ../LinkedList/LinkedList.o ../ChainNode/ChainNode.o + g++ main.o ../LinkedList/LinkedList.o ../ChainNode/ChainNode.o LinkStack.o -o week4 -g + +main.o: main.cpp + g++ -c main.cpp -o main.o -g + +LinkStack.o: LinkStack.cpp + g++ -c LinkStack.cpp -o LinkStack.o -g + +../LinkedList/LinkedList.o: ../LinkedList/LinkedList.cpp + g++ -c ../LinkedList/LinkedList.cpp -o ../LinkedList/LinkedList.o -g + +../ChainNode/ChainNode.o: ../ChainNode/ChainNode.cpp + g++ -c ../ChainNode/ChainNode.cpp -o ../ChainNode/ChainNode.o -g + +clean: + rm -f *.o ../LinkedList/LinkedList.o ../ChainNode/ChainNode.o LabW4 \ No newline at end of file diff --git a/OOP/LabW4/LinkedList/LinkedList.cpp b/OOP/LabW4/LinkedList/LinkedList.cpp new file mode 100644 index 0000000..c8446de --- /dev/null +++ b/OOP/LabW4/LinkedList/LinkedList.cpp @@ -0,0 +1,495 @@ +#include +#include "LinkedList.h" + +using namespace std; + +bool LinkedList::isFull() +{ + // * try to create new node + ChainNode *p = new ChainNode; + bool returnBool; // * can create new node + if (p != NULL) + returnBool = false; + else + returnBool = true; + + delete p; + + return returnBool; +} + +bool LinkedList::isEmpty() +{ + // * if curSize = 0 is true + // * if curSize != 0 is fale + return curSize == 0; +} + +LinkedList::LinkedList() +{ + // * set curSize = 0 + curSize = 0; + + firstNode = new ChainNode(0, NULL); +} + +LinkedList::~LinkedList() +{ + // * set p to dummy + ChainNode *p = firstNode; + + // * iterative node + while (p != NULL) + { + ChainNode *n = p->next; + + delete p; + p = n; + } +} + +int LinkedList::size() +{ + // * get Size to User + return curSize; +} + +int LinkedList::indexOf(int e) +{ + if (isEmpty()) + { + cerr << "[Massage Error] : memory is empty!!!" << endl; + return -1; + } + + int i = 0; + ChainNode *p = firstNode->next; + + // * loop find index of element + while (p->element != e && i < curSize - 1) + { + p = p->next; // * p get move address + + i++; + } + if (p->element != e) + { + cerr << "[Massage Error] : not found" << endl; + return -1; + } + + return i; +} + +int LinkedList::get(int i) +{ + // * check empty and full + if (isEmpty()) + { + cerr << "[Massage Error] : Empty Stack!!!" << endl; + return -1; + } + + // * check lower + if (i < 0) + { + cerr << "[Massage Warning] : i is lower bound" << endl; + return -1; + } + // * check upper + else if (i >= curSize) + { + cerr << "[Massage Warning] : i is over bound" << endl; + return -1; + } + + int j = 0; + // * get element from index + ChainNode *p = firstNode->next; + // * how i get element from index?? + while (j != i && j <= curSize - 1) + { + p = p->next; + j++; + } + + return p->element; +} + +void LinkedList::set(int i, int e) +{ + // * check empty + if (isEmpty()) + { + cerr << "[Massage Error] : Empty Stack!!!" << endl; + return; + } + + // * check lower + if (i < 0) + { + cout << "[Massage Warning] : i is lower bound" << endl; + return; + } + // * check upper + else if (i >= curSize) + { + cout << "[Massage Warning] : i is over bound" << endl; + return; + } + + // * loop to index [i] <-- + // * set element to index something + int j = 0; + ChainNode *p = firstNode->next; + + while (j != i && j <= curSize - 1) + { + p = p->next; + j++; + } + p->element = e; +} + +int LinkedList::remove(int i) +{ + // * check empty and full + if (isEmpty()) + { + cerr << "[Massage Error] : Empty Stack!!!" << endl; + return -1; + } + + // * check lower + if (i < 0) + { + cout << "[Massage Warning] : i is lower bound" << endl; + return -1; + } + // * check upper + else if (i >= curSize) + { + cout << "[Massage Warning] : i is over bound" << endl; + return -1; + } + + int j = 0; + // * node p -> before node to delete + ChainNode *p = firstNode; + + while (j < i) + { + p = p->next; + j++; + } + + // * q = delete + ChainNode *q = p->next; + // * node r -> after delete node + ChainNode *r = q->next; + + int temp = q->element; + + delete q; + p->next = r; + + curSize--; + + return temp; +} + +void LinkedList::add(int i, int e) +{ + // * check full + if (isFull()) + { + cerr << "[Massage Error] : memory is full!!!" << endl; + return; + } + + // * check lower bound + if (i < 0) + { + cout << "[Massage Warning] : Lower bound" << endl; + cout << "[Massage] : Index is changed to 0" << endl; + i = 0; + } + // * check upper bound + else if (i > curSize) + { + cout << "[Massage Warning] : upper bound" << endl; + cout << "[Massage] : Index is changed to " << curSize << endl; + i = curSize; + } + + // * p point to dummy + ChainNode *p = firstNode; + + // * move p to forward for i times + for (int j = 0; j < i; j++) + p = p->next; + + // * q point to new node + ChainNode *q = new ChainNode(e, p->next); + + // * adjust link + p->next = q; + + // * increment cursize + curSize++; +} + +void LinkedList::clear() +{ + // * u can't set curSize = 0 + // * set p to dummy + ChainNode *p = firstNode->next; + + // * iterative node + while (p != NULL) + { + ChainNode *n = p->next; + + delete p; + p = n; + } + + // * set next of dummy point to NULL + firstNode->next = NULL; + + // * set current size to zero + curSize = 0; +} + +int LinkedList::max() +{ + // * check empty and full + if (isEmpty()) + { + cerr << "[Massage Error] Empty Stack!!!" << endl; + return -1; + } + + // * move node --> next and get high value + // * loop .next + ChainNode *p = firstNode->next; + + int i = 0; + int n = p->element; + + while (i <= curSize - 1, p = p->next) + { + if (n < p->element) + { + // * get value 1 > value 2 + n = p->element; + } + // * move node --> p.next + i++; + } + + return n; +} + +int LinkedList::min() +{ + // * check empty list + if (isEmpty()) + { + cerr << "[Massage Error] : memory is empty!!!" << endl; + return -1; + } + + // * move node --> next and get low value + // * loop .next + ChainNode *p = firstNode->next; + + int i = 0; + int n = p->element; + + while (i <= curSize - 1, p = p->next) + { + if (n > p->element) + { + // * get value 1 < value 2 + n = p->element; + } + // * move node --> p.next + i++; + } + + return n; +} + +int LinkedList::oddCount() +{ + // * check empty list + if (isEmpty()) + { + cerr << "[Massage Error] : memory is empty!!!" << endl; + return -1; + } + + int i = 0; + int count = 0; + + ChainNode *p = firstNode; + + while (p != NULL, p = p->next) + { + // * checking value divide 2 is false + // * count += p->element % 2; + if (p->element % 2) + count++; + + // * if(p->element % 2 == true){ + // * cout << p->element << ","; + // * Odd number counting + // * count = p->element; + + // * count += p->element % 2; + // * cout << count << ","; + + // * Odd number counting + } + + return count; +} + +int LinkedList::evenCount() +{ + if (isEmpty()) + { + cerr << "[Massage Error] : memory is empty!!!" << endl; + return -1; + } + + int cur = curSize; + + return cur -= oddCount(); +} + +float LinkedList::sum() +{ + if (isEmpty()) + { + cerr << "[Massage Error] : memory is empty!!!" << endl; + return -1; + } + + int i = 0, sum = 0; + + ChainNode *p = firstNode; + + while (i < curSize - 1, p = p->next) + { + // * + value for find sum + sum += p->element; + ++i; + } + + return sum; +} + +float LinkedList::mean() +{ + if (isEmpty()) + { + cerr << "[Massage Error] : memory is empty!!!" << endl; + return -1; + } + float mean = 0; + + // * set node p to first node + ChainNode *p = firstNode->next; + // * find value by summation divide curSize + // * another way we can find value by round num + mean = sum() / curSize; + + return mean; +} + +void LinkedList::sort() +{ + if (isEmpty()) + { + cerr << "[Massage Error] Empty Stack!!!" << endl; + return; + } + + // * sort minimum value to max value + // * loop checking element + // * swap element + int i = 0; + + // * loop from curSize + while (i < curSize - 1) + { + int j = 0; + ChainNode *p = firstNode->next; + // * loop for swap element + while (j < curSize - 1) + { + if (p->element > p->next->element) + { + // * swap element + swap(p, p->next); + } + p = p->next; + ++j; + } + ++i; + } +} + +void LinkedList::appendRandom(int round) +{ + int i = 0, rand = 0; + // * loop with round + while (i < round) + { + // * random number < 101 + rand = random() % 101; + add(curSize, rand); + ++i; + } +} + +void LinkedList::swap(ChainNode *p, ChainNode *q) +{ + if (isEmpty()) + { + cerr << "[Massage Error] : memory is empty!!!" << endl; + return; + } + + // * just swap element function + int t = p->element; + p->element = q->element; + q->element = t; +} + +void LinkedList::display() +{ + // * show L to L:(?,?), size: corSize/MaxSize + // * L: {1,2,3} + cout << "[Massage] -> L: {"; + if (curSize == 0) + { + cout << "}, "; + } + else + { + ChainNode *p = firstNode->next; + for (int i = 0; i < curSize; i++, p = p->next) + { + cout << p->element; + if (i < curSize - 1) + cout << ", "; + else + cout << "}, "; + } + } + + cout << "Size: " << curSize << endl; +} \ No newline at end of file diff --git a/OOP/LabW4/LinkedList/LinkedList.h b/OOP/LabW4/LinkedList/LinkedList.h new file mode 100644 index 0000000..f181a51 --- /dev/null +++ b/OOP/LabW4/LinkedList/LinkedList.h @@ -0,0 +1,45 @@ +#if !defined(_LINKEDLSIT_H) +#define _LINKEDLSIT_H +#include "../ChainNode/ChainNode.h" + +class LinkedList +{ +private: // * data + ChainNode *firstNode; + int curSize; + +protected: + bool isFull(); + bool isEmpty(); + +public: // * method + LinkedList(); // ! contructor *important (1)* + ~LinkedList(); // ! destructor + + // * basic method + int size(); + int indexOf(int e); + int get(int i); + void set(int i, int e); + int remove(int i); + void add(int i, int e); + + void clear(); + int max(); + int min(); + + int oddCount(); + int evenCount(); + float sum(); + float mean(); + void sort(); + + void appendRandom(int round); + + void swap(ChainNode *p, ChainNode *q); + + // * addition method + void display(); +}; + +#endif // _LINKEDLSIT_H \ No newline at end of file diff --git a/OOP/LabW4/LinkedList/main.cpp b/OOP/LabW4/LinkedList/main.cpp new file mode 100644 index 0000000..7ceace8 --- /dev/null +++ b/OOP/LabW4/LinkedList/main.cpp @@ -0,0 +1,138 @@ +/*************************************************************************************** + * ID: 65037743 + * NAME: Mr.Kidsadakorn Nuallaoong + * DESCRIPTION: LinkList Testing + ***************************************************************************************/ +#include +#include "LinkedList.h" + +using namespace std; + +int main() +{ + int choice,index,element, round; + LinkedList list; +do +{ + cout << "----------------- MENU -----------------" << endl << endl; + + cout << "================ DISPLAY ===============" << endl << endl; + + list.display(); + + cout << endl << "========================================" << endl << endl; + + cout << "1: Add element to List, add()" << endl; + cout << "2: Remove element from list, remove()" << endl; + cout << "3: Set element to list, set()" << endl; + cout << "4: Get element from list, get()" << endl; + cout << "5: Index of element, indexOf()" << endl; + cout << "6: List size, size()" << endl; + cout << "7: Clear list, clear()" << endl; + cout << "8: Find maximum number from list, max()" << endl; + cout << "9: Find minimum number from list, min()" << endl; + cout << "10: Find odd number from list, oddcount()" << endl; + cout << "11: Find even number from list, evencount()" << endl; + cout << "12: Find sum value, sum()" << endl; + cout << "13: Calculate average value from list, mean()" << endl; + cout << "14: Sorting element, sort()" << endl; + cout << "15: Append random number to list, appendRandom()" << endl; + cout << "0: Exit!!!!" << endl; + + cout << "----------------- INPUT -----------------" << endl << endl; + + cout << "Enter your choice : " ; cin >> choice; + + switch (choice) + { + case 1: // * add + cout << "Enter index : "; cin >> index; + cout << "Enter element : "; cin >> element; + list.add(index, element); + break; + + case 2: // * remove + cout << "Enter index : "; cin >> index; + element = list.remove(index); + cout << "[Massage] : Remove -> " << element << endl; + break; + + case 3: // * set + cout << "Enter index : "; cin >> index; + cout << "Enter element : "; cin >> element; + list.set(index, element); + break; + + case 4: // * get + cout << "Enter index : "; cin >> index; + element = list.get(index); + cout << "[Massage] list[" << index << "] is " << element << endl; + break; + + case 5: // * index of + cout << "Enter element : "; cin >> element; + index = list.indexOf(element); + cout << "[Massage] : Index of Element " << element << " is : " << index << endl; + break; + + case 6: // * list size + cout << "[Massage] Current List Size is " << list.size() << endl; + break; + + case 7: // * clear + list.clear(); + break; + + case 8: // * max + element = list.max(); + cout << "[Massage] : MaxValue is : " << element << endl; + break; + + case 9: // * min + element = list.min(); + cout << "[Massage] : MinValue is : " << element << endl; + break; + + case 10: // * odd + element = list.oddCount(); + cout << "[Massage] : Odd number -> " << element << endl; + break; + + case 11: // * even + element = list.evenCount(); + cout << "[Massage] : Even number -> " << element << endl; + break; + + case 12: // * sum value + element = list.sum(); + cout << "[Massage] : Summation is " << element << endl; + break; + + case 13: // * mean + element = list.mean(); + cout << "[Massage] : Average is " << element << endl; + + break; + + case 14: // * sort + list.sort(); + break; + + case 15: // * appendRandom + cout << "[massage] : Round to random number : "; cin >> round; + list.appendRandom(round); + break; + + case 0: + cout << "Bye...." << endl << endl; + cout << "-------------- Call method --------------" << endl; + break; + + default: // * Exit + cout << "[Massage] Wrong choice,try again..." << endl; + } + +}while(choice != 0); + + return 0; +} \ No newline at end of file diff --git a/OOP/LabW4/LinkedList/makefile b/OOP/LabW4/LinkedList/makefile new file mode 100644 index 0000000..afb8165 --- /dev/null +++ b/OOP/LabW4/LinkedList/makefile @@ -0,0 +1,20 @@ +run: + @g++ -c main.cpp -o main.o -g + @g++ -c LinkedList.cpp -o LinkedList.o -g + @g++ -c ../ChainNode/ChainNode.cpp -o ../ChainNode/ChainNode.o -g + @g++ main.o LinkedList.o ../ChainNode/ChainNode.o -o LabW4 -g + + @./LabW4 + + @rm -f *.o ../ChainNode/ChainNode.o LabW4 + +all: main.o + g++ main.o LinkedList.o ../ChainNode/ChainNode.o -o LabW4 -g + +main.o: main.cpp LinkedList.cpp ../ChainNode/ChainNode.cpp + g++ -c main.cpp -o main.o -g + g++ -c LinkedList.cpp -o LinkedList.o -g + g++ -c ../ChainNode/ChainNode.cpp -o ../ChainNode/ChainNode.o -g + +clean: + rm -f *.o ../ChainNode/ChainNode.o LabW4 diff --git a/OOP/LabW5/ChainNode/ChainNode.cpp b/OOP/LabW5/ChainNode/ChainNode.cpp new file mode 100644 index 0000000..09c6e0d --- /dev/null +++ b/OOP/LabW5/ChainNode/ChainNode.cpp @@ -0,0 +1,8 @@ +#include +#include "ChainNode.h" + +ChainNode::ChainNode(int e, ChainNode *p) +{ + element = e; + next = p; +} \ No newline at end of file diff --git a/OOP/LabW5/ChainNode/ChainNode.h b/OOP/LabW5/ChainNode/ChainNode.h new file mode 100644 index 0000000..e85d0fe --- /dev/null +++ b/OOP/LabW5/ChainNode/ChainNode.h @@ -0,0 +1,15 @@ +#if !defined(_CHAINNODE_H) +#define _CHAINNODE_H + +#include + +class ChainNode +{ + public: + int element; + ChainNode *next; + public: + ChainNode(int e = 0, ChainNode *p = NULL); +}; + +#endif // _CHAINNODE_H diff --git a/OOP/LabW5/ChainNode/main.cpp b/OOP/LabW5/ChainNode/main.cpp new file mode 100644 index 0000000..8040029 --- /dev/null +++ b/OOP/LabW5/ChainNode/main.cpp @@ -0,0 +1,18 @@ +/*************************************************************************************** + * ID: 65037743 + * NAME: Mr.Kidsadakorn Nuallaoong + * DESCRIPTION: ArrayStack Testing + ***************************************************************************************/ +#include +#include "ChainNode.h" + +using namespace std; + +int main() +{ + ChainNode a; // * ==> e = 0, p = NULL + ChainNode b(10, &a); // * ==> e = 10, p = &a + ChainNode c(20, &b); // * ==> e = 20, p = &b + + return 0; +} diff --git a/OOP/LabW5/ChainNode/makefile b/OOP/LabW5/ChainNode/makefile new file mode 100644 index 0000000..e67dfde --- /dev/null +++ b/OOP/LabW5/ChainNode/makefile @@ -0,0 +1,18 @@ +run: + @g++ -c main.cpp -o main.o -g + @g++ -c ChainNode.cpp -o ChainNode.o -g + @g++ main.o ChainNode.o -o LabW3 -g + + @./LabW3 + + @rm -f *.o LabW3 + +all: main.o ChainNode.o + g++ main.o ChainNode.o -o week4 -g + +main.o: main.cpp ChainNode.cpp + g++ -c main.cpp -o main.o -g + g++ -c ChainNode.cpp -o ChainNode.o -g + +clean: + rm -f *.o LabW3 diff --git a/OOP/LabW5/LinkStack/LinkStack.cpp b/OOP/LabW5/LinkStack/LinkStack.cpp new file mode 100644 index 0000000..8117fed --- /dev/null +++ b/OOP/LabW5/LinkStack/LinkStack.cpp @@ -0,0 +1,68 @@ +#include "LinkStack.h" + +void LinkStack::push(int e) +{ + // * add to element + add(size(), e); +} + +int LinkStack::pop() +{ + // * remove element index Size - 1 + return remove(size() - 1); +} + +int LinkStack::top() +{ + // * get element index Size - 1 + return get(size() - 1); +} + +bool LinkStack::isEmpty() +{ + return LinkedList::isEmpty(); +} + +bool LinkStack::isFull() +{ + return LinkedList::isFull(); +} + +void LinkStack::swapTopTwo() +{ + // * get and remove element + int a = pop(); + int b = pop(); + + // * add element to first index + push(a); + push(b); +} + +void LinkStack::moveMaxToTop() +{ + // * find max value + int a = max(); + // * get and remove max stack + remove(indexOf(max())); + // * add element a to last index + push(a); +} + +void LinkStack::sortStack() +{ + // * get sorting + sort(); +} + +void LinkStack::flip() +{ + int j = size() - 1; + + for (j; j > 0; --j) + { + // * get and remove element index[0] + // * add element to j-1/r + add(j, remove(0)); + } +} \ No newline at end of file diff --git a/OOP/LabW5/LinkStack/LinkStack.h b/OOP/LabW5/LinkStack/LinkStack.h new file mode 100644 index 0000000..53c7d7f --- /dev/null +++ b/OOP/LabW5/LinkStack/LinkStack.h @@ -0,0 +1,25 @@ +#if !defined(_LINKSTACK_H) +#define _LINKSTACK_H + +#include "../LinkedList/LinkedList.h" + +class LinkStack : public LinkedList +{ + private: + int maxSize; + + public: + void push(int e); + int pop(); + int top(); + + bool isEmpty(); + bool isFull(); + + void swapTopTwo(); + void moveMaxToTop(); + void sortStack(); + void flip(); +}; + +#endif // _LINKSTACK_H \ No newline at end of file diff --git a/OOP/LabW5/LinkStack/main.cpp b/OOP/LabW5/LinkStack/main.cpp new file mode 100644 index 0000000..8cb507d --- /dev/null +++ b/OOP/LabW5/LinkStack/main.cpp @@ -0,0 +1,163 @@ +/*************************************************************************************** + * ID: 65037743 + * NAME: Mr.Kidsadakorn Nuallaoong + * DESCRIPTION: LinkStack Testing + ***************************************************************************************/ +#include +#include "LinkStack.h" + +using namespace std; + +int main() +{ + LinkStack Stack; + + int choice, index, element, round; + + do + { + cout << "----------------- MENU -----------------" << endl + << endl; + + cout << "================ DISPLAY ===============" << endl + << endl; + + Stack.display(); + + cout << endl + << "========================================" << endl + << endl; + + cout << "1: Add element to last Stack, push()" << endl; + cout << "2: Get last element, pop()" << endl; + cout << "3: Get top element, top()" << endl; + cout << "4: Clear element in Stack, clear()" << endl; + + cout << "5: Check size of Stack is Empty?, isEmpty()" << endl; + cout << "6: Check size of Stack is Full?, isFull()" << endl; + cout << "7: Find maximum number from Stack, max()" << endl; + cout << "8: Find minimum number from Stack, min()" << endl; + + cout << "9. Random and add to Stack, appendRandom()" << endl; + cout << "10. Swap two element on pop, swapToTwo()" << endl; + cout << "11. move element to top Stack, moveToTop()" << endl; + + cout << "12: Find odd number from list, oddcount()" << endl; + cout << "13: Find even number from list, evencount()" << endl; + cout << "14: Find sum value, sum()" << endl; + cout << "15: Calculate average value from list, mean()" << endl; + + cout << "16. Sorting and stacking low to high, sortStack()" << endl; + cout << "17. flip list, flip()" << endl; + + cout << "0: Exit!!!!" << endl; + + cout << endl; + + cout << "Enter your choice : "; + cin >> choice; + + switch (choice) + { + case 1: // * push + cout << "[Massage] Enter element : "; + cin >> element; + Stack.push(element); + break; + + case 2: // * pop + element = Stack.pop(); + cout << "[Massage] get element : " << element << endl; + break; + + case 3: // * top + element = Stack.top(); + cout << "[Massage] Element on top : " << element << endl; + break; + + case 4: // * clear + Stack.clear(); + break; + + case 5: + if(Stack.isEmpty() != false){ + cout << "[Massage] is empty!!!" << endl; + }else{ + cout << "[Massage] Can't add Stack!!!" << endl; + } + break; + + case 6: + if(Stack.isFull() != false){ + cout << "[Massage] is Full!!!" << endl; + }else{ + cout << "[Massage] Can add Stack!!!" << endl; + } + break; + + case 7: // * max + element = Stack.max(); + cout << "[Massage] : MaxValue is : " << element << endl; + break; + + case 8: // * min + element = Stack.min(); + cout << "[Massage] : MinValue is : " << element << endl; + break; + + case 9: // * appendrandom + cout << "Enter number of numbers : "; + cin >> round; + Stack.appendRandom(round); + break; + + case 10: // * swapToTwo + Stack.swapTopTwo(); + break; + + case 11: // * moveToTop + Stack.moveMaxToTop(); + break; + + case 12: // * oddcount + element = Stack.oddCount(); + cout << "[Massage] Odd counting " << element << endl; + break; + + case 13: // * evencount + element = Stack.evenCount(); + cout << "[Massage] Even counting " << element << endl; + break; + + case 14: + element = Stack.sum(); + cout << "[Massage] Summation is " << element << endl; + break; + + case 15: + element = Stack.mean(); + cout << "[Massage] Mean is " << element << endl; + break; + + case 16: + Stack.sortStack(); + break; + + case 17: // * moveToTop + Stack.flip(); + break; + + case 0: + cout << "Bye...." << endl + << endl; + cout << "-------------- Call method --------------" << endl; + break; + + default: // * Exit + cout << "[Massage] Wrong choice,try again..." << endl; + } + + } while (choice != 0); + + return 0; +} \ No newline at end of file diff --git a/OOP/LabW5/LinkStack/makefile b/OOP/LabW5/LinkStack/makefile new file mode 100644 index 0000000..1cd64b5 --- /dev/null +++ b/OOP/LabW5/LinkStack/makefile @@ -0,0 +1,28 @@ +run: + @g++ -c main.cpp -o main.o -g + @g++ -c LinkStack.cpp -o LinkStack.o -g + @g++ -c ../LinkedList/LinkedList.cpp -o ../LinkedList/LinkedList.o -g + @g++ -c ../ChainNode/ChainNode.cpp -o ../ChainNode/ChainNode.o -g + @g++ main.o ../LinkedList/LinkedList.o ../ChainNode/ChainNode.o LinkStack.o -o LabW4 -g + + @./LabW4 + + @rm -f *.o ../LinkedList/LinkedList.o ../ChainNode/ChainNode.o LabW4 + +all: main.o LinkStack.o ../LinkedList/LinkedList.o ../ChainNode/ChainNode.o + g++ main.o ../LinkedList/LinkedList.o ../ChainNode/ChainNode.o LinkStack.o -o week4 -g + +main.o: main.cpp + g++ -c main.cpp -o main.o -g + +LinkStack.o: LinkStack.cpp + g++ -c LinkStack.cpp -o LinkStack.o -g + +../LinkedList/LinkedList.o: ../LinkedList/LinkedList.cpp + g++ -c ../LinkedList/LinkedList.cpp -o ../LinkedList/LinkedList.o -g + +../ChainNode/ChainNode.o: ../ChainNode/ChainNode.cpp + g++ -c ../ChainNode/ChainNode.cpp -o ../ChainNode/ChainNode.o -g + +clean: + rm -f *.o ../LinkedList/LinkedList.o ../ChainNode/ChainNode.o LabW4 \ No newline at end of file diff --git a/OOP/LabW5/LinkedList/LinkedList.cpp b/OOP/LabW5/LinkedList/LinkedList.cpp new file mode 100644 index 0000000..c8446de --- /dev/null +++ b/OOP/LabW5/LinkedList/LinkedList.cpp @@ -0,0 +1,495 @@ +#include +#include "LinkedList.h" + +using namespace std; + +bool LinkedList::isFull() +{ + // * try to create new node + ChainNode *p = new ChainNode; + bool returnBool; // * can create new node + if (p != NULL) + returnBool = false; + else + returnBool = true; + + delete p; + + return returnBool; +} + +bool LinkedList::isEmpty() +{ + // * if curSize = 0 is true + // * if curSize != 0 is fale + return curSize == 0; +} + +LinkedList::LinkedList() +{ + // * set curSize = 0 + curSize = 0; + + firstNode = new ChainNode(0, NULL); +} + +LinkedList::~LinkedList() +{ + // * set p to dummy + ChainNode *p = firstNode; + + // * iterative node + while (p != NULL) + { + ChainNode *n = p->next; + + delete p; + p = n; + } +} + +int LinkedList::size() +{ + // * get Size to User + return curSize; +} + +int LinkedList::indexOf(int e) +{ + if (isEmpty()) + { + cerr << "[Massage Error] : memory is empty!!!" << endl; + return -1; + } + + int i = 0; + ChainNode *p = firstNode->next; + + // * loop find index of element + while (p->element != e && i < curSize - 1) + { + p = p->next; // * p get move address + + i++; + } + if (p->element != e) + { + cerr << "[Massage Error] : not found" << endl; + return -1; + } + + return i; +} + +int LinkedList::get(int i) +{ + // * check empty and full + if (isEmpty()) + { + cerr << "[Massage Error] : Empty Stack!!!" << endl; + return -1; + } + + // * check lower + if (i < 0) + { + cerr << "[Massage Warning] : i is lower bound" << endl; + return -1; + } + // * check upper + else if (i >= curSize) + { + cerr << "[Massage Warning] : i is over bound" << endl; + return -1; + } + + int j = 0; + // * get element from index + ChainNode *p = firstNode->next; + // * how i get element from index?? + while (j != i && j <= curSize - 1) + { + p = p->next; + j++; + } + + return p->element; +} + +void LinkedList::set(int i, int e) +{ + // * check empty + if (isEmpty()) + { + cerr << "[Massage Error] : Empty Stack!!!" << endl; + return; + } + + // * check lower + if (i < 0) + { + cout << "[Massage Warning] : i is lower bound" << endl; + return; + } + // * check upper + else if (i >= curSize) + { + cout << "[Massage Warning] : i is over bound" << endl; + return; + } + + // * loop to index [i] <-- + // * set element to index something + int j = 0; + ChainNode *p = firstNode->next; + + while (j != i && j <= curSize - 1) + { + p = p->next; + j++; + } + p->element = e; +} + +int LinkedList::remove(int i) +{ + // * check empty and full + if (isEmpty()) + { + cerr << "[Massage Error] : Empty Stack!!!" << endl; + return -1; + } + + // * check lower + if (i < 0) + { + cout << "[Massage Warning] : i is lower bound" << endl; + return -1; + } + // * check upper + else if (i >= curSize) + { + cout << "[Massage Warning] : i is over bound" << endl; + return -1; + } + + int j = 0; + // * node p -> before node to delete + ChainNode *p = firstNode; + + while (j < i) + { + p = p->next; + j++; + } + + // * q = delete + ChainNode *q = p->next; + // * node r -> after delete node + ChainNode *r = q->next; + + int temp = q->element; + + delete q; + p->next = r; + + curSize--; + + return temp; +} + +void LinkedList::add(int i, int e) +{ + // * check full + if (isFull()) + { + cerr << "[Massage Error] : memory is full!!!" << endl; + return; + } + + // * check lower bound + if (i < 0) + { + cout << "[Massage Warning] : Lower bound" << endl; + cout << "[Massage] : Index is changed to 0" << endl; + i = 0; + } + // * check upper bound + else if (i > curSize) + { + cout << "[Massage Warning] : upper bound" << endl; + cout << "[Massage] : Index is changed to " << curSize << endl; + i = curSize; + } + + // * p point to dummy + ChainNode *p = firstNode; + + // * move p to forward for i times + for (int j = 0; j < i; j++) + p = p->next; + + // * q point to new node + ChainNode *q = new ChainNode(e, p->next); + + // * adjust link + p->next = q; + + // * increment cursize + curSize++; +} + +void LinkedList::clear() +{ + // * u can't set curSize = 0 + // * set p to dummy + ChainNode *p = firstNode->next; + + // * iterative node + while (p != NULL) + { + ChainNode *n = p->next; + + delete p; + p = n; + } + + // * set next of dummy point to NULL + firstNode->next = NULL; + + // * set current size to zero + curSize = 0; +} + +int LinkedList::max() +{ + // * check empty and full + if (isEmpty()) + { + cerr << "[Massage Error] Empty Stack!!!" << endl; + return -1; + } + + // * move node --> next and get high value + // * loop .next + ChainNode *p = firstNode->next; + + int i = 0; + int n = p->element; + + while (i <= curSize - 1, p = p->next) + { + if (n < p->element) + { + // * get value 1 > value 2 + n = p->element; + } + // * move node --> p.next + i++; + } + + return n; +} + +int LinkedList::min() +{ + // * check empty list + if (isEmpty()) + { + cerr << "[Massage Error] : memory is empty!!!" << endl; + return -1; + } + + // * move node --> next and get low value + // * loop .next + ChainNode *p = firstNode->next; + + int i = 0; + int n = p->element; + + while (i <= curSize - 1, p = p->next) + { + if (n > p->element) + { + // * get value 1 < value 2 + n = p->element; + } + // * move node --> p.next + i++; + } + + return n; +} + +int LinkedList::oddCount() +{ + // * check empty list + if (isEmpty()) + { + cerr << "[Massage Error] : memory is empty!!!" << endl; + return -1; + } + + int i = 0; + int count = 0; + + ChainNode *p = firstNode; + + while (p != NULL, p = p->next) + { + // * checking value divide 2 is false + // * count += p->element % 2; + if (p->element % 2) + count++; + + // * if(p->element % 2 == true){ + // * cout << p->element << ","; + // * Odd number counting + // * count = p->element; + + // * count += p->element % 2; + // * cout << count << ","; + + // * Odd number counting + } + + return count; +} + +int LinkedList::evenCount() +{ + if (isEmpty()) + { + cerr << "[Massage Error] : memory is empty!!!" << endl; + return -1; + } + + int cur = curSize; + + return cur -= oddCount(); +} + +float LinkedList::sum() +{ + if (isEmpty()) + { + cerr << "[Massage Error] : memory is empty!!!" << endl; + return -1; + } + + int i = 0, sum = 0; + + ChainNode *p = firstNode; + + while (i < curSize - 1, p = p->next) + { + // * + value for find sum + sum += p->element; + ++i; + } + + return sum; +} + +float LinkedList::mean() +{ + if (isEmpty()) + { + cerr << "[Massage Error] : memory is empty!!!" << endl; + return -1; + } + float mean = 0; + + // * set node p to first node + ChainNode *p = firstNode->next; + // * find value by summation divide curSize + // * another way we can find value by round num + mean = sum() / curSize; + + return mean; +} + +void LinkedList::sort() +{ + if (isEmpty()) + { + cerr << "[Massage Error] Empty Stack!!!" << endl; + return; + } + + // * sort minimum value to max value + // * loop checking element + // * swap element + int i = 0; + + // * loop from curSize + while (i < curSize - 1) + { + int j = 0; + ChainNode *p = firstNode->next; + // * loop for swap element + while (j < curSize - 1) + { + if (p->element > p->next->element) + { + // * swap element + swap(p, p->next); + } + p = p->next; + ++j; + } + ++i; + } +} + +void LinkedList::appendRandom(int round) +{ + int i = 0, rand = 0; + // * loop with round + while (i < round) + { + // * random number < 101 + rand = random() % 101; + add(curSize, rand); + ++i; + } +} + +void LinkedList::swap(ChainNode *p, ChainNode *q) +{ + if (isEmpty()) + { + cerr << "[Massage Error] : memory is empty!!!" << endl; + return; + } + + // * just swap element function + int t = p->element; + p->element = q->element; + q->element = t; +} + +void LinkedList::display() +{ + // * show L to L:(?,?), size: corSize/MaxSize + // * L: {1,2,3} + cout << "[Massage] -> L: {"; + if (curSize == 0) + { + cout << "}, "; + } + else + { + ChainNode *p = firstNode->next; + for (int i = 0; i < curSize; i++, p = p->next) + { + cout << p->element; + if (i < curSize - 1) + cout << ", "; + else + cout << "}, "; + } + } + + cout << "Size: " << curSize << endl; +} \ No newline at end of file diff --git a/OOP/LabW5/LinkedList/LinkedList.h b/OOP/LabW5/LinkedList/LinkedList.h new file mode 100644 index 0000000..f181a51 --- /dev/null +++ b/OOP/LabW5/LinkedList/LinkedList.h @@ -0,0 +1,45 @@ +#if !defined(_LINKEDLSIT_H) +#define _LINKEDLSIT_H +#include "../ChainNode/ChainNode.h" + +class LinkedList +{ +private: // * data + ChainNode *firstNode; + int curSize; + +protected: + bool isFull(); + bool isEmpty(); + +public: // * method + LinkedList(); // ! contructor *important (1)* + ~LinkedList(); // ! destructor + + // * basic method + int size(); + int indexOf(int e); + int get(int i); + void set(int i, int e); + int remove(int i); + void add(int i, int e); + + void clear(); + int max(); + int min(); + + int oddCount(); + int evenCount(); + float sum(); + float mean(); + void sort(); + + void appendRandom(int round); + + void swap(ChainNode *p, ChainNode *q); + + // * addition method + void display(); +}; + +#endif // _LINKEDLSIT_H \ No newline at end of file diff --git a/OOP/LabW5/LinkedList/main.cpp b/OOP/LabW5/LinkedList/main.cpp new file mode 100644 index 0000000..7ceace8 --- /dev/null +++ b/OOP/LabW5/LinkedList/main.cpp @@ -0,0 +1,138 @@ +/*************************************************************************************** + * ID: 65037743 + * NAME: Mr.Kidsadakorn Nuallaoong + * DESCRIPTION: LinkList Testing + ***************************************************************************************/ +#include +#include "LinkedList.h" + +using namespace std; + +int main() +{ + int choice,index,element, round; + LinkedList list; +do +{ + cout << "----------------- MENU -----------------" << endl << endl; + + cout << "================ DISPLAY ===============" << endl << endl; + + list.display(); + + cout << endl << "========================================" << endl << endl; + + cout << "1: Add element to List, add()" << endl; + cout << "2: Remove element from list, remove()" << endl; + cout << "3: Set element to list, set()" << endl; + cout << "4: Get element from list, get()" << endl; + cout << "5: Index of element, indexOf()" << endl; + cout << "6: List size, size()" << endl; + cout << "7: Clear list, clear()" << endl; + cout << "8: Find maximum number from list, max()" << endl; + cout << "9: Find minimum number from list, min()" << endl; + cout << "10: Find odd number from list, oddcount()" << endl; + cout << "11: Find even number from list, evencount()" << endl; + cout << "12: Find sum value, sum()" << endl; + cout << "13: Calculate average value from list, mean()" << endl; + cout << "14: Sorting element, sort()" << endl; + cout << "15: Append random number to list, appendRandom()" << endl; + cout << "0: Exit!!!!" << endl; + + cout << "----------------- INPUT -----------------" << endl << endl; + + cout << "Enter your choice : " ; cin >> choice; + + switch (choice) + { + case 1: // * add + cout << "Enter index : "; cin >> index; + cout << "Enter element : "; cin >> element; + list.add(index, element); + break; + + case 2: // * remove + cout << "Enter index : "; cin >> index; + element = list.remove(index); + cout << "[Massage] : Remove -> " << element << endl; + break; + + case 3: // * set + cout << "Enter index : "; cin >> index; + cout << "Enter element : "; cin >> element; + list.set(index, element); + break; + + case 4: // * get + cout << "Enter index : "; cin >> index; + element = list.get(index); + cout << "[Massage] list[" << index << "] is " << element << endl; + break; + + case 5: // * index of + cout << "Enter element : "; cin >> element; + index = list.indexOf(element); + cout << "[Massage] : Index of Element " << element << " is : " << index << endl; + break; + + case 6: // * list size + cout << "[Massage] Current List Size is " << list.size() << endl; + break; + + case 7: // * clear + list.clear(); + break; + + case 8: // * max + element = list.max(); + cout << "[Massage] : MaxValue is : " << element << endl; + break; + + case 9: // * min + element = list.min(); + cout << "[Massage] : MinValue is : " << element << endl; + break; + + case 10: // * odd + element = list.oddCount(); + cout << "[Massage] : Odd number -> " << element << endl; + break; + + case 11: // * even + element = list.evenCount(); + cout << "[Massage] : Even number -> " << element << endl; + break; + + case 12: // * sum value + element = list.sum(); + cout << "[Massage] : Summation is " << element << endl; + break; + + case 13: // * mean + element = list.mean(); + cout << "[Massage] : Average is " << element << endl; + + break; + + case 14: // * sort + list.sort(); + break; + + case 15: // * appendRandom + cout << "[massage] : Round to random number : "; cin >> round; + list.appendRandom(round); + break; + + case 0: + cout << "Bye...." << endl << endl; + cout << "-------------- Call method --------------" << endl; + break; + + default: // * Exit + cout << "[Massage] Wrong choice,try again..." << endl; + } + +}while(choice != 0); + + return 0; +} \ No newline at end of file diff --git a/OOP/LabW5/LinkedList/makefile b/OOP/LabW5/LinkedList/makefile new file mode 100644 index 0000000..afb8165 --- /dev/null +++ b/OOP/LabW5/LinkedList/makefile @@ -0,0 +1,20 @@ +run: + @g++ -c main.cpp -o main.o -g + @g++ -c LinkedList.cpp -o LinkedList.o -g + @g++ -c ../ChainNode/ChainNode.cpp -o ../ChainNode/ChainNode.o -g + @g++ main.o LinkedList.o ../ChainNode/ChainNode.o -o LabW4 -g + + @./LabW4 + + @rm -f *.o ../ChainNode/ChainNode.o LabW4 + +all: main.o + g++ main.o LinkedList.o ../ChainNode/ChainNode.o -o LabW4 -g + +main.o: main.cpp LinkedList.cpp ../ChainNode/ChainNode.cpp + g++ -c main.cpp -o main.o -g + g++ -c LinkedList.cpp -o LinkedList.o -g + g++ -c ../ChainNode/ChainNode.cpp -o ../ChainNode/ChainNode.o -g + +clean: + rm -f *.o ../ChainNode/ChainNode.o LabW4 diff --git a/OOP/LabW5/StackApplications/main.cpp b/OOP/LabW5/StackApplications/main.cpp new file mode 100644 index 0000000..6d81901 --- /dev/null +++ b/OOP/LabW5/StackApplications/main.cpp @@ -0,0 +1,173 @@ +/*************************************************************************************** + * ID: 65037743 + * NAME: Mr.Kidsadakorn Nuallaoong + * DESCRIPTION: StackApplications + ***************************************************************************************/ + +#include +#include +#include +#include "../LinkStack/LinkStack.h" + +using namespace std; + +int presedence(char c) +{ + // * get point of important for operator + switch (c) + { + case '+': + case '_': + return 0; + + case '*': + case '/': + return 1; + + case '^': + return 2; + + default: + return -1; + } +} + +int main(void) +{ + LinkStack Stack; + // * operators '+', '_', '*', '/', '^', '(', ')' + // * operands: 0, 1, ..., 9 + + // * ex. "3 + 5" => "35+" + // *string infix = "((3+6)*4/2-2^3)/5"; + string infix = "2^(1+4/2)^2"; + string postfix = ""; + int result = 0; + + // * display infix + cout << "infix: " << infix << endl; + + // * 1.infix to postfix econversion (p.11) + + for (int i = 0; i < infix.length(); i++) + { + char c = infix[i]; + + // * check operands + if (c >= '0' && c <= '9') + { + postfix += c; + } + // * checking operator + else if (c == '+' || c == '-' || + c == '*' || c == '/' || + c == '^' || + c == '(' || c == ')') + { + // * check stack isEmpty!!! + if (Stack.isEmpty()) + { + Stack.push(c); + } + else + { + // * find operand in String + // * but sorting work process / Stacking + // * Ex. 3+5 = 35+ --> logic + if (c == '(') + { + Stack.push(c); + } + else if (c == ')') + { + char temp; + do + { + temp = (char)Stack.pop(); + if (temp != '(') + { + postfix += temp; + } + } while (temp != '('); + } + // * checking wrong process ----> sorting by important + else if (presedence(c) > presedence(Stack.top())) + { + Stack.push(c); + } + else if (presedence(c) <= presedence(Stack.top())) + { + postfix += (char)Stack.pop(); + Stack.push(c); + } + } + } + } + + // * set new string without () + while (!Stack.isEmpty()) + { + // * get operand to postfix + postfix += (char)Stack.pop(); + } + + // * display postfix + cout << "Postfix : " << postfix << endl; + + // * 2.postfix calculation (p.31) + // * 2.1 loop for all postfix + for (int i = 0; i < postfix.length(); ++i) + { + if ((char)postfix[i] >= '0' && (char)postfix[i] <= '9') + { + // * push operand to Stack + Stack.push(postfix[i] - 48); + } + // * check opeator in array string + // * if array string have operator move to calculate + else if ((char)postfix[i] == '+' || (char)postfix[i] == '-' || + (char)postfix[i] == '*' || (char)postfix[i] == '/' || + (char)postfix[i] == '^' || (char)postfix[i] == '(' || + (char)postfix[i] == ')') + { + int Sa = Stack.pop(); // * get last Stack + int Sb = Stack.pop(); // * get almost the top of Stack + // * Check operator for calculate + switch ((char)postfix[i]) + { + case '+': + // * almost the top of Stack + last Stack + Stack.push(Sb + Sa); + break; + + case '-': + // * almost the top of Stack - last Stack + Stack.push(Sb - Sa); + break; + + case '*': + // * almost the top of Stack * last Stack + Stack.push(Sb * Sa); + break; + + case '/': + // * almost the top of Stack / last Stack + Stack.push(Sb / Sa); + break; + + case '^': + // * almost the top of Stack ^ last Stack + Stack.push(pow(Sb, Sa)); + break; + } + } + // * checking work proceess + // * Stack.display(); + } + + // * set result = reemaining + result = Stack.pop(); // * (char)ascii number + + // * display result + cout << "result : " << result << endl; +} \ No newline at end of file diff --git a/OOP/LabW5/StackApplications/makefile b/OOP/LabW5/StackApplications/makefile new file mode 100644 index 0000000..790ffab --- /dev/null +++ b/OOP/LabW5/StackApplications/makefile @@ -0,0 +1,25 @@ +run: + @g++ -c main.cpp -o main.o -g + @g++ -c ../LinkedList/LinkedList.cpp -o ../LinkedList/LinkedList.o -g + @g++ -c ../LinkStack/LinkStack.cpp -o ../LinkStack/LinkStack.o -g + @g++ -c ../ChainNode/ChainNode.cpp -o ../ChainNode/ChainNode.o -g + @g++ main.o ../LinkedList/LinkedList.o ../LinkStack/LinkStack.o ../ChainNode/ChainNode.o -o StackApplications -g + + @./StackApplications + + @rm -f *.o ../LinkedList/LinkedList.o ../LinkStack/LinkStack.o ../ChainNode/ChainNode.o StackApplications + +all: main.o ../LinkedList/LinkedList.o ../LinkStack/LinkStack.o ../ChainNode/ChainNode.o + g++ main.o ../LinkedList/LinkedList.o ../LinkStack/LinkStack.o ../ChainNode/ChainNode.o -o StackApplications -g + +main.o: main.cpp + g++ -c main.cpp -o main.o -g +../LinkedList/LinkedList.o: ../LinkedList/LinkedList.cpp + g++ -c ../LinkedList/LinkedList.cpp -o ../LinkedList/LinkedList.o -g +../LinkStack/LinkStack.o: ../LinkStack/LinkStack.cpp + g++ -c ../LinkStack/LinkStack.cpp -o ../LinkStack/LinkStack.o -g +../ChainNode/ChainNode.o: ../ChainNode/ChainNode.cpp + g++ -c ../ChainNode/ChainNode.cpp -o ../ChainNode/ChainNode.o -g + +clean: + rm -f *.o ../LinkedList/LinkedList.o ../LinkStack/LinkStack.o ../ChainNode/ChainNode.o StackApplications \ No newline at end of file