本题要求你实现一个稍微更值钱一点的 AI 英文问答程序,规则是:
1.无论用户说什么,首先把对方说的话在一行中原样打印出来;
2.消除原文中多余空格:把相邻单词间的多个空格换成 1 个空格,把行首尾的空格全部删掉,把标点符号前面的空格删掉;
3.把原文中所有大写英文字母变成小写,除了 I;
4.把原文中所有独立的 can you、could you 对应地换成 I can、I could—— 这里“独立”是指被空格或标点符号分隔开的单词;
5.把原文中所有独立的 I 和 me 换成 you;
6.把原文中所有的问号 ? 换成惊叹号 !;
7.在一行中输出替换后的句子作为 AI 的回答。
输入格式:
输入首先在第一行给出不超过 10 的正整数 N,随后 N 行,每行给出一句不超过 1000 个字符的、以回车结尾的用户的对话,对话为非空字符串,仅包括字母、数字、空格、可见的半角标点符号。
输出格式:
按题面要求输出,每个 AI 的回答前要加上 AI: 和一个空格。
输入样例:
6
Hello ?
Good to chat with you
can you speak Chinese?
Really?
Could you show me 5
What Is this prime? I,don 't know
输出样例:
Hello ?
AI: hello!
Good to chat with you
AI: good to chat with you
can you speak Chinese?
AI: I can speak chinese!
Really?
AI: really!
Could you show me 5
AI: I could show you 5
What Is this prime? I,don 't know
AI: what Is this prime! you,don't know
暴力还是挺好的
#include <iostream>
using namespace std;
int main() {
int n;
int a;
char c;
cin >> n;
string f, m;
scanf("%c", &c);
for (int j = 0; j < n; ++j) {
a=0;
m.clear();
f.clear();
getline(cin, f);
cout << f<<endl;
cout<<"AI: ";
//消去行首空格(0)
if (f[a] == ' ') {
while (f[a] == ' ') {
unsigned int p=0;
p=(unsigned int) (a);
f.erase(p,1);
}
}
int len=f.length()-1;
//消去行尾空格(len)
if(f[len]==' '){
while(f[len]== ' '){
unsigned int p=0;
p=(unsigned int) (len);
f.erase(p,1);
len--;
}
}
for (int i = a; i <= len; ++i) {
//将所有的大写字母变为小写字母(除I以外)
if (f[i] >= 'A' && f[i] <= 'Z' && f[i] != 'I') {
f[i] = char(f[i] + 32);
}
//搜索所有连续空格并删除,每删除一次string f长度减一
if (f[i] == ' ') {
while (f[i+1] == ' ') {
unsigned int p=0;
p=(unsigned int) (i+1);
f.erase(p,1);
len--;
}
}
}
for (int i = a; i <= len; ++i) {
//将标点符号前面的相连空格舍去
if (f[i] == ' ') {
if(f[i+1]<'0' ||(f[i+1]>'9' && f[i+1]<'A') || (f[i+1]>'Z' && f[i+1]<'a') || f[i+1]>'z'){}
else
m += f[i];
}
else if(f[i]=='?'){
f[i]='!';
m += f[i];
}
//假如string f简化后,前1个字符为I,则在string m 加you
else if(f[i]=='I' && (f[i-1]>'z' || (f[i-1]<'a' && f[i-1]!='I')) && (f[i+1]>'z' || (f[i+1]<'a' && f[i+1]!='I'))){
m+="you";
}
//假如string f简化后,前2个字符为me,则在string m 加you
else if(f[i]=='m' && f[i+1]=='e' && (f[i-1]>'z' || (f[i-1]<'a' && f[i-1]!='I')) && (f[i+2]>'z' || (f[i+2]<'a' && f[i+2]!='I'))){
i++;
m+="you";
}
//假如string f简化后,前7个字符为can you,则在string m 加I can
else if(f[i]=='c' && f[i+1]=='a' && f[i+2]=='n' && f[i+3]==' ' && f[i+4]=='y' && f[i+5]=='o' && f[i+6]=='u' && (f[i+7]>'z' || (f[i+7]<'a' && f[i+7]!='I')) && (f[i-1]>'z' || (f[i-1]<'a' && f[i-1]!='I'))){
m += "I can";
i+=6;
}
//假如string f简化后,前9个字符为could you,则在string m 加I could
else if(f[i]=='c' && f[i+1]=='o' && f[i+2]=='u' && f[i+3]=='l' && f[i+4]=='d' && f[i+5]==' ' && f[i+6]=='y' && f[i+7]=='o' &&f[i+8]=='u' &&(f[i+9]>'z' || (f[i+9]<'a' && f[i+9]!='I')) &&(f[i-1]>'z' || (f[i-1]<'a' && f[i-1]!='I'))){
m += "I could";
i+=8;
}
else{
m += f[i];
}
}
if(j!=n-1)
cout<<m<<endl;
else
cout<<m;
}
return 0;
}