程序员人生 网站导航

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

栏目:php教程时间:2015-03-25 11:42:50
#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; }


 

------分隔线----------------------------
------分隔线----------------------------

最新技术推荐