On the realization of a string expression evaluation

Because of its lack of active thinking, logical thinking is not clear enough, so the master of the younger brother I arranged a job, a string expression evaluation, in the hope to achieve the purpose of my exercise logical thinking ability.
Which lasted 14 days, to complete the job, knowledge and technology is not advanced, the purpose is to exercise logical thinking ability. Here also want to have a share related needs of students at problem-solving ideas, inadequacies also hope you have educated us, pointing out. Thank you.

To resolve the first of operator precedence is determined problem to solve this problem, later learned that the postfix expression (i.e., reverse Polish notation), the expression determined first decomposed into reverse Polish notation, and then remove each operator according to figures corresponding operation. It is the last calculated value of the expression
involves string string, Vector dynamic arrays, reverse Polish notation, the iterator (and removed from the main conversion range for use) related (by the corresponding analytical line, and does not appear on the book) Know how.

Low version (0-9 supports only integer calculation can be calculated comprises operator + - * /% ^ & | )
code is as follows: `@ Prototype: Double Exper is getting (const char * expr);
// Use: retval = Expr Double ( "*. 1 + 2 (10/4) -3 ^ 1234");
#include <the iostream>
#include <String>
#include <sstream>
#include <Vector>
#include <Stack>
#include < cstdio>
the using namespace STD;

bool gettruefalse (char a) // determines whether the current symbol is a symbol
{
IF (A == '+' || A == '-' || A == '*' || A == '/' A || == '%' || \
A == '^' || A == '|' || A == '&' || A == '(' || A == ')')
return to true;
the else
return to false;
}

getPriority int (char C)
{
int TEMP;
IF (C == '(')
TEMP =. 6;
the else IF (C == '*' || C == '/' || C == '%')
TEMP . 5 =;
the else IF (C == '+' || C == '-')
TEMP =. 4;
the else IF (C == '&')
TEMP =. 3;
the else IF (C == '^')
TEMP . 1 =;
the else IF (C == '|')
TEMP =. 1;
the else IF (C == '')) // If ''), the symbols have been taken into the new string until it reaches' ( '(. 6)
TEMP = 0;
return TEMP;
}

getnbl String (String STR)
{
String OK; // convert into a container for storing postfix expression
vector <char> flag; // storage containers for converting symbol

while (! str.length () = 0 ) // old string before the conversion is complete, an infinite loop
{
for (int A = 0; A <str.length (); A ++) // Traversal string
{
IF (gettruefalse (str [a])) // if the current character is a symbol
{
// If the container is empty symbol, a symbol or the current priority is greater than the priority of the vessel symbol, or a symbol as the last symbol of the container (press-fit
if (flag.size () == 0 || (getPriority (STR [A])> getPriority (* (flag.end () -. 1))) || getPriority (* (flag.end () -. 1)) ==. 6)
{
flag.push_back (str [a]); // current symbol into the symbol container
str.erase (str.begin (), str.begin ( ) + 1); // old truncation symbol string
break; / / out loop for re-iterate
}
the else IF (getPriority (STR [A]) == 0) // a) morpholin
{
str.erase (str.begin (), str.begin () +. 1); // delete off the old string), symbol and sign of the vessel to a new string until it encounters (
the while (getPriority (* (flag.end () -! 1)) = 6) // encountered (before unlimited pop container symbols
{
ok + = * (flag.end () - 1); // add symbols to the new symbol character container
flag.erase (flag.end () - 1) ; // just to add a new character to the character deleted go
}
IF ((getPriority (* (flag.end () -. 1)) ==. 6)) // the (removed
{
flag.erase (flag.end () -. 1);
BREAK; // skip to the beginning while at cycle
}
}
the else IF (getPriority (STR [a]) <getPriority = (* (flag.end () -. 1))) // current symbol equal to or less than the priority of the last one of the symbols container
{
OK + = * ( flag.end () - 1); // add symbols to the new symbol character container
flag.erase (flag.end () - 1) ; // add to the character just deleting the new character
BREAK;
}
}
else // if the current character is not a symbol (that is, the decimal numbers or)
{
OK + STR = [a]; // new string into the digital
str.erase (a, 1); // old character remove the digital stream
BREAK; // for loop out, again traversing
}
}
}
// old empty string, the string temporarily storing sequentially points out into new characters
while (flag.size() != 0)
{
ok += *(flag.end() - 1);
flag.erase(flag.end() - 1);
}
return ok;
}

int jisuan (int a, char b , int c) // according to the sign of the corresponding digital computing
{
int NUM = 0; // calculations
IF (B == '+')
NUM = A + C;
the else IF ( == B '-')
NUM = A - C;
the else IF (B == '*')
NUM = A * C;
the else IF (B == '/')
NUM = A / C;
the else IF (B = = '%')
NUM = A% C;
the else IF (B == '^')
NUM = A ^ C;
the else IF (B == '&')
NUM = A & C;
the else IF (B == ' | ')
NUM = A | C;
the else IF (B ==' ') M
NUM = A && C;
the else IF (B ==' N ')
NUM = A || C;
return NUM;
}

getCount int (String NBL)
{
Vector <int> the nums;
int A = 0;
int answer; // store result
String zhuanhuan;
the while (A <nbl.size ())
{
IF (gettruefalse (NBL [A])) / / If symbol
{// penultimate number is removed from the container as the number a, the last number as B, the symbol operations performed
answer = jisuan (* (nums.end ( ) - 2), nbl [a] , * (nums.end () -. 1));
nums.erase (nums.end () - 2, nums.end ());
nums.push_back (answer);
a ++; // the next character to traverse a
}
the else / / digital personal digital pressure vessel
{
zhuanhuan NBL = + [A];
answer the atof = (zhuanhuan.c_str ());
nums.push_back (answer);
zhuanhuan.clear ();
A ++;
}
}
return * (nums.end () - 1); // container is the last digit of the result
}

main int ()
{
char CH [100];
COUT << "input character string you want to calculate the expression (only positive integer arithmetic 0-9):" << endl;
gets_s (CH);
COUT << " the original contents of the string is: "<< ch << endl;

string str (ch); // for string conversion operations
string nbl; // for storing converted into postfix expression container
nbl = getnbl (str); // call the function character string to Reverse Polish string
"after turning reverse Polish notation:" COUT << << endl << NBL;
int NUM = getCount (NBL);
COUT << "string:" << << CH "result is:" << num << endl;

system("pause");
fflush(stdin);
return 0;
}`

 

