1、其中需要注意的是在输入中文字符串格式处理时,使用getline(cin,string);能够较好的保存格式,而使用cin>>string;会导致一些格式错误(空格会被截断,而getline能够保留空格,遇到换行符才被截断)。 2、该题需要保证在判断“ ”时要进行flag值的及时修改,否则会导致例如“ ",这种格式无法被正确识别的问题。 3、本题是可以输入多段的需要使用while循环进行输入操作。 4、在判断双引号的时候 ,正确表达方式为:
string s=""; s=s+a[i]+a[i+1];s不能用""代替,也不能写成s+=的结合方式,都会使得中文字符没有意义。
题目入口
#include<iostream> #include<string> using namespace std; int main() { string a; int flag = 1; while(getline(cin,a)){ flag = 1; for (int i = 0; i < a.size(); i++) { // cout<<a.size(); string s = ""; s = s + a[i] + a[i + 1]; if (s == "“"|| s == "”") { flag = !flag; } if (a[i] == ',') { cout << ","; } else if (a[i] == '.') { cout << "。"; } else if (a[i] == '!') { cout << "!"; } else if (a[i] == '"') { if (flag == 1) { cout << "“"; flag = 0; } else { cout << "”"; flag = 1; } } else if (a[i] == '<'&&a[i + 1] == '<') { cout << "《"; i++; } else if (a[i] == '>'&&a[i + 1] == '>') { cout << "》"; i++; } else if (a[i] == '?') { cout << "?"; } else { cout << a[i]; } } cout << endl; } return 0; }