Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- double strToDouble(char str[]){
- int i=0;
- double result=0;
- int p=0;
- _Bool negativeSignP=0;
- _Bool negativeSignN=0;
- if (str[i]=='-'){
- negativeSignN=1;
- i++;
- }
- for ( ; str[i]>='0' && str[i]<='9'; i++){
- result=result*10+str[i]-'0';
- }
- if (str[i]=='.'){
- int k=10;
- i++;
- for ( ; str[i]>='0' && str[i]<='9'; i++, k*=10){
- result=result+(double)(str[i]-'0')/k;
- }
- }
- if (str[i]=='e'){
- i++;
- if (str[i]=='-'){
- negativeSignP=1;
- i++;
- }
- for ( ; str[i]>='0' && str[i]<='9'; i++){
- p=p*10+str[i]-'0';
- }
- int j;
- for (j=0; j<p; j++){
- if (negativeSignP)
- result/=10.0;
- else
- result*=10.0;
- }
- }
- if (negativeSignN){
- result*=(-1);
- }
- return result;
- }
- int main(void) {
- char n[50];
- scanf("%s", n);
- printf("%f", strToDouble(n));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment