問題: 分析一個CPP檔案中空格行數,純程式碼行數,純注釋行數,代碼和注釋混合行數,總程式碼數。
一開始寫時,我用了相當混亂的狀態標識方法,導致條理不清,很多情況沒有考慮到。後來Jumping老人家略花幾個小時(小於3小時),華麗麗地敲出了一份標準狀態機器代碼。參考了下,順便也學了下狀態機器的基本用法。今天敲完後,總結了幾點,狀態機器的架構和狀態模式差不多,為了避免多個switch-case的用法,而採用多態根據輸入的條件輪轉各種狀態。所以狀態機器的重點還在於分析狀態上,因問題不同而不同,也就沒有統一的模板。
以下代碼以這麼個思路進行的。
1,空格行 isCode = false, isComment = false;
2,混合行 isCode = true, isComment = true;
3,程式碼 非"//", "/*", " ", "/r"
4,注釋行 除了"/""打頭
public class StateMachine extends Sprite<br />{<br />private var lastStr: String = "";<br />private var fileStr: String = null;<br />private var currDeal: Function = normal_Deal;<br />private var isCode: Boolean = false;<br />private var isComment: Boolean = false;<br />private var isString: Boolean = false;<br />private var isCommentLine: Boolean = false;</p><p>private var codeCount: int = 0;<br />private var commentCount: int = 0;<br />private var mixCount: int = 0;<br />private var totalCount: int = 0;<br />private var blankCount: int = 0;</p><p>public function StateMachine()<br />{<br />readFile();<br />}</p><p>public function readFile(): void<br />{<br />var load: URLLoader = new URLLoader();<br />load.addEventListener(Event.COMPLETE, dealControl);<br />load.load(new URLRequest("D://2.cpp"));<br />}</p><p>public function dealControl(event: Event): void<br />{<br />fileStr = String(event.target.data);<br />fileStr += "/r";<br />var index: int = 0;<br />while (fileStr.length != index)<br />{<br />currDeal = currDeal(fileStr.charAt(index));<br />index++;<br />}<br />trace("code " + codeCount.toString());<br />trace("comment " + commentCount.toString());<br />trace("blankCount " + blankCount.toString());<br />trace("mixCount " + mixCount.toString());<br />trace("totalCount " + totalCount.toString());<br />}</p><p>public function normal_Deal(m: String): Function<br />{<br />if (lastStr == "")<br />{<br />lastStr = m;<br />return normal_Deal;<br />}<br />else if (m == "/r")<br />{<br />return normal_EndLine();<br />}<br />else if (lastStr == "/" && m == "/" && !isString)<br />{<br />isCommentLine = true;<br />return comment_Deal;<br />}<br />else if (lastStr == "/" && m == "*" && !isString)<br />{<br />return comment_Deal;<br />}<br />else if (m == "/"" || m == "/'")<br />{<br />isCode = true;<br />isString = true;<br />}<br />else if (m != " " && m != "/t" && m != "/")<br />{<br />isCode = true;<br />}<br />lastStr = m;<br />return normal_Deal;<br />}</p><p>public function comment_Deal(m: String): Function<br />{<br />isComment = true;<br />if(lastStr == "*" && m == "/")<br />{<br />return normal_Deal;<br />}<br />else if (m == "/r")<br />{<br />return comment_EndLine();<br />}<br />lastStr = m;<br />return comment_Deal;<br />}</p><p>public function comment_EndLine(): Function<br />{<br />totalCount++;<br />if (isComment && isCode)<br />{<br />mixCount++;<br />}<br />else if (isComment && !isCode)<br />{<br />commentCount++;<br />}</p><p>lastStr = "";<br />isCode = false;<br />isString = false;</p><p>if (isCommentLine)<br />{<br />isCommentLine = false;<br />isComment = false;<br />return normal_Deal;<br />}<br />return comment_Deal;<br />}</p><p>public function normal_EndLine(): Function<br />{<br />totalCount++;<br />if (isComment && !isCode)<br />{<br />commentCount++;<br />}<br />else if (isComment && isCode)<br />{<br />mixCount++;<br />}<br />else if (!isComment && isCode)<br />{<br />codeCount++;<br />}<br />else if (!isComment && !isCode)<br />{<br />blankCount++;<br />}<br />isCode = false<br />isComment = false;<br />isString = false;<br />lastStr = "";</p><p>return normal_Deal;<br />}<br />}
最後:
本來這個問題早在幾個月前就要處理了,到了最近分析html的內容的時候,發現,flex在這方面並不強大,久沒碰c之類的底層工具,生疏得無法即時解決問題本身,phython,很好的工具,工具到用時方知不會。這時候,發現自己會的東西,並不是最好的能解決問題的。用flex解決這些問題,猶如隔鞋抓癢,不痛快,不徹底。所以空閑時,還是多練練一些底層工具,沒有太多的封裝,外加很好的思路,輕裝上陣,所向無敵,挺好,挺好。