Advertisement
Infernale

NCTU LAB 17/10 NUM 3 [MyString.cpp]

Oct 17th, 2019
479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | None | 0 0
  1. #include <algorithm>
  2. #include <map>
  3. #include "MyString.h"
  4.  
  5. MyString::MyString(string s){
  6.   this->s = s;
  7. }
  8.  
  9. void MyString::setS(string str){
  10.   this->s = str;
  11. }
  12.  
  13. string MyString::getS(){
  14.   return s;
  15. }
  16.  
  17. MyString MyString::removePunctuation(){
  18.   string result;
  19.   remove_copy_if(s.begin(), s.end(), back_inserter(result), std::ptr_fun<int, int>(ispunct));
  20.   return MyString(result);
  21. }
  22.  
  23. MyString MyString::toUpper(){
  24.   string temp = this->s;
  25.   transform(temp.begin(), temp.end(), temp.begin(),::toupper);
  26.   return MyString(temp);
  27. }
  28.  
  29. MyString MyString::reverse(){
  30.   string temp = this->s;
  31.   int n = temp.length();
  32.   for (int i = 0; i < n/2; i++)
  33.       swap(temp[i], temp[n-i-1]);
  34.   return MyString(temp);
  35. }
  36.  
  37. MyString MyString::toMirror(){
  38.   string str = this->s;
  39.   for(int i=0; i<str.size(); i++){
  40.     int pos = normal.find(str[i]);
  41.     if(mirror[pos] != ' '){
  42.       //cout << "Found! " << mirror[pos] << endl;
  43.       str[i] = mirror[pos];
  44.     }
  45.   }
  46.   return MyString(str);
  47. }
  48.  
  49. void showMyStringType(){
  50.   //Show Type
  51. }
  52.  
  53. MyString MyString::longestPeekPalindrome(){
  54.   string str = this->s;
  55.   int n = str.size();
  56.   bool palCheckTab[n][n];
  57.   for(int i = 0; i<n; i++)
  58.      for(int j = 0; j<n; j++)
  59.         palCheckTab[i][j] = false;
  60.   int maxLength = 1;
  61.   for(int i = 0; i < n; ++i)
  62.      palCheckTab[i][i] = true;
  63.   int start = 0;
  64.   for(int i = 0; i < n-1; ++i){
  65.      if(str[i] == str[i+1]){
  66.         palCheckTab[i][i+1] = true;
  67.         start = i;
  68.         maxLength = 2;
  69.      }
  70.   }
  71.   for(int k = 3; k <= n; ++k){
  72.      for(int i = 0; i < n-k+1 ; ++i){
  73.         int j = i + k - 1;
  74.         if(palCheckTab[i+1][j-1] && str[i] == str[j]){
  75.            palCheckTab[i][j] = true;
  76.            if(k > maxLength) {
  77.               start = i;
  78.               maxLength = k;
  79.            }
  80.         }
  81.      }
  82.   }
  83.   str = str.substr(start, maxLength);
  84.   return MyString(str);
  85. }
  86.  
  87. void MyString::checkStringType(){
  88.   map<char, char> M;
  89.   for(int i=0; i<normal.size(); i++){
  90.     M[normal[i]] = mirror[i];
  91.   }
  92.   bool palin, mirror;
  93.   palin = mirror = true;
  94.   string str = this->s;
  95.   int n = str.size();
  96.   for(int i=0; i<=n/2; i++){
  97.     if(str[i] != str[n-i-1]){
  98.       palin = false;
  99.     }
  100.     if(str[n-i-1] != M[str[i]]){
  101.       mirror = false;
  102.     }
  103.   }
  104.   if(palin && mirror){
  105.     cout << "Both" << endl;
  106.   }else if(palin && !mirror){
  107.     cout << "Normal" << endl;
  108.   }else if(!palin && mirror){
  109.     cout << "Mirror" << endl;
  110.   }else{
  111.     cout << "Neither" << endl;
  112.   }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement