原创:实现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 ...
随机推荐
- python基础学习日记(一)注释(二)算术运算符(三)变量的基本使用
一.python程序的注释 注释部份程序运行时不起作用.用于说明代码的用途 1.单行注释 # 开始的一行文字,为单行注释 # 单行注释 print("hello python") ...
- X86逆向15:OD脚本的编写技巧
本章节我们将学习OD脚本的使用与编写技巧,脚本有啥用呢?脚本的用处非常的大,比如我们要对按钮事件进行批量下断点,此时使用自动化脚本将大大减小我们的工作量,再比如有些比较简单的压缩壳需要脱壳,此时我们也 ...
- Swagger学习(一、入门)
简单 入门(效果) SwaggerConfig.class @Configuration //变成配置文件 @EnableSwagger2 //开启swagger2 public class Swag ...
- O058、Snapshot Volume 操作
参考https://www.cnblogs.com/CloudMan6/p/5657744.html Snapshot 可以为 volume 创建快照,快照中保存了 volume当前的状态,以后可 ...
- LeetCode:595.大的国家
题目链接:https://leetcode-cn.com/problems/big-countries/ 题目 这里有张 World 表 +-----------------+------------ ...
- global.css
global.css /* 页面元素初始化和常用样式定义-start */ /*======== 全局 ========*/ body, div, dl, dt, dd, ul, ol, li, h1 ...
- java基础4(线程)
1.请简单描述什么是并行,什么是并发? 并行:指两个或多个事件在同一时刻发生(同时发生). 并发:指两个或多个事件在同一个时间段内发生. 通俗易懂版: 你吃饭吃到一半,电话来了,你一直到吃完了以后才去 ...
- 阿里云环境中配置tomcat7可能出现的问题及解决方法
前提是安装好了tomcat,但是输入ip+端口无法访问,那么情况有一下几种 (1)可能防火墙没有关闭 systemctl stop firewalld.service #停止firewall syst ...
- 盗取连接你wifi的人的qq
#本文内容仅供个人娱乐学习 思路: 使用wireshark监听笔记本的wifi热点,拦截捕获连接你的wifi热点的人的手机qq网络数据包,从网络数据包中分析取出两个qq空间的两个coookie值,使用 ...
- MySQL单表查询实例
数据表准备 ```mysql create table emp( id int not null unique auto_increment, name varchar(20) not null, ...