逆波兰数的计算(栈)(char与sting转换int)

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值