呼叫《赵四老师》

vloso 2018-06-06 02:51:30
加精
#include<iostream>
#include <string.h>
#include<string>
using namespace std;
int main()
{
char str[100];
for(int i=0;i<10;i++)
{
cin>>str;
while(true)
{
if(int(str[0])>=0)
{
cout<<"不是中文请重新输入:";
cin>>str;
}
else
{
cout<<"是中文";
break;
}
}
return 0;
}
}


之前贴子已结,看了你的代码 结合一下情况,请看看这个可行不,,《《代码的意思是拒绝输入非中文字符》》

代码原理是通过ascii码 进行判断,中文为负数其他为正数

还有个问题 经过测试好像不能 str[0]=str,str[1]=str;进行数组保存数据?


...全文
3496 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
fsdfdsgtfertfe 2018-06-19
  • 打赏
  • 举报
回复
if(int(str[0])>=0) 感觉最应该改的是这句呢str[0] 改为str[i] 才能保存
N阶魔方 2018-06-14
  • 打赏
  • 举报
回复
世界上没有什么事不是一顿烧烤解决不了的,如是有,那就是两顿。
赵4老师 2018-06-08
  • 打赏
  • 举报
回复
引用 26 楼 sugar13 的回复:
[quote=引用 7 楼 zhao4zhong1 的回复:] 运行#3楼代码生成的goal.bmp转为goal.gif后:
看到这图真是眼熟啊,1楼的代码是从我这收藏的吧[/quote] 虽然代码确实是我从你处收藏的,但你当初写这段代码的动机是在我怂恿下才有的。你忘了吗?
失散糖 2018-06-08
  • 打赏
  • 举报
回复
引用 7 楼 zhao4zhong1 的回复:
运行#3楼代码生成的goal.bmp转为goal.gif后:
看到这图真是眼熟啊,1楼的代码是从我这收藏的吧
shiter 2018-06-07
  • 打赏
  • 举报
回复
问题上升到了哲学高度
引用 15 楼 zhao4zhong1 的回复:
[quote=引用 14 楼 zhao4zhong1 的回复:] 到目前为止,应该还没人能100%准确地定义啥叫汉字。我觉得。
啥叫中文,啥叫非中文,一样。[/quote]
赵4老师 2018-06-07
  • 打赏
  • 举报
回复
引用 14 楼 zhao4zhong1 的回复:
到目前为止,应该还没人能100%准确地定义啥叫汉字。我觉得。
啥叫中文,啥叫非中文,一样。
赵4老师 2018-06-07
  • 打赏
  • 举报
回复
到目前为止,应该还没人能100%准确地定义啥叫汉字。我觉得。
赵4老师 2018-06-07
  • 打赏
  • 举报
回复
日语中的汉字呢?
赵4老师 2018-06-07
  • 打赏
  • 举报
回复
全角字母,比如 A 到底是不是汉字?
赵4老师 2018-06-07
  • 打赏
  • 举报
回复
引用 21 楼 DelphiGuy 的回复:
如果windows的话很容易判断:

#include <stdio.h>
#include <windows.h>
 
#define GB2312 936

int main()
{
  char buf[256];
  char *p = buf;
  SetConsoleCP(GB2312);
  SetConsoleOutputCP(GB2312);
  scanf("%s", buf);
  while (*p++)
    if (IsDBCSLeadByteEx(GB2312, *p)) p++;
    else
    {
      printf("不是中文请重新输入\n%s\n", buf);
      for (int i = 1; i < p - buf; i++) putchar(32);
      puts("^\n");
      break;
    }

  return 0;
}
用你这段代码判断一下#11楼的那个字试试。
  • 打赏
  • 举报
回复
如果windows的话很容易判断:

#include <stdio.h>
#include <windows.h>
 
#define GB2312 936

int main()
{
  char buf[256];
  char *p = buf;
  SetConsoleCP(GB2312);
  SetConsoleOutputCP(GB2312);
  scanf("%s", buf);
  while (*p++)
    if (IsDBCSLeadByteEx(GB2312, *p)) p++;
    else
    {
      printf("不是中文请重新输入\n%s\n", buf);
      for (int i = 1; i < p - buf; i++) putchar(32);
      puts("^\n");
      break;
    }

  return 0;
}
ooolinux 2018-06-07
  • 打赏
  • 举报
回复
引用 19 楼 zhao4zhong1 的回复:
[quote=引用 18 楼 u010165006 的回复:] [quote=引用 14 楼 zhao4zhong1 的回复:] 到目前为止,应该还没人能100%准确地定义啥叫汉字。我觉得。
老赵怎么定义汉字?[/quote] 甲骨文是一种汉字。[/quote]
赵4老师 2018-06-07
  • 打赏
  • 举报
