C++程式設計 第7周 字串操作
阿新 • • 發佈:2019-01-10
遇到的問題:
1. 訪問陣列出現“不確定”的提示,後來是發現數組的命名與C++的某些操作命令重合了。
2. 對於“83s”這樣的字串,stoi的命令會直接得出83,需要一個個的字元去判斷是否是數字。
---------------------------------以下是題目--------------------------------------------------------------
描述
給定n個字串(從1開始編號),每個字串中的字元位置從0開始編號,長度為1-500,現有如下若干操作:
copy N X L:取出第N個字串第X個字元開始的長度為L的字串。
add S1 S2:判斷S1,S2是否為0-99999之間的整數,若是則將其轉化為整數做加法,若不是,則作字串加法,返回的值為一字串。
find S N:在第N個字串中從左開始找尋S字串,返回其第一次出現的位置,若沒有找到,返回字串的長度。
rfind S N:在第N個字串中從右開始找尋S字串,返回其第一次出現的位置,若沒有找到,返回字串的長度。
insert S N X:在第N個字串的第X個字元位置中插入S字串。
reset S N:將第N個字串變為S。
print N:列印輸出第N個字串。
printall:列印輸出所有字串。
over:結束操作。
其中N,X,L可由find與rfind操作表示式構成,S,S1,S2可由copy與add操作表示式構成。
輸入
第一行為一個整數n(n在1-20之間)
接下來n行為n個字串,字串不包含空格及操作命令等。
接下來若干行為一系列操作,直到over結束。
輸出
根據操作提示輸出對應字串。
程式碼在OpenJudge上通過了,但是Coursera並沒有,估計是因為stoi這個函式。#include<iostream> #include<string> using namespace std; int n = 0; string a[20];//收集所有字串 string Command[500]; int index = 0;//收集所有命令,index為命令的索引 string StringCopy(); int StringFind(); int StringRfind(); string StringAdd()//判斷S1,S2是否為0-99999之間的整數,若是則將其轉化為整數做加法,若不是,則作字串加法,返回的值為一字串。 { index++; string S1, S2; if (Command[index] == "add") S1 = StringAdd(); else if (Command[index] == "copy") S1 = StringCopy(); else S1 = Command[index]; index++; if (Command[index] == "add") S2 = StringAdd(); else if (Command[index] == "copy") S2 = StringCopy(); else S2 = Command[index]; for (int i = 0; i < S1.size(); i++) if ((S1.at(i) < '0') || (S1.at(i) > '9')) return (S1 + S2); for (int i = 0; i < S2.size(); i++) if ((S2.at(i) < '0') || (S2.at(i) > '9')) return (S1 + S2); if ((stoi(S1) <= 99999)&(stoi(S1) >= 0)&(stoi(S2) <= 99999)&(stoi(S2) >= 0)) { int result = (stoi(S1) + stoi(S2)); return to_string(result); } else return (S1 + S2); } string StringCopy()//取出第N個字串第X個字元開始的長度為L的字串。 { index++; int N, X, L; if (Command[index] == "find") N = StringFind(); else if (Command[index] == "rfind") N = StringRfind(); else N = stoi(Command[index]); index++; if (Command[index] == "find") X = StringFind(); else if (Command[index] == "rfind") X = StringRfind(); else X= stoi(Command[index]); index++; if (Command[index] == "find") L = StringFind(); else if (Command[index] == "rfind") L = StringRfind(); else L= stoi(Command[index]); return a[N - 1].substr(X, L); } int StringFind()//在第N個字串中從左開始找尋S字串,返回其第一次出現的位置,若沒有找到,返回字串的長度。 { index++; string S; int N; if (Command[index] == "copy") S = StringCopy(); else if (Command[index] == "add") S = StringAdd(); else S = Command[index]; index++; if (Command[index] == "find") N = StringFind(); else if (Command[index] == "rfind") N = StringRfind(); else N = stoi(Command[index]); if(a[N - 1].find(S)!=string::npos) return a[N - 1].find(S); else return a[N - 1].length(); } int StringRfind()//在第N個字串中從右開始找尋S字串,返回其第一次出現的位置,若沒有找到,返回字串的長度。 { index++; string S; int N; if (Command[index] == "copy") S = StringCopy(); else if (Command[index] == "add") S = StringAdd(); else S = Command[index]; index++; if (Command[index] == "find") N = StringFind(); else if (Command[index] == "rfind") N = StringRfind(); else N = stoi(Command[index]); if (a[N - 1].rfind(S)) return a[N - 1].rfind(S); else return a[N - 1].length(); } void StringInsert()//在第N個字串的第X個字元位置中插入S字串。 { index++; string S; int N, X; if (Command[index] == "copy") S = StringCopy(); else if (Command[index] == "add") S = StringAdd(); else S = Command[index]; index++; if (Command[index] == "find") N = StringFind(); else if (Command[index] == "rfind") N = StringRfind(); else N = stoi(Command[index]); index++; if (Command[index] == "find") X = StringFind(); else if (Command[index] == "rfind") X = StringRfind(); else X = stoi(Command[index]); a[N - 1].insert(X, S); } void StringReset()//將第N個字串變為S。 { index++; string S; int N; if (Command[index] == "copy") S = StringCopy(); else if (Command[index] == "add") S = StringAdd(); else S = Command[index]; index++; if (Command[index] == "find") N = StringFind(); else if (Command[index] == "rfind") N = StringRfind(); else N = stoi(Command[index]); a[N - 1] = S; } void StringPrint()//列印輸出第N個字串 { index++; int N; if (Command[index] == "find") N = StringFind(); else if (Command[index] == "rfind") N = StringRfind(); else N = stoi(Command[index]); cout << a[N - 1]<<endl; } void StringPriAll()//列印輸出所有字串 { for (int i = 0; i < n; i++) cout << a[i] << endl; } int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; Command[i-1] != "over"; i++) cin >> Command[i]; for (index=0;Command[index] != "over"; index++) { if (Command[index] == "insert") StringInsert(); else if (Command[index] == "reset") StringReset(); else if (Command[index] == "print") StringPrint(); else if (Command[index] == "printall") StringPriAll(); else if (Command[index] == "over") break; } return 0; }