-->
当前位置:首页 > 题库 > 正文内容

7-3 2017final英文语句格式简单检查 (30 分)

Luz4年前 (2021-03-08)题库1593
7-3 2017final英文语句格式简单检查 (30 分)

英文书写中,句首字母通常为大写,其余为小写,单词“I”除外,单词与单词之间用一个空格隔开,句中用“,”断句,句末用“.”结束,“,”和“.”与其前置单词间无需空格隔开。 Word等文本编辑器通常根据以上规则,对我们输入的英文语句进行自动修正。 请编写一个功能,可对输入的一句英文句子,根据以上规则,修订为正确格式后输入。 例如,对于输入的英文句子“This is an Example with one mistake.”, 由于单词“Example”中的字符"E"应该为小写"e",所以修订后输出该句子的正确格式 This is an example with one mistake.

注意:

1、 每组测试数据仅包括一个以字符“.”结束的英文句子。

2、 输入的英文句子中出现的字符包括二十六个大写英文字母(ASCII码65~90),二十六个小写英文字母(ASCII码97~122),“,”和“.”,空格字符。

输入格式:

以字符“."为结束字符的一个英文句子。

输出格式:

输入英文句子经过格式纠错后的输出。

输入样例:

This is an Example with one mistake.

输出样例:

This is an example with one mistake.
作者
YU
单位
福州大学
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
#include<iostream>
#include<string>
using namespace std;

int main()
{
    char *transform(char *);
    string str, result, space(" "), point("."); 
    bool flag(true);
    int pos(0);
    getline(cin, str);

    if('a' <= str[pos] && str[pos] <= 'z')
        result.append(transform(&str[pos]), 1);
    else
        result.append(&str[pos], 1);

    while(str[++pos] != '.')
    {
        if(!flag && str[pos] != ' ')
        {
            result.append(space);
            flag = true;
        }

        if(flag)
        {
            if(str[pos] == 'I' && (str[pos - 1] == ' ' || str[pos - 1] == ',') && (str[pos + 1] == ' ' || str[pos - 1] == ',' || str[pos - 1] == '.'))
            {
                result.append(&str[pos], 1);
            }
            else if(str[pos] == 'i' && (str[pos - 1] == ' ' || str[pos - 1] == ',') && (str[pos + 1] == ' ' || str[pos + 1] == ',' || str[pos + 1] == '.'))
            {
                result.append(transform(&str[pos]), 1);
            }
            else if('A' <= str[pos] && str[pos] <= 'Z')
            {
                result.append(transform(&str[pos]), 1);
            }
            else if(str[pos] == ',')
            {
                for(int i(1); str[pos + i] != '.'; i++)
                {
                    if(str[pos + i] == ' ')
                        continue;
                    else if(str[pos + i] == ',' || str[pos + i] == '.')
                    {
                        pos += i - 1;
                        break;
                    }
                    else
                    {
                        result.append(&str[pos], 1);
                        flag = false;
                        break;
                    }
                }
            }
            else if(str[pos] == ' ')
            {
                for(int i(1); str[pos + i] != '.'; i++)
                {
                    if(str[pos + i] == ' ')
                        continue;
                    else if(str[pos + i] == ',' || str[pos + i] == '.')
                    {
                        pos += i - 1;
                        break;
                    }
                    else
                    {
                        result.append(space);
                        pos += i - 1;
                        break;
                    }
                }
            }
            else
            {
                result.append(&str[pos], 1);
            }
        }
    }

    result.append(point);
    cout << result;
    return 0;
}

char *transform(char *ch)
{
    if('A' <= *ch && *ch <= 'Z')
        *ch += 32;
    else if('a' <= *ch && *ch <= 'z')
        *ch -= 32;

    return ch;
}


发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。