| 60.179.0.* |
1楼 |
|
|
- 共有19篇贴子
|
4楼 #include <stdlib.h> const char dos_line_end = '\n'; /* actually "\r\n" */ const char unix_line_end = '\r'; const char mac_line_end = '\n'; int main(int argc, char *argv[]) { const char default_line_end = #if defined(__UNIX__) unix_line_end; #elif defined(WIN32) dos_line_end; #else mac_line_end; #endif int c; char line_end = default_line_end; int word_count = 0; int line_count = 0; int in_a_word = 0; /* false */ FILE *tf; printf("[+] Word Count by akuma@tds\n"); if (argc < 2) { printf("[+] usage: %s file [type]\n", argv[0]); printf(" file: the file to be counted\n"); printf(" type: u for unix style (\\r), d for dos style (\\r\\n)," " m for Mac OS (\\n)\n"); return 1; } if (argc > 2) switch(argv[2][0]) { case 'u': case 'U': printf("[-] unix style: \\r will be count as the end of line\n"); line_end = unix_line_end; break; case 'd': case 'D': printf("[-] dos style: \\r\\n will be count as the end of line\n"); line_end = dos_line_end; break; case 'm': case 'M': printf("[-] mac os style: \\n will be count as the end of line\n"); line_end = mac_line_end; break; default: printf("[-] ingorne unknown type switch: %c\n", argv[2][0]); } if (NULL == (tf = fopen(argv[1], "r"))) { printf("[-] error on opening file: %s\n", argv[1]); return 2; } while (EOF != (c = fgetc(tf))) if (line_end == c) in_a_word = 0, line_count++; else if (isspace( c )) in_a_word = 0; else if (!in_a_word) in_a_word = 1, word_count++; if (line_count || word_count) line_count++; fclose(tf); printf("[-] File: %s\n", argv[1]); printf(" contains: %d word(s) in %d line(s).\n", word_count, line_count); return 0; } |
|
|
|
| 202.114.58.* |
7楼 !你在什么地方撒,来给我解释解释 |
|
|
| 59.64.133.* |
8楼 你还有脸说自己学了一年??? 我运行正常!!(编译环境:DevC++4.9.9.2 + winXP ) ps:我学了一个月. 稍作修改如下: #include <stdio.h> #include <stdlib.h> #include<ctype.h> const char dos_line_end = '\n'; /* actually "\r\n" */ const char unix_line_end = '\r'; const char mac_line_end = '\n'; int main(int argc, char *argv[]) { const char default_line_end = #if defined(__UNIX__) unix_line_end; #elif defined(WIN32) dos_line_end; #else mac_line_end; #endif int c; char line_end = default_line_end; int word_count = 0; int line_count = 0; int in_a_word = 0; /* false */ FILE *tf; printf("[+] Word Count by akuma@tds\n"); system("pause"); if (argc < 2) { printf("[+] usage: %s file [type]\n", argv[0]); printf(" file: the file to be counted\n"); printf(" type: u for unix style (\\r), d for dos style (\\r\\n)," " m for Mac OS (\\n)\n"); system("pause"); return 1; } if (argc > 2) switch(argv[2][0]) { case 'u': case 'U': printf("[-] unix style: \\r will be count as the end of line\n"); line_end = unix_line_end; break; case 'd': case 'D': printf("[-] dos style: \\r\\n will be count as the end of line\n"); line_end = dos_line_end; system("pause"); break; case 'm': case 'M': printf("[-] mac os style: \\n will be count as the end of line\n"); line_end = mac_line_end; system("pause"); break; default: printf("[-] ingorne unknown type switch: %c\n", argv[2][0]); system("pause"); } if (NULL == (tf = fopen(argv[1], "r"))) { printf("[-] error on opening file: %s\n", argv[1]); system("pause"); return 2; } while (EOF != (c = fgetc(tf))) if (line_end == c) in_a_word = 0, line_count++; else if (isspace( c )) in_a_word = 0; else if (!in_a_word) in_a_word = 1, word_count++; if (line_count || word_count) line_count++; fclose(tf); printf("[-] File: %s\n", argv[1]); printf(" contains: %d word(s) in %d line(s).\n", word_count, line_count); system("pause"); return 0; } 编译运行结果: [+] Word Count by akuma@tds 请按任意键继续. . . [+] usage: C:\Dev-Cpp\complied\character_count.exe file [type] file: the file to be counted type: u for unix style (\r), d for dos style (\r\n), m for Mac OS (\n) 请按任意键继续. . . 使用结果: C:\>\Dev-Cpp\complied\character_count.exe C:\Dev-Cpp\complied\character_count.cp p d [+] Word Count by akuma@tds 请按任意键继续. . . [-] dos style: \r\n will be count as the end of line 请按任意键继续. . . [-] File: C:\Dev-Cpp\complied\character_count.cpp contains: 262 word(s) in 86 line(s). 请按任意键继续. . . |
|
|
|
9楼 这两个写反了: const char unix_line_end = '\r'; const char mac_line_end = '\n'; 应该改为: const char unix_line_end = '\n'; const char mac_line_end = '\r'; |
|
|
|
| 221.12.62.* |
13楼 |
|
|
| 221.12.62.* |
14楼 |
|
|
| 221.12.62.* |
15楼 |
|
|
| 218.25.126.* |
16楼 |
|
|
| 218.25.126.* |
17楼 |
|
|
| 222.133.62.* |
18楼 freelinux123@163.com 密码: 123321 或者到 http://p2p.uying.com/html/20060628/913026099.stm 去下, 如果还需要其他的帮助请联系我 qq 591319715 email : y_z_q123@163.com 希望大家顶下,做linux内核分析的视频教程很累人,嗓子都哑了 ^_^ |
|
|
| 202.205.105.* |
19楼 知识太少了`~~ -_-! |
|
|
| 221.213.43.* |
22楼 File:实验 Description:求用户输入的string数组的单词的个数及字符数及输入的行数 Programmer:xxxx Date:07/12/2007 */(注:用c-free3.5编译器) #include<stdio.h> /*用getRow()函数来计算输入的字符串的行数并返回行数*/ int getRow(char string[]) { int i,rownumb; for(i=0,rownumb=0;(string[i]=getchar())!=EOF;i++) /*用getchar()函数输入字符串*/ if(string[i]=='\n') /*设置'\n'的个数为行数rownumb*/ rownumb++; return(rownumb); /*返回行数rownumb*/ } /*函数lengthNumb()用来求字符个数并返回字符的个数*/ int lengthNumb(char list[]) { int i=0,length=1; for( ;list[i]!=EOF;i++) /*length includes'\0'字符*/ length++; return(length); /*返回字符的个数*/ } /*函数wordNumb()用来求单词的个数并返回单词的个数wordnumb*/ int wordNumb(char list[]) { int j,t,wordnumb=0; t=0; /*设置一个索引值为t变量*/ for(j=0;list[j]!=EOF;j++) { if((list[j]==' ')||(list[j]=='\n')) /*对空格和\n进行判断*/ t=0; else if(t==0) { t=1; wordnumb++; } } return(wordnumb); /*返回单词的个数wordnumb*/ } /*主函数main()函数的开始*/ void main() { char numb[300]; /*定义一个字符数组*/ int rownumb,wordnumb,length; printf("Please enter a string:\n"); rownumb=getRow(numb); /*计算输入行数的函数getRow()在这儿被调用*/ printf("The string is located in %d rows.\n",rownumb); /*输出行数*/ length=lengthNumb(numb); /*求字符个数的函数在这里被调用*/ printf("The length of the string are %d.\n",length); /*输出字符数*/ wordnumb=wordNumb(numb); /*求单词个数的函数在这里被调用*/ printf("The string you entered includes %d words.\n",wordnumb); /*输出单词个数*/ } |
|
|
