c語言中一些公用的方法

來源:互聯網
上載者:User

這段由於學業的原因,最近與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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.