搜索
bottom↓
回复: 9

讨论c语言实现状态机的几种方法的比较

[复制链接]

出0入0汤圆

发表于 2005-3-21 13:39:37 | 显示全部楼层 |阅读模式
实现状态机有switch() case:XXX结构,还有链表结构。不知道这2种实现方法各有什么不同吗?



好象链表比较简洁,且可以在运行时动态分配空间。



欢迎大家讨论

阿莫论坛20周年了!感谢大家的支持与爱护!!

知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)

出0入0汤圆

发表于 2005-3-21 21:45:39 | 显示全部楼层
你指的是不是那种用于产生随机数和加密序列的有限状态机(Finite State Machine)?

如果是的话,我倒编过几个程序(C/汇编)
-----此内容被itboy82811于2005-03-21,22:45:24编辑过

出0入0汤圆

发表于 2005-3-21 21:56:16 | 显示全部楼层
贴一个C#的完整代码供参考:LFSR类以及相关调用代码



using System;

using bit = System.Byte;



namespace RDLibrary

{

        // 线性反馈移位寄存器封装类

        // 架构采用快捷的Galois实现

        //

        sealed class LFSR

        {

                private                bool        deBruijn;                        // 产生de Bruijn序列?

                private                uint        stages;                                // 阶数

                private                uint        minStages;

                private                uint        maxStages;

                private                uint        initialState;                // 初始态

                private                uint        state;                                // 当前态

                private                uint        zeros;                                // 检测de Bruijn序列中连零的个数

                private                uint        inputMask;                        // 输入端掩码

                private                uint        polynomial;                        // 当前本原多项式

               

                #region 本原多项式表,polynomials[0]对应于2阶LFSR,依此类推

                public readonly uint[] polynomials =

                {

                        0x00000003, 0x00000005, 0x00000009, 0x00000009,

                        0x00000021, 0x00000041, 0x00000071, 0x00000021,

                        0x00000081, 0x00000201, 0x00000941, 0x00001601,

                        0x00002A01, 0x00004001, 0x00006801, 0x00004001,

                        0x00000801, 0x00064001, 0x00020001, 0x00080001,

                        0x00200001, 0x00040001, 0x00B00001, 0x00400001,

                        0x03100001, 0x06400001, 0x02000001, 0x08000001,

                        0x25000001, 0x10000001, 0x46000001

                };

                #endregion



                public bit Output

                {

                        get { return (bit)(state & 1); }

                }



                public uint State

                {

                        get { return state; }

                }



                public LFSR()

                {

                        deBruijn                = false;

                        minStages                = 2;

                        maxStages                = minStages + (uint)polynomials.Length - 1;

                        stages                        = maxStages;

                        polynomial                = polynomials[stages - minStages];

                        inputMask                = (uint)1 << (int)(stages - 1);

                        initialState        = 1;

                        state                        = initialState;

                        zeros                        = 0;

                }



                public void ClockIt()

                {

                        if ((state & 1) == 1)

                        {

                                if (deBruijn)

                                {

                                        // run of zeros broken

                                        zeros = 0;

                                }



                                state = (state ^ polynomial) >> 1;

                                state ^= inputMask;

                        }

                        else

                        {

                                if (deBruijn)

                                {

                                        if (++zeros != stages - 1)

                                        {

                                                state >>= 1;

                                        }

                                }

                                else

                                {

                                        state >>= 1;

                                }

                        }

                }



                public void Reset(uint stages, uint initialState, bool deBruijn)

                {

                        if (stages < minStages || stages > maxStages)

                        {

                                throw new Exception("Stages of LFSR is out of range!");

                        }



                        this.stages = stages;

                        polynomial = polynomials[stages - minStages];

                        inputMask = (uint)1 << (int)(stages - 1);

                       

                        if (initialState == 0 || (stages < 32 && initialState >= ((uint)1 << (int)stages)))

                        {

                                throw new Exception("Initial state of LFSR is out of range!");

                        }



                        this.initialState = initialState;

                        state = initialState;

                        zeros = 0;

                       

                        this.deBruijn = deBruijn;

                }

        }

}





// 产生随机数序列

public static void GenerateRandomBytes(byte[] bytesOut)

{

        uint seed;

        do

        {

                seed = (uint)DateTime.Now.Ticks;  // based on system datetime

        } while (seed == 0);  // LFSR can not be fed with zero

               

        LFSR prng = new LFSR();  // Pseudo-random number generator

        prng.Reset(32, seed, false);



        for (int i = 0; i < bytesOut.Length; i++)

        {

                bytesOut = 0;



                for (int j = 0; j < 8; j++)

                {

                        prng.ClockIt();

                        bytesOut >>= 1;

                        bytesOut ^= (byte)(prng.Output << 7);

                }

        }

}

出0入0汤圆

发表于 2005-3-22 00:46:59 | 显示全部楼层
重点就是原理了,具体工具没有什么差别的

出0入0汤圆

发表于 2005-7-14 13:44:19 | 显示全部楼层
上网看代码????兄弟给个原理先呀!不然怎么看你的代码??注释少得可怜,比较的好的源代码注释是应该占整个篇幅的1/3的呀!!!!

出0入0汤圆

发表于 2010-3-27 12:02:06 | 显示全部楼层
回复【2楼】itboy82811
-----------------------------------------------------------------------

谢谢分享

出0入0汤圆

发表于 2013-8-12 23:08:20 | 显示全部楼层
mask                                          

出0入0汤圆

发表于 2013-8-13 18:34:13 | 显示全部楼层
不明白状态机原理

出0入0汤圆

发表于 2013-8-30 13:46:08 | 显示全部楼层
我也不大明白状态机

出0入0汤圆

发表于 2013-11-25 21:02:48 | 显示全部楼层
好主意啊
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-7-23 22:19

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表