原创:实现atoi函数
#include <stdio.h>
#include <stdlib.h>
#include <limits.h> int my_atoi(char *str)
{
if(NULL == str)
{
printf("argc is null\n");
return -;
}
while(' ' == *str)
str++;
int flag = ;
if('-' == *str)
{
flag = -;
str++;
}
if('+' == *str)
{
str++;
}
unsigned int sum = ;
while(*str)
{
if(*str >= '' && *str <= '')
{
int tmp = *str - '';
sum *= ;
sum += tmp;
//此处判断溢出的目的是避免sum在计算过程中溢出
if(( == flag) && (sum > INT_MAX))
{
printf("int 正数溢出\n");
return -;
}
//计算最小int的绝对值,注意要使用unsigned int否则会溢出
unsigned int absIntMin = (unsigned int)INT_MAX + ;
if((- == flag) && (sum > absIntMin))
{
printf("int 负数溢出\n");
return -;
}
}
else
{
printf("str (%s) exists illegal character\n", str);
return -;
}
str++;
}
return flag * sum;
}
测试代码
#include <float.h>
#include <limits.h>
extern int my_atoi(char *str);
int main(int argc, char *argv[])
{
char a[] = {'\0'};
while()
{
scanf("%s", a);
printf("count = %d\n", my_atoi(a));
}
return ;
}
原创:实现atoi函数的更多相关文章
- atoi()函数
原型:int atoi (const char *nptr) 用法:#include <stdlib.h> 功能:将字符串转换成整型数:atoi()会扫描参数nptr字符串,跳过前 ...
- C语言itoa()函数和atoi()函数详解(整数转字符C实现)
1.int/float to string/array: C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串,下面列举了各函数的方法及其说明. ● itoa():将 ...
- 题目1003:A+B ---c_str(),atoi()函数的使用;remove , erase函数的使用
#include<stdio.h> #include<stdlib.h> int sw(char *a){ ,c=; while(a[i]){ ') c=c*+a[i]-'; ...
- atoi()函数的实现
atoi()函数的功能:将字符串转换成整型数:atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符串时('\0')才结束转化,并将结果返回( ...
- C语言itoa()函数和atoi()函数详解(整数转字符)
http://c.biancheng.net/cpp/html/792.html C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. 以下是用itoa()函数将整 ...
- C语言itoa函数和atoi 函数
C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串.以下是用itoa()函数将整数转 换为字符串的一个例子: # include <stdio.h> ...
- 【C语言】模拟实现atoi函数
atoi(表示 ascii to integer)是把字符串转换成整型数的一个函数. atoi()函数会扫描参数 nptr字符串,跳过前面的空白字符(例如空格,tab缩进等,可以通过isspace( ...
- String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )
String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...
- atoi()函数(转载)
atoi()函数 原型:int atoi (const char *nptr) 用法:#include <stdlib.h> 功能:将字符串转换成整型数:atoi()会扫描参数np ...
- 源码实现 --> atoi函数实现
atoi函数实现 atoi()函数的功能是将一个字符串转换为一个整型数值. 例如“12345”,转换之后的数值为12345,“-0123”转换之后为-123. #include <stdio.h ...
随机推荐
- django初步了解2
目录 django初步了解2 表的字段增删改查 数据的增删改查 反向解析和分组 路由分发 名称空间 伪静态 虚拟环境 django初步了解2 表的字段增删改查 新增的字段 1.直接提供默认值 defa ...
- 搞懂ZooKeeper到底是做啥的
一.ZooKeeper是啥 ZooKeeper概念 ZooKeeper是一个开源的分布式协调服务(a service for coordinating processes of distributed ...
- Spring实战(十)Spring AOP应用——为方法引入新功能、为对象引入新方法
切面最基本的元素是通知和切点,切点用于准确定位应该在什么地方应用切面的通知. 1.Spring借助AspectJ的切点表达式语言来定义Spring切面 在Spring中,要使用AspectJ的切点表达 ...
- spring 的工厂类
spring 的工厂类 1. 工厂类 BeanFactory 和 ApplicationContext 的区别. ApplicationContext 是 BeanFactory 的子接口,提供了比父 ...
- postpreSQL和oracle数据库的递归
oracle: --包含自身 select * from sec_org start with org_id ='9767FA56D52680AEE043C0A8670580AE' --开始节点 co ...
- C#面向对象13 文件类操作 Path/File/FileStream
1.path using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...
- 利用宏方便地书写raw string literals
以前一直没用过标准库的regex,今天写一个hlsl的解析工具的时候用了一下,发现用字符串字面值写regular expression的时候非常不方便,特别是每个“\”字符都要被识别为转义,只能写成“ ...
- ES6基础之——get 与 set
class Chef{ constructor(food){ this.food = food; thid.dish = []; } //getter get menu(){ return this. ...
- 【Zabbix】分布式监控系统Zabbix【一】
一.Zabbix功能及特性简介 Zabbix可以获取cpu,内存,网卡,磁盘,日志等信息 1.Zabbix数据收集方式: a.Agent客户端(Agent客户端支持多平台部署) b.如果是无法安装客户 ...
- fetch的文件流下载及下载进度获取
下载过程中,获取进度,fetch API并没有提供类似xhr和ajax的 progress所以用 getReader()来循环读取大小 let size = 0; fetch( URL() + `/s ...