c語言isdigit函數(shù)用法?
#include <stdio.h> #include <ctype.h> #define LINELEN 80 #define MAXMUNLEN 20 int main(int argc, char *argv[]) { char buffer[LINELEN]; char number[MAXMUNLEN];//記錄有效數(shù)據(jù) char *fgets_rtn = NULL; char *num_ptr=number; int ch, isnum=0,//是否有效標記 sig=0,//正負號標記 num=0,//數(shù)字標記 poin=0;//小數(shù)點標記 while ((fgets_rtn=fgets(buffer, LINELEN, stdin))!=NULL) { if (*fgets_rtn=='\n')break;//空行退出 while ((ch=*fgets_rtn++)!='\0')//檢測每個字符 { switch (ch) { case '\n':ch='\0';break;//是有效數(shù)據(jù)跳過回車符結(jié)束 case '+': case '-': if (sig) isnum=0;//下同無效數(shù)據(jù) else { if(num||poin) isnum=0; else//未標記 ,下同 { sig++; isnum++; } } break; case '.': if(poin) isnum=0; else { poin++; isnum++; } break; default: if (isdigit(ch)) { num++; isnum++; } else if (isspace(ch)) { if(isnum) isnum=0; } else { num++; //設(shè)置無效數(shù)據(jù) isnum=0; } break; }//end switch if (isnum)//如果是有效字符,寫入number數(shù)據(jù) *num_ptr++=ch; else { if(sig||poin||num) { *num_ptr='\0'; break;//結(jié)束本次檢測 } } }//end while if (isnum&&num)//判斷 if (poin) printf("%s為有效double型!\n",number); else printf("%s為有效整型!\n",number); else { //printf("%s為無效數(shù)據(jù)!\n",buffer);//會輸出回車符,不完善。 fgets_rtn=buffer; while((ch=*fgets_rtn++)!='\n'&&ch!='\0') putchar(ch);//如果不能用putchar用://printf("%c",ch); printf("為無效數(shù)據(jù)!\n"); } isnum=sig=num=poin=0;//置0 num_ptr=number; *num_ptr='\0'; }//end while return 0; }網(wǎng)站導(dǎo)航
- zblogPHP模板zbpkf
- zblog免費模板zblogfree
- zblog模板學(xué)習(xí)zblogxuexi
- zblogPHP仿站zbpfang