【字串處理演算法】字串包含的演算法設計及C代碼實現【轉】

來源:互聯網
上載者:User

標籤:atoi   原創   多個   gcc   sof   inux   內容   編譯   程式   

轉自:http://blog.csdn.net/zhouzhaoxiong1227/article/details/50679587

著作權聲明:本文為博主原創文章,對文章內容有任何意見或建議,歡迎與作者單獨交流,作者QQ():245924426。

一、需求描述

輸入一個由數字構成的字串,編寫程式將該字串轉換為整數並輸出。

 

例如,如果輸入的字串是“12345”,那麼輸出的整數是12345。注意,不要使用C語言的庫函數atoi。

 

二、演算法設計

我們都知道,如果給定一個整數123,那麼其表示方法是:123=1*100+2*10+3。也就是說,一個整數是由其各位上的數字按照位元求和組成的。

 

因此,這個需求的解決方案很簡單,只要將字串中的各位元字按照其位元相加就行了。在此過程中,要考慮一些特殊情況。

 

程式的總體流程1所示。

圖1 程式的總體流程

 

三、特殊流程考慮

在編寫程式的過程中,我們要對輸入的數字串的長度及格式多做考慮,如:

1.如果輸入的字串中包含了除數字之外的其它字元,那麼程式直接返回,不進行後續處理。

 

2.如果數字串是以一個或多個字元0開頭的,則要先將其去掉之後再進行後續處理。

 

3.因為在c語言中,整型(int)所能表示的最大數為2147483647,所以如果輸入的數字串大於了“2147483647”,那麼程式直接返回,不進行後續處理。

 

四、程式碼

 

   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  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 158 159 160
/*********************************************************************** 著作權 (C)2016, Zhou Zhaoxiong。** 檔案名稱: StrToInt.c* 檔案標識: 無* 內容摘要: 將字串轉換為整數* 其它說明: 例如, 將"123"轉換為123* 目前的版本: V1.0* 作 者: Zhou Zhaoxiong* 完成日期: 20160218***********************************************************************/#include <stdio.h>#include <limits.h> // 由於在代碼中使用了INT_MAX, 因此要包含該標頭檔  // 重新定義資料類型typedef signed char INT8;typedef int INT32;typedef unsigned int UINT32;  // 函式宣告INT32 CalIntVal(INT32 iBitLen);INT32 JudgeIfOverFlow(INT8 *pszTestStr);  /*********************************************************************** 功能描述: 主函數* 輸入參數: 無* 輸出參數: 無* 返 回 值: 0-執行成功 其它-執行失敗* 其它說明: 無* 修改日期 版本號碼 修改人 修改內容* ---------------------------------------------------------------------* 20160218 V1.0 Zhou Zhaoxiong 建立***********************************************************************/INT32 main(){INT8 szInputStr[100] = {0};INT8 szTestStr[100] = {0};INT32 iResultInt = 0; // 轉換之後的整數最大支援2147483647UINT32 iPosFlag = 0;UINT32 iTestStrLen = 0;UINT32 iBitVal = 0;INT32 iRetVal = 0; printf("Max value of int is %d\n", INT_MAX); // 求出int的最大值 printf("Please input the string: \n");scanf("%s", szInputStr);printf("InputStr=%s\n", szInputStr); // 判斷輸入的字串中是否有除數字之外的其它字元, 若有, 則直接退出for (iPosFlag = 0; iPosFlag < strlen(szInputStr); iPosFlag ++){if (szInputStr[iPosFlag] < ‘0‘ || szInputStr[iPosFlag] > ‘9‘){printf("%s is not a digital string, please check!\n", szInputStr);return -1;}} // 如果字串前面有字元0, 則將其去掉iPosFlag = 0;while (szInputStr[iPosFlag] == ‘0‘){iPosFlag ++;} // 擷取去除0之後的字串值strncpy(szTestStr, szInputStr+iPosFlag, strlen(szInputStr)-iPosFlag); // 判斷字串是否大於2147483647, 若是, 則直接退出iRetVal = JudgeIfOverFlow(szTestStr);if (iRetVal != 0){printf("%s is bigger than INT_MAX(2147483647), please check!\n", szTestStr);return -1;} // 計算字串對應的整數值iTestStrLen = strlen(szTestStr);iResultInt = 0;for (iPosFlag = 0; iPosFlag < iTestStrLen; iPosFlag ++){iBitVal = szTestStr[iPosFlag] - ‘0‘; // 計算每一位對應的數字iResultInt = iResultInt + iBitVal * CalIntVal(iTestStrLen-iPosFlag);} printf("ResultInt=%d\n", iResultInt); return 0; }  /*********************************************************************** 功能描述: 判斷輸入的字串是否溢出* 輸入參數: pszTestStr-測試字串* 輸出參數: 無* 返 回 值: 1-溢出 0-未溢出* 其它說明: 判斷字串是否大於2147483647, 若是, 則溢出* 修改日期 版本號碼 修改人 修改內容* ---------------------------------------------------------------* 20160218 V1.0 Zhou Zhaoxiong 建立***********************************************************************/INT32 JudgeIfOverFlow(INT8 *pszTestStr){UINT32 iTestStrLen = 0;INT8 szProcessedStr[100] = {0};INT8 szMaxValOfInt[100] = {0}; snprintf(szMaxValOfInt, sizeof(szMaxValOfInt)-1, "%d", INT_MAX); // 求出int的最大值 iTestStrLen = strlen(pszTestStr); if (iTestStrLen > strlen(szMaxValOfInt)) // 長度超過{return 1;}else if (iTestStrLen == strlen(szMaxValOfInt)) // 長度相等{if (strcmp(pszTestStr, szMaxValOfInt) > 0) // 溢出{return 1;}else{return 0;}}else // 測試字串長度小於"2147483647"的長度, 未溢出{return 0;}}   /*********************************************************************** 功能描述: 求字串中的每一位所對應的整數值* 輸入參數: iBitLen-對應整數的第多少位* 輸出參數: 無* 返 回 值: 該位所對應的整數值* 其它說明: 無* 修改日期 版本號碼 修改人 修改內容* ---------------------------------------------------------------* 20160218 V1.0 Zhou Zhaoxiong 建立***********************************************************************/INT32 CalIntVal(INT32 iBitLen){ if (iBitLen == 1) // 個位{return 1;}else{return 10 * CalIntVal(iBitLen-1);}}
 來自CODE的代碼片StrToInt.c

 

 

