Making a string into a c++ line of code

B

bsmith1111

I'm working on a simple calculator similar to the MS Calculator in
visual c++ 6.0. I'm trying to teach myself the visual aspect of c++ on
my own time, so I've decided to jump right into a program after reading
through a lot of material that has gotten me familiar with it.

I have a result edit box at the top of the window which holds whatever
is being typed in via the numerical buttons and arithmetic buttons (+,
-, *, /). Each numerical digit will add its number (a string) to the
result edit box (making the result's string larger). Eventually, the
edit box may contain-> 6+3+2-4*8, all in a string.
When I press the enter button on my calculator, I would like the
program to just convert the edit box's content (the string) to the
result; in this case the box would contain -21 (following the default
order of operations).

I've looked around on the internet for some time as well as my several
c++ books - I have been unable to find anything remotely related to
what I want to do, and am starting to assume it can not be done since I
see no way a compiled program will interact with a line of code. Can
someone please help me? Thanks for your time.
 
T

TB

(e-mail address removed) skrev:
I'm working on a simple calculator similar to the MS Calculator in
visual c++ 6.0. I'm trying to teach myself the visual aspect of c++ on
my own time, so I've decided to jump right into a program after reading
through a lot of material that has gotten me familiar with it.

I have a result edit box at the top of the window which holds whatever
is being typed in via the numerical buttons and arithmetic buttons (+,
-, *, /). Each numerical digit will add its number (a string) to the
result edit box (making the result's string larger). Eventually, the
edit box may contain-> 6+3+2-4*8, all in a string.
When I press the enter button on my calculator, I would like the
program to just convert the edit box's content (the string) to the
result; in this case the box would contain -21 (following the default
order of operations).

I've looked around on the internet for some time as well as my several
c++ books - I have been unable to find anything remotely related to
what I want to do, and am starting to assume it can not be done since I
see no way a compiled program will interact with a line of code. Can

The string is not code. It's input and it's up to you to parse it.
someone please help me? Thanks for your time.

It's rather simple. This is a perfect project to get a feeling for
implementing algorithms.

(See for example Operator-precedence parsing.) There is a lot of
information about this on the Internet, e.q.:

http://www.engr.mun.ca/~theo/Misc/exp_parsing.htm
http://www.cs.princeton.edu/introcs/44stack/
http://www.willamette.edu/~fruehr/348/lab3.html
 
A

Alf P. Steinbach

* (e-mail address removed):
I'm working on a simple calculator similar to the MS Calculator in
visual c++ 6.0. I'm trying to teach myself the visual aspect of c++ on
my own time, so I've decided to jump right into a program after reading
through a lot of material that has gotten me familiar with it.

I have a result edit box at the top of the window which holds whatever
is being typed in via the numerical buttons and arithmetic buttons (+,
-, *, /). Each numerical digit will add its number (a string) to the
result edit box (making the result's string larger). Eventually, the
edit box may contain-> 6+3+2-4*8, all in a string.
When I press the enter button on my calculator, I would like the
program to just convert the edit box's content (the string) to the
result; in this case the box would contain -21 (following the default
order of operations).

I've looked around on the internet for some time as well as my several
c++ books - I have been unable to find anything remotely related to
what I want to do, and am starting to assume it can not be done since I
see no way a compiled program will interact with a line of code. Can
someone please help me? Thanks for your time.

This is probably a FAQ -- check the FAQ.

Anyway, you can code an expression evaluator yourself, or find and use
an existing one.

Here's a very simple one, that only works for very simple expressions
and only in Microsoft Windows (which is the OS you're using):

#include <cstdlib> // std::system
#include <iostream> // std::cin, std::cout
#include <istream> // operator>>
#include <fstream> // std::ifstream
#include <ostream> // operator<<, std::endl
#include <stdexcept> // std::runtime_error
#include <string> // std::string, std::getline

double eval( std::string const& s )
{
char const filename[] = "calcresult.txt";

// Microsoft Windows.
std::system( ("set /a " + s + " >" + filename).c_str() );
std::ifstream resultFile( filename );
double result;
resultFile >> result;
if( resultFile.fail() ) { throw std::runtime_error( "Oops" ); }
return result;
}

int main()
{
std::string expression;

std::cout << "Enter a numeric expression: ";
std::getline( std::cin, expression );
try
{
std::cout << eval( expression ) << std::endl;
return EXIT_SUCCESS;
}
catch( std::exception const& x )
{
std::cerr << "!" << x.what() << std::endl;
return EXIT_FAILURE;
}
}
 
P

Phlip

Alf said:
Anyway, you can code an expression evaluator yourself, or find and use an
existing one.

This one doesn't suck (in my exalted opinion):

http://www.xpsd.org/cgi-bin/wiki?RecursiveDescentParserCpp

Note how my techniques make adding new features to the parser very, very
easy.

Warning: Inventing new languages on the fly is a bad habit. If this were my
project, I would link to Ruby, and evaluate arbitrarily complex expressions
with only a few lines of C++.
 
B

bsmith1111

TB - thanks for letting me know what it was that I was trying to do.
I've read into a lot about parsing now, and now know what it is and how
the several methods are accomplished.
Alf - I used your parser, and it works well for simple things. I've
tried to implement a decimal feature into it, but to no avail.
Phlip - I tried your parser - it works very well as far as I can tell,
but I can not figure out how to input a string value into your parser
(a lot of your syntax looks foreign....I'm trying to figure out what
much of it does). I also looked into Ruby, but I think I'm going to
stick to C++ for parsing for now, even though it will be much more
challenging and time-consuming.

I also have found another expression parser (very well detailed, and
also easily understandable, though older code) at
http://www.dcs.qmul.ac.uk/~keithc/compilers/posted-expr-code-96-04-026.html,
but as in Alf's parser, I've been unable to implement decimal numbers
into it (I'm using 1+1.01 as a test expression, but can not get it to
work and output the answer of 2.01). Any more ideas will be greatly
appreciated - thanks for all of your help.
 
S

Sgt. York

TB - thanks for letting me know what it was that I was trying to do.
I've read into a lot about parsing now, and now know what it is and how
the several methods are accomplished.
Alf - I used your parser, and it works well for simple things. I've
tried to implement a decimal feature into it, but to no avail.
Phlip - I tried your parser - it works very well as far as I can tell,
but I can not figure out how to input a string value into your parser
(a lot of your syntax looks foreign....I'm trying to figure out what
much of it does). I also looked into Ruby, but I think I'm going to
stick to C++ for parsing for now, even though it will be much more
challenging and time-consuming.

I also have found another expression parser (very well detailed, and
also easily understandable, though older code) at
http://www.dcs.qmul.ac.uk/~keithc/compilers/posted-expr-code-96-04-026.html,
but as in Alf's parser, I've been unable to implement decimal numbers
into it (I'm using 1+1.01 as a test expression, but can not get it to
work and output the answer of 2.01). Any more ideas will be greatly
appreciated - thanks for all of your help.

The OP has fallen out of my buffer so forgive me if I'm off-topic here
but speaking of parsers, I have just used the boost spirit parser for a
project and found it to be excellent. I highly recommend it.

-York
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top