国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > php教程 > 字符转换为数字,以|分割

字符转换为数字,以|分割

来源:程序员人生   发布时间:2015-03-25 11:42:50 阅读次数:2079次
#include <stdio.h> #include <stdlib.h> #define INT_MAX ((1<<31)⑴) #define INT_MIN (1<<31) #define isRight(x) (x==' ' || x=='+' || x=='-' || (x>='0'&&x<='9')) int getNum(char* s, int begin, int end) { int flag = 1; int num = 0; while (begin<=end && s[begin] == ' ') begin++; if (s[begin] == '-') { flag = ⑴; begin++; } if (s[begin] == '+') begin++; while (begin<=end) { if (s[begin]<'0' || s[end]>'9') { return 0; } if (num>INT_MAX/10 || (INT_MAX==INT_MAX/10 && (s[begin]-'0')>INT_MAX%10)) return (flag==⑴) ? INT_MIN : INT_MAX; num = num*10+s[begin]-'0'; begin++; } return num*flag; } /*以|分隔符的数值转换 1面*/ void strToNum(char* s) { int a[20]; int i = 0; int begin; int end; int count = 0; if (s == NULL) return; while (s[i]!='' && s[i]=='|') i++; while (s[i] != '') { begin = i; while (s[i]!='' && s[i]!='|' && isRight(s[i])) { i++; } if (s[i]=='' || s[i]=='|') { end = i⑴; a[count++] = getNum(s, begin, end); printf("a[%d] %d ", count⑴, a[count⑴]); } else { while (s[i]!='' && s[i]!='|') { i++; } if (s[i] == '') return; } i++; } } int main() { char s[] = "111111|⑴23213|dsafd| adf | ⑴23123213213123|adaew|12334243243243"; strToNum(s); //printf("max int %d, min int %d ", INT_MAX, INT_MIN); return 0; }


 

生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生