Article 17th: exercise code for calculating the number of lines of code, article 17th

Source: Internet
Author: User

Article 17th: exercise code for calculating the number of lines of code, article 17th

Some Classes in Foundation are used:

NSManager

NSString

NSArray

//// Main. m // 10-calculate the number of lines of code /// Created by xxx on 15/9/16. // Copyright (c) 2015 itcast. all rights reserved. // # import <Foundation/Foundation. h> int countLineOfFile (NSString * path) {// obtain the File manager NSFileManager * manager = [NSFileManager defaultManager]; // determine whether the path is a file that can be computed. The path is BOOL isFile = NO; BOOL exist = [manager fileExistsAtPath: path isDirectory: & isFile]; if (! Exist) {// the file path of path does not exist return 0;} // if the path exists if (isFile) {// The path under path is a folder // get all content in the current folder: folder and file NSArray * fileArray = [manager contentsOfDirectoryAtPath: path error: nil]; int count = 0; // view all files and folders for (NSString * file in fileArray) {count + = countLineOfFile ([NSString stringWithFormat: @ "% @/% @", path, file]);} return count;} else {// obtain the file extension NSString * extension = [path pathExtension]; I F (! ([Extension isEqualToString: @ "h"] | [extension ission tostring: @ "m"] | [extension ission tostring: @ "c"] | [extension ission tostring: @ "cpp"]) return 0; // obtain all content of the file whose path is path NSString * fileConten = [NSString stringWithContentsOfFile: path encoding: NSUTF8StringEncoding error: nil]; // a string is divided into multiple strings by '\ n', that is, each row serves as a string NSArray * array = [fileConten componentsSeparatedByString: @ "\ n"]; // for (NSString * str in array) // NSLog (@ "% @", str); return (int) array. count ;}} int main (int argc, const char * argv []) {// count the number of lines of code in a file or folder, including empty rows int count = countLineOfFile (@ "/Users/qujie/Documents/OC Program/10-Number of calculated code lines"); NSLog (@ "count = % d", count ); // NSString * str = @ "123-456-789"; // NSArray * array = [str componentsSeparatedByString: @ "-"]; // NSLog (@ "% @", array); return 0 ;}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.