五、程式測試

我們將編寫好的程式“StrToInt.c”上傳到Linux機器,並使用“gcc -g -o StrToIntStrToInt.c”命令對該程式進行編譯,產生“StrToInt”檔案。下面對程式進行詳細的測試。

 

1.輸入字串為“12345”時,程式運行情況如下:

Max value of int is 2147483647

Please input the string:

12345

InputStr=12345

ResultInt=12345

 

2.輸入字串為“-12345”時,程式運行情況如下:

Max value of int is 2147483647

Please input the string:

-12345

InputStr=-12345

-12345 is not a digital string, please check!

 

3.輸入字串為“123456a”時,程式運行情況如下:

Max value of int is 2147483647

Please input the string:

123456a

InputStr=123456a

123456a is not a digital string, please check!

 

4.輸入字串為“012345”時,程式運行情況如下:

Max value of int is 2147483647

Please input the string:

012345

InputStr=012345

ResultInt=12345

 

5.輸入字串為“0123450”時,程式運行情況如下:

Max value of int is 2147483647

Please input the string:

0123450

InputStr=0123450

ResultInt=123450

 

6.輸入字串為“2147483647”時,程式運行情況如下:

Max value of int is 2147483647

Please input the string:

2147483647

InputStr=2147483647

ResultInt=2147483647

 

7.輸入字串為“2147483648”時,程式運行情況如下:

Max value of int is 2147483647

Please input the string:

2147483648

InputStr=2147483648

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

 

8.輸入字串為“123456789012”時,程式運行情況如下:

Max value of int is 2147483647

Please input the string:

123456789012

InputStr=123456789012

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

 

可見,對於上面考慮到的幾種特殊情況,程式均能做出正確的處理。

 

六、需求擴充

基於本文中的需求和程式,我們可考慮對需求進行以下擴充:

1.不限制輸入的字串中只能包含數字,也可以在開頭包含“+”或“-”。如果字串是以“+”開頭,那麼最後輸出的整數是正整數;如果字串是以“-”開頭,那麼最後輸出的整數是負整數。

 

2.如果輸入的數字串大於了“2147483647”,那麼程式直接輸出整數值為2147483647。

【字串處理演算法】字串包含的演算法設計及C代碼實現【轉】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.