c字符串转整数
2014-07-06 19:36
141 查看
// StringToInt.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <stdio.h> #define n 100 /* isdigit(char a) 判断字符是否为数字字符即字符是否在['0'-'9']之间 是返回true 其他返回false */ bool isdigit(char a){ if (a >= '0'&&a <= '9') return true; else return false; } /* myatoi(char *s) 将字符串转换为整型数字 */ int myatoi(char *s){ int res = 0; bool neg_flag = false; if (s == NULL){ return 0; } if (s[0] == '+' || s[0] == '-') neg_flag = (*s++!='+');//判断是否为负数 while (isdigit(*s)) res = res * 10 + (*s++-'0');//将字符串累加为整数 return neg_flag ? -res : res;//负数返回它的相反数 } int _tmain(int argc, _TCHAR* argv[]) { //char x[] = {'1','0','0'}; //scanf("%s", &str); char x[100]; int len = 0; for (; len < 5; len++){ scanf("%c", &x[len]); } printf("%d\n", myatoi(x)); return 0; }
参考网址:http://baike.baidu.com/view/653935.htm?fr=aladdin
相关文章推荐
- 华为机试——任意正整数编码为字符串
- 微软算法100题20 字符串转整数 atoi
- 通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。
- 输入一个整数,将这个整数以字符串的形式逆序输出 程序不考虑负数的情况,若数字含有0,则逆序形式也含有0,如输入为100,则输出为001
- 整数字符串转化
- 文件读写及字符串与整数的相互转换
- 将一个字符串转换成一个整数
- 在Java中使用标准输入输出设备进行字符串,整数浮点数等 的输入输出操作
- 【剑指offer】C语言:实现函数可以将一个字符串转换为对应的整数,如+1234输出1234
- 【整理】C/C++中字符串与整数之间的相互转换
- 把字符串转换成整数
- 输入一个表示整数的字符串,把该字符串转换成整数并输出(实现atoi函数功能)
- 字符串转换为整数
- 不使用库函数将整数转换为字符串
- 整数转字符串
- 将字符串转换成对应的整数(简单版,后续会出复杂版的)
- 华为:通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串
- ruby中的整数、浮点数、字符串之间的相互转换
- Python学习笔记:02 基础:变量、整数、字符串、赋值、基本输出、注释、代码块
- 剑指offer 面试题49 把字符串转换成整数