格雷码生成算法
2007-10-05 22:07
246 查看
格雷码生成算法
头文件:
/**//**//**///////////////////////////////////////////
// 文件名:GrayCode.h
// 功 能:产生N位格雷码算法头文件
/**//**//**///////////////////////////////////////////
#include <iostream>
#include <string>
#include <cstdio>
#include <string>
#include <stack>
#include <conio.h>
using namespace std;
//定义结点数据结构
typedef struct CodeNode...{
char *code; //格雷码串
struct CodeNode *next; //指向上一个码段的指针
}CodeNode, *CodeList;
//函数声明
void GenerateGrayCode( int n );
CodeList CreateCodeList();
void IncreaseCode( CodeList L );
void PrintGrayCode( CodeList L );
void ReleaseGrayCode( CodeList L );
char *CodeCat( char *des, char *src );
//定义全局变量
CodeList L;
cpp文件:
/**//**//**///////////////////////////////////////////
// 文件名:GrayCode.cpp
// 功 能:产生N位格雷码算法实现文件
/**//**//**///////////////////////////////////////////
#include "GrayCode.h"
void main()
...{
int nbits = 1;
char c;
while( nbits != 0)
...{
cout<<"Please input the bits of GrayCode(0:exit):";
cin>>nbits;
GenerateGrayCode( nbits ); //产生编码
cout<<"Done!"<<" Print Codes(y/n)?";
if( ((c = getch()) == 'y') || ((c = getch()) == 'Y') )
...{
printf(" ");
PrintGrayCode( L ); //打印编码
}
printf(" ");
}
ReleaseGrayCode( L ); //释放内存
}
void GenerateGrayCode( int n ) //算法主体
...{
if(n == 1)
...{
L = CreateCodeList();
}
else
...{
GenerateGrayCode( n-1 );
IncreaseCode( L );
}
}
CodeList CreateCodeList() //建立链表
...{
CodeList Head = (CodeList)malloc(sizeof(CodeNode));
CodeList p = (CodeList)malloc(sizeof(CodeNode));
CodeList q = (CodeList)malloc(sizeof(CodeNode));
p->code = "0"; q->code = "1";
p->next = q; q->next = NULL;
Head->next = p;
return Head;
}
void ReleaseGrayCode( CodeList L ) //释放内存空间
...{
CodeList q;
while( L )
...{
q = L;
L = L->next;
free(q);
}
}
void IncreaseCode( CodeList L) //做编码的一位扩展
...{
CodeList p;
p = L;
int count = 0;
stack <char*> CodeStack; //定义栈
while( p->next )
...{
CodeStack.push( p->next->code );
p->next->code = CodeCat("0",p->next->code );
p = p->next;
count++;
}
while( count > 0 )
...{
CodeList q = (CodeList)malloc(sizeof(CodeNode));
q->code = CodeCat( "1",CodeStack.top() );
CodeStack.pop();
q->next = NULL;
p->next = q;
p = p->next;
count--;
}
}
void PrintGrayCode( CodeList L ) //打印编码
...{
CodeList p;
p = L->next;
while(p)
...{
cout<<p->code<<endl;
p = p->next;
}
}
char *CodeCat( char *des, char *src ) //粘合两个字符串
...{
char *stone = (char*)malloc((strlen(src)+strlen(des))*sizeof(char));
strcpy(stone,des);
strcat(stone,src);
return stone;
}
呵呵,就这么多了,谢谢各位!
头文件:
/**//**//**///////////////////////////////////////////
// 文件名:GrayCode.h
// 功 能:产生N位格雷码算法头文件
/**//**//**///////////////////////////////////////////
#include <iostream>
#include <string>
#include <cstdio>
#include <string>
#include <stack>
#include <conio.h>
using namespace std;
//定义结点数据结构
typedef struct CodeNode...{
char *code; //格雷码串
struct CodeNode *next; //指向上一个码段的指针
}CodeNode, *CodeList;
//函数声明
void GenerateGrayCode( int n );
CodeList CreateCodeList();
void IncreaseCode( CodeList L );
void PrintGrayCode( CodeList L );
void ReleaseGrayCode( CodeList L );
char *CodeCat( char *des, char *src );
//定义全局变量
CodeList L;
cpp文件:
/**//**//**///////////////////////////////////////////
// 文件名:GrayCode.cpp
// 功 能:产生N位格雷码算法实现文件
/**//**//**///////////////////////////////////////////
#include "GrayCode.h"
void main()
...{
int nbits = 1;
char c;
while( nbits != 0)
...{
cout<<"Please input the bits of GrayCode(0:exit):";
cin>>nbits;
GenerateGrayCode( nbits ); //产生编码
cout<<"Done!"<<" Print Codes(y/n)?";
if( ((c = getch()) == 'y') || ((c = getch()) == 'Y') )
...{
printf(" ");
PrintGrayCode( L ); //打印编码
}
printf(" ");
}
ReleaseGrayCode( L ); //释放内存
}
void GenerateGrayCode( int n ) //算法主体
...{
if(n == 1)
...{
L = CreateCodeList();
}
else
...{
GenerateGrayCode( n-1 );
IncreaseCode( L );
}
}
CodeList CreateCodeList() //建立链表
...{
CodeList Head = (CodeList)malloc(sizeof(CodeNode));
CodeList p = (CodeList)malloc(sizeof(CodeNode));
CodeList q = (CodeList)malloc(sizeof(CodeNode));
p->code = "0"; q->code = "1";
p->next = q; q->next = NULL;
Head->next = p;
return Head;
}
void ReleaseGrayCode( CodeList L ) //释放内存空间
...{
CodeList q;
while( L )
...{
q = L;
L = L->next;
free(q);
}
}
void IncreaseCode( CodeList L) //做编码的一位扩展
...{
CodeList p;
p = L;
int count = 0;
stack <char*> CodeStack; //定义栈
while( p->next )
...{
CodeStack.push( p->next->code );
p->next->code = CodeCat("0",p->next->code );
p = p->next;
count++;
}
while( count > 0 )
...{
CodeList q = (CodeList)malloc(sizeof(CodeNode));
q->code = CodeCat( "1",CodeStack.top() );
CodeStack.pop();
q->next = NULL;
p->next = q;
p = p->next;
count--;
}
}
void PrintGrayCode( CodeList L ) //打印编码
...{
CodeList p;
p = L->next;
while(p)
...{
cout<<p->code<<endl;
p = p->next;
}
}
char *CodeCat( char *des, char *src ) //粘合两个字符串
...{
char *stone = (char*)malloc((strlen(src)+strlen(des))*sizeof(char));
strcpy(stone,des);
strcat(stone,src);
return stone;
}
呵呵,就这么多了,谢谢各位!
相关文章推荐
- 格雷码生成算法详解(c语言)
- 自动生成格雷码算法
- 两种n位格雷码生成算法
- 格雷码生成算法分析及实现(C/OC)
- 生成格雷码序列算法(C++)
- 算法与数据结构——算法题 45:生成格雷码(腾讯笔试题) ? 待解决
- 基于格雷码生成算法的n选m排列算法
- Gray码的生成算法(直接由二进制数转格雷码)
- 也谈自动生成格雷码算法
- 利用XDAS wizard tools生成xDM算法框架
- 【算法导论】最小生成树扩展
- 完美迷宫生成算法
- 排列生成算法--序列法
- 全排列生成算法(三)
- Prim最小生成树算法
- 【算法导论】最小生成树之Kruskal法
- 最小生成树Prim算法理解
- 《统计学习方法》读书笔记-----决策树:ID3,C4.5生成算法和剪枝
- 体积阴影的生成算法
- 《统计学习方法》读书笔记-----决策树:ID3,C4.5生成算法和剪枝