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.