OLD VERSION NEW VERSION WITH STL
| //eval.cpp
// this program: // - reads lines from standard input, each line represents an // infix expression consisting of non-negative real numbers and // valid operators;each line sould not contain more than 79 characters // - converts an infix expression to a postfix expression // - evaluates a postfix expression // - for each line of input, prints the original input string // and the result of evaluation // - if an error detected, prints an error message and takes an // appropriate action // exception handling is used to deal with error conditions #include <iostream.h>
int isoperator(char);
const int sizein = 80;
//max size of input line(infix //Expression)
while (cin.get(in, sizein)) { // try block
in2post(in, post);
// catch block
if (strcmp(str, "underflow") == 0) {
else if (strcmp(str, "opndst") == 0) {
else if (strcmp(str, "parentheses") == 0) {
else if (strcmp(str, "zero") == 0) {
else if (strcmp(str, "overflow") == 0) {
else if (strcmp(str, "empty_postfix") == 0)
catch (char ch) {
catch (int size) {
//control is transferred here after any
of the above catch //handlers
} // end while return 0; } // end main
// functions to convert and evaluate an expression: //converts an infix expression into postfix, returns
a pointer to
assert(infix);
Stack<char> *operst = new Stack<char>; //Stack
of //operators while (*infix) {
while (!operst->isempty()) {
*postfix = '\0';
//evaluates a postfix expression and returns the
result
double opnd1, opnd2, result;
assert(postfix);
result = opndst.pop(); } //returns true if oper is a valid operator, otherwise
returns false
return oper == '+' || oper == '-' ||
//returns a number that characterizes precedence
of an //operator:
//returns the result of the operation oper on
the opnd1 and //opnd2
if (oper == '+')
|
//eval.cpp
// this program: // - reads lines from standard input, each line represents an // infix expression consisting of non-negative real numbers // and valid operators;each line sould not contain more than // 79 characters // - converts an infix expression to a postfix expression // - evaluates a postfix expression // - for each line of input, prints the original input string // and the result of evaluation // - if an error detected, prints an error message and takes an // appropriate action // exception handling is used to deal with error conditions #include <iostream.h>
int isoperator(char);
const int sizein = 80;
//max size of input line (infix //expression)
main() {
while (cin.get(in, sizein)) { // try block
in2post(in, post);
// catch block
if (strcmp(str, "underflow") == 0) {
else if (strcmp(str, "opndst") == 0) {
else if (strcmp(str, "parentheses") == 0) {
else if (strcmp(str, "zero") == 0) {
else if (strcmp(str, "overflow") == 0) {
else if (strcmp(str, "empty_postfix") == 0)
catch (char ch) {
catch (int size) {
//control is transferred here after any
of the above catch //handlers
} // end while return 0; } // end main
// functions to convert and evaluate an expression: //converts an infix expression into postfix, returns
a pointer to
assert(infix);
stack<char> operst; //Stack
of operators
while (*infix) {
while (!operst.empty()) {
*postfix = '\0';
double opnd1, opnd2, result;
assert(postfix);
result = opndst.top(); }
return oper == '+' || oper == '-' ||
//returns a number that characterizes precedence
of an //operator:
//returns the result of the operation oper on
the opnd1 and //opnd2
if (oper == '+')
|