如何使用c ++计算txt文件中的文本数量 [英] how to count the number of text numbers in a txt file using c++

查看:119
本文介绍了如何使用c ++计算txt文件中的文本数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

the text file looks like:
one hello hi
twenty-five billion
fifty maths
three thousand and two

output:
count the number of text numbers: 4

please Give me any idea..

推荐答案

你好。在我开始之前,我不是一个专业的程序员,它只是我的爱好,我在C编程,但这里是我的建议如何开始:



1你的程序似乎是一些解析器/词法分析器的任务,如Lex和Yacc(FLEX& BISON)以及其他类似的东西。



我试图学习Lexx和Yaxx,但对于那些没有经验的人来说它有点复杂,所以我放弃了。但有一天我会回到它,因为它是解析这种东西的强大工具,在这种情况下它会派上用场。



如果你真的想要在C ++中自己编程,这是我将如何继续:



1:打开文件并将其读入缓冲区



2:逐字读取缓冲区(将其标记化)并将该单词与首先需要创建的预定义库进行比较。



0:创建与数字相关的单词缓冲区(您的文本编号库),如:



Hello there. Before I start, I'm not a professional programmer, its just my hobby and I'm programming in C, but here are my suggestions how to start:

1. Your program seems to be a task for some parser/lexical analyzer like Lex and Yacc (FLEX & BISON) and other stuff like this.

I was trying to learn Lexx and Yaxx, but its a bit complicated for someone who does not have experiences with it and so I quit. But I will return to it somedays, because its a powerful tool for parsing and this kind of stuff and it comes in handy in such cases.

In case you really want to program it by yourself in C++ here is how I would proceed:

1: Open the file and read it into a buffer

2: Read the buffer word by word (tokenize it) and compare the word to some predefined library which you need to create on the first place.

0: Create a buffer of words related to numbers (your text numbers library) like:

char *digits[] = {"zero", "one", "two", "three"...};




char *tenths[] = {"twenty", "thirty", "forty"....}





和其他词语如下:





and other words like:

char *rest[] = {"hundred", "thousand", "million", ....}





现在让我们回到第二点。从文件中获取令牌后(您可以使用strtok()函数获取令牌),您可以将该单词与库中的单词进行比较。



比较是最复杂的任务。你必须以某种方式决定,如果它是一个数字或一个像125255这样的数字,它将在文本中类似于:



一百二十五数千两百五十五



或类似的东西。



你需要认识到,如果这是一个复杂的数字,通常是一个数字,或者这些是6个不同的数字。所以你需要创建一个方案,如:



检查一下这个词。

这是一个数字吗?是的 - >现在决定,如何进行 - >检查它是否是一百,一千,百万左右。是吗?是的,那么我们假设,这是一个复数,你必须将一和百计为1。这实际上称为gramatic(在YACC中使用等)。根据这个你区分不同的情况,并决定,做什么。在你的情况下,只是决定,如果它是一个复杂或简单的nubmer并增加你的变量。



希望你会发现这对初学者很有用。 : - )



Now lets return to our second point. After you get the token back from the file (you can use strtok() function to get the tokens), you can compare the word with words from your library.

The comparison is the most complex task. You have to decide somehow, if its a single digit or a number like 125255, which would be in text something like:

"one hundred and twentyfive thousands two hundred and fiftyfive"

or something like that.

You need to recognize, if its a complex number like this which is in general a single number or if these are 6 different numbers. So you need to create a scheme like:

Check the word.
Is it a number? Yes -> now decide, how to proceed -> Check if its a hundred, thousand, million or so. Is it? Yes, then we assume, that is a complex number, and you have to count the "one" and "hundred" as 1 number. This is actually called a gramatic (used in YACC and so). According to this you distinguish between different cases and decide, what to do. In your case "just" decide, if its a complex or simple nubmer and increase your variable.

Hopefully you'll find this useful for a starter. :-)


按照这个

点击以获得答案 [ ^ ]


这篇关于如何使用c ++计算txt文件中的文本数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