华为2道机试题:
review:
/**
@date:2011/09/02 review (not to use the fun atoi())
@author:weedge
*/
#include <stdio.h>
#include <stdlib.h>
void take_num(const char *strIn, int *n, unsigned int *outArray){
if(strIn == NULL){
printf("this is null string.");
}else{
int i = 0;
while(*strIn){
if(*strIn >= '0' && *strIn <='9'){
outArray[i] = 0;
while(*strIn >= '0' && *strIn <='9'){
outArray[i] = outArray[i]*10 + ((*strIn++) - '0');
}
i++;
}
strIn++;
}
*n = i;
}
}
void take_num1(const char *strIn, int *n, unsigned int *outArray){ //use atoi()
if(strIn == NULL){
printf("this is null string.");
}else{
int i = 0;
while(*strIn){
if(*strIn >= '0' && *strIn <='9'){
const char *begstr = strIn;
while(*strIn >= '0' && *strIn <='9'){
strIn++;
}
outArray[i] = atoi(begstr);/*atoi() just read back the front number*/
i++;
}
strIn++;
}
*n = i;
}
}
int main(){
char *strIn = "ab00cd+123000fght456-25 3.005fgh";
int n=0;
unsigned int outArray[20];
take_num1(strIn,&n,outArray);
printf("n:%d/n",n);
for(int i=0; i<n; i++){
printf("outArray[%d]=%u/n",i,outArray[i]);
}
system("pause");
return 0;
}