//PAT1044 bug测试函数 #includeusing namespace std; #include int main() { string s; char str1[13][4] = { "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou","0" }; string str2[13] = { "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec","tret" }; getline(cin, s); int num = 0; if (s[0] >= 97) { string s1; string s2; //进行分段,从空格开始,分成两段 if (s[3] == ' ') { //将前三项给s1 for (int i = 0; i < 3; i++) { s1[i] = s[i]; } s1[3] = ' '; //后三项给s2 for (int i = 4; i < 8; i++) { s2[i - 4] = s[i]; } cout << "s1 = " << s1 << endl; cout << "s2 = " << s2 << endl; } else { s2 = s; cout << "s2 = " << s2 << endl; } for (int i = 1; i < 14; i++) { if (s1 == str1[i - 1]) { num += i * 13; } else if (s2 == str2[i - 1]) { if (i == 13) { i = 0; } num += i * 1; break; } } cout << num << endl; num = 0; } }
string subscript out of range(未解决)
查阅资料,得出结论,bug原因应该是数组越界
事实上,问题出在了给c++类型的字符串进行中断赋值0的问题上,即对已经封装好的c++类即对string类使用 强制终止是无用的
因为string不因 的出现而终止,其终止取决于封装在string类内的函数



