Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从字符串中添加所有数字_C++ - Fatal编程技术网

C++ 从字符串中添加所有数字

C++ 从字符串中添加所有数字,c++,C++,我正在尝试添加字符串中的所有数字,但我不知道如何添加2位数字。 例:ab32d3 我希望答案是35。 这是我的代码: int main() { int max=0,min=100000,sum=0,totalsum=0; string s; ifstream in ("Unos.txt"); while(getline(in,s)) { cout<<s<<endl; for(int i=0;i<s.length();++i) {

我正在尝试添加字符串中的所有数字,但我不知道如何添加2位数字。 例:ab32d3 我希望答案是35。 这是我的代码:

int main()
{
int max=0,min=100000,sum=0,totalsum=0;
string s;
ifstream in ("Unos.txt");

while(getline(in,s))
{
    cout<<s<<endl;
    for(int i=0;i<s.length();++i)
    {
        if(s[i]>max)
        {
            max=s[i]-'0';
        }
        if(s[i]<min)
        {
            min=s[i]-'0';
        }
        if(isdigit(s[i]))
        {
            sum=10*sum+s[i]-'0';
        }
        else
        {
            totalsum+=sum;
            sum=0;
        }
    }
}
totalsum+=sum;
cout<<"Najveci broj je: "<<max<<endl;
cout<<"Najmanji broj je: "<<min<<endl;
cout<<"Zbir svih brojeva je: "<<totalsum<<endl;
return 0;
intmain()
{
int max=0,min=100000,sum=0,totalsum=0;
字符串s;
ifstream in(“Unos.txt”);
while(getline(in,s))
{
试试这个:

#include <iostream>
#include <string>
#include <cctype>

using namespace std;

int main() {
   string str = "ab32d3", temp;
   int sum = 0;

   for ( int i = 0; i < str.size(); i++ ) {
       if ( isdigit(str[i]) ) temp.push_back(str[i]);
       else {
           if ( temp.size() > 0 ) {
               sum += stoi(temp);
               temp = string();
           }   
       }
   }

   // Last number (if exists)
   if ( temp.size() > 0 ) sum += stoi(temp);

   cout << sum << endl;

   return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
字符串str=“ab32d3”,温度;
整数和=0;
对于(int i=0;i0){
总和+=stoi(温度);
temp=string();
}   
}
}
//最后一个号码(如果存在)
如果(温度大小()>0)总和+=stoi(温度);
不能包含
#包括
int main(){
std::string s=“ab32d3”;
int值_so_far=0;
int-total=0;
对于(int i=0;istd::cout From
ab32d3
35是如何得出的。使用48也比使用“0”更好。我的意思是不添加数字,但如果有两个数字一个接一个地出现,将其视为2位数字而不是3+2+3,我需要它来做32+3,感谢“0”,你能解释为什么使用“0”更好,它做什么吗?Bec因为它更明显地表明,您正在删除
'0'
的ASCII值,而不必从头开始,记住它的值为48@Tibor您可以尝试使用正则表达式捕捉子字符串,如
/\d+/g
,然后使用
std::stoi
将它们转换为int。请记住,确保它们确实是数字表示stoi未声明。是否包含字符串头?
\include
好的,那么编译器不支持
c++11
。将
stoi(temp)
更改为
atoi(temp.c_str()) @ TiBoR包含 < /Cord>,然后再试一次,它说在C++ 98模式中不允许基于范围的“AF”循环,但是现在我尝试从文本文件中读取文本,它以两行写,这给了我很多。value@Tibor-您已移动目标帖子。请逐行读取文件。将其用作函数模板。至少nd最大值适用于多行,但这并不适用t@tibor-在您的代码中,您甚至没有声明
max
min
实际上,只有当第2行的第一个字符是数字时,它才起作用
#include <iostream>
#include <cctype>

int main() {

    std::string s = "ab32d3";
    int value_so_far = 0;
    int total = 0;

    for (int i = 0; i < s.length(); ++i)
    {
        if (isdigit(s[i])) { // This keeps a running total until the number ends
            value_so_far = 10 * value_so_far + s[i] - '0';
        } else { // Here the number has ended - Add to the total and reset
            total += value_so_far;
            value_so_far = 0;
        }
    }

    // Got to the end - Add what is left!
    total += value_so_far;

    std::cout << "Ans:" << total << std::endl;
    // your code goes here
    return 0;
}