回复
引用 18 楼 u010165006 的回复:
[quote=引用 14 楼 zhao4zhong1 的回复:] 到目前为止,应该还没人能100%准确地定义啥叫汉字。我觉得。
老赵怎么定义汉字?[/quote] 甲骨文是一种汉字。
ooolinux 2018-06-07
  • 打赏
  • 举报
回复
引用 14 楼 zhao4zhong1 的回复:
到目前为止,应该还没人能100%准确地定义啥叫汉字。我觉得。
老赵怎么定义汉字?
啊大1号 2018-06-07
  • 打赏
  • 举报
回复
建议了解一下ANSI,GBK,UTF, UCS等编码
舉杯邀明月 2018-06-06
  • 打赏
  • 举报
回复
ooolinux 2018-06-06
  • 打赏
  • 举报
回复
老赵这次真货多~
赵4老师 2018-06-06
  • 打赏
  • 举报
回复
再供参考:
#pragma comment(lib,"user32")
#pragma comment(lib,"gdi32")
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
extern "C" HWND WINAPI GetConsoleWindow();
void HideTheCursor() {
    CONSOLE_CURSOR_INFO cciCursor;
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

    if(GetConsoleCursorInfo(hStdOut, &cciCursor)) {
        cciCursor.bVisible = FALSE;
        SetConsoleCursorInfo(hStdOut, &cciCursor);
    }
}
void ShowTheCursor() {
    CONSOLE_CURSOR_INFO cciCursor;
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

    if(GetConsoleCursorInfo(hStdOut, &cciCursor)) {
        cciCursor.bVisible = TRUE;
        SetConsoleCursorInfo(hStdOut, &cciCursor);
    }
}
int main() {
    HWND  hwnd;
    HDC   hdc;
    HFONT hfont;
    wchar_t wc[2];

    system("color F0");
    system("cls");
    HideTheCursor();
    hwnd  = GetConsoleWindow();
    hdc   = GetDC(hwnd);
    hfont = CreateFont(48,0,0,0,0,0,0,0,GB2312_CHARSET ,0,0,0,0,"宋体-方正超大字符集");
    SelectObject(hdc,hfont);
    wc[0]=0xD854u;
    wc[1]=0xDC00u;
    TextOutW(hdc,10,10,wc,2);
    DeleteObject(hfont);
    ReleaseDC(hwnd,hdc);
    getch();
    system("color 07");
    system("cls");
    ShowTheCursor();
    return 0;
}
#if 0
代理项或代理项对是一对共同表示单个字符的 16 位 Unicode 编码值。需要记住的关键一点是:
代理项对实际上是 32 位单个字符,不能再假定一个 16 位 Unicode 编码值正好映射到一个字符。

使用代理项对
代理项对的第一个值是高代理项,包含介于 U+D800 到 U+DBFF 范围内的 16 位代码值。
该对的第二个值是低代理项,包含介于 U+DC00 到 U+DFFF 范围内的值。通过使用代理项对,
16 位 Unicode 编码系统可以对已由 Unicode 标准定义的一百多万个其他字符 (220) 进行寻址。

在传递给 XmlTextWriter 方法的任何字符串中都可以使用代理项字符。不过,代理项字符在编写的
XML 中应该有效。例如,万维网联合会 (W3C) 建议不允许在元素或属性的名称中使用代理项字符。
如果字符串包含无效的代理项对,则引发异常。

另外,可以使用 WriteSurrogateCharEntity 写出与代理项对相对应的字符实体。字符实体以十六
进制格式写出,并用以下公式生成:

(highChar -0xD800) * 0x400 + (lowChar -0xDC00) + 0x10000

如果字符串包含无效的代理项对,则引发异常。下面的示例显示将代理项对作为输入的 WriteSurrogateCharEntity 方法。

C#复制
 // The following line writes 𐀀.
WriteSurrogateCharEntity ('\uDC00', '\uD800');
下面的示例生成一个代理项对文件,将其加载到 XmlReader 中,并用新的文件名保存文件。
然后,原始文件和新文件被加载回应用程序的 XML 文档对象模型 (DOM) 结构中以进行比较。

C#复制
 char lowChar, highChar;
char [] charArray = new char[10];
FileStream targetFile = new FileStream("SurrogatePair.xml",
      FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);

lowChar = Convert.ToChar(0xDC00);
highChar = Convert.ToChar(0xD800);
XmlTextWriter tw = new XmlTextWriter(targetFile, null);
tw.Formatting = Formatting.Indented;
tw.WriteStartElement("root");
tw.WriteStartAttribute("test", null);
tw.WriteSurrogateCharEntity(lowChar, highChar);
lowChar = Convert.ToChar(0xDC01);
highChar = Convert.ToChar(0xD801);
tw.WriteSurrogateCharEntity(lowChar, highChar);
lowChar = Convert.ToChar(0xDFFF);
highChar = Convert.ToChar(0xDBFF);
tw.WriteSurrogateCharEntity(lowChar, highChar);

