The "C-language" numeric string is converted to the corresponding number of this string.

Source: Internet
Author: User

(1) The int ascii_to_integer (char *str) function is implemented.

Requirement: This string argument must contain one or more numbers, and the function should convert the numbers to integers and return the integer. If the string argument contains any non-numeric characters, the function returns zero. Don't worry about arithmetic overflow.

tip: Every number you find, multiply the current value by 10 and add the value to the value represented by the new number.

Directly on the code:

#include <stdio.h> #include <assert.h>intAscii_to_integer (Char*Str){intn =0;assert(Str); while(*Str!=' + ')    { while(*Str>=' 0 '&& *Str<=' 9 ')//Determine if the string is all numeric{N. = nTen+ (*Str-' 0 ');Str++; }returnN }return 0;}intMain () {CharA[] ="12345"; printf"%d\n", Ascii_to_integer (a));return 0;}

Explain:

The character pointer subtracts ' 0 ' (corresponding to an ASCII value of 48), converting its corresponding ASCII value to an integer type. The first loop *str points to the character ' 1 ', its corresponding ASCII code value is 49, and ' 0 ' corresponds to the ASCII value 48, so the use of "*str-' 0 '" is to convert the character ' 1 ' to the number 1, and so on.
.
.
.
.
.
.
.

(2) The Double my_atof (char *str) function is implemented.

Requirement: 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; "12.34" returns-12.34

#include <stdio.h>#include <assert.h>DoubleMy_atof (Char*Str){Doublen =0.0;DoubleTMP =10.0;intFlag =0; AssertStr);if(*Str=='-') {flag =1;Str++; } while(*Str>=' 0 '&& *Str<=' 9 ') {n = *Ten+ (*Str-' 0 ');Str++; }if(*Str='. ')      {Str++; while(*Str>=' 0 '&& *Str<=' 9 ') {n = n + (*Str-' 0 ')/tmp;; TMP = TMP *Ten;Str++; }      }if(Flag = =1) {n = ñ; }returnn;}intMain () {CharA[] ="12.345678";CharB[] =" -12.345678"; printf"%f\n%f\n", My_atof (a), my_atof (b));return 0;}

Direct:


.
.
.
.
.
.
.
(3) The int my_atoi (char *str) function is implemented.

Requirement: converts a numeric string to a number corresponding to the string (including positive integers, negative integers).

For example: "12" returns 12, "123" returns-123;

#include <stdio.h> #include <assert.h>intMy_atoi (Char*Str){intn =0;intFlag =0;assert(Str);if(*Str=='-') {flag =1;Str++; } while(*Str>=' 0 '&& *Str<=' 9 ') {n = *Ten+ (*Str-' 0 ');Str++; }if(Flag = =1) {n = ñ; }returnn;}intMain () {CharA[] =" A";CharB[] =" -123"; printf"%d\n%d\n", My_atoi (a), My_atoi (b));return 0;}

.
.
.
.
.
.
The implementation of the above three functions is very similar, just one, the others.

The "C-language" numeric string is converted to the corresponding number of this string.

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.