C++ handling input read from a text file and recursive function

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 2415 Views - Last Post: 14 September 2011 - 05:58 PM Rate Topic: -----

#1 XodoX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 04-November 10

C++ handling input read from a text file and recursive function

Posted 13 September 2011 - 04:38 PM

I am using getline to read input from a text file. I know there might be better options, but I want to go with getline. It stores the input in line. The input is just a text file with mathematical operations ( only * and + ), so something like 1*10+30*3 etc. The getline gets me each full line. Since it is stored in line, so I have to do something to get the data out of it? I figured I can just use a loop to check for certain conditions, but that's where I run into problems. How do I do this - the calculation? I have to somehow tell the code how to solve those multiplications and additions with a recursive function. I have no idea how to do this. Does anybody know?

Edit: Forgot to post it.

std::ifstream inFile("file1.txt", std::ios::in);
if ( ! inFile.is_open() ){
std::cerr << "Error: unable to open file for input" << std::endl;
return 1;
}


std::ofstream outFile( "file2.txt", std::ios::app ); // Open with append flag
if ( ! outFile.is_open() ){
std::cerr << "Error, unable to open file for output" << std::endl;
return 2;
}

while ( inFile.good() ){
std::string line;
getline( inFile, line)


outFile << line << std::endl; // Write the file to the output file
}


This post has been edited by XodoX: 13 September 2011 - 04:44 PM


Is This A Good Question/Topic? 0
  • +

Replies To: C++ handling input read from a text file and recursive function

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: C++ handling input read from a text file and recursive function

Posted 13 September 2011 - 04:51 PM

draw the expression evaluation tree to get the feel of how you can apply recursion to it..

1*10+30*3

             +
           /    \
          *      *
         / \    / \
         1  10  30 3


hope this helps.

This post has been edited by mostyfriedman: 13 September 2011 - 04:52 PM

Was This Post Helpful? 0
  • +
  • -

#3 XodoX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 04-November 10

Re: C++ handling input read from a text file and recursive function

Posted 13 September 2011 - 04:56 PM

View Postmostyfriedman, on 13 September 2011 - 04:51 PM, said:

draw the expression evaluation tree to get the feel of how you can apply recursion to it..

1*10+30*3

             +
           /    \
          *      *
         / \    / \
         1  10  30 3


hope this helps.


Sorry, it dosen't. Maybe loop for the * first and then for the +... don't know.

multiplying two integers would be this:

int multiply(int x, int y)

{

if (x == 0)

{

return 0;

}

if (x == 1)

{

return x;

}

if (x > 1)

{

return (x + multiply(x,--y));

}


}



But I want to add addition and it needs to do a combination of both.. no idea.

This post has been edited by XodoX: 13 September 2011 - 05:04 PM

Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: C++ handling input read from a text file and recursive function

Posted 13 September 2011 - 05:57 PM

The idea is that i've split the expression at the '+' which has the lowest precedence, I evaluate the expression of the right side of the '+', and on the left side, then I add them together..
Was This Post Helpful? 0
  • +
  • -

#5 XodoX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 04-November 10

Re: C++ handling input read from a text file and recursive function

Posted 13 September 2011 - 06:28 PM

View Postmostyfriedman, on 13 September 2011 - 05:57 PM, said:

The idea is that i've split the expression at the '+' which has the lowest precedence, I evaluate the expression of the right side of the '+', and on the left side, then I add them together..


I still don't understand it because isn't this what I said - search for the + first, add them and then look for the * ? If so, how? Just a loop?
Was This Post Helpful? 0
  • +
  • -

#6 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: C++ handling input read from a text file and recursive function

Posted 13 September 2011 - 06:35 PM

that way you will not get correct output.. remember that * has higher precedence than +, so addition should be applied last, that's why its at the top of the tree.
Was This Post Helpful? 0
  • +
  • -

#7 XodoX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 04-November 10

Re: C++ handling input read from a text file and recursive function

Posted 13 September 2011 - 07:02 PM

View Postmostyfriedman, on 13 September 2011 - 06:35 PM, said:

