自编的从键盘读取数据的函数
程序代码:
#include <conio.h> #include <string.h> #include "get.h" void Print(wchar_t cBuffer[]); void Print(int iVar); // 主程序入口 void main(void) { wchar_t cText[1024]; int iVar; GET Get; Print(L"请输入50个字符以内的字符串: "); if (Get.Read(cText, 50)) { Print(L"\n您输入的字符串是: "); Print(cText); } else { Print(L"\n== 输入中断 =="); } Print(L"\n\n请输入10个字符以内的整数: "); iVar = Get.Read(10); Print(L"\n您输入的整数是: "); Print(iVar); Print(L"\n\n按Esc键结束程序……"); while (_getwch() != K_ESC) { ; } } // 输出字符串 void Print(wchar_t cBuffer[]) { while (*cBuffer) { _putwch(*cBuffer); cBuffer++; } } // 输出整数 void Print(int iVar) { wchar_t Buffer[256]; unsigned int nCount; bool IsNegative; size_t Length = 0; if (iVar != 0) { IsNegative = (iVar < 0); if (IsNegative) { iVar = -iVar; } Length = 0; nCount = 0; while (iVar > 0) { nCount++; if (nCount > 3) { Buffer[Length++] = ','; nCount = 1; } Buffer[Length] = '0' + iVar % 10; iVar /= 10; Length++; } if (IsNegative) { Buffer[Length++] = '-'; } } Buffer[Length] = '\0'; _wcsrev(Buffer); Print(Buffer); }