0

我正在做 UVa 问题 10082,我正在尝试读取一些示例输入来测试我的解决方案。但是,当我阅读文本时'''CCC,它会输出;;XXX. 请注意,只有 2 个分号,而应该有 3 个,因为输入中有 3 个单引号。为什么 getline() 忽略第一个单引号?

这是我的代码:

#include <iostream>
#include <string>

using namespace std;

char mapToWertyu(char c)
{
    char newC = c;
    char wertyu[] = {'1','2','3','4','5','6','7','8','9','0','-','=',
                     'Q','W','E','R','T','Y','U','I','O','P','[',']','\\',
                     'A','S','D','F','G','H','J','K','L',';','\'',
                     'Z','X','C','V','B','N','M',',','.','/'};
    char qwerty[] = {'~','1','2','3','4','5','6','7','8','9','0','-','=',
                     'Q','W','E','R','T','Y','U','I','O','P','[',']','\\',
                     'A','S','D','F','G','H','J','K','L',';','\'',
                     'Z','X','C','V','B','N','M',',','.','/'};
    if(c=='A' || c=='Q' || c=='Z')
        return c;

    for(int i = 0; i < 47; ++i)
    {
        if(wertyu[i]==c)
        {
            newC = qwerty[i];
            break;
        }
    }
    return newC;
}

int main()
{
    string input;
    while(cin.peek()!=-1)
    {
        getline(cin,input);
        for(int i = 0; i < input.length(); ++i)
        {
            if(input[i]!='\\')
                cout << mapToWertyu(input[i]);
        }
        cin.ignore(1,'\n');
        cout << endl;
    }
    return 0;
}
4

1 回答 1

1

因为你告诉它。std::cin.ignore( 1, '\n' )如果不忽略一个角色, 你应该怎么做。std::getline 提取'\n'字符,即使它没有将其放入字符串中。

对于其余部分,您没有正确输入。对于初学者,std::cin.peek()将返回范围[0...UCHAR_MAX]或中的整数EOFEOF通常被定义为 -1,但这当然不能保证。但更一般地说:为什么不使用通常的成语:

while ( std::getline( std::cin, input ) ) {
    //  ...
}

mapToWertyu您还可以在每次调用它时构造数组。这绝对不是你想要做的。您可以只使用一个静态数组,直接由字符索引,这确实使程序依赖于编码。但是,要使用两个数组:

static char const wertyu[] = { ... };
static char const qwerty[] = { ... };
char const* pos = std::find( std::begin( wertyu ), std::end( wertyu ), c );
return pos == std::end( wertyu )
    ? c
    : qwerty[ pos - std::begin( wertyu ) ];

以更简单的方式解决问题。(并且不需要特殊情况'A','Q''Z'。如果您不想对其进行转码,请不要将它们放在表中。)

或者...

struct Map
{
    char from;
    char to;
};
static Map const map[] =
{
    { '1', '~' },
    { '2', '1' },
    //  ...
};
Map const* pos = std::find_if( std::begin( map ), std::end( map ),
                    []( char ch ) { return c == ch; } );
return pos == std::end( map )
    ? c
    : pos->to;

这具有使精确映射可见的优点。

或者,如果您 100% 确定您永远不必担心线程:

struct Map
{
    char from;
    char to;
};
static Map map[] =
{
    { '1', '~' },
    { '2', '1' },
    //  ...
    {  0,   0  }
};
Map* sentinal = std::end( map ) - 1;
sentinal->to = sentinal->from = c;
Map const* pos = std::find_if( std::begin( map ), std::end( map ),
                    []( Map const& entry ) { return c == entry.from; } );
return pos->to;

通过插入哨兵,您可以确保找到该条目。

或者您可以对地图进行排序,然后使用std::lower_bound.

mapToWertyu另外,当它映射到查询时,为什么要调用该函数?

于 2013-11-04T19:19:44.173 回答