// Add 10 random surrogate pairs.
// As Unicode, the high bytes are in lower
// memory; for example, word 6A21 as 21 6A.
// The high or low is in the logical sense.
Random random = new Random();
for (int i = 0; i < 10; ++i) {
      lowChar = Convert.ToChar(random.Next(0xDC00, 0xE000));
      highChar = Convert.ToChar(random.Next(0xD800, 0xDC00));
      charArray[i] = highChar;
      charArray[++i] = lowChar;
}
tw.WriteChars(charArray, 0, charArray.Length);

for (int i = 0; i < 10; ++i) {
      lowChar = Convert.ToChar(random.Next(0xDC00, 0xE000));
      highChar = Convert.ToChar(random.Next(0xD800, 0xDC00));
      tw.WriteSurrogateCharEntity(lowChar, highChar);
}

tw.WriteEndAttribute();
tw.WriteEndElement();
tw.Flush();
tw.Close();

XmlTextReader r = new XmlTextReader("SurrogatePair.xml");

r.Read();
r.MoveToFirstAttribute();
targetFile = new FileStream("SurrogatePairFromReader.xml",
       FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);

tw = new XmlTextWriter(targetFile, null);
tw.Formatting = Formatting.Indented;
tw.WriteStartElement("root");
tw.WriteStartAttribute("test", null);
tw.WriteString(r.Value);
tw.WriteEndAttribute();
tw.WriteEndElement();
tw.Flush();
tw.Close();

// Load both result files into the DOM and compare.
XmlDocument doc1 = new XmlDocument();
XmlDocument doc2 = new XmlDocument();
doc1.Load("SurrogatePair.xml");
doc2.Load("SurrogatePairFromReader.xml");
if (doc1.InnerXml != doc2.InnerXml) {
      Console.WriteLine("Surrogate Pair test case failed");
}
在使用 WriteChars 方法(一次写出一个缓冲区的数据)写出时,输入中的代理项对可能
会在一个缓冲区内被意外拆分。由于代理项值是定义完善的,如果 WriteChars 遇到来自
较低范围或者较高范围的 Unicode 值,它将该值标识为代理项对的一半。当遇到
 WriteChars 将导致从拆分代理项对的缓冲区写入的情况时,将引发异常。使用
  IsHighSurrogate 方法检查缓冲区是否以高代理项字符结束。如果缓冲区中的最后一个
  字符不是高代理项,可以将该缓冲区传递给 WriteChars 方法。

请参见
概念
使用 XmlTextWriter 创建格式正确的 XML
XmlTextWriter 的 XML 输出格式设置
XmlTextWriter 的命名空间功能

#endif
赵4老师 2018-06-06
  • 打赏
  • 举报
回复
完美的输入合法性验证是不存在的。
赵4老师 2018-06-06
  • 打赏
  • 举报
回复
再供参考:
#define _UNICODE 1
#pragma warning(disable:4305 4309)
#pragma comment(lib,"ole32")
#pragma comment(lib,"comsupp")
#include <cstdio>
#include <objbase.h>
#include <comip.h>
#include <mlang.h>
#include <tchar.h>

typedef _com_ptr_t<_com_IIID<IMultiLanguage3, &IID_IMultiLanguage3> > IMultiLanguage3Ptr;

int main() {
    CoInitialize(NULL);
    {
        IMultiLanguage3Ptr pML(CLSID_CMultiLanguage, NULL, CLSCTX_INPROC);
        char data[] = { 0xD6, 0xD0, 0xCE, 0xC4, 0xB1, 0xE0, 0xC2, 0xEB };
        int isize = sizeof(data);
        DetectEncodingInfo result[32];
        int result_count = sizeof(result) / sizeof(result[0]);
        HRESULT hr = pML->DetectInputCodepage(MLDETECTCP_NONE, 0, data, &isize, result, &result_count);
        if (!SUCCEEDED(hr))
        {
            fprintf(stderr, "Failed with 0x%x\n", hr);
            CoUninitialize();
            return hr;
        }
        for (int i = 0; i < result_count; i++)
        {
            WCHAR desc[100] = {0};
            pML->GetCodePageDescription(result[i].nCodePage, result[i].nLangID, desc, 100);
            printf("CP:%d (%S)\n", result[i].nCodePage, desc);
        }
    }
    CoUninitialize();
    return 0;
}
//CP:936 (Chinese Simplified (GB2312))
//
加载更多回复(8)

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