The "C language" writes a function that converts a numeric string to a number corresponding to the string (including positive floating-point numbers, negative floating-point numbers)

Source: Internet
Author: User
Tags function prototype
/* Write a function that converts a numeric string to a number corresponding to this string (including positive floating-point numbers, negative floating-point numbers) For example: "12.34"  returns 12.34 "123.34" return-123.34 function prototype: Double My_atof (char *str ) */#include <stdio.h>double my_atof (char  const *STR) {int count = 0;int n = 0;double p = 0.0;if (*str = = '-') {n = 1;str++;} while (*str! = ') ') {//Judge the decimal if (*str = = '. ') {count++;str++;continue;} Remember the number of decimal moves        if (count) {count = Count *;}    p = p * + (*STR-' 0 ');    str++;} Divide by the number of decimal points    p = p/count;if (n = = 1) {p =-p;} return p;} int main () {    char *p = " -12.34";p rintf ("converted to:%f\n", My_atof (p)); return 0;} 

The "C language" writes a function that converts a numeric string to a number corresponding to the string (including positive floating-point numbers, negative floating-point numbers)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.