473,232 Members | 1,419 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,232 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 2440
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
1
by: Rune Runnestø | last post by:
Hi, I have made a small program that doesn't work quite the way it should. It is a guestbook for the web, where visitors can write back their greetings. The program consists of 3 files: -...
3
by: sklett | last post by:
This BUG that is so ridiculous I can't believe they shipped 05 is making my life hell. I have tried the various solutions found on the web and none of them have worked thus far. What's even more...
351
by: CBFalconer | last post by:
We often find hidden, and totally unnecessary, assumptions being made in code. The following leans heavily on one particular example, which happens to be in C. However similar things can (and...
18
by: dbahooker | last post by:
team i'm having a tough time getting these data readers to work correctly I'd just like to be able to centralize my GetDataReader functions; so i can pass a simple SQL statement and be passed...
7
by: John Salmon | last post by:
I'm working with two libraries, one written in old school C, that returns a very large chunk of data in the form of a C-style, NUL-terminated string. The other written in a more modern C++ is...
5
by: Andy B | last post by:
I am trying to figure out how to make an object instance available for all methods of a class. I tried to do something like this: public class test { TheObject Instance = new TheObject();...
11
by: Rafe | last post by:
Hi, I'm working within an application (making a lot of wrappers), but the application is not case sensitive. For example, Typing obj.name, obj.Name, or even object.naMe is all fine (as far as...
8
by: sheldoncs | last post by:
System.InvalidOperationException was unhandled Message="Client found response content type of 'text/plain; charset=utf-8', but expected 'text/xml'. The request failed with the error message: --...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.