tags:

views:

133

answers:

5

Hi there! I have a text file which i geline to a string. the file is like this: 0.2abc 0.2 .2abc .2 abc.2abc abc.2 abc0.20 .2 . 20

i wanna check the result then parse it in to separate float. The result is: 0.2 0.2abc 2 20 2abc abc0.20 abc

this is expalined: check if there is 2 digit (before and after '.' (full stop)) whether with char or not. If only 1 site of the '.' is digit the '.' will be full stop.

The question is, how can i parse a STRING to separate result like that? I did use iterator to check the '.' and pos of it, but still got stuck.

Thx alot

Mongoloid

A: 

sounds like you could use some regex to extract your number.

Try this regex in order to extract the floating values within a string.

[0-9]+\.[0-9]+

Keep in mind that this won't extract integer values. ie 234abc

I don't know if there is a built-in way to use regex in c++ but i found this library with a quick google search which allows you to use regex in c++

Konstantinos
i'm not allowed using any other class. because i've just the beginner for this assignment.I getline then parse them then put into a bintree. Fist i have to parse those. That is 1 line of the text. there is other thing which i have to parse such as white space (multi-ws), common characters @#$%>< end etc...Thx for replying
+1  A: 

The first thing you need to do is split the input in words. Easy, just don't use .getline() but instead rely on `while (cin >> strWord ) { /* do stuff with word*/ };

The second thing is to kick out bad input words early: words of 2 characters or less, with more than one ., or with the . first or last.

You now know that the . is somewhere in the middle. find() will give you an iterator. ++ and -- give you the next and previous iterators. * gives you the character that the iterator points to. isdigit() tells you whether that character is a digit. Add ingredients together and you're done.

MSalters
is the cin >> strWord is istringstream? and is it default by whitespace? I have searched many article and question about this. However, i couldnt understand clearly of how to use it properly wit overload operators.
MSalters
A: 

Sounds like you should look at the "Interpreter" Design Pattern.

Or you could use the "State" Design Pattern and do it by hand.

There should be plenty of examples of both on the web.

iain
+1  A: 

Hi Mongoloid

Seems like some fairly complicated advice above -- and not necessarily helpful.

Your question does not make it entirely clear what the end result should look like. Do you want an array of floating point numbers? Do you just want the sum? Do you want to print out the results?

If you want help with homework, the best policy is to post your own attempt and then others can help you improve it, to make it work.

One approach that might help is to try to break the string into sub-strings (tokens) and discard the junk.

  • Write a function that accepts a character and returns true (this is part of a floating point number) or false (it isn't).
  • Scan along the string using an iterator or an index.
  • While current char is not part of a token, skip it.
  • If you find a token char, while current char is part of a token, copy it to another string
  • etc. to get all floating point substrings.
  • Then you can use std::stringstream or ::atof() to convert.

Have a bit of a go and post what you can get done.

Michael J
A: 

I have a text file to read:

Today!is-not-a-good-day, the>problem>is?this "what"link

More.about.decimal 0.2abc 0.2 .2abc .2 abc.2abc abc.2 abc0.20 .2 . 20

More;sample/about'thing'which\call=(separator?of)all Today

This is the best line

the program will check the special characters first then add to bintree then count them. the problem of ispunct() and isspace() is is fine. however, with the full stop. as the line More.about.decimal 0.2abc 0.2 .2abc .2 abc.2abc abc.2 abc0.20 .2 . 20

the program will check decimal first. with the '.' between 2 digit => decimal such as 0.2 or 0.2abc or abc0.2 . otherwise, it will be come full stop such as .2 => 2; abc.2 => abc AND 2; and etc...

I was trying but the program doesn't compile. I m trying now with string pos and npos. then i will put it later on.

thx and regard.

p.s i can only use bintree (no map) and no template just function.