对一个英文书写的文件,进行单词和行数的统计问题?

到百度贴吧首页
新闻   网页   贴吧   知道   MP3   图片   视频   百科
    吧内搜索 | 帮助
  • 共有19篇贴子

对一个英文书写的文件,进行单词和行数的统计问题?

60.179.0.*

1楼

对一个英文书写的文件,进行单词和行数的统计问题

4楼

#include <stdio.h>
#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;
}

6楼


202.114.58.*

7楼

你假的吧,我学了1年c语言,不知道你在放什么屁
!你在什么地方撒,来给我解释解释

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';

10楼

有的C语言语句我没学到!!
书上的连SETCOLOR都没有~~

11楼

楼上的,setcolor 不是标准 C 的东西,也就是不保正一定有这个函数,纯粹教 C 语言的书不应该涉及这些非标准的东西的。

12楼

会不会流程图啊
221.12.62.*

13楼

流程图谁知道怎么弄吗???
221.12.62.*

14楼

能不能给我具体分析一下啊,谢谢
221.12.62.*

15楼

各位帮帮忙吧,我真的很急,拜托拜托
218.25.126.*

16楼

能不能告诉我在一篇文章中找出相同的单词并说出出现的次数。拜托了
218.25.126.*

17楼

用c语言编,真是拜托了
222.133.62.*

18楼

打家好,我有linux内核分析的教程 ,需要的话请到
 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楼

太高深了吧~~~我要答辩呀`~~要学好久啦~~
知识太少了`~~
-_-!

20楼

靠谁挖坟……

21楼

真好!
我也想找个高手帮我解决一个问题。
也是关于英文单词的!

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); /*输出单词个数*/
}

发表回复

内 容:
用户名:
  
©2010 Baidu 贴吧协议  意见反馈