c语言code大集合
来源:互联网 发布:网络营销软件排行 编辑:程序博客网 时间:2024/06/15 22:59
//字符串转数字
#include<stdio.h>
#include<math.h>
int myAtoi(char* str)
{
if (str == NULL)
{
printf("字符串为空,输入无效");
return -1;
}
while (*str == ' ') //去掉起始空串
{
str++;
}
if (*str == '\0')
{
printf("字符串为空串");
return 0;
}
int sign = (*str == '-') ? -1 : 1;//确定符号位
if (*str == '+' || *str == '-')
{
str++;
}
int result = 0;
while (*str >= '0'&& *str <= '9')
{
result = result * 10 + (*str - '0');//*str - '0'指针差得到纯整数
str++;
}
return result*sign;
}
int main()
{
printf("%d\n", myAtoi(" 12345"));
printf("%d\n", myAtoi(" +12345 "));
printf("%d\n", myAtoi(" -12345"));
printf("%d\n", myAtoi(" "));
printf("%d\n", myAtoi(""));
}
//output
12345
12345
- 12345
字符串为空串0
字符串为空串0
#include<stdio.h>
#include<math.h>
int myAtoi(char* str)
{
if (str == NULL)
{
printf("字符串为空,输入无效");
return -1;
}
while (*str == ' ') //去掉起始空串
{
str++;
}
if (*str == '\0')
{
printf("字符串为空串");
return 0;
}
int sign = (*str == '-') ? -1 : 1;//确定符号位
if (*str == '+' || *str == '-')
{
str++;
}
int result = 0;
while (*str >= '0'&& *str <= '9')
{
result = result * 10 + (*str - '0');//*str - '0'指针差得到纯整数
str++;
}
return result*sign;
}
int main()
{
printf("%d\n", myAtoi(" 12345"));
printf("%d\n", myAtoi(" +12345 "));
printf("%d\n", myAtoi(" -12345"));
printf("%d\n", myAtoi(" "));
printf("%d\n", myAtoi(""));
}
//output
12345
12345
- 12345
字符串为空串0
字符串为空串0
请按任意键继续. . .
//数字转字符串
#include<stdio.h>
#include<math.h>
char* myItoa(int num)
{
char str[1024];
int sign = num, i = 0, j = 0;
char temp[11];
if (sign < 0)
{
num = -num;
}
while (num>0)
{
temp[i] = num % 10 + '0';
num /= 10;
i++;
}
//while (num > 0);
//temp[i] = '\0';
i--;//back 最后一次自增
if (sign < 0)//加符号
str[j++] = '-';
while (i >= 0)
{
str[j++] = temp[i--];
}
str[j] = '\0';
return str;
}
int main()
{
printf("%s\n", myItoa(-123456));
printf("%s\n", myItoa(+123456));
return 0;
}
//output
-123456
123456
请按任意键继续. . .
0 0
- c语言code大集合
- 常见基础算法大集合(C语言实现)
- 【C语言的学习】第一回合:入门大集合
- 【C语言的学习】第二回合:命名规则大集合
- 【C语言的学习】第六回合:输入输出函数大集合
- 【C语言的学习】第八回合:函数大集合
- 【C语言的学习】第十回合:数组大集合
- 【C语言的学习】第十一回合:预处理命令大集合
- 【C语言的学习】第十二回合:指针知识大集合
- 【C语言的学习】第十三回合:结构体大集合
- 【C语言的学习】第十五回合:文件大集合
- 【转载】C语言字符串处理函数 - 大集合
- C语言第六回合:输入输出相关出函数大集合
- C语言第十四回合:结构体大集合
- c语言集合
- c语言指针集合
- c语言题目集合
- c语言题目集合
- JAVA基础再回首(二十三)——操作基本数据类型的流、内存操作流、打印流、随机访问流、合并流、序列化流
- poj 2328 Gussing Game
- I2C总线信号时序总结
- 实验二 任务一 打招呼APP
- Matlab实现文件读写的几种方法(确实很好,不错)
- c语言code大集合
- 30多条mysql数据库优化方法,千万级数据库记录查询轻松解决
- Linux---基本命令
- 阿里巴巴前端大赛源码,非常好,可以下载。
- Linux命令简介
- Matlab中图片保存的5种方法
- Stm32利用SWD下载的设置
- 如何在原有的iOS项目中添加cordova
- 加工生产调度(Johnson算法 双机流水作业调度问题)