本文介绍了C源代码行计数功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 完成这个C源代码行计数功能需要花费很多时间。 您如何看待它? / ************************************************** ***************************** *功能:size_t linecnt(char * filenm); *作者: jh ********** @ gmail.com ,删除foobar以发送电子邮件 *日期:2008.4.12 *描述:C源代码行数。没有评论&没有空格线 算了。 ************************* ************************* ************************* *** / #include< stddef.h> #include< stdio.h> #include < string.h> #include< ctype.h> #include< errno.h> #define LEN 1024 size_t linecnt(char * filenm) { const char lcmt [] =" / *" ;; const char rcmt [] =" * /" ;; const char C99cmt [] =" //" ;; const char esc =''\\''; const char dqm =''\"''; int lcmt_open = 0; / * 1:last" / *"仍然打开; 0:不是。* / int dqm_open = 0; / * 1:last仍然打开; 0:还没有。* / size_t cnt = 0; char buf [LEN] = {''\ 0''}; FILE * fileptr; char * p1,* p2; errno = 0; if(!(fileptr) = fopen(filenm," r"))&& errno) { printf("%s:%d:%s,%s \\ \\ n,__ FILE __,_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ while(fgets(buf,LEN,fileptr)) { / * lcmt从一行开始(lcmt之前的空格 省略),不是 *计算* / if(!lcmt_open&&(p1 = strstr(buf,lcmt))) { for(p2 = buf; p2!= p1; p2 ++) { if(!isspace(* p2) )) { cnt ++; 休息; } } { if(p2 - 1> = buf&& *(p2 - 1)!= esc) { dqm_open = dqm_open? 0:1; } } if(!dqm_open) { lcmt_open = 1; } if(lcmt_open&& strstr(p1,rcmt)) { lcmt_open = 0; } for(;!lcmt_open&&(p1 = strchr(p1,dqm)); p1 ++) { if(p1 - 1> = buf&& *(p1 - 1)!= esc) { dqm_open = dqm_open? 0:1; } } } / * lcmt_open,不算* / 否则if(lcmt_open) { if(p1 = strstr(buf,rcmt)) { lcmt_open = 0; for(p1 + = 2; p1< buf + strlen(buf); p1 ++) { if(!isspace(* p1)&& * p1!= 0) { cnt ++; 休息; } } } } / * C99cmt从一行开始,不计算* / 否则if(p1 = strstr(buf,C99cmt)) { for(p2 = buf; p2!= p1; p2 ++) { if(!isspace(* p2)) { cnt ++; 休息; } } } / * cnt ++,处理空间线和dqm_open状态也* / 其他 { for(p1 = buf; p1< buf + strlen(buf); p1 ++) { if(!isspace(* p1)) { cnt ++; break; } } } for(p1 = buf; !lcmt_open&& (p1 = strchr(p1,dqm)); p1 ++) { if(p1 - 1> = buf&& *(p1 - 1)!= esc) { dqm_open = dqm_open? 0:1; } } } fclose(fileptr); 返回cnt; } #include< stdio.h> int main(int argc,char * argv []) { if(argc!= 2) printf(" Usage:%s< filename> ;, C源代码行数。-jhl \ n", argv [0]); else printf("%d \ n", linecnt(argv [1])); 返回0; } 感谢您的时间! It takes mu so time to finish this C source code line count function.What do you think about it?/************************************************** ****************************** Function : size_t linecnt(char *filenm);* Author : jh**********@gmail.com, remove foobar for email* Date : 2008.4.12* Description: C source code line count. No comments & no space linescounted. ************************************************** ****************************/ #include <stddef.h>#include <stdio.h>#include <string.h>#include <ctype.h>#include <errno.h> #define LEN 1024size_t linecnt(char *filenm){const char lcmt[] = "/*";const char rcmt[] = "*/";const char C99cmt[] = "//";const char esc = ''\\'';const char dqm = ''\"'';int lcmt_open = 0; /*1:last "/*" still opens; 0:notyet.*/int dqm_open = 0; /*1:last " still opens; 0:not yet.*/size_t cnt = 0;char buf[LEN] = {''\0''};FILE *fileptr;char *p1, *p2; errno = 0;if (!(fileptr = fopen(filenm, "r")) && errno){printf("%s:%d: %s, %s\n", __FILE__, __LINE__, filenm,strerror(errno));return -1;}while (fgets(buf, LEN, fileptr)){/*lcmt starts at the begining of a line (spaces before lcmtomitted), not*counted*/if (!lcmt_open && (p1 = strstr(buf, lcmt))){for (p2 = buf; p2 != p1; p2++){if (!isspace(*p2)){cnt++;break;}}for (p2 = buf; (p2 = strchr(p2, dqm)) && p2 < p1; p2++){if (p2 - 1 >= buf && *(p2 - 1) != esc){dqm_open = dqm_open ? 0 : 1;}}if (!dqm_open){lcmt_open = 1;}if (lcmt_open && strstr(p1, rcmt)){lcmt_open = 0;}for (; !lcmt_open && (p1 = strchr(p1, dqm)); p1++){if (p1 - 1 >= buf && *(p1 - 1) != esc){dqm_open = dqm_open ? 0 : 1;}}} /*lcmt_open, not counted*/else if (lcmt_open){if (p1 = strstr(buf, rcmt)){lcmt_open = 0;for (p1 += 2; p1 < buf + strlen(buf); p1++){if (!isspace(*p1) && *p1 != 0){cnt++;break;}}}} /*C99cmt starts at the begining of a line, not counted*/else if (p1 = strstr(buf, C99cmt)){for (p2 = buf; p2 != p1; p2++){if (!isspace(*p2)){cnt++;break;}}} /*cnt++, deal with space lines and dqm_open state also*/else{for (p1 = buf; p1 < buf + strlen(buf); p1++){if (!isspace(*p1)){cnt++;break;}}}for (p1 = buf; !lcmt_open && (p1 = strchr(p1, dqm)); p1++){if (p1 - 1 >= buf && *(p1 - 1) != esc){dqm_open = dqm_open ? 0 : 1;}}}fclose(fileptr);return cnt;}#include <stdio.h>int main(int argc, char *argv[]){if (argc != 2)printf("Usage: %s <filename>, C source code line count. -jhl\n",argv[0]);elseprintf("%d\n", linecnt(argv[1]));return 0;}Thank you for your time!推荐答案 4月12日,3:35 * am,lovecreatesbea ... @ gmail.com < lovecreatesbea ... @ gmail.comwrote: On Apr 12, 3:35*am, "[email protected]"<[email protected]: 完成这个C源代码行计数功能需要花费很多时间。 It takes mu so time to finish this C source code line count function. 抱歉错误拼写,我的意思是我需要一些时间。 Sorry for the error spell, I meant "It takes me some time". 4月11,12:38 * pm,lovecreatesbea ... @ gmail.com < lovecreatesbea ... @ gmail.comwrote: On Apr 11, 12:38*pm, "[email protected]"<[email protected]: 4月12日,3:35 * am,lovecreatesbea ... @ gmail.com < lovecreatesbea ... @ gmail.comwrote: On Apr 12, 3:35*am, "[email protected]"<[email protected]: 完成此C源代码行计数功能需要花费很多时间。 It takes mu so time to finish this C source code line count function. 抱歉错误拼写,我的意思是我需要一些时间。 Sorry for the error spell, I meant "It takes me some time". 不计算评论是错误的。它们是代码的重要组成部分。 It''s a mistake not to count comments. They are an important part ofthe code. 4月11日,12:49 * pm,user923005< dcor ... @ connx.comwrote: On Apr 11, 12:49*pm, user923005 <[email protected]: 4月11日,12:38 * pm,lovecreatesbea ... @ gmail.com < lovecreatesbea ... @ gmail.comwrote: On Apr 11, 12:38*pm, "[email protected]"<[email protected]: 4月12日,3:35 * am,lovecreatesbea ... @ gmail.com ; On Apr 12, 3:35*am, "[email protected]" < lovecreatesbea ... @ gmail.comwrote: <[email protected]: 需要亩所以有时间完成这个C源代码行计数功能。 It takes mu so time to finish this C source code line count function. 对于错误拼写,我的意思是我需要一些时间。 Sorry for the error spell, I meant "It takes me some time". 不计算评论是错误的。 *它们是代码中b / b 的重要组成部分。 It''s a mistake not to count comments. *They are an important part ofthe code. 我的个人建议是保持简单: / * wc:计算行数,字数,字符数* / #include< stdio.h> int main(int argc,char * argv []) { int c, i, inword; FILE * fp; long linect, wordct, charct; long tlinect = 0, twordct = 0, tcharct = 0; i = 1; fp = stdin; printf(" lines单词chars file \ n"); printf(" ======= ======= ======= ===== \ n" ;); do { if(argc 1&&(fp = fopen(argv [i]," r"))== NULL){ fprintf(stderr,wc:不能打开%s \ nn,argv [i]); 继续; } linect = wordct = charct = inword = 0; while((c = getc(fp))!= EOF){ charct ++; if(c ==''\ n'') linect ++; if (c ==''''|| c ==''\t''|| c ==''\ n'') inword = 0; else if(inword == 0){ inword = 1; wordct ++; } } printf("%7ld%7ld%7ld" ,linect,wordct,charct); printf(argc 1?"%s \ n":" \ n",argv [i]); fclose(fp); tlinect + = linect; twordct + = wordct; tcharct + = charct; } while(++ i< argc); if(argc 2){ printf(" ======= === ==== ======= ===== \ n"); printf("%7ld%7ld%7ld total \ n",tlinect,twordct, tcharct); } 返回0; } My personal recommendation is to keep it simple like this: /* wc: count lines, words, chars */#include <stdio.h> int main(int argc, char *argv[]){int c,i,inword;FILE *fp;long linect,wordct,charct;long tlinect = 0,twordct = 0,tcharct = 0; i = 1;fp = stdin;printf(" lines words chars file\n");printf("======= ======= ======= =====\n");do {if (argc 1 && (fp = fopen(argv[i], "r")) == NULL) {fprintf(stderr, "wc: can''t open %s\n", argv[i]);continue;}linect = wordct = charct = inword = 0;while ((c = getc(fp)) != EOF) {charct++;if (c == ''\n'')linect++;if (c == '' '' || c == ''\t'' || c == ''\n'')inword = 0;else if (inword == 0) {inword = 1;wordct++;}}printf("%7ld %7ld %7ld ", linect, wordct, charct);printf(argc 1 ? "%s\n" : "\n", argv[i]);fclose(fp);tlinect += linect;twordct += wordct;tcharct += charct;} while (++i < argc);if (argc 2) {printf("======= ======= ======= =====\n");printf("%7ld %7ld %7ld total\n", tlinect, twordct, tcharct);}return 0;} 这篇关于C源代码行计数功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-30 07:32