一个有限状态机

有限状态机和上下文无关文法是相对应的,其在很多场合都很有用途。

写了一个识别<p>***</p> 的有限状态机的函数:

功能识别所有的开头为<p>结尾为</p>,中间为任意字符的字符串。

 


       
private void openFileDelInitialFileAndSaveTargetFile(string filePath,int iCount)
        
{
            
try
            
{
                
int count = 0;
                StringBuilder sbToWrite 
= new StringBuilder();
                StreamReader sr 
= new StreamReader(filePath, System.Text.Encoding.Default);
                
for (int i = 0; i < 4; i++)
                
{
                    sbToWrite.Append(sr.ReadLine());
                    sbToWrite.Append(
" ");
                    count
++;
                }

                
string content = sr.ReadToEnd();
                sr.Close();
                
                
==识别用的有限状态机 ==

                File.Delete(filePath);
                
if (count > 4)
                
{
                    
int index = filePath.LastIndexOf('.');//dddd:dddf.txt
                    
//filePath = filePath.Substring(0, index);
                    
//filePath += ".txt";

                    index 
= filePath.LastIndexOf('/');
                    filePath 
= filePath.Substring(0, index);
                    filePath 
+= "/"+iCount.ToString() + ".txt";
                    StreamWriter sw 
= new StreamWriter(filePath, false, Encoding.Default);
                    sw.Write(sbToWrite.ToString());
                    sw.Close();
                }

            }

            
catch (Exception e)
            
{
                Console.Write(
"Error:when try to weite the target file!");
                Console.Write(e.Message);
            }



        }

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值