Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are +
, -
, *
, /
. Each operand may be an integer or another expression.
Some examples
["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6直接AC代码:
class Solution {
public:
int res=0;
int a = 0;
int b = 0;
stack<int> ck;
void take(){
a = ck.top();
ck.pop();
b = ck.top();
ck.pop();
}
int evalRPN(vector<string>& tokens) {
// 用迭代器遍历数组
for( vector<string>::iterator iter = tokens.begin() ; iter != tokens.end() ; iter ++ ){
if( *iter == "+" ){
take();
res = a + b;
ck.push(res);
}
else if( *iter == "-" ){
take();
res = b - a;
ck.push(res);
}
else if( *iter == "*" ){
take();
res = b * a;
ck.push(res);
}
else if( *iter == "/" ){
take();
res = b / a;
ck.push(res);
}
else
ck.push( atoi((*iter).c_str()) ); //atoi 将char型转换成int型 c_str() 将string型转换char型
}
return ck.top();
}
};
//atoi 将char*型转换成int型 c_str()为string的成员函数 将string型转换char*型
一组测试:
#include <iostream>
#include <string>
using namespace std;
int main()
{
char a[] = "123456";
char *b = "98765";
string str = "188940";
int resA;
int resB;
resA = atoi(a);
cout << resA << endl;
resB = atoi(b);
cout << resB << endl;
int resStr;
resStr = atoi(str.c_str());
cout << resStr << endl;
return 0;
}
c_str()第二个例子:
对于c_str()的说明:
Description
Generates a null-terminated sequence of characters (c-string) with the same content as the string object and returns it as a pointer to an array of characters.
A terminating null character is automatically appended.
The returned array points to an internal location with the required storage space for this sequence of characters plus its terminating null-character, but the values in this array should not be modified in the program and are only granted to remain unchanged until the next call to a non-constant member function of the string object.
Return Value
Pointer to an internal array containing the c-string equivalent to the string content.int main ()
{
char* cstr, *p;
string str ("Please split this phrase into tokens");
cstr = new char [str.size()+1];
const char *tt = str.c_str();
cout << "tt = " << tt << endl;
cout << "str = " << str << endl;
strcpy(cstr,str.c_str());
cout << "cstr = " << cstr << endl;
// cstr now contains a c-string copy of str
/******************************************************/
p=strtok(cstr," ");
while (p!=NULL)
{
cout << p << endl;;
p=strtok(NULL," ");
}
/******************************************************/
delete[] cstr;
return 0;
}
c_str()虽然返回char*型 但是本身数据不改变
strtok()函数原型:
char * strtok ( char * str, const char * delimiters );
A sequence of calls to this function split str into tokens, which are sequences of contiguous characters spearated by any of the characters that are part of delimiters.
On a first call, the function expects a C string as argument for str, whose first character is used as the starting location to scan for tokens. In subsequent calls, the function expects a null pointer and uses the position right after the end of last token as the new starting location for scanning.
To determine the beginning and the end of a token, the function first scans from the starting location for the first character not contained in separator (which becomes the beginning of the token). And then scans starting from this beginning of the token for the first character contained in separator, which becomes the end of the token.
This end of the token is automatically replaced by a null-character by the function, and the beginning of the token is returned by the function.
Once the terminating null character of str has been found in a call to strtok, all subsequent calls to this function with a null pointer as the first argument return a null pointer.
/* strtok example */ #include <stdio.h> #include <string.h> int main () { char str[] ="- This, a sample string."; char * pch; printf ("Splitting string \"%s\" into tokens:\n",str);
//参数 delimiters可以是多个分隔符pch = strtok (str, " ,.-"); while (pch != NULL) { printf ( "%s\n",pch); pch = strtok (NULL, " ,.-"); } return 0;}Output:
Splitting string "- This, a sample string." into tokens: This a sample string |