//编写函数long fun(char *p),将一个数字字符串转换为一个整数
#include<stdio.h>
#include<string.h>
long fun ( char *p)
{
int i, len, t;
long x=0;
len=strlen(p);
if(p[0]=='-')
{
t=-1; len--; p++;
}
else t=1;
while(*p)
x=10*x+(*p-48),p++;
return x*t;
}
main()
{
char s[6];
long n;
printf("Enter a string:\n") ;
gets(s);
n = fun(s);
printf("%ld\n",n);
}
#include<stdio.h>
#include<string.h>
long fun ( char *p)
{
int i, len, t;
long x=0;
len=strlen(p);
if(p[0]=='-')
{
t=-1; len--; p++;
}
else t=1;
while(*p)
x=10*x+(*p-48),p++;
return x*t;
}
main()
{
char s[6];
long n;
printf("Enter a string:\n") ;
gets(s);
n = fun(s);
printf("%ld\n",n);
}