Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   File Input/Output (http://www.velocityreviews.com/forums/t524520-file-input-output.html)

nkomli@gmail.com 07-24-2007 09:09 PM

File Input/Output
 
Okay, I want to allow the user to define their own variables and
equations and then use them to run a program. The user picks the
option to create a list of variables


If the user enters in r d and s at the prompt
how would you define each as a double variable?



Secondly I want to allow the user to enter in an equation that would
stored in a file than spit out when the program is running to
calculate the values. Let's say the user put

r+d+s


I was thinking of opening up a filestream and reading the user entered
equation into a file as a string

cin >> equation

fileout << equation


then reading it back into the string variable once the user chooses
the option to calculate values.

filein >> equation

and using the variables and the equation to calculate x.

x = equation

But I think it will give me errors since x just might spit out the
string. Can someone help me figure out a way to get this to work?

Thanks


Victor Bazarov 07-24-2007 09:21 PM

Re: File Input/Output
 
nkomli@gmail.com wrote:
> Okay, I want to allow the user to define their own variables and
> equations and then use them to run a program. The user picks the
> option to create a list of variables
> [..]


Programming an interpreter has been covered in literature for quite
some time now. Please google for "C++ expression interpreter".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask



ThreeRight 07-25-2007 07:05 AM

try using peek() and putback();
http://www.java2s.com/Code/Cpp/File/...inifstream.htm

nkomli@gmail.com 07-31-2007 02:06 AM

Re: File Input/Output
 
On Jul 24, 2:21 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> nko...@gmail.com wrote:
> > Okay, I want to allow the user to define their own variables and
> > equations and then use them to run a program. The user picks the
> > option to create a list of variables
> > [..]

>
> Programming an interpreter has been covered in literature for quite
> some time now. Please google for "C++ expression interpreter".
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask



Actually, I only get mostly this and related comments back with
google. I only want to parse equations and numerical values so
hopefully it won't be too complicated


Alf P. Steinbach 07-31-2007 02:17 AM

Re: File Input/Output
 
* nkomli@gmail.com:
> Okay, I want to allow the user to define their own variables and
> equations and then use them to run a program. The user picks the
> option to create a list of variables
>
>
> If the user enters in r d and s at the prompt
> how would you define each as a double variable?
>
>
>
> Secondly I want to allow the user to enter in an equation that would
> stored in a file than spit out when the program is running to
> calculate the values. Let's say the user put
>
> r+d+s
>
>
> I was thinking of opening up a filestream and reading the user entered
> equation into a file as a string
>
> cin >> equation


Use std::getline, you don't want tokens you want the whole line.



> fileout << equation
>
>
> then reading it back into the string variable once the user chooses
> the option to calculate values.


Uh, what's that got to do with anything? You could let the program do
anything in the meantime. You could let it play Tetris.

>
> filein >> equation
>
> and using the variables and the equation to calculate x.
>
> x = equation
>
> But I think it will give me errors since x just might spit out the
> string. Can someone help me figure out a way to get this to work?


Simplest is to use the user's equation to generate code in some
programming language (which could even be C++), then let your program
compile it if necessary, and run it.

Alternatively, look at e.g. the Boost Spirit parsing framework.

There's probably an example somewhere of using Spirit to do just what
you ask, a simple expression parser.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Victor Bazarov 07-31-2007 12:32 PM

Re: File Input/Output
 
nkomli@gmail.com wrote:
> On Jul 24, 2:21 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
>> nko...@gmail.com wrote:
>>> Okay, I want to allow the user to define their own variables and
>>> equations and then use them to run a program. The user picks the
>>> option to create a list of variables
>>> [..]

>>
>> Programming an interpreter has been covered in literature for quite
>> some time now. Please google for "C++ expression interpreter".
>>
>> V
>> --
>> Please remove capital 'A's when replying by e-mail
>> I do not respond to top-posted replies, please don't ask

>
>
> Actually, I only get mostly this and related comments back with
> google. I only want to parse equations and numerical values so
> hopefully it won't be too complicated


Get a hold of a copy of TC++PL by Stroustrup and open chapter 6
("Expressions"). It contains an example of expression interpreter
you could model/extend on.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask



Sjouke Burry 07-31-2007 09:27 PM

Re: File Input/Output
 
Victor Bazarov wrote:
> nkomli@gmail.com wrote:
>> On Jul 24, 2:21 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
>>> nko...@gmail.com wrote:
>>>> Okay, I want to allow the user to define their own variables and
>>>> equations and then use them to run a program. The user picks the
>>>> option to create a list of variables
>>>> [..]
>>> Programming an interpreter has been covered in literature for quite
>>> some time now. Please google for "C++ expression interpreter".
>>>
>>> V
>>> --
>>> Please remove capital 'A's when replying by e-mail
>>> I do not respond to top-posted replies, please don't ask

>>
>> Actually, I only get mostly this and related comments back with
>> google. I only want to parse equations and numerical values so
>> hopefully it won't be too complicated

>
> Get a hold of a copy of TC++PL by Stroustrup and open chapter 6
> ("Expressions"). It contains an example of expression interpreter
> you could model/extend on.
>
> V

Get c-snips from
http://www.cs.umu.se/~isak/snippets/
They have code for eval.c a simple command line calculator.
Also a ton of other small, usefull c cource.
Enjoy.

Jerry Coffin 08-05-2007 07:54 PM

Re: File Input/Output
 
In article <46afa934$0$25485$ba620dc5@text.nova.planet.nl>,
burrynulnulfour@ppllaanneett.nnlll says...

[ ... ]

> Get c-snips from
> http://www.cs.umu.se/~isak/snippets/
> They have code for eval.c a simple command line calculator.
> Also a ton of other small, usefull c cource.


Just FWIW, the home of this collection is www.snippets.org.

--
Later,
Jerry.

The universe is a figment of its own imagination.


All times are GMT. The time now is 05:08 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.