Windows中

来源:互联网 发布:随机数预测算法 编辑:程序博客网 时间:2024/06/09 21:01
int compare(CString str1,CString str2){int i,j;i=0;j=0;string temp1,temp2;int num1,num2;int length=min(str1.GetLength(),str2.GetLength()); while(i<length&&j'9'||str1.GetAt(i)<'0'||str2.GetAt(j)>'9'||str2.GetAt(j)<'0')//字符串至少有一个不是数字时的处理方法。 { if(str1.GetAt(i)==str2.GetAt(j)){ i++;j++;continue;} else if(str1.GetAt(i)>str2.GetAt(j))return 1; else return -1; } ////////////////分割线//////////////// while(str1.GetAt(i)<='9'&&str1.GetAt(i)>='0')//两个字符串都是数字时的处理方法{temp1+=str1.GetAt(i);i++;} while(str2.GetAt(j)<='9'&&str2.GetAt(j)>='0'){temp2+=str2.GetAt(j);j++;} num1=atoi(temp1.c_str());//字符串转成数字,例如int *i="12"; int n=atoi(i);println("%d",n); 结果是12 num2=atoi(temp2.c_str()); if(num1==num2) //当字符串转成的数字大小相等时,例如123=123或者012=12{ if(temp1.length()temp2.length())return -1; else continue; } else if(num1>num2)//当字符串转成的数字大小不相等时,便可直接得出结果。 return 1; else return -1; } return (str1.GetLength()>str2.GetLength())?1:-1; }
0 0
原创粉丝点击