Small exercise state machine-analysis of the number of lines of CPP File Code

Source: Internet
Author: User
Tags readfile

Problem: analyze the number of lines of space in a CPP file, the number of lines of pure code, the number of lines of pure comments, the number of lines of mixed code and comments, and the total number of lines of code.

 

At the beginning of writing, I used a rather chaotic state identification method, which resulted in unclear Organization and many situations were not taken into account. Later, jumping took a few hours (less than three hours) to produce a standard state machine code. By the way, I also learned the basic usage of the state machine. Today, I have summarized several points. The frame of the state machine is similar to the state mode. To avoid multiple switch-case usage, polymorphism is used to rotate various states based on input conditions. Therefore, the focus of the state machine is to analyze the status. Because of the different problems, there is no unified template.

 

The following code uses this idea.

1. iscode = false, iscomment = false;

2. Mixed line iscode = true, iscomment = true;

3. The code line is not "//", "/*", "", "/R"

4. Except "/",

 

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 balancer count: 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.tar get. 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 ("balancer count" + balancer count. 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 commen T_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/>}

 

Finally:

I had to deal with this problem a few months ago. When I analyzed HTML content recently, I found that flex was not powerful in this regard and had not touched underlying tools such as C for a long time, unfamiliar and cannot solve the problem in real time. phython, a good tool, is unknown when the tool is used. At this time, it is not the best solution to discover what you will do. Using flex to solve these problems is like scratching your shoes. So when I am idle, I still need to practice some underlying tools. I don't have much encapsulation, and I have a good idea. I am so light-loaded and invincible. It's quite good.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.