首頁>Club>
8
回覆列表
  • 1 # 小福子487

    #include <stdio.h> #include <ctype.h> #define LINELEN 80 #define MAXMUNLEN 20 int main(int argc, char *argv[]) {     char buffer[LINELEN];     char number[MAXMUNLEN];//記錄有效資料      char *fgets_rtn = NULL;     char *num_ptr=number;     int ch,     isnum=0,//是否有效標記      sig=0,//正負號標記      num=0,//數字標記      poin=0;//小數點標記      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;//是有效資料跳過回車符結束              case '+':             case '-':                 if (sig)                      isnum=0;//下同無效資料                 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++;  //設定無效資料                      isnum=0;                 }                                     break;             }//end switch             if (isnum)//如果是有效字元,寫入number資料                  *num_ptr++=ch;             else             {                 if(sig||poin||num)                 {                     *num_ptr='\0';                     break;//結束本次檢測                 }             }         }//end while         if (isnum&&num)//判斷              if (poin)                 printf("%s為有效double型!\n",number);             else                 printf("%s為有效整型!\n",number);         else         {             //printf("%s為無效資料!\n",buffer);//會輸出回車符,不完善。              fgets_rtn=buffer;             while((ch=*fgets_rtn++)!='\n'&&ch!='\0')                 putchar(ch);//如果不能用putchar用://printf("%c",ch);              printf("為無效資料!\n");         }         isnum=sig=num=poin=0;//置0         num_ptr=number;         *num_ptr='\0';     }//end while     return 0; }

  • 中秋節和大豐收的關聯?
  • 低調做人的詞語?