Advertisement
coregame

C++ Lab3 SortString

Jan 25th, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstdlib>
  4. #include <string>
  5. #include <cmath>
  6. #include <algorithm>
  7. #include <regex>
  8. using namespace std;
  9. enum Order { ASCENDING, DESCENDING };
  10.  
  11. class AutoSortedWordList {
  12. public:
  13.     AutoSortedWordList();
  14.     void setWord(string word, int index);
  15.     void setOrderOption(Order order);
  16.     void print();
  17. private:
  18.     const static int NUM_WORD = 5;
  19.     string list[NUM_WORD];
  20.     Order order;
  21.  
  22.     void sort();
  23.     //void swap(int i, int j);
  24. };
  25.  
  26. AutoSortedWordList::AutoSortedWordList(){
  27.     setOrderOption(ASCENDING);
  28. }
  29.  
  30. void AutoSortedWordList::setOrderOption(Order order) {
  31.     AutoSortedWordList::order = order;
  32.     sort();
  33. }
  34.  
  35. void AutoSortedWordList::setWord(string word, int index) {
  36.     if (index >= 0 && index < NUM_WORD) {
  37.         list[index] = word;
  38.         cout << "Add \"" << word << "\" at the index " << index << endl;
  39.     }
  40.     else
  41.         cout << "Cannot add word: index out of range!!!\n\n\n";
  42.     sort();
  43. }
  44.  
  45. void AutoSortedWordList::print() {
  46.     cout << "All words in list: " << endl;
  47.     for (int i = 0; i < NUM_WORD; i++)
  48.         cout << "\t" << list[i] << endl;
  49. }
  50.  
  51. void AutoSortedWordList::sort() {
  52.     int counter = 0;
  53.     for (int index = 0; index < NUM_WORD; index++) {
  54.         if (list[index] != "\0")
  55.             counter++;
  56.     }
  57.  
  58.     //int N = sizeof(list) / sizeof(list[0]);
  59.     std::sort(list, list+counter);
  60.     if (AutoSortedWordList::order == DESCENDING) {
  61.         std::reverse(list, list+counter);
  62.     }
  63.        
  64. }
  65.  
  66. int main() {
  67.     AutoSortedWordList aWordList;
  68.  
  69.     aWordList.setWord("Hello", 0);
  70.     aWordList.setWord("Konnichiwa", 1);
  71.     aWordList.setWord("Hallo", 2);
  72.     cout << "\n\n ASCENDING 3" << endl;
  73.     aWordList.print();
  74.  
  75.     aWordList.setWord("Ciao", 3);
  76.     cout << "\n\n ASCENDING 4" << endl;
  77.     aWordList.print();
  78.  
  79.     aWordList.setWord("Ni hao", 4);
  80.     aWordList.setWord("alo", 5);
  81.  
  82.     cout << "\n\nDefault : ASCENDING ORDER" << endl;
  83.     aWordList.print();
  84.  
  85.     cout << "\n\nDECESDING ORDER" << endl;
  86.     aWordList.setOrderOption(DESCENDING);
  87.     aWordList.print();
  88.  
  89.     system("pause");
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement