#include <stdio.h> #include <stdlib.h> #define INT_MAX ((1<<31)-1) #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 = -1; 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==-1) ? INT_MIN : INT_MAX; num = num*10+s[begin]-'0'; begin++; } return num*flag; } /*以|分隔符的数值转换 一面*/ void strToNum(char* s) { int a[20]; int i = 0; int begin; int end; int count = 0; if (s == NULL) return; while (s[i]!='\0' && s[i]=='|') i++; while (s[i] != '\0') { begin = i; while (s[i]!='\0' && s[i]!='|' && isRight(s[i])) { i++; } if (s[i]=='\0' || s[i]=='|') { end = i-1; a[count++] = getNum(s, begin, end); printf("a[%d] %d\n", count-1, a[count-1]); } else { while (s[i]!='\0' && s[i]!='|') { i++; } if (s[i] == '\0') return; } i++; } } int main() { char s[] = "111111|-123213|dsafd| adf | -123123213213123|adaew|12334243243243"; strToNum(s); //printf("max int %d, min int %d\n", INT_MAX, INT_MIN); return 0; }
时间: 2024-10-19 09:33:04