Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - C language programming design: Convert a numeric string into a long integer with the same face value. If the string "2345210" is input, is it 2345210?
C language programming design: Convert a numeric string into a long integer with the same face value. If the string "2345210" is input, is it 2345210?

The logic of your code is to start from the high bit, then multiply by 10 and then accumulate. The idea is okay, but the code is wrong. 1: Do not use commas in the conditional part of the for statement. That is a comma expression, and only the rightmost m>=0 works. 2: The prototype of the pow function is double type. What you want is an integer type. If a strong transformation occurs in the middle, there will be errors. For example, if you enter "100000".

The simplest way is to write a pow function yourself to replace the library function.

The following is changed exactly according to your code:

#include

#include

#include

long pow2(int n,int m)

{

int i;

long sum =1;

for(i=0;i

sum*=n;

return sum;

}

long fun(char *s)

{

int i,n,m;

long sum=0;

n=strlen(s);

m=n-1;

for(i=0;i=0;i++,m --)

sum+=(s[i]-'0')*pow2(10,m);

return sum;

}

int main()

{

printf("Convert the string "2345210" into a numerical value =%ld\n", fun("2345210"));

printf("Convert the string "10000000" into a numerical value =%ld\n", fun("10000000"));

return 0;

}