*
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?