atoi函数实现

来源:互联网 发布:淘宝中奖客服电话 编辑:程序博客网 时间:2024/05/02 01:54
#define  _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>//atoi函数int str_int(const char* str){const char* ptr = str;    //ptr保存str字符串开头int temp=0;if (*str == '-'||*str=='+'){str++;}while (*str!='\0'){if (*str < '0'&&*str > '9')break;temp = temp * 10 + (*str - '0');   //把单字符转换成整数str++;}if (*ptr == '-'){temp = -temp;}return temp;}void main(){printf("Please intput a number string: ");char str[10];scanf("%s", &str);int n = str_int(str);printf("\nOutput:%d ",n);system("pause");}

0 0
原创粉丝点击