Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.7 kB
1
Indexable
#include<iostream>
#include"function.h"
using namespace std;

string table = "0123456789abcdefghijklmnopqrstuvwxyz";

String_Calculator::String_Calculator(const string str){
    s = str;
}
String_Calculator& String_Calculator::Add(const string str){
    s += str;
    return *this;
}
String_Calculator& String_Calculator::Subtract(const string str){
    size_t pos = s.find(str);
    string tmp = s;
    if(pos >= s.length()){
        s = "error";
    }
    else{
        s = "";
        int i;
        for(i = 0; i < pos; i++){
            s += tmp[i];
        }
        i += str.length();
        for(i; i < tmp.length(); i++){
            s += tmp[i];
        }
    }
    return *this;
}
String_Calculator& String_Calculator::Shift(const string str){
    int sh = 0;
    for(int i = 0; i < str.length(); i++){
        if(str[i] >= '0' && str[i] <= '9')
            sh = sh*10 + (str[i]-'0');
        else{
            s = "error";
            return *this;
        }
    }
    string tmp = s;
    s = "";
    sh = sh % 36;
    int len = tmp.length();
    for(int i = 0; i < len; i++){
        if(tmp[i] >= '1' && tmp[i] <= '9'){ //是數字
            if(tmp[i]+sh > '9'){
                s += table[(tmp[i]+sh-'0')%36];
            }
            else{
                s += (tmp[i]+sh);
            }
        }
        else{ //是字母
            if(tmp[i]+sh > 'z'){
                s += table[tmp[i]+sh-'z'-1];
            }
            else{
                s += (tmp[i]+sh);
            }
        }
    }
    return *this;
}
void String_Calculator::output() const{
    cout << s << endl;
}