Click here to Skip to main content
15,895,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
CSS
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..
Posted
Comments
Member 10419043 16-Jan-14 4:52am    
please give me coding for count the number of text numbers in a txt file using c++

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:

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

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


and other words like:

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


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. :-)
 
Share this answer
 
v2
Comments
Richard MacCutchan 15-Jan-14 4:17am    
A very good analysis of the problem.

It is actually a little simpler than that, you just need to divide the number by 10 keeping the remainder. This remainder gives an index into the array for the units ("zero", "one", etc). If the number is still non-zero, then repeat that process for the tens, hundreds, thousands etc. Then put all the words to gether in order and you have the number in words.
HellMaster[cz] 15-Jan-14 13:47pm    
Hi Richard, honestly I don't understand how you mean the division. Because the guy who asked is not working with real numbers but numbers in textual form.

Lets go from the text our member included. There nothing to divide or em I missing something? :-)
follow this
click to get answer[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900