超长正整数相加
請設計一個算法完成兩個超長正整數的加法。用字符串來解決,按位加然后考慮進位就OK。
下面是代碼實現:
| 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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | #ifndef?_ADDLONGINTERGER_ #define?_ADDLONGINTERGER_ /*================================?Macros?===================================*/ #define?FALSE??0 #define?TURE?1 /*=============================?Include?Function?===============================*/ int?AddLongInteger(char?*addend,?char?*augend,char?*result); #endif//_ADDLONGINTERGER_ #include"AddLongInteger.h" #include<string.h> /***********************************************************/ /************函數名:AddCharInerger*************************/ /************入口參數:加數字符和被加數字符*****************/ /************返回值:相加結果*******************************/ /************函數功能:求兩個數相加結果*********************/ /***********************************************************/ static?int?AddCharInerger(char?addend,char?augend) { ????int??ret?=?(addend?-'0')?+?(augend?-?'0'); ????return?ret; } /***********************************************************/ /************函數名:AddLongInteger*************************/ /************入口參數:加數字符串和被加數字符串*************/ /************返回值:相加結果*******************************/ /************函數功能:求兩個超長數相加*********************/ /***********************************************************/ int?AddLongInteger(char?*addend,char?*augend,char?*result) { ????int?index?=?0; ????int?tmp?=?0; ????int?sizeAddend?=?strlen(addend); ????int?sizeAugend?=?strlen(augend); ????int?tempNumber?=?sizeAddend; ????//判斷是否正確輸入 ????if((NULL?==?addend)&&(NULL?==?augend)&&(NULL?==?result)) ????{ ????????return?FALSE; ????} ????if(tempNumber?<=?sizeAugend) ????{ ????????tempNumber?=?sizeAugend; ????} ????while(tempNumber) ????{ ????????int?temp?=?AddCharInerger(addend[sizeAddend-1],augend[sizeAugend-1])+tmp; ????????//判斷是否要進位 ????????if(temp?>=?10) ????????{ ????????????result[index]?=?temp%10?+?'0'; ????????????tmp?=?temp/10; ????????} ????????else ????????{ ????????????tmp?=?0; ????????????result[index]?=?temp?+?'0'; ????????} ????????index++; ????????sizeAddend--; ????????//判斷是否越界 ????????if(sizeAddend?==?0) ????????{ ????????????sizeAddend?+=?1; ????????????addend[sizeAddend-1]?=?'0'; ????????} ????????sizeAugend--; ????????//判斷是否越界 ????????if(sizeAugend?==?0) ????????{ ????????????sizeAugend?+=?1; ????????????augend[sizeAugend-1]?=?'0'; ????????} ????????tempNumber--; ????} ????//最后是否有進位 ????if(tmp?>?0) ????{ ????????result[index]?=?(tmp?+?'0'); ????????result[index+1]?=?'\0'; ????} ????else ????{ ????????result[index]?=?'\0'; ????} ????return?TURE; } #include"AddLongInteger.h" #include<stdio.h> #include<stdlib.h> #include<string.h> /***********************************************************/ /************函數名:inputArray*****************************/ /************入口參數:加數字符串和被加數字符串和結果*********/ /************返回值:判斷是否成功***************************/ /************函數功能:輸入*********************************/ /**********************************************************/ int?inputArrayM(char?**addend,char?**augend,char?**result) { ????char?ch?=?0; ????int?countAddend?=?0; ????int?countAugend?=?0; ????printf("請輸入加數:"); ????*addend?=?(char?*)malloc(sizeof(char)); ????if(NULL?==?*addend) ????{ ????????return?FALSE; ????} ????while((ch?=?getchar())?!=?'\n') ????{ ????????? ????????*(*addend+countAddend)?=?ch; ????????countAddend++; ????????*addend?=?(char?*)realloc(*addend,(countAddend+1)*sizeof(char)); ????????if(NULL?==?*addend) ????????{ ????????????return?FALSE; ????????} ????} ????*((*addend)+countAddend)?=?'\0'; ????printf("請輸入被加數:"); ????*augend?=?(char?*)malloc(sizeof(char)); ????if(NULL?==?*addend) ????{ ????????return?FALSE; ????} ????while((ch?=?getchar())?!=?'\n') ????{ ????????? ????????*(*augend+countAugend)?=?ch; ????????countAugend++; ????????*augend?=?(char?*)realloc(*augend,(countAugend+1)*sizeof(char)); ????????if(NULL?==?*addend) ????????{ ????????????return?FALSE; ????????} ????} ????*((*augend)+countAugend)?=?'\0'; ????if(countAugend?>?countAddend) ????{ ????????*result?=?(char?*)malloc((countAugend+3)*sizeof(char)); ????} ????else ????{ ????????*result?=?(char?*)malloc((countAddend+3)*sizeof(char)); ????} ????return?TURE; } /* 程序的入口main */ int?main() { ????int?index?=?0; ????char?*addend?=?NULL; ????char?*augend?=?NULL; ????char?*result?=?NULL; ????inputArrayM(&addend,&augend,&result); ????if(FALSE?==?AddLongInteger(addend,augend,result)) ????{ ????????printf("計算出錯\n"); ????????return?0; ????} ????index?=?strlen(result)-1; ????for(;index>=0;index--) ????{ ????????printf("%c",result[index]); ????} ????printf("\n"); ????system("pause"); ????free(addend); ????free(augend); ????free(result); ????return?0; } |
總結
- 上一篇: webservice axis1.4生成
- 下一篇: Linux系统入门之如何安装Linux系