Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <cstdlib>
- #include <string>
- #include <cmath>
- #include <algorithm>
- #include <regex>
- using namespace std;
- enum Order { ASCENDING, DESCENDING };
- class AutoSortedWordList {
- public:
- AutoSortedWordList();
- void setWord(string word, int index);
- void setOrderOption(Order order);
- void print();
- private:
- const static int NUM_WORD = 5;
- string list[NUM_WORD];
- Order order;
- void sort();
- //void swap(int i, int j);
- };
- AutoSortedWordList::AutoSortedWordList(){
- setOrderOption(ASCENDING);
- }
- void AutoSortedWordList::setOrderOption(Order order) {
- AutoSortedWordList::order = order;
- sort();
- }
- void AutoSortedWordList::setWord(string word, int index) {
- if (index >= 0 && index < NUM_WORD) {
- list[index] = word;
- cout << "Add \"" << word << "\" at the index " << index << endl;
- }
- else
- cout << "Cannot add word: index out of range!!!\n\n\n";
- sort();
- }
- void AutoSortedWordList::print() {
- cout << "All words in list: " << endl;
- for (int i = 0; i < NUM_WORD; i++)
- cout << "\t" << list[i] << endl;
- }
- void AutoSortedWordList::sort() {
- int counter = 0;
- for (int index = 0; index < NUM_WORD; index++) {
- if (list[index] != "\0")
- counter++;
- }
- //int N = sizeof(list) / sizeof(list[0]);
- std::sort(list, list+counter);
- if (AutoSortedWordList::order == DESCENDING) {
- std::reverse(list, list+counter);
- }
- }
- int main() {
- AutoSortedWordList aWordList;
- aWordList.setWord("Hello", 0);
- aWordList.setWord("Konnichiwa", 1);
- aWordList.setWord("Hallo", 2);
- cout << "\n\n ASCENDING 3" << endl;
- aWordList.print();
- aWordList.setWord("Ciao", 3);
- cout << "\n\n ASCENDING 4" << endl;
- aWordList.print();
- aWordList.setWord("Ni hao", 4);
- aWordList.setWord("alo", 5);
- cout << "\n\nDefault : ASCENDING ORDER" << endl;
- aWordList.print();
- cout << "\n\nDECESDING ORDER" << endl;
- aWordList.setOrderOption(DESCENDING);
- aWordList.print();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement