现在的位置: 首页 > 综合 > 正文

曼彻斯特和差分曼彻斯特编码的实现 C程序

2013年10月27日 ⁄ 综合 ⁄ 共 4526字 ⁄ 字号 评论关闭

 

 //----------------------------------------------------------------------------     |
//  标题: 曼彻斯特和差分曼彻斯特编码的实现                                        |
//  分析:曼彻斯特编码是将每个码元的中央实现跳变,具体的码字表示为:                                                                   |
//        1->10,0->01.                                                             |
//        差分曼彻斯特编码每个码元依前一码元而定,遇1跳变,遇0保持                                                                 |
//        若前为:01,则1->10  0->01                                               |
//        若前为:10,则1->01 0->10                                                |
//  实现:定义两个数组,一个用来放置输入要编码的序列,另一个用来放编码后的序列                                                                         |
//                                                                           |
//                                                                           |
//----------------------------------------------------------------------------

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 头文件
#include <stdio.h>
#include 
<assert.h>
#include
<string.h>

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// 全局变量

#define M 10
int j;   //指向编码后序列的数组下标
int i;   //输入码字的数组下标
int length;   //求值输入数组的长度

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// 参数表

int Direct_code(char str0[])  //直接编码
{
 
char dirct_code[2*M];
 memset(dirct_code,
0,2*M);
 dirct_code[
0]='0';
 dirct_code[
1]='1';
 j
=2;
 
extern length;
  
for(i=0;i<length;i++)
 
{
     
// 循环入口数据
     printf("current character is: %c ",str0[i]);

        
// 循环处理,0 ->01 1 ->10
  if(str0[i]=='0')   {dirct_code[j++]='0';dirct_code[j++]='0';} 
  
else if(str0[i]=='1'{dirct_code[j++]='1';dirct_code[j++]='1';}
        
else {printf("input error,exit........ "); return 1;}  // 输入出错
        
// 循环处理后数据
   printf("-----");
     printf(
"after process: %c%c ",dirct_code[j-2],dirct_code[j-1]);
 }

    
    
// 结果字符串加上终结符
    dirct_code[j]=0;

    
// 输出结果
 printf("------------------------------------------- ");
 printf(
"Direct_code coding is:%s ",dirct_code);
 
return 0;
}



int Manchester(char str0[])   //曼彻斯特编码
{
 
char Manchester[2*M];
 memset(Manchester,
0,2*M);
 Manchester[
0]='0';
 Manchester[
1]='1';
 j
=2;
 
extern length;
 
  
for(i=0;i<length;i++)
 
{
     
// 循环入口数据
     printf("current character is: %c ",str0[i]);

        
// 循环处理,0 ->01 1 ->10
  if(str0[i]=='0')   {Manchester[j++]='0';Manchester[j++]='1';} 
  
else if(str0[i]=='1'{Manchester[j++]='1';Manchester[j++]='0';}
        
else {printf("input error,exit........ "); return 1;}  // 输入出错
        
// 循环处理后数据
   printf("-----");
     printf(
"after process: %c%c ",Manchester[j-2],Manchester[j-1]);
 }

    
    
// 结果字符串加上终结符
    Manchester[j]=0;
    
    
// 输出结果
 printf("------------------------------------------- ");
 printf(
"Manchester coding is :%s ",Manchester);
 
return 0;
}



int Dif_Manchester(char str0[])  //差分曼彻斯特编码
{
 
char Dif_Manch[2*M];
 memset(Dif_Manch,
0,2*M);    //初始化数组
 Dif_Manch[0]='0';
 Dif_Manch[
1]='1';
 j
=2;
 
extern length;

 
for(i=0;i<length;i++)
 
{
     
// 循环入口数据
     printf("current character is: %c ",str0[i]);

        
// 循环处理,0 ->01 1 ->10
  if(str0[i]=='0')
  
{
   Dif_Manch[j
++]=Dif_Manch[j-3];
   Dif_Manch[j
++]=Dif_Manch[j-2];
  }

  
else if(str0[i]=='1'
  
{
   Dif_Manch[j
++]=Dif_Manch[j-2];
   Dif_Manch[j
++]=Dif_Manch[j-3];
  }

        
else {printf("input error,exit........ "); return 1;}  // 输入出错
        
// 循环处理后数据
   printf("-----");
     printf(
"after process: %c%c ",Dif_Manch[j-2],Dif_Manch[j-1]);
 }

    
    
// 结果字符串加上终结符
    Dif_Manch[j]=0;
    
    
// 输出结果
 printf("------------------------------------------- ");
 printf(
"Dif_Manchester coding is :%s ",Dif_Manch);
 
return 0;
}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////////

// 入口点
int main(int argc, char* argv[])
{

 
    
char str0[M];
 
 
// 获取输入数据
 printf("please input the number string u want(it must less than 10): ");
 scanf(
"%s",str0);
    
// 验证输入数据是否正确,可以用assert之类
 printf("what u input is------:: %s ",str0);
 length
=strlen(str0);
    assert(length 
<M );  //设置断言,数组越界检查
    

</p>

<div style=

抱歉!评论已关闭.