user-0 提问: c语言编写函数int divide(char *ss，int *a)，其功能是：从左到右依次扫描字符串ss，将串中连续出现的数字串转换成整数，并将其依次保存到a

回复:
user: user-1
content: 修改如下：#include <iostream.h> #include <string.h> int divide(char *ss,int *a) {     int j,n=0,i,k;     char temp[100];     for (i=0;ss[i]!='\0';i++)     {         j = 0;         k = 0;         while (ss[i] >= '0' && ss[i] <= '9')         {             if(k==0 && i!=0 && ss[i-1]=='-') //负数处理              {                                            temp[j] = ss[i-1];               j++;               k++;              }             temp[j] = ss[i];             j++;             i++;         }         if (j!=0)         {             temp[j] = '\0';             *a = atoi(temp);             a++;             n++;             i--;         }          }    return n; }  int  getSum(int *a,int  n) {    int sum=0,i;    for(i=0;i<n;i++)         sum += a[i];    return  sum; }  int main(int argc, char* argv[]) {     char ss[100];     int  n,sum=0,a[100];     gets(ss);     n = divide(ss, a);     sum = getSum(a,n);     cout<<"sum = "<<sum<<endl;      system("pause");     return 0; }

user: user-1
content: if (k ==0&&i!=0&& ss[i-1]==‘-‘)

user: user-1
content: 还是有漏洞，4#楼说的有道理，这句判断再封一道：if (k==0&&ssi!=0&&ss[i-1])

user: user-2
content: 引用 3 楼 qzjhjxj 的回复:前一种对负数处理有漏洞，修改如下，供参考：#include <iostream.h> #include <string.h> int divide(char *ss,int *a) {     int j,n=0,i,k;     char temp[100];     for (i=0;ss[i]!='\0';i++)     {         j = 0;         k = 0;         while (ss[i] >= '0' && ss[i] <= '9')         {             if(k==0 && ss[i-1]=='-') //负数处理              {               temp[j] = ss[i-1];               j++;               k++;              }             temp[j] = ss[i];             j++;             i++;         }         if (j!=0)         {             temp[j] = '\0';             *a = atoi(temp);             a++;             n++;             i--;         }          }    return n; }  int  getSum(int *a,int  n) {    int sum=0,i;    for(i=0;i<n;i++)         sum += a[i];    return  sum; }  int main(int argc, char* argv[]) {     char ss[100];     int  n,sum=0,a[100];     gets(ss);     n = divide(ss, a);     sum = getSum(a,n);     cout<<"sum = "<<sum<<endl;      system("pause");     return 0; } 在判断负数时，即判断'-'时，如果i是0，那么ss[i-1]会怎么样？

user: user-1
content: 前一种对负数处理有漏洞，修改如下，供参考：#include <iostream.h> #include <string.h> int divide(char *ss,int *a) {     int j,n=0,i,k;     char temp[100];     for (i=0;ss[i]!='\0';i++)     {         j = 0;         k = 0;         while (ss[i] >= '0' && ss[i] <= '9')         {             if(k==0 && ss[i-1]=='-') //负数处理              {               temp[j] = ss[i-1];               j++;               k++;              }             temp[j] = ss[i];             j++;             i++;         }         if (j!=0)         {             temp[j] = '\0';             *a = atoi(temp);             a++;             n++;             i--;         }          }    return n; }  int  getSum(int *a,int  n) {    int sum=0,i;    for(i=0;i<n;i++)         sum += a[i];    return  sum; }  int main(int argc, char* argv[]) {     char ss[100];     int  n,sum=0,a[100];     gets(ss);     n = divide(ss, a);     sum = getSum(a,n);     cout<<"sum = "<<sum<<endl;      system("pause");     return 0; }

user: user-2
content: #include  <stdio.h> #include  <stdlib.h> #include  <ctype.h>  int  divide(char  *ss,int  *a); int  getSum(int  *a,int  n);  int  main() {     char  ss[100];     int  a[100],n,sum;     fgets(ss, 100, stdin);     n  =  divide(ss,a);     sum  =  getSum(a,n);     printf("%d\n",sum);      return  0; }  int  divide(char  *ss,int  *a) {     char *p = ss;     int num = -1, i, sign;      sign = 1;     i = 0;     while (*p) {         while (*p && isdigit(*p)) {             if (num < 0) {                 if (p-ss >= 1 && *(p-1) == '-')                     sign *= -1;                 num = 0;             }             num = (num * 10) + *p - '0';             p++;         }         if (num >= 0) {             a[i++] = num *sign;             num = -1;             sign = 1;         }         if (*p == '\0')             break;         p++;     }      return i; } int  getSum(int  *a,int  n) {     int  sum  =  0,i;     for(i=0;i<n;i++){         sum  =  sum+a[i];     }     return  sum; } 供参考～

user: user-1
content: 供参考：#include <iostream.h> #include <string.h> int divide(char *ss,int *a) {     int j,n=0,i,k;     char temp[100];     for (i=0;ss[i]!='\0';i++)     {         j = 0;         while (ss[i] >= '0' && ss[i] <= '9' || ss[i] == '-')         {             temp[j] = ss[i];             j++;             i++;         }         if (j!=0)         {             temp[j] = '\0';             *a = atoi(temp);             a++;             n++;             i--;         }          }    return n; }  int  getSum(int *a,int  n) {    int sum=0,i;    for(i=0;i<n;i++)         sum += a[i];    return  sum; }  int main(int argc, char* argv[]) {     char ss[100];     int  n,sum=0,a[100];     gets(ss);     cout<<endl;     n = divide(ss, a);     sum = getSum(a,n);     cout<<"sum = "<<sum<<endl;     system("pause");     return 0; }