472,110 Members | 2,297 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,110 software developers and data experts.

Making a string into a c++ line of code

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.

Mar 4 '06 #1
5 2327
TB
bs********@isp.com 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

--
TB @ SWEDEN
Mar 4 '06 #2
* bs********@isp.com:
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;
}
}


--
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?
Mar 4 '06 #3
Alf P. Steinbach wrote:
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?Rec...scentParserCpp

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++.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Mar 4 '06 #4
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/co...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.

Mar 9 '06 #5
bs********@isp.com wrote:
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/co...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
Mar 9 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Rune Runnestø | last post: by
351 posts views Thread by CBFalconer | last post: by
18 posts views Thread by dbahooker | last post: by
7 posts views Thread by John Salmon | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.