C 字符串转数字
来源:互联网 发布:h5棋牌游戏源码 编辑:程序博客网 时间:2024/04/29 19:55
问题:给定一个字符串输入,将其转换为相应的数字
涉及知识点:字符串处理
分析: 1.输入字符串的合法性检查
2.对于顺序读入的字符串,如何确定相应的权重
3.处理非法字符串
4.对于整数,小数,负数的处理
首先放上 C库函数中atoi函数的原型:
int ascii_to_int(char const *string){ int value; value = 0; while(*string <= '0'&&*string >= '9') { value *= 10; value += *string - '0'; ++string; } if(*string != '0') value = 0; return value;}
这个原函数只对输入为数字字符的字符串进行处理,如果输入为非数字字符则不予处理,返回0
自己写的:
#include <stdio.h>#include <math.h>#include <stdbool.h>#define SIZE 500long double strToInt(char *str);char* dealWithStr(char *str);bool isNagetiveNumber(char *str);bool isPureDecimalNumber(char *str);bool isNagetive_PureDecimalNumber(char *str);int main(int argc, const char * argv[]){ char *orginal_Str; char ch[SIZE]; printf("Please input the orginal string( Only numbers):\n"); scanf("%s",ch); orginal_Str = ch; char *checked_Str = dealWithStr(orginal_Str); long double rs; if (checked_Str != NULL) { rs = strToInt(checked_Str); printf("The int number of the string is %Lf\n",rs); }else{ printf("Invalid input!\n"); } return 0;}/*** 将字符串转换为Int型整数 ***/long double strToInt(char *str){ long double result = 0; long double resultForIntegerPart = 0; long double resultForDecimalPart = 0; int count = 0; char *tempStr = str; bool isNagetive = false; bool isIntegerPart = true; int countFordecimalPart = 0; if (isNagetive_PureDecimalNumber(str) || isNagetiveNumber(str)) { isNagetive = true; } /*** 分析字符串,将其划分为整数和小数两个部分,分别计算 ***/while(*tempStr != '\0'){ switch (*tempStr) { case '-': tempStr++; break; case '.':{ isIntegerPart = false; count = 0; tempStr++; } break; default:{ int number = (int)(*tempStr - '0'); /*** 整数部分 ***/ if (isIntegerPart) { if(0 == count) resultForIntegerPart += number; else{ resultForIntegerPart = resultForIntegerPart * 10 + number; } count++; tempStr++; }else{ /*** 小数部分 ***/ if(0 == count) resultForDecimalPart += number; else{ resultForDecimalPart = resultForDecimalPart * 10 + number; } count++; countFordecimalPart++; tempStr++; } } break; } } /*** 如果是整数部分 ***/ if (isIntegerPart) { result = resultForIntegerPart; }else{ result = resultForIntegerPart + resultForDecimalPart * pow(10,-countFordecimalPart); } if (isNagetive) { result = result * -1; } return result;}/*** 处理字符串,只允许输入数字字符串 ***/char* dealWithStr(char *str){ char *temp = str; int countForMinus = 0; int countForDecimalPoint = 0; if (isNagetive_PureDecimalNumber(str)) { countForDecimalPoint++; countForMinus++; temp += 2; }else if (isNagetiveNumber(str)){ countForMinus++;; temp++; }else if(isPureDecimalNumber(str)){ countForDecimalPoint++; temp++; } /*** 允许输入数字,负号,小数点 ***/ while (*temp != '\0') { if((*temp >= '0' && *temp<= '9') || *temp == '.') { switch (*temp) { case '.': countForDecimalPoint++; break; default: break; } temp++; }else{ break; } } if ((countForDecimalPoint == 0 || countForDecimalPoint == 1) && (countForMinus == 0 || countForMinus == 1 ) && *temp == '\0') return str; else return NULL;}/*** 判断是否为负数 ***/bool isNagetiveNumber(char *str){ bool result = false; char *temp = str; if (*temp == '-') { result = true; } return result;}/*** 判断是否为纯小数 ***/bool isPureDecimalNumber(char *str){ bool result = false; char *temp = str; if (*temp == '.') { result = true; } return result;}/*** 判断是否为负纯小数 ***/bool isNagetive_PureDecimalNumber(char *str){ bool result = false; char position_0 = *str; str++; char position_1 = *str; if (position_0 == '-' && position_1 == '.') { result = true; } return result;}
0 0
- C 字符串转数字
- c语言 字符串转数字
- C 语言常用字符串转数字函数
- C语言实现字符串转数字
- c/字符串,字符转数字,数字…
- c语言中字符串转数字函数
- 字符串转数字,数字转字符串C函数
- c/c++ 数字转字符串, 字符串转数字
- C语言字符串转数值
- C语言字符串转数值
- C语言字符串转数值
- 从字符串转数字
- js字符串转数字
- javascript 字符串转数字
- CString 字符串 转数字
- 字符串转数字
- 罗马字符串转数字
- java字符串转数字
- webrtc(不定时更新)
- poj 3264 线段树
- x的n次方(编程珠玑第四章)
- openwrt启动进程分析
- 舞伴配对问题
- C 字符串转数字
- HTML5 的富文本编辑器工具栏Popline
- iOS 打印出视图中所有的子视图的名称
- 多控制器管理 ——程序启动过程
- Netgear JNR3300 调试总结
- L-BFGS的原理及在回归分析中的应用
- C99的restrict关键词
- TOMCAT目录结构
- S3C2440的RTC解析