📄 globalfunc.cpp
字号:
return INPUT_ERR;
}
if(month==1 || month==3 || month==5 ||
month==7 || month==8 || month==10 || month==12){
if(day <= 31){
return SUCC;
}
else{
return INPUT_ERR;
}
}
if(month==4 || month==6 || month==9 || month==11){
if(day<=30){
return SUCC;
}
else{
return INPUT_ERR;
}
}
//检查是否闰年:400整除,或者4整除但不被100整除
if( ((year%400) == 0 ) || ( (year%4==0) && (year%100!=0) ) ){
flag = 1;
}
else {
flag = 0;
}
if(flag && day>29){
return INPUT_ERR;
}
if(!flag && day>28){
return INPUT_ERR;
}
return SUCC;
}
///检查字符串输入的日期格式是否正确
/**
* StrDate : 待检查的日期,字符串格式 \n
* 返回 : SUCC/其他
*/
int CheckStrDateFormat(const unsigned char *StrDate)
{
//Check Input
if(StrDate==NULL){
return INPUT_ERR;
}
if(strlen((char*)StrDate)!=8){
return INPUT_ERR;
}
unsigned char buf_year[5];
unsigned char buf_month[3];
unsigned char buf_day[3];
memcpy(buf_year, StrDate, 4); buf_year[4] = '\0';
memcpy(buf_month, StrDate+4, 2); buf_month[2] = '\0';
memcpy(buf_day, StrDate+6, 2); buf_day[2] = '\0';
int year = atoi((char*)buf_year);
int month = atoi((char*)buf_month);
int day = atoi((char*)buf_day);
int flag;
if( year < 1900 ){
return INPUT_ERR;
}
if( month > 12 || month <= 0 ){
return INPUT_ERR;
}
if( day<=0 ){
return INPUT_ERR;
}
if(month==1 || month==3 || month==5 ||
month==7 || month==8 || month==10 || month==12){
if(day <= 31){
return SUCC;
}
else{
return INPUT_ERR;
}
}
if(month==4 || month==6 || month==9 || month==11){
if(day<=30){
return SUCC;
}
else{
return INPUT_ERR;
}
}
//检查是否闰年:400整除,或者4整除但不被100整除
if( ((year%400) == 0 ) || ( (year%4==0) && (year%100!=0) ) ){
flag = 1;
}
else {
flag = 0;
}
if(flag && day>29){
return INPUT_ERR;
}
if(!flag && day>28){
return INPUT_ERR;
}
return SUCC;
}
///检查字符串表示的整数格式是否正确,正确时返回相应的整数
/**
* IntStr : 待检查的字符串表示的整数 \n
* Int : 返回输入的整数 \n
* 返回 : SUCC/FAIL
*/
int CheckIntInput(const char *IntStr, unsigned int &Int)
{
unsigned int i;
char buf_tmp[20];
const char *pBuf = NULL;
if(IntStr==NULL){
return FAIL;
}
pBuf = IntStr;
while(*pBuf=='0'){//略过前'0'
pBuf ++;
}
if(strlen(IntStr)==0 || strlen(pBuf)>10){
return FAIL;
}
//检查是否全是数字
for(i=0;i<strlen(IntStr);i++){
if(!isdigit(IntStr[i])){
return FAIL;
}
}
//检查是否越界
sprintf(buf_tmp, "%u", 0xFFFFFFFF);
if(strlen(pBuf)==10 && strcmp(buf_tmp, pBuf)<0){
return FAIL;
}
Int = 0;
while(*pBuf!=0){
Int = Int * 10 + ((*pBuf) -'0');
pBuf ++;
}
return SUCC;
}
///检查字符串表示的十六进制数格式是否正确,正确时返回相应的一半字节的十六进制缓冲区
/**
* HexStr : 待检查的字符串表示的十六进制字符串, 如"1234" \n
* HexCount : 待检查的十六进制数的字节数, 如2 \n
* HexBuf : 返回实际的十六进制串, 如0x12, 0x34 \n
* 返回 : SUCC/FAIL
*/
int CheckHexInput(const char *HexStr, unsigned int HexCount, unsigned char *HexBuf)
{
unsigned int i;
char buf[500];
if(HexStr==NULL){
return FAIL;
}
if(strlen(HexStr)!=HexCount*2){
return FAIL;
}
memcpy(buf, HexStr, HexCount * 2);
//一边检查,一边转换成十六进制数
for(i=0;i<HexCount*2;i++){
if(!isxdigit(buf[i])){
return FAIL;
}
if(buf[i]>='A' && buf[i]<='F'){
buf[i] = buf[i] - 'A' + 0xA;
}
else if(buf[i]>='a' && buf[i]<='f'){
buf[i] = buf[i] - 'a' + 0xA;
}
else{
buf[i] = buf[i] - '0';
}
}
for(i=0;i<HexCount;i++){
HexBuf[i] = (buf[2*i]<<4) + buf[2*i+1];
}
return SUCC;;
}
///检查字符串表示的BCD数据串格式是否正确,正确时返回相应的一半字节的十六进制缓冲区
/**
* BcdStr : 待检查的字符串表示的BCD字符串, 如"1234" \n
* BcdCount : 待检查的BCD的字节数, 如2 \n
* BcdBuf : 返回实际的十六进制串, 如0x12, 0x34 \n
* 返回 : SUCC/FAIL
*/
int CheckBcdInput(const char *BcdStr, unsigned int BcdCount, unsigned char *BcdBuf)
{
unsigned int i;
char buf[500];
if(BcdStr==NULL){
return FAIL;
}
if(strlen(BcdStr)!=BcdCount*2){
return FAIL;
}
memcpy(buf, BcdStr, BcdCount * 2);
//一边检查,一边转换成十六进制数
for(i=0;i<BcdCount*2;i++){
if(!isdigit(buf[i])){
return FAIL;
}
buf[i] = buf[i] - '0';
}
for(i=0;i<BcdCount;i++){
BcdBuf[i] = (buf[2*i]<<4) + buf[2*i+1];
}
return SUCC;;
}
///检查字符串表示的金额格式是否正确,正确时返回相应的以分为单位的金额
/**
* AmountBuf : 待检查的字符串格式的金额 \n
* Amount : 返回实际的以分为单位的金额 \n
* 返回 : SUCC/FAIL
*/
int CheckAmountInput(const char *AmountBuf, unsigned int &Amount)
{
unsigned int i;
int len_int, dotflag, len_flt;
const char *pBuf = NULL;
char buf_tmp1[500], buf_tmp2[20];
//检查输入
if(strlen(AmountBuf)==0){
return FAIL;
}
//分析整数、小数部分的长度
len_int = 0;
len_flt = 0;
dotflag = 0;
for(i=0;i<strlen(AmountBuf);i++){
if(AmountBuf[i]=='.'){
if(dotflag==1 || len_int==0){//未输入整数部分或已输入小数点
return FAIL;
}
else{
dotflag = 1;
}
}
else{
if(!isdigit(AmountBuf[i])){//输入的不是数字
return FAIL;
}
if(dotflag==0){
len_int ++;
}
else{
len_flt ++;
if(len_flt>2){//小数部分超过2位
return FAIL;
}
}
}
}
//去掉小数点后复制到新缓冲区,小数部分不足两位则补0
memset(buf_tmp1, 0, sizeof(buf_tmp1));
memcpy(buf_tmp1, AmountBuf, len_int);
memcpy(buf_tmp1+len_int, AmountBuf+len_int+1, len_flt);
for(i=0;i<(unsigned int)(2-len_flt);i++){
buf_tmp1[len_int+len_flt+i] = '0';
}
//检查输入是否越界
pBuf = buf_tmp1;
while(*pBuf=='0'){//略过前'0'
pBuf ++;
}
if(strlen(pBuf)>10){
return FAIL;
}
sprintf(buf_tmp2, "%u", 0xFFFFFFFF);
if(strlen(pBuf)==10 && strcmp(buf_tmp2, pBuf)<0){
return FAIL;
}
Amount = 0;
while(*pBuf!=0){
Amount = Amount * 10 + ((*pBuf) -'0');
pBuf ++;
}
return SUCC;
}
///检查字符串表示的项目数量格式是否正确,正确时返回数量和小数部分的长度
/**
* FltNumBuf : 以字符串格式输入的总参要求的数量 \n
* Num : 返回实际的去掉小数点后的数量 \n
* LenOfFloatPart : 返回实际的小数部分的长度 \n
* 返回 : SUCC/FAIL
*/
int CheckFltNumInput(const char *FltNumBuf, unsigned int &Num, unsigned char &LenOfFloatPart)
{
int len_int, dotflag, len_flt;
int j;
unsigned int i;
char buf_tmp1[500], buf_tmp2[200];
const char *pBuf = NULL;
if(FltNumBuf==NULL){
return FAIL;
}
if(strlen(FltNumBuf)==0){
return FAIL;
}
//分析整数部分和小数部分的长度
len_int = 0;
len_flt = 0;
dotflag = 0;
for(i=0;i<strlen(FltNumBuf);i++){
if(FltNumBuf[i]=='.'){
if(dotflag==1 || len_int==0){//未输入整数部分或已输入小数点
return FAIL;
}
else{
dotflag = 1;
}
}
else{
if(!isdigit(FltNumBuf[i])){//输入的不是数字
return FAIL;
}
if(dotflag==0){
len_int ++;
}
else{
len_flt ++;
}
}
}
//去掉小数点后复制到新缓冲区
memset(buf_tmp1, 0, sizeof(buf_tmp1));
memcpy(buf_tmp1, FltNumBuf, len_int);
memcpy(buf_tmp1+len_int, FltNumBuf+len_int+1, len_flt);
//去掉小数部分的尾0
if(dotflag==1 && len_flt!=0){
while(buf_tmp1[len_int+len_flt-1]=='0'){
buf_tmp1[len_int+len_flt-1] = 0;
len_flt--;
if(len_flt==0){
break;
}
}
}
//检查输入是否越界
pBuf = buf_tmp1;
j=1;
while(*pBuf=='0'){//略过前'0'
pBuf ++;
//if(j++==len_int+len_flt){ //判断输入的是否都是0
// return FAIL;
//}
}
if(strlen(pBuf)>8){
return FAIL;
}
sprintf(buf_tmp2, "%u", 0xFFFFFF);
if(strlen(pBuf)==8 && strcmp(buf_tmp2, pBuf)<0){
return FAIL;
}
Num = 0;
while(*pBuf!=0){
Num = Num * 10 + ((*pBuf) -'0');
pBuf ++;
}
LenOfFloatPart = len_flt;
return SUCC;
}
///检查输入的IP地址是否正确
/**
* IPBuf : 待检查的IP地址 \n
* 返回 : SUCC/FAIL
*/
int CheckIPInput(const char *IPBuf)
{
unsigned int i;
int j,k;
char buf[4][4];
if(IPBuf==NULL){
return FAIL;
}
if(strlen(IPBuf)==0){
return FAIL;
}
for(i=0;i<4;i++){
memset(buf[i], 0, sizeof(buf[i]));
}
//检查小数点的个数,并复制各段到相应缓冲区
j = 0;
k = 0;
for(i=0;i<strlen(IPBuf);i++){
if(IPBuf[i]=='.'){
if(i==0){
return FAIL;
}
j ++;
k = 0;
if(j>3){
return FAIL;
}
}
else{
if(!isdigit(IPBuf[i])){
return FAIL;
}
buf[j][k] = IPBuf[i];
k++;
if(k>3){
return FAIL;
}
}
}
if(j!=3){
return FAIL;
}
for(j=0;j<4;j++){
if(atoi(buf[j])>255){
return FAIL;
}
}
return SUCC;
}
///询问用户,根据询问方式,要求用户输入相应回答
/**
* Promt : 询问的提示信息 \n
* flag : YESNO, OKCANCEL, YESNOCANCEL或其它, 默认及其它为YESNO \n
* 返回 : YES/NO 或 OK/CANCLE 或 YES/NO/CANCEL \n
* 对于图形界面的版本 : \n
* 返回 : IDYES/IDNO, IDOK/IDCANCEL, IDYES/IDNO/IDCANCEL
*/
int QueryUser(const char *Promt, int flag)
{
if(flag==YESNOCANCEL){
HideProgressInfo();
return MyMessageBox(Promt, "询问", MB_ICONQUESTION | MB_YESNOCANCEL);
}
else if(flag==OKCANCEL){
HideProgressInfo();
return MyMessageBox(Promt, "询问", MB_ICONQUESTION | MB_OKCANCEL);
}
else{
HideProgressInfo();
return MyMessageBox(Promt, "询问", MB_ICONQUESTION | MB_YESNO);
}
}
///测试输入输出函数
void TestInputFunctions()
{
while(1){
/*
unsigned char HexBuf[50];
int HexCount = 8;
GetHexInput("请输入税控卡使用口令", HexCount, HexBuf);
PrintBuffer("口令", HexBuf, HexCount);
*/
/*
unsigned int Num;
GetIntInput("请输入发票号", Num);
printf("发票号:%u\n", Num);
*/
/*
unsigned int Amount;
GetAmountInput("请输入单价", Amount);
printf("单价:%u, %.2f\n", Amount, Amount/100.0);
*/
/*
unsigned int Num;
unsigned char LenOfFloat;
int i, power;
GetFltNumInput("请输入数量", Num, LenOfFloat);
power = 1;
for(i=0;i<LenOfFloat;i++){
power *= 10;
}
printf("数量:%u,%d, %.8f", Num, LenOfFloat, Num * 1.0 / power);
*/
/*
char IPBuf[500];
GetIPInput("请输入IP地址", IPBuf);
printf("IP地址:%s", IPBuf);
*/
}
}
//记录集与结构之间的相互转换
void Rec2Struct_DogInfo(CDogInfoSet *pdiSet, struct DogInfo &di)
{
}
void Struct2Rec_DogInfo(struct DogInfo di, CDogInfoSet *pdiSet)
{
}
void Rec2Struct_OwnerInfo(COwnerInfoSet *poiSet, struct OwnerInfo &oi)
{
}
void Struct2Rec_OwnerInfo(struct OwnerInfo oi, COwnerInfoSet *poiSet)
{
}
void Rec2Struct_ImmunityRec(CImmunityRecSet *pirSet, struct ImmunityRec &ir)
{
}
void Struct2Rec_ImmunityRec(struct ImmunityRec ir, CImmunityRecSet *pirSet)
{
}
void Rec2Struct_OwnerChangeRec(COwnerChangeRecSet *pocrSet, struct OwnerChangeRec &ocr)
{
}
void Struct2Rec_OwnerChangeRec(struct OwnerChangeRec ocr, COwnerChangeRecSet *pocrSet)
{
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -