我的思路是将字符串从后往前进行操作,也就是对应整数的个,十,百位这样,代码如下,经供参考
#include <stdio.h>
#include <string.h>
int main()
{
char str[128] = {0};
char c;
signed int goal = 0;
printf("printf a str:\n");
int i;
i = 0;
while(1)
{
if((c = getchar()) != '\n')
str[i] = c;
else
break;
i++;
}
char* p = str;
while((*p) != '\0')//让指针指到字符串最后
{
p++;
}
p--;
int temp;
int count = 1;//记位数用
for(i = 0; i < strlen(str); i++)
{
if((*p) >= 48 && (*p) <= 57)
{
temp = *p - 48;
goal = goal + temp * count;
}
else
{
goal = 0 - goal;//判断是否为负数
}
p--;
count = count * 10;
}
printf("goal:\n");
printf("%d\n",goal);
return 0;
}