that way you will not get correct output.. remember that * has higher precedence than +, so addition should be applied last, that's why its at the top of the tree.


Nvm. I know. Just made a mistake. In the second post I said * and then +.

That still leave the question - how. Maybe bool and then you can check somehow..

Or like this example here

http://www.cplusplus...ng/string/find/
Was This Post Helpful? 0
  • +
  • -

#8 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: C++ handling input read from a text file and recursive function

Posted 13 September 2011 - 07:03 PM

let me explain why this is simple using recursion. First of all, what is an expression??. an expression can be anything of these forms:

EXP := a (where a is an integer)
EXP := EXP + EXP
EXP := EXP * EXP

this is just a simple definition of an expression (+ and * only are used), it can either be an integer, or an expression plus an expression, or an expression multiplied by an expression.

Now if we have an expression, how can we evaluate it??..we have to obey the rules of operator precedence, so since we are only dealing with + and * here, + should be the last operator to be applied. So if the expression is for example 4*2+7*8+1 then we need to perform the multiplications first (4*2)+(7*8)+1. then we apply the addition.

So as you can see if we break the expression up into a tree structure to see how it will get evaluated, it will look like this

           +
       /       \
      *         +
     / \       /  \
    4   2      1   *
                  / \
                  7  8



I hope you understand this one..if you still have any questions let me know.
Was This Post Helpful? 0
  • +
  • -

#9 XodoX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 04-November 10

Re: C++ handling input read from a text file and recursive function

Posted 13 September 2011 - 07:08 PM

Yeah, I forgot. If I do it using bool or the other example there, that wouldn't be a recursive function, right? But a recursive function is a loop. I still don't get where you're going with the recursion because I don't see a pattern. I know I have to obey the rules, but my question is how do I do this using C++. I understand what you're trying to say about the calculation, but sorry, I still don't see how I can do this in C++....
Was This Post Helpful? 0
  • +
  • -

#10 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: C++ handling input read from a text file and recursive function

Posted 13 September 2011 - 07:23 PM

ok, I am starting to understand what you're having a hard time understanding now. let's say the expression is 5*5+3*6, since i am gonna do the addition last, i am gonna put it aside...now I am left with 2 expressions 5*5, and 3*6, I need to evaluate both, then add them together. How do I do this in C++?, find_first_of() is all you need.
Was This Post Helpful? 0
  • +
  • -

#11 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: C++ handling input read from a text file and recursive function

Posted 13 September 2011 - 07:40 PM

you may also wanna take a look at substr()
Was This Post Helpful? 0
  • +
  • -

#12 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: C++ handling input read from a text file and recursive function

Posted 13 September 2011 - 07:53 PM

try to give it an honest try, if you can't figure it out then here's the code..try to understand it though.

Spoiler


you may want to check the documentation if you find a function I've used that you don't understand.
Was This Post Helpful? 1
  • +
  • -

#13 XodoX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 04-November 10

Re: C++ handling input read from a text file and recursive function

Posted 13 September 2011 - 08:33 PM

View Postmostyfriedman, on 13 September 2011 - 07:53 PM, said:

try to give it an honest try, if you can't figure it out then here's the code..try to understand it though.

Spoiler


you may want to check the documentation if you find a function I've used that you don't understand.


Thank you very much. I looked at find_first_of() and I understand that. Not sure yet about substr, though, but I will figure this out. "exp" would be "line" ( OP ) in this case, correct?
Was This Post Helpful? 0
  • +
  • -

#14 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: C++ handling input read from a text file and recursive function

Posted 13 September 2011 - 08:37 PM

Quote

"exp" would be "line" ( OP ) in this case, correct?


I don't understand what you mean here.
Was This Post Helpful? 0
  • +
  • -

#15 XodoX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 04-November 10

Re: C++ handling input read from a text file and recursive function

Posted 14 September 2011 - 05:55 AM

"Eval" is the string "exp". Since the getline stores the input in "line", I put line instead of "exp". It directly handles the input, does it not?

static int eval(string line)


This post has been edited by XodoX: 14 September 2011 - 06:04 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2