程式設計實習之STL專項練習
阿新 • • 發佈:2019-02-20
#include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> #include <string> #include <sstream> using namespace std; string s[50],cmd,s1,s2; int n,N,x,l,p; int getInt(); string getstring(); bool isDight(string s) { if (s.size()>=6) return false; for (int i = 0; i <s.size();++i) if (s[i]>'9' || s[i]<'0') return false; return true; } int rfind() { string s1 = getstring(); int N = getInt(); int p = s[N].rfind(s1); if (p == string::npos) return s[N].size(); else return p; } int find() { string s1 = getstring(); int N = getInt(); int p = s[N].find(s1); if (p == string::npos) return s[N].size(); else return p; } string copy() { int N = getInt(); int x = getInt(); int l = getInt(); return s[N].substr(x,l); } string add() { string s1 = getstring(); string s2 = getstring(); if (isDight(s1) && isDight(s2)) { int k = atoi(s1.c_str())+atoi(s2.c_str()); string s3=""; while (k>0) s3= char(k%10+'0')+s3,k/=10; return s3; } else return s1+s2; } string insert() { string s1 = getstring(); int N = getInt(); int x = getInt(); return s[N].insert(x,s1); } void reset() { string s1 = getstring(); int N = getInt(); s[N] = s1; } int getInt() { string temp; cin>>temp; if (temp == "find") return find(); else if (temp == "rfind") return rfind(); else { int num = 0; for (int i = 0; i <temp.size();++i) num = num*10+temp[i]-'0'; return num; } } string getstring() { string temp; cin>>temp; if (temp == "copy") return copy(); else if (temp == "add") return add(); else return temp; } int main() { freopen("1.in","r",stdin); freopen("1.out","w",stdout); cin>>n; for (int i= 1; i <= n;++i) cin>>s[i]; while (true) { cin>>cmd; switch(cmd[0]) { case 'o': exit(0); break; case 'c': s[0] = copy(); break; case 'a': s[0] = add(); case 'f': s[0] = find(); break; case 'r': if (cmd == "rfind") { s[0] = rfind(); } else reset(); break; case 'i': s[0] = insert(); break; case 'p': if (cmd.size() == 5) cout << s[getInt()]<<endl; else { for (int i = 1; i <= n;++i) cout<<s[i]<<endl; } break; default: break; } } return 0; }