在C++ Win32控制台应用程序中输出unicode符号π和≈。

6

我对编程还比较陌生,但似乎 ASCII 标准字符集中并没有包含 π(pi) 符号。

我想知道是否有办法让控制台输出 π 符号,以便精确表达某些数学公式的答案。


1
你的控制台字体是什么?只需使用CMD.EXE进行检查。它因Windows版本而异,可以自定义。 - MSalters
3个回答

3

除了使用STL之类的其他方法,我不是很确定,但是你可以使用Win32来完成这个操作,使用WriteConsoleW

HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
LPCWSTR lpPiString = L"\u03C0";

DWORD dwNumberOfCharsWritten;
WriteConsoleW(hConsoleOutput, lpPiString, 1, &dwNumberOfCharsWritten, NULL);

1
我正在学习这方面的知识,如果我有错误,请纠正。
看起来这是一个三步骤的过程:
1. 使用cout、cin、string等宽版本。例如:wcout、wcin、wstring。 2. 在使用流之前将其设置为Unicode友好模式。 3. 配置目标控制台以使用Unicode兼容字体。
现在你应该可以使用那些时髦的åäö了。
示例:
#include <iostream>
#include <string>
#include <io.h>

// We only need one mode definition in this example, but it and several other
// reside in the header file fcntl.h.

#define _O_WTEXT        0x10000 /* file mode is UTF16 (translated) */
// Possibly useful if we want UTF-8
//#define _O_U8TEXT       0x40000 /* file mode is UTF8  no BOM (translated) */ 

void main(void)
{
    // To be able to write UFT-16 to stdout.
    _setmode(_fileno(stdout), _O_WTEXT);
    // To be able to read UTF-16 from stdin.
    _setmode(_fileno(stdin), _O_WTEXT);

    wchar_t* hallå = L"Hallå, värld!";

    std::wcout << hallå << std::endl;

      // It's all Greek to me. Go UU!
    std::wstring etabetapi = L"η β π";

    std::wcout << etabetapi << std::endl;

    std::wstring myInput;

    std::wcin >> myInput;

    std:: wcout << myInput << L" has " << myInput.length() << L" characters." << std::endl;

    // This character won't show using Consolas or Lucida Console
    std::wcout << L"♔" << std::endl;
}

1

微软CRT不太懂Unicode,因此可能需要绕过它并直接使用WriteConsole()。我假设您已经编译为Unicode,否则您需要明确使用WriteConsoleW()


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接