Complete Edition (positive or negative integers may be calculated at this time (no decimal point), the operator also includes + - * /% & && ^ | ||):

```
//原型: double Exper(const char * expr);
//使用: double retval = Expr("1*2+(10/4)-3^1234");
#include<iostream>
#include<string>
#include<vector>
#include<cstdio>
using namespace std;

int gettruefalse (char a) // determine those precoding Zeina为符No.
{
an if (a == '1' || a == '2' || a == '3' || a == '4' || a == '5' || \
a == '6' || a == '7' || a == '8' || a == '9' || a == '0')
return 2;
If Else (A == 'Tasu' || A == '-' || A == '*' || A == '/' || A == 'Pasento' || \
A == '^' A == || '|' || A == 'Ando' || A == '(' || A == ')' || A == 'M' || A == 'N')
Return 1;
the else
return 0;
}

int getpriority (char c) // prioritized
{
int TEMP;
IF (C == '(')
TEMP =. 8;
the else IF (C == '*' || C == '/' C == || '%')
TEMP =. 7;
the else IF (C == '+' || C == '-')
TEMP =. 6;
the else IF (C == '&')
TEMP =. 5;
the else IF (C == '^')
TEMP =. 4;
the else IF (C == '|')
TEMP =. 3;
the else IF (C == 'M') is equivalent to // &&
TEMP = 2;
the else IF (C == 'N' ) // equivalent ||
TEMP =. 1;
the else IF (C == '')) // If ''), the symbols have been taken into the new string until it reaches '(' (. 6)
TEMP 0 =;
return TEMP;
}

getVariant void (String & STR)
{
int A = 0; // for traversing
for (a = 0; a < str.size (); a ++) // original string - (symbol) with the corresponding identifier Alternatively
{
IF ((A == 0) && (STR [A] == '-'))
{
STR [A] = '.';
}
the else IF ((STR [A] == '-') && ( ! gettruefalse (str [a - 1 ]) == 1) && (str [a - 1] = ')')) // find - and confirm whether the current symbol
{
STR [A] = '.';
}
}
for (a = 0; a < str.size (); a ++) // original string ||, && alternative with corresponding identifier
{
IF (STR [a] == '|' && STR [a . 1 +] == '|')
{
STR [A +. 1] = 'N';
str.erase (A,. 1);
}
IF (STR [A] == '&' && STR [A +. 1] = = '&')
{
str[a + 1] = 'M';
str.erase(a, 1);
}
}
}

getnbl String (String STR)
{
getVariant (STR); // call the function with the marker in the replacement string ||, &&, - (minus)
String OK; // convert into a container for storing postfix expression
vector <char> flag; // storage containers for converting symbols
int a = 0; // iterate for a string subscript
int b = a; // b as the starting point of the digital traversing!

while (a <str.length ()) // traverse the entire string
{
IF ((gettruefalse (STR [A])) ==. 1) the current character is a symbol //
{
// symbol container is empty or the priority of the current symbol end of the container is greater than the priority symbol, or end of the current container (, pushed into the container
if (flag.size () == 0 || (getpriority (str [a])> getpriority (* (flag.end () - 1) )) \
|| * (flag.end () -. 1) == '(')
{
flag.push_back (STR [A]);
A ++;
}
the else IF (STR [A] == ')') // If) is not pressed in, the pop-up vessel until it encounters symbols (
{
the while (* (flag.end () -!. 1) = '(') is not the current symbol // (, the pop-up container symbols
{
OK + = * (flag.end () -. 1);
OK + = ''; // each symbol interval as a space
flag.erase (flag.end () -. 1);
}
flag.erase (flag.end () - 1); // delete the vessel (
a ++; // skip '') symbol
}
else if (getpriority (str [a ]) <= getpriority (* (flag.end () - 1))) // priority less than the current symbol equal to the symbol or the last symbol of the container
{
OK + = * (flag.end ( ) - 1); // add to a new symbol character symbol container
OK + = '';
flag.erase (flag.end () - 1); // just added to the character of the new character omitted
}
}
else // not a symbol, the symbol is found traversal string
{
B = a; // record the current position of the traversing of
the while (a <str.size ())
{
IF ((gettruefalse (STR [a]). 1 == )) // if the current character is a symbol
{
ok.append (str.begin () + B, str.begin () + a); // will traverse all characters starting position to the detection position is spliced to the new symbol string, plus the space as a spacer and
OK + = '';
BREAK; // for out re-determination loop
}
the else IF ((a == (str.size () -. 1)) && (gettruefalse (STR [ a]) == 2)) // the last character has been traversed and the last character is a digit
{
a ++;
ok.append (str.begin () + b, str.begin () + a); // be traversed to find the starting position of all characters splice location to a new symbol string, and add a space as a spacer
+ = OK '';
BREAK; // for out re-determination loop
}
a ++;
}
}
}
the while (flag.size () = 0!) // after traversing the original string, all the symbols of the symbol container eject
{
* + = OK (flag.end () -. 1);
OK + = ''; // each symbol interval as a space
flag.erase (flag.end () -. 1);
}
return OK;
}

int getanswer(int a, char b, int c)
{
int num = 0;//计算结果
if (b == '+')
num = a + c;
else if (b == '-')
num = a - c;
else if (b == '*')
num = a * c;
else if (b == '/')
num = a / c;
else if (b == '%')
num = a % c;
else if (b == '^')
num = a ^ c;
else if (b == '&')
num = a & c;
else if (b == '|')
num = a | c;
else if (b == 'M')
num = a && c;
else if (b == 'N')
num = a || c;
return num;

}

int getcount (string nbl) // calculate the reverse Polish notation
{
int NUM = 0; // result
String zhuanhuan;
Vector <int> the nums; // digital container
int a = 0; // Traversal string subscript
int b = a; // record the time scales traversing
the while (a <nbl.size ())
{
IF (gettruefalse (NBL [a]) ==. 1) for the current symbol //
{
IF (((NBL [a] == '/') || (nbl [a] == '%')) && (* (nums.end () - 1) == 0)) // division calculation and remainder, if the dividend is 0 , given
{
COUT << "divisor is not 0, the program exits" << endl;
System ( "PAUSE");
exit (-1);
}

NUM = getAnswer (* (nums.end () - 2), NBL [ a], * (nums.end () - 1)); // pop two digital container and the digital to calculate these two numbers according to the symbol
nums.erase (nums.end () - 2, nums.end () ); // calculate after removing these two numbers, the result back into the container
nums.push_back (NUM);
A ++;// traverse the next character
}
IF the else (NBL [A] == '')
{
A ++;
}
the else
{
B = A; // Get the recording start position of the space
the while (A <nbl.size ())
{
IF (NBL [A] == ' ') // find space
{
IF (NBL [B] ==' ') // if the number is negative (as at the start)..
{
NBL [B] =' - '; // convert to decimal negative No.
}
zhuanhuan.append (nbl.begin () + B, nbl.begin () + A);
NUM = the atof (zhuanhuan.c_str ()); // convert the character string into digital numbers and pressed into the container
zhuanhuan .clear ();
nums.push_back (NUM);
BREAK;
}
A ++;
}

}
}
num = *(nums.end() - 1);
return num;
}

main int ()
{
char CH [100];
COUT << "input character string you want to calculate the expression (only integer arithmetic):" << endl;
gets_s (CH);
String STR (CH); // with string for carrying out switching operation
int S = 0;
the while (S <str.size ()) // check whether the correct user input expression
{
iF (gettruefalse (STR [S]) == 0)
{
COUT << " input error string expression "<< endl;
System (" PAUSE ");
return 0;
}
S ++;
}
string NBL; // for storing converted into postfix expression container
nbl = getnbl (str); // call the function string to RPN string
int num; // expression results
NUM = getCount (NBL);
cout << "into the reverse Polish notation:" << nbl << ", ( -),. M (&&), N (|| ) is the marker "<< endl;
COUT <<" string expression: "<< ch <<"The results:" << endl << NUM;
COUT << "results:" << endl << NUM;
System ( "PAUSE");
fflush (stdin);
return 0;
}
`` `
The result:

 

Guess you like

Origin www.cnblogs.com/aaaguai/p/11129853.html