"String processing algorithm" string contains the algorithm design and C code implementation "Go"

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/zhouzhaoxiong1227/article/details/50679587

Copyright NOTICE: This article for Bo Master original article, have any comments or suggestions to the article content, welcome to communicate with the author alone, the author QQ (): 245924426.

I. Description of requirements

Enter a string of numbers, and the program converts the string to an integer and outputs it.

For example, if the input string is "12345", the output integer is 12345. Note that you do not use the C-language library function atoi.

Second, the algorithm design

As we all know, if given an integer 123, then it is represented by: 123=1*100+2*10+3. That is, an integer is made up of the numbers on each of them in terms of the sum of the digits.

Therefore, the workaround for this requirement is simple, as long as you add the numbers in the string to the number of digits. In this process, there are some special cases to consider.

The overall process of the program is shown in 1.

Figure 1 The overall process of the program

III. Special Process Considerations

In the process of writing a program, we have to consider the length and format of the input number string, such as:

1. If the input string contains characters other than the number, the program returns directly without subsequent processing.

2. If the number string starts with one or more characters 0, it must be removed before subsequent processing.

3. Because in the C language, integer (int) can represent a maximum number of 2147483647, so if the number of input string is greater than "2147483647", then the program returns directly, no subsequent processing.

Iv. Code of the program

   1   2   3   4   5   6   7   8  9  30 of each of the above. The all-in-a  -  ------  57 All-in-a  -  The all-in-a-  84  94  ------  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 1 58 159 160
/*********************************************************************** All rights reserved (C), Zhou Zhaoxiong. ** File name: strtoint.c* File ID: none* Content Summary: Convert a string to an integer* Other instructions: For example, convert "123" to 123* Current version: V1.0* Author: Zhou Zhaoxiong* Completion Date: 20160218***********************************************************************/#include <stdio.h>#include <limits.h> //Because Int_max is used in the code, the header file is included //Redefine data typetypedef signed char INT8; typedef INT INT32; typedef unsigned int UINT32; //Function declarationINT32 calintval(INT32 ibitlen); INT32 judgeifoverflow(INT8 *pszteststr); /*********************************************************************** Function Description: main function* Input parameters: none* Output parameters: None* Return value: 0-execution succeeded other-execution failed* Other instructions: none* Modified Date version number modify the content of the person* ---------------------------------------------------------------------* 20160218 V1.0 Zhou Zhaoxiong created***********************************************************************/INT32 main() {INT8 szinputstr[+] = {0}; INT8 szteststr[+] = {0}; INT32 iresultint = 0; //integer maximum support 2147483647 after conversion UINT32 Iposflag = 0; UINT32 Iteststrlen = 0; UINT32 ibitval = 0; INT32 iretval = 0; printf("Max value of int is%d\ n", Int_max); //Find the maximum value of int printf("Please input the string: \ n"); scanf("%s", szinputstr); printf("inputstr=%s\ n", szinputstr); //Determine if there are any characters in the input string other than the number, and if so, exit directlyfor (iposflag = 0; Iposflag < strlen(szinputstr); Iposflag + +) {if (szinputstr[iposflag] < ' 0 ' | | szinputstr[iposflag] > ' 9 ') {printf("%s is not a digital string, check! \ n", szinputstr); return -1; }} //If the string is preceded by a character 0, remove itiposflag = 0; while (szinputstr[iposflag] = = ' 0 ') {Iposflag + +; }//Gets the string value after removing 0strncpy(szteststr, szinputstr+iposflag, strlen(szinputstr) -iposflag); //Determine if the string is greater than 2147483647, and if so, exit directlyiretval = judgeifoverflow(szteststr); if (iretval! = 0) {printf("%s is bigger than Int_max (2147483647), please check! \ n", szteststr); return -1; }//Calculate the integer value corresponding to the stringIteststrlen = strlen(szteststr); iresultint = 0; for (iposflag = 0; Iposflag < Iteststrlen; Iposflag + +) {ibitval = szteststr[iposflag] - ' 0 '; //calculate each digit corresponding to the number iresultint = iresultint + ibitval * calintval(iteststrlen-iposflag );}printf("resultint=%d\ n", iresultint); return 0; }/*********************************************************************** Function Description: Determine if the input string is overflow* Input parameters: pszteststr-Test String* Output parameters: None* Return value: 1-overflow 0-No overflow* Other instructions: Determine if the string is greater than 2147483647, if so, then overflow* Modified Date version number modify the content of the person* ---------------------------------------------------------------* 20160218 V1.0 Zhou Zhaoxiong created***********************************************************************/INT32 judgeifoverflow(INT8 *pszteststr) {UINT32 Iteststrlen = 0; INT8 szprocessedstr[+] = {0}; INT8 szmaxvalofint[+] = {0}; snprintf(szmaxvalofint, sizeof(szmaxvalofint)-1, "%d ", Int_max); //Find the maximum value of int Iteststrlen = strlen(pszteststr); if (iteststrlen > strlen(szmaxvalofint)) //length exceeds {return 1; }Else if (iteststrlen = = strlen(szmaxvalofint)) //equal length {if (strcmp(pszteststr, szmaxvalofint) > 0) //Overflow {return 1; }Else{return 0; }}Else //test string length less than "2147483647" length, not overflow {return 0; }}/*********************************************************************** Function Description: To find the integer value corresponding to each bit in the string* Input Parameters: ibitlen-the number of digits corresponding to the integer* Output parameters: None* return value: The integer value corresponding to the bit* Other instructions: none* Modified Date version number modify the content of the person* ---------------------------------------------------------------* 20160218 V1.0 Zhou Zhaoxiong created***********************************************************************/INT32 calintval(INT32 ibitlen) { if (ibitlen = = 1) //digit {return 1; }Else{return * calintval(ibitlen-1); }}
code slices from codesStrtoint.c

