C语言gets和atoi

Write a simple program that allows the user to type in a mark ( use gets and atoi ) and then prints the grade using the if-else statements .

一、atoi()——把字符串转换成整型数
考点:字符串转换为数字时,对相关ASCII码的理解。

C实现:

#include <ctype.h> 
#include <stdio.h> 
int atoi (char s[]); 
int main(void ) 
{ 
char s[100]; 
gets(s); 
printf("integer=%d\n",atoi(s)); 
return 0; 
} 
int atoi (char s[]) 
{ 
int i,n,sign; 
for(i=0;isspace(s[i]);i++)//跳过空白符; 
sign=(s[i]=='-')?-1:1; 
if(s[i]=='+'||s[i]==' -')//跳过符号 
  i++; 
for(n=0;isdigit(s[i]);i++) 
        n=10*n+(s[i]-'0');//将数字字符转换成整形数字 
return sign *n; 
}