這段由於學業的原因,最近與C有所接觸,面向過程與物件導向還是有點區別。
但是方法還是可以寫成公用的...下面幾個函數感覺用的比較經常點,就貼上來了...
/* 25 ******************子串判斷****************************//* 參數 ******************SourceStr源串,DesStr判斷串****************************/int StringJudge(char SourceStr[],char DesStr[]){ int results=0,i; i=strlen(DesStr);/*擷取子串的長度*/ while((SourceStr=strstr(SourceStr,DesStr))!=NULL) /*在字串中尋找到字串第一次出現的地方*/ { SourceStr=SourceStr+i;/*位置往後移動*/ results++; } return results;}/* 26 ******************判斷輸入參數類型是否正確****************************/int JudgeInputData(char *str, int status){int i=0;int len = strlen(str);int boolPoint = 0;switch(status){case ISNUM:for (i=0; i<len; i++){if (!(str[i]>=48 && str[i]<=57)){return 0;}}return 1;break;case ISCHAR:for (i=0; i<len; i++){if (!( (str[i]>=65 && str[i]<=90) || (str[i]>=97 && str[i]<=122) )){return 0;}}return 1;break;case ISFLOAT:for (i=0; i<len; i++){if ((str[i]>=48 && str[i]<=57) || (str[i]=='.' && boolPoint==0)){if (str[i]=='.'){boolPoint++;}continue;}return 0;}return 1;break;}return 1;}/* 27 ******************輸入參數並對其進行判斷操作****************************//* 參數: ****************** *str(輸入字元),len(輸入字元長度),*printInfo(輸入字元提示),status(輸入參數類型),max(限定輸入最大值)****************************/void InputData(char *str, int len, char *printInfo, int status,int max){char ch[1024]={0};char buf[1024]={0};int i = 0;memset(str,0,sizeof(str));printf(printInfo);// 列印提示資訊,讓使用者輸入sprintf(buf,"輸入有誤請重新輸入!\n%s",printInfo);switch(status){case ISNUM:while(1){scanf("%s",ch);strncpy(str,ch,len);if (JudgeInputData(ch,status) == 1){if(atoi(str)>max){sprintf(buf,"輸入超過範圍%d,請重新輸入!\n%s",len,printInfo);}elsebreak;}printf(buf);// 列印出錯資訊,讓使用者重新輸入}break;case ISCHAR:while(1){scanf("%s",ch);if (JudgeInputData(ch,status) == 1){break;}printf(buf);// 列印出錯資訊,讓使用者重新輸入}strncpy(str,ch,len);break;case ISFLOAT:while(1){scanf("%s",ch);strncpy(str,ch,len);if (JudgeInputData(ch,status) == 1){if(atoi(str)>max){sprintf(buf,"輸入超過範圍%d,請重新輸入!\n%s",max,printInfo);}elsebreak;}printf(buf);// 列印出錯資訊,讓使用者重新輸入}break;default:break;}
}