Five, the procedure test

We will write the program "strtoint.c" upload to the Linux machine, and use the "Gcc-g-o strtointstrtoint.c" command to compile the program, generate "Strtoint" file. The program is tested in detail below.

1. When the input string is "12345", the program runs as follows:

Max value of int is 2147483647

Please input the string:

12345

inputstr=12345

resultint=12345

2. When the input string is "12345", the program runs as follows:

Max value of int is 2147483647

Please input the string:

-12345

inputstr=-12345

-12345 is not a digital string, please check!

3. When the input string is "123456a", the program runs as follows:

Max value of int is 2147483647

Please input the string:

123456a

inputstr=123456a

123456A is not a digital string, check!

4. When the input string is "012345", the program runs as follows:

Max value of int is 2147483647

Please input the string:

012345

inputstr=012345

resultint=12345

5. When the input string is "0123450", the program runs as follows:

Max value of int is 2147483647

Please input the string:

0123450

inputstr=0123450

resultint=123450

6. When the input string is "2147483647", the program runs as follows:

Max value of int is 2147483647

Please input the string:

2147483647

inputstr=2147483647

resultint=2147483647

7. When the input string is "2147483648", the program runs as follows:

Max value of int is 2147483647

Please input the string:

2147483648

inputstr=2147483648

2147483648 is bigger than Int_max (2147483647), please check!

8. When the input string is "123456789012", the program runs as follows:

Max value of int is 2147483647

Please input the string:

123456789012

inputstr=123456789012

123456789012 is bigger than Int_max (2147483647), please check!

Can be seen, for the above considerations of the special circumstances, the program will be able to make the correct treatment.

Vi. Expansion of demand

Based on the requirements and procedures in this article, we can consider the following extensions to the requirements:

1. Do not limit the input string to include only numbers, or you can include "+" or "-" at the beginning. If the string starts with "+", then the last integer output is a positive integer, and if the string begins with "-", the last integer that is output is a negative integer.

2. If the input number string is greater than "2147483647", then the program directly outputs an integer value of 2147483647.

"String processing algorithm" string contains the algorithm design and C code implementation "Go"

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.