# 应用递归函数，字符串转换为数字

0

0

#include <math.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

```void StringToInteger(char* str, int* number)
{
int len, temp = 0;
if(str == NULL || (len = strlen(str)) == 0)
{
*number = 0;
return;
}
if(len == 1)
{
*number = *str - '0';
return;
}
StringToInteger(str + 1, &temp);
*number = (*str - '0') * (int)pow(10, len - 1) + temp;
}```

=================这是第二个版本================

version2
```void StringToInteger(char* str, int* number)
{
int len, temp = 0;
if(str == NULL || (len = strlen(str)) == 0)
{
*number = 0;
return;
}
if(len == 1)
{
*number = *str - '0';
return;
}
char* substr = strdup(str);
substr[len - 1] = 0;
StringToInteger(substr, &temp);
free(substr);
*number = str[len - 1] - '0' + temp * 10;
}```

int main(int argc, char* argv[])
{
int n;
char str[] = "12345";
StringToInteger(str, &n);
printf("n= %ld\n", n);
return 0;
}

hoodlum1980 | 小虾三级 |园豆：543 | 2012-05-31 20:00

```void StringToInteger(char* str, int* number)
{
int len, temp = 0;
if(str == NULL || (len = strlen(str)) == 0)
{
*number = 0;
return;
}
if(len == 1)
{
*number = *str - '0';
return;
}
*number = str[len - 1] - '0';
str[len - 1] = 0;
StringToInteger(str, &temp);
*number += temp * 10;
}```

char str[] = "12345";
StringToInteger(str, &n);

StringToInteger("12345", &n);

```void StringToInteger(const char* str, int* number, int len)
{
if(len == 1)
{
*number = *str - '0';
}
else
{
int temp;
StringToInteger(str, &temp, len - 1);
*number = str[len - 1] - '0' + temp * 10;
}
}```

int n;

char* str = "12345";
StringToInteger(str, &n, strlen(str));
printf("n= %ld\n", n);

hoodlum1980 | 园豆：543 (小虾三级) | 2012-05-31 20:06

@半分微醉: 坑爹啊，留作业跑这抄答案来了。。。

@hoodlum1980: 哥们对于你写的这个程序我由衷的佩服,小弟还有一些疑惑,不知能否加个qq当面探讨一下qq919943272

```void StringToInteger(char* str, int* number)
{
char ch;
int len, temp = 0;
if(str == NULL || (len = strlen(str)) == 0)
{
*number = 0;
return;
}
if(len == 1)
{
*number = *str - '0';
return;
}
ch = str[len - 1];
str[len - 1] = 0;
StringToInteger(substr, &temp);
str[len - 1] = ch;
*number = str[len - 1] - '0' + temp * 10;
}```
hoodlum1980 | 园豆：543 (小虾三级) | 2013-05-05 01:25

```#include <string.h>
#include <stdio.h>
void StringToInteger(char *str, int *number)
{
static int base;
if(*str == 0)
{
base = 1;
*number = 0;
}
else
{
StringToInteger(str + 1, number);
*number += (*str - '0') * base;
base *= 10;
}
}
int main(int argc, char* argv[])
{
int number;
StringToInteger("56789", &number);
printf("number = %ld\n", number);
return 0;
}```

hoodlum1980 | 园豆：543 (小虾三级) | 2013-05-05 01:39

01  int StrToInt(char *str, int *base)
02  {
03      if(str[1] == 0)
04      {
05          *base = 1;
06          return *str - '0';
07      }
08      else
09      {
10          int tmp = StrToInt(str + 1, base);
11          *base *= 10;
12          return (*str - '0') * (*base) + tmp;
13      }
14  }
15
16  int main(int argc, char* argv[])
17  {
18      int number, base;
19      number = StrToInt("12345", &base);
20      printf("number = %ld\n", number);
21      return 0;
22  }
hoodlum1980 | 园豆：543 (小虾三级) | 2013-05-05 19:25

0

@半分微醉: 你呀的就是,我这正做着c语言呢,上网查查资料,正好发现和我遇到的问题一样,我这还感叹呢,真巧啊,真巧啊,真巧啊,真巧...越看这题越眼熟,再一对,尼玛坑爹啊,连最后标点两个句号都一样,这不是坑我吗,还有我雷哥...

@我想我笑:我说不是就不是。 有点大脑好不好，这么大的学校，那就非得是成少雷的学生吗？

您需要登录以后才能回答，未注册用户请先注册