public class Test { public static void main(String[] args) { String a = "482658353"; System.out.println(Test.parseInt(a)); } public static int parseInt(String s) { int result = 0; boolean negative = false; int i = 0, len = s.length(); int limit = -Integer.MAX_VALUE; int multmin; int digit; if (len > 0) { char firstChar = s.charAt(0); if (firstChar < '0') { // Possible leading "+" or "-" if (firstChar == '-') { negative = true; limit = Integer.MIN_VALUE; } i++; } multmin = limit / 10; while (i < len) { // Accumulating negatively avoids surprises near MAX_VALUE digit = Character.digit(s.charAt(i++), 10); result *= 10; result -= digit; } } return negative ? result : -result; } }不考虑异常什么的
package 测试;public class Ceshi { public static void main(String[] args){ String str = "123456"; int str1=0; int x=1; for(int i=0;i<str.length();i++) { str1+=(str.charAt(str.length()-i-1)-'0')*x; x*=10; } System.out.println(str1+" "); } }
javaAPI中应该还要添加 抛出RuntimeException, public static int parseInt(String s) for(int i=0;i<s.length;i++){ if(!Character.isDigit(s.cahrAt(i))){ throw new RuntimeException( "输入字符串格式不正确") break;} }
看这个可以不package prictice;public class Demo { public static void main(String []args){ int a=0; String str="123415325"; for(int i=0;i<str.length();i++){ char c=str.charAt(i); int b=(int)(c)-48; a=a+(int)(Math.pow(10,str.length()-i-1)*b); } System.out.println(a); System.out.println(a+1); }
public class Test { public static void main(String[] args) {
String a = "482658353";
System.out.println(Test.parseInt(a));
} public static int parseInt(String s) {
int result = 0;
boolean negative = false;
int i = 0, len = s.length();
int limit = -Integer.MAX_VALUE;
int multmin;
int digit; if (len > 0) {
char firstChar = s.charAt(0);
if (firstChar < '0') { // Possible leading "+" or "-"
if (firstChar == '-') {
negative = true;
limit = Integer.MIN_VALUE;
}
i++;
}
multmin = limit / 10;
while (i < len) {
// Accumulating negatively avoids surprises near MAX_VALUE
digit = Character.digit(s.charAt(i++), 10);
result *= 10;
result -= digit;
}
}
return negative ? result : -result;
}
}不考虑异常什么的
package 测试;public class Ceshi {
public static void main(String[] args){
String str = "123456";
int str1=0;
int x=1;
for(int i=0;i<str.length();i++)
{
str1+=(str.charAt(str.length()-i-1)-'0')*x;
x*=10;
}
System.out.println(str1+" ");
}
}
public static int parseInt(String s) for(int i=0;i<s.length;i++){
if(!Character.isDigit(s.cahrAt(i))){
throw new RuntimeException(
"输入字符串格式不正确")
break;}
}
public static void main(String []args){
int a=0;
String str="123415325";
for(int i=0;i<str.length();i++){
char c=str.charAt(i);
int b=(int)(c)-48;
a=a+(int)(Math.pow(10,str.length()-i-1)*b);
}
System.out.println(a);
System.out.println(a+1);
}
}
不要滥用Math.pow,这里完全不需要。。
不要滥用Math.pow,这里完全不需要。。为什么啊
不要滥用Math.pow,这里完全不需要。。为什么啊
Math.pow速度太慢,而且由于它对double操作可能丢失精度造成某些问题,
再者本来基本运算就能解决问题,你要去调用一个函数,如果这种操作执行多次,光是函数调用的开销就是很大的
不要滥用Math.pow,这里完全不需要。。为什么啊
Math.pow速度太慢,而且由于它对double操作可能丢失精度造成某些问题,
再者本来基本运算就能解决问题,你要去调用一个函数,如果这种操作执行多次,光是函数调用的开销就是很大的学习了
#include<limits>
#include<string>
using namespace std;bool StrToInt(const char* str,long long int & result)
{
long long int num=0;
bool flag=true; if(str==NULL || *str == '\0')//字符串为空时,转换失败,此处不能用if(str == NULL)
{
flag=false;
return flag ;
} while(isspace(*str)) //跳过空格
{
str++;
} bool minus = false;
if(*str == '+')//通过第一个有效字符判断是正是负
str ++;
else if(*str == '-')
{
str ++;
minus = true;
} while(*str!='\0')//字符串结束标志
{
if(isdigit(*str))
{
num = num * 10 + (*str - '0');
if(num < 0)//整数溢出的情况,若溢出肯定变为负数
{
flag=false;
break;
}
str ++;
}
else
{
if(num==0) //当遇到第一个非数字字符时,num为0,转换失败
flag=false;
break;
} }
if(flag)
{
if(minus)
result = 0 - num;//转化为负数的方式
else
result=num;
}
return flag;
}int main ()
{
int i=0;
char myStr[100];
//cin.getline(myStr,20);
long long int temp; cout<<"输出long long int型的最大值:"<<endl
<<std::numeric_limits<long long int>::max()<<endl;
cout<<"请输入第"<< ++i <<"组测试数据"<<endl;
while(cin.getline(myStr,100))
{
if(StrToInt(myStr,temp))
{
cout<<myStr<<"转化成功,结果为:"<<temp<<endl;
}
else
cout<<"转化失败!"<<endl ;
cout<<"请输入第"<< ++i <<"组测试数据"<<endl;
}
}
不要滥用Math.pow,这里完全不需要。。为什么啊
Math.pow速度太慢,而且由于它对double操作可能丢失精度造成某些问题,
再者本来基本运算就能解决问题,你要去调用一个函数,如果这种操作执行多次,光是函数调用的开销就是很大的Math.pow 算是调用API吗