用宽字符写得一个英语单词练习

内容分享11小时前发布
0 0 0

目前C语言的教材,都是用ANSI C写得,实则工作后,我们更多的是用宽字符,所以我写了一个宽字符的实例。

_T()和L的区别: _T() 是定义了unicode就是宽字符,没有就是ANSI,L””是不管什么时候都是宽字符。

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。

//

#include “stdafx.h”

#include “iostream”

using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{

TCHAR *word[] = {_T(“one”),_T(“two”),_T(“three”),_T(“four”)};

char daan[80];

for (int i = 0; i < 4; i++)

{

wcout <<“题目是:”<< word[i] << endl;

wcout<<“答案是:”;

gets_s((char*)daan, 80);//由于没有输入宽字符串的,所以只好用窄字符先输入,然后再转让换成宽的。

size_t convertedChars = 0;//自定义宽字符的结束符

size_t charNum = strlen(daan)+1;//要转得是多少个

wchar_t* dest = new wchar_t[charNum];//开辟结果空间

mbstowcs_s(&convertedChars, dest, charNum, daan, _TRUNCATE);

if (_tcscmp(word[i], dest) == 0) //_tcscmp是字符的比较

wcout << “OK!” << endl;

else wcout << “error” << endl;

delete[] dest;

}

fflush(stdin);

getchar();

return 0;

}

© 版权声明

相关文章

暂无评论

none
暂无评论...