首页 > 代码库 > 输入字符的分类和Byte类型数字的比较

输入字符的分类和Byte类型数字的比较

#include <stdio.h>
#include <string.h>
#include <windows.h>
BOOL charcmp(byte by[],int len,char ch[],int lensc,int &num)
{
bool rtn = FALSE;
int n = 0;
bool isfirst = TRUE;
int j = 0;
if ((len != 0)&&(len < lensc))
{
for (int i = 0; i<= lensc-len;i++)
{
for(j = 0;j<len;j++)
{
if ( ch[i+j] !=(char)by[j])
{
rtn = FALSE;
isfirst = true;
n = 0;
num = -1;
break;
}
else
{
rtn = TRUE;
if (isfirst)
{
n = i;
num = i;
isfirst = FALSE;
}
}
}
if (len <= j)
{
return rtn;
}
}
}
else
{
rtn = FALSE;
}
return rtn;
}
void main()
{
char ch[500];
memset(ch,0,500);
byte by[8] = {0};
by[0] = 0X01;
by[1] = 0x04;
by[2] = 0x00;
by[3] = 0x00;
by[4] = 0x00;
by[5] = 0x08;
by[6] = 0xF1;
by[7] = 0xCC;
byte by1[8] = {0};
by1[0] = 0X0A;
by1[1] = 0x04;
by1[2] = 0x00;
by1[3] = 0x02;
by1[4] = 0x00;
by1[5] = 0x08;
by1[6] = 0x51;
by1[7] = 0x77;
byte by2[8] = {0};
by2[0] = 0X03;
by2[1] = 0x04;
by2[2] = 0x01;
by2[3] = 0x00;
by2[4] = 0x00;
by2[5] = 0x10;
by2[6] = 0xF1;
by2[7] = 0xD8;
byte by3[2] = {0};
by2[0] = 0X11;
by2[1] = 0x04;
memcpy(ch,by,8);
memcpy(ch+8,by1,8);
memcpy(ch+16,by2,8);
int num = -1;
bool flg = charcmp(by,8,ch,24,num);
if (flg)
{
int l = num;
printf("%d\n",num);
}
flg = charcmp(by1,8,ch,24,num);
if (flg)
{
int m = num;
printf("%d\n",num);
}
flg = charcmp(by2,8,ch,24,num);
if (flg)
{
int n = num;
printf("%d\n",num);
}
flg = charcmp(by3,8,ch,24,num);
if (flg)
{
int p = num;
printf("%d\n",num);
}
}


#include<iostream>

using namespace std;


int main()
{
    int state = 0;
    int nch = 0,nspace = 0,nother = 0,ndigit = 0;
    int ch;
    while((ch = getchar()) != EOF && ch != ‘\n‘)
    {
if(isalpha(ch))
   nch++;
else if(isdigit(ch))
   ndigit++;
else if(isspace(ch))
   nspace++;
else
   nother++;


    }
    cout << "数字: " << ndigit << endl;
    cout << "空格: " << nspace << endl;
    cout << "字母: " << nch << endl;
    cout << "其他: " << nother << endl;
    return 0;

}


输入字符的分类和Byte类型数字的比较