读取混合unicode字符和整数的输入

时间:2018-07-29 08:36:33

标签: c++ unicode wchar

我正在尝试从用户那里读取以下输入:

3
5 斤 2 两
7 斤 3 两
6 斤

解决此问题:https://uva.onlinejudge.org/external/125/12555.pdf

但是,到目前为止,我还没有成功。我已经在Google上搜索了很多,但是我无法将其组合在一起,并且每次都会得到错误的输入。此外,我不确定控制台是否支持Unicode,是否正确传递了输入信息,这又对我来说是一个负担,因为我对Unicode的使用已经不确定。请帮忙。

我尝试过:

int main()
{
    int t,a,b;
    wchar_t ignore;
    float res;

    cin >> t; //number of test cases
    for(int k=1;k<=t;k++)
    {
        a=b=0;
        cin >> a;
        wcin >> ignore;

        if(cin.peek() != '\n')
        {
            cin >>  b;
            wcin >> ignore;
        }


        cout << "a : " << a << " b : " << b << endl;
    }
    return 0;
}

我的输出:

a : 5 b : 0
a : 0 b : 0
a : 0 b : 0

编辑 我也尝试了一些变化,但是输入仍然错误:

int main()
{
    int t,a,b;
    wchar_t ignore;
    float res;

    wcin >> t; //number of test cases
    cout << t << endl;
    for(int k=1;k<=t;k++)
    {
        a=b=0;
        wcin >> a;
        wcin >> ignore;

        if(wcin.peek() != '\n')
        {
            wcin >>  b;
            wcin >> ignore;
        }


        cout << "a : " << a << " b : " << b << endl;
    }
    return 0;
}

EDIT2 这在Ubuntu机器上对我有用,但是在线法官仍然拒绝我的解决方案。同样在Windows bash上,我仍然得到错误的输出:

#include <iostream>
#include <locale>
#include <clocale>

using namespace std;

int main()
{
    int t,a,b;
    wchar_t ignore;
    std::locale loc ("");
    std::locale::global (loc);
    std::setlocale(LC_ALL, NULL);
    std::setlocale(LC_ALL, "");    
    wcin.imbue (loc);

    wcin >> t; //number of test cases
    for(int k=1;k<=t;k++)
    {
        a=b=0;
        wcin >> a;
        wcin >> ignore;

        if(wcin.peek() != L'\n')
        {
            wcin >>  b;
            wcin >> ignore;
        }


        cout << "Case " << k << ": " << a*0.5+b*0.05 << endl;
    }
    return 0;
}

0 个答案:

没有答案
相关问题