📜  处理字符串 - CPP 中的 CodeChef 解决方案 - C++ 代码示例

📅  最后修改于: 2022-03-11 14:44:47.076000             🧑  作者: Mango

代码示例1
#include
#include
using namespace std;

int sum(string exp);
bool IsNumber(char ch);

int main()
{
    string exp;
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> exp;
        int result = sum(exp);
        cout << result << endl;
    }
    

    return 0;
}
//
int sum(string exp)
{
    stack s;
    int sum = 0;
    for (int i = 0; i < exp.length(); i++)
    {
        if (IsNumber(exp[i]))
        {
            s.push(exp[i] - '0');
            sum += s.top();
        }
        else
        {
            continue;
        }
    }
    return sum;
}
//
bool IsNumber(char ch)
{
    if (ch >= '0' && ch <= '9')
        return true;
    return false;
}