C語言根據(jù)協(xié)議分割獲取字符串單元的實現(xiàn)代碼
協(xié)議做如下規(guī)定:
規(guī)定數(shù)據(jù)協(xié)議:
序列號 長度 狀態(tài)字 數(shù)據(jù)長度 數(shù)據(jù)1 數(shù)據(jù)2 數(shù)據(jù)3
以空格作為數(shù)據(jù)單元。
ep:
00001 00007 1 3 34567 26358 32698 (1) (2) (3)(4) (5) (6) (7)
如ep所示:
(1)00001就是數(shù)字1,即代表序列號為1 (2)00007就是數(shù)字7,即代表長度為7 (3)1代表狀態(tài)字 (4)3代表數(shù)字長度 (5)34567代表數(shù)據(jù)1 (6)26358代表數(shù)據(jù)2 (7)32698代表數(shù)據(jù)3
這樣就找到規(guī)律了,假設(shè)數(shù)據(jù)都為整型或者負(fù)整型,我們就可以來實現(xiàn)以下代碼:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//根據(jù)空格拆分字符串 
int partition(char *src, char *par, int pos)
{
 int i,j;
 i = pos;
 //取得一個非空字符 
 while(src[i] == ' ')
  ++i;
 if(src[i] != '\0')
 {
  j = 0;
  while((src[i] != '\0') && (src[i] != ' '))
  {
   //判斷條件是否滿足 
 if((src[i] > '9') || (src[i] < '0') && (src[i] != '-'))
  return -1 ;
   par[j] = src[i];
   ++i;
   ++j;
  }
  par[j]='\0';
  return i;
 }
 else
  return -1;
}
int main(void)
{
 int serial_number ;
 int lenght ;
 int status ;
 int data_length;
 int data1,data2,data3; 
 int position = 0;
 int para_flag = 1 ; 
 int parameter_item = 0;
 char partition_string[20] = {0};
 char *data = "00001 00007 1 3 34567 26358 32698"; 
 while(para_flag)
 {
 if(para_flag == 0)
  break ; 
 if((position = partition(data,partition_string,position)) != -1)
 {
  ++parameter_item ;
  switch(parameter_item)
  {
  case 1:
   serial_number = atoi(partition_string);
   break ;
  case 2:
   lenght = atoi(partition_string);
   break ;
  case 3:
   status = atoi(partition_string); 
   break ;
  case 4:
   data_length = atoi(partition_string); 
   break ;
  case 5:
   data1 = atoi(partition_string); 
   break ;
  case 6:
   data2 = atoi(partition_string); 
   break ;
  case 7: 
   data3 = atoi(partition_string); 
   para_flag = 0 ;
   break ;
  }
 }
 }
 printf("序號:%d\n",serial_number);
 printf("長度:%d\n",lenght);
 printf("狀態(tài)字:%d\n",status);
 printf("數(shù)據(jù)長度:%d\n",data_length);
 printf("數(shù)據(jù)1:%d\n",data1);
 printf("數(shù)據(jù)2:%d\n",data2);
 printf("數(shù)據(jù)3:%d\n",data3);
 return 0; 
} 
運行結(jié)果:
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對我們的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
欄 目:C語言
下一篇:利用C語言解決八皇后問題以及解析
本文標(biāo)題:C語言根據(jù)協(xié)議分割獲取字符串單元的實現(xiàn)代碼
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/640.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
 - 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)式函數(shù)庫
 - 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
 - 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對數(shù)怎么表達(dá)
 - 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
 - 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
 - 04-02c語言沒有round函數(shù) round c語言
 - 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
 - 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
 - 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘
 


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
 - 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
 - 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
 - 04-02c語言用函數(shù)寫分段 用c語言表示分段
 - 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
 - 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
 - 04-02c語言沒有round函數(shù) round c語言
 - 04-02c語言分段函數(shù)怎么求 用c語言求分段
 - 04-02C語言中怎么打出三角函數(shù) c語言中怎
 - 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
 
隨機閱讀
- 08-05織夢dedecms什么時候用欄目交叉功能?
 - 01-10delphi制作wav文件的方法
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
 - 01-11ajax實現(xiàn)頁面的局部加載
 - 08-05DEDE織夢data目錄下的sessions文件夾有什
 - 01-10C#中split用法實例總結(jié)
 - 04-02jquery與jsp,用jquery
 - 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 - 01-10使用C語言求解撲克牌的順子及n個骰子
 


