c語(yǔ)言怎么把負(fù)號(hào)字符轉(zhuǎn)化為數(shù)字負(fù)號(hào)?
#include <stdio.h>#include <stdlib.h>
long fun(char *p){
// 判斷是否有負(fù)號(hào)
int flag=0;
long sum=0;
if(*p=='-'){
flag=1;
p++;
}
while(*p){
sum=sum*10+(*p-48);
p++;
}
if(flag){
sum=-1*sum;
}
return sum;
}
int main()
{
char s[6];
long n;
// 輸入字符串
printf("Enter a string:\n");
gets(s);
n=fun(s);
printf("%轉(zhuǎn)換后的數(shù)字為:ld\n",n);
return 0;
}