Home > Back-end >  C language function int divide (char * ss, int * a), its function is: from left to right in turn ss
C language function int divide (char * ss, int * a), its function is: from left to right in turn ss

Time:12-02

Write function int divide (char * ss, int * a), its function is: from left to right in turn ss scan the string will be continuous string of Numbers into an integer, and, in turn, save it to a point in the array, the function returns a number of integer in the array to save

In the main function of the main through a call to divide function and getSum function calculating the Numbers in a given string consisting of a number of integers and,

L input line of string does not exceed 80 characters, conditions should be considered negative integers

# include & lt; Stdio. H>
# include & lt; Stdlib. H>
# include & lt; Ctype. H>

Int divide (char * ss, int * a);
Int getSum (int * a, int n);

Int main ()
{
Char ss [100].
Int a [100], n, sum;
Gets (ss);
N=divide (ss, a);
Sum=getSum (a, n);
Printf (" % d \ n ", sum);
Return 0;
}
Int divide (char * ss, int * a)
{



}
Int getSum (int * a, int n)
{
Int sum=0, I;
for(i=0; iSum=sum + a [I];
}
Return the sum.
}

CodePudding user response:

Reference:
 # include & lt; Iostream. H> 
#include
Int divide (char * ss, int * a)
{
Int j, n=0, I, k;
Char \ [100].
for (i=0; Ss [I]!='\ 0'; I++)
{
J=0;
While (ss [I] & gt;='0' & amp; & Ss [I] <='9' | | ss [I]=='-')
{
Temp=ss [j] [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; iThe sum +=a [I];
Return the sum.
}

Int main (int arg c, char * argv [])
{
Char ss [100].
Int n, sum=0, a, [100].
Gets (ss);
CoutN=divide (ss, a);
Sum=getSum (a, n);
Cout<" Sum="& lt; System (" pause ");
return 0;
}

CodePudding user response:

 # include & lt; Stdio. H> 
# include & lt; Stdlib. H>
# include & lt; Ctype. H>

Int divide (char * ss, int * a);
Int getSum (int * a, int n);

Int main ()
{
Char ss [100].
Int a [100], n, sum;
The 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 & amp; & Isdigit (* p)) {
If (num & lt; 0 {
If (p - ss & gt;=1 & amp; & * (p - 1)=='-')
Sign *=1;
Num=0;
}
Num=(num * 10) + * p - '0';
P++;
}
If (num & gt;=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; iSum=sum + a [I];
}
Return the sum.
}

For your reference ~

CodePudding user response:

Before a flawed handling the negative, modified as follows, for reference:
 # include & lt; Iostream. H> 
#include
Int divide (char * ss, int * a)
{
Int j, n=0, I, k;
Char \ [100].
for (i=0; Ss [I]!='\ 0'; I++)
{
J=0;
K=0;
While (ss [I] & gt;='0' & amp; & Ss [I] <='9')
{
If (k==0 & amp; & Ss [I - 1]=='-')//negative processing
{
Temp=ss [j] [I - 1);
j++;
k++;
}
Temp=ss [j] [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; iThe sum +=a [I];
Return the sum.
}

Int main (int arg c, char * argv [])
{
Char ss [100].
Int n, sum=0, a, [100].
Gets (ss);
N=divide (ss, a);
Sum=getSum (a, n);
Cout<" Sum="& lt;
System (" pause ");
return 0;
}

CodePudding user response:

reference QZJHJXJ reply: 3/f
before a flawed handling the negative, modified as follows, for reference:
 # include & lt; Iostream. H> 
#include
Int divide (char * ss, int * a)
{
Int j, n=0, I, k;
Char \ [100].
for (i=0; Ss [I]!='\ 0'; I++)
{
J=0;
K=0;
While (ss [I] & gt;='0' & amp; & Ss [I] <='9')
{
If (k==0 & amp; & Ss [I - 1]=='-')//negative processing
{
Temp=ss [j] [I - 1);
j++;
k++;
}
Temp=ss [j] [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; iThe sum +=a [I];
Return the sum.
}

Int main (int arg c, char * argv [])
{
Char ss [100].
Int n, sum=0, a, [100].
Gets (ss);
N=divide (ss, a);
Sum=getSum (a, n);
Cout<" Sum="& lt;
System (" pause ");
return 0;
}

nullnullnullnullnullnullnullnullnullnullnull