124-C语言atoi(字符串→数字)
发布时间
阅读量:
阅读量
题目1:(依靠库函数)实现int Myatoi(const char *str);
要求:
1.“123”->123//数字字符转数字:数字字符-‘0’
2.“123x456”->123//遇到字母,就停止
3." 123456 "->123456//可以处理空格
4." -123456 "->-123456//可以处理正负值
5.“123456478912345789”->如果超过了当前类型的最大值,就转成最大值
#include <iostream>
#include <string.h>
#include <assert.h>
#include <ctype.h>
using namespace std;
int Myatoi(const char* str)
{
assert(str != NULL);
//处理前面的空格
while (*str == ' ')
{
str++;
}
//处理符号
int flg = 1;
if (*str == '-')
{
flg = -1;
全部评论 (0)
还没有任何评论哟~
