任意进制数的加法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了任意进制数的加法相关的知识,希望对你有一定的参考价值。
实现一个2-35位任意进制的加法。输入有整型表示进制是多少;两个需要相加的字符串。对于不合法的输入输出-1.
比如:
20
jj
10
10j
30
gggt
1
ggh0
程序实现如下:
1 #include <string> 2 #include <iostream> 3 4 #define MAX_NUM 100000 //最多这么多位字符。发现再多的话程序崩溃 5 using namespace std; 6 7 void reversal(string &s,unsigned long long len) 8 { 9 for (unsigned long long i = 0; i < len / 2; i++) 10 { 11 char tmp = s[i]; 12 s[i] = s[len - i - 1]; 13 s[len - i - 1] = tmp; 14 } 15 } 16 17 bool mytoInt(string s, int a[], unsigned long long len,unsigned short N) 18 { 19 for (unsigned long long i = 0; i < len; i++) 20 { 21 if (s[i] >= ‘0‘ && s[i] <= ‘9‘) 22 { 23 a[i] = s[i] - 48; 24 } 25 else if (s[i] >= ‘a‘ && s[i] <= ‘z‘) 26 { 27 a[i] = s[i] - ‘a‘ + 10; 28 } 29 if (a[i] >= N)//判断输入是否合法;比如进制是10,但输入了a这样的字符。 30 { 31 return true; 32 } 33 } 34 return false; 35 } 36 char myInttoChar(int a) 37 { 38 if (a <= 9) 39 { 40 return a + 48; 41 } 42 else 43 { 44 return a + ‘a‘ - 10; 45 } 46 47 } 48 int main() 49 { 50 unsigned short N; 51 string stra, strb; 52 53 while (cin >> N ) 54 { 55 int sa[MAX_NUM] = { 0 }, sb[MAX_NUM] = {0}; 56 57 cin >> stra >> strb; 58 if (N < 2 || N > 35) 59 { 60 cout << -1 << endl; 61 break; 62 } 63 if (stra == "" || strb == "") 64 { 65 cout << -1 << endl; 66 break; 67 } 68 unsigned long long lena = stra.length(); 69 unsigned long long lenb = strb.length(); 70 reversal(stra,lena); 71 reversal(strb,lenb); 72 bool flag1 = mytoInt(stra, sa,lena,N); 73 bool flag2 = mytoInt(strb, sb,lenb,N); 74 if (flag1 || flag2) 75 { 76 cout << -1 << endl; 77 break; 78 } 79 unsigned long long i, j; 80 81 if (lena > lenb)//a的长度大于等于b 82 { 83 i = 0; 84 for (i = 0; i < lenb; i++) 85 { 86 sa[i] = sa[i] + sb[i]; 87 sa[i + 1] = sa[i + 1] + sa[i] / N; 88 sa[i] = sa[i] % N; 89 } 90 while (sa[i] >= N)//a剩下的位数进行调整;高位 91 { 92 if (i == lena - 1) 93 { 94 int tmp = sa[i] / N; 95 sa[i] = sa[i] % N; 96 cout << myInttoChar(tmp); 97 break; 98 } 99 sa[i + 1] = sa[i + 1] + sa[i] / N; 100 sa[i] = sa[i] % N; 101 i++; 102 } 103 //输出 104 unsigned long long max = lena > i ? lena : i;//若最高位有进位则i大 105 for (int j = max - 1; j >= 0; j--) 106 { 107 cout << myInttoChar(sa[j]); 108 } 109 cout << endl; 110 } 111 else if (lena < lenb) 112 { 113 i = 0; 114 for (i = 0; i < lena; i++) 115 { 116 sb[i] = sb[i] + sa[i]; 117 sb[i + 1] = sb[i + 1] + sb[i] / N; 118 sb[i] = sb[i] % N; 119 } 120 while (sb[i] >= N) 121 { 122 if (i == lenb - 1) 123 { 124 int tmp = sb[i] / N; 125 sb[i] = sb[i] % N; 126 cout << myInttoChar(tmp); 127 break; 128 } 129 sb[i + 1] = sb[i + 1] + sb[i] / N; 130 sb[i] = sb[i] % N; 131 i++; 132 } 133 // 输出 134 unsigned long long max = lenb > i ? lenb : i;//若最高位有进位则i大 135 for (int j = max - 1; j >= 0; j--) 136 { 137 cout << myInttoChar(sb[j]); 138 } 139 cout << endl; 140 } 141 else if (lena == lenb) 142 { 143 i = 0; 144 for (i = 0; i < lenb - 1; i++) 145 { 146 sa[i] = sa[i] + sb[i]; 147 sa[i + 1] = sa[i + 1] + sa[i] / N; 148 sa[i] = sa[i] % N; 149 } 150 sa[i] = sa[i] + sb[i]; 151 if (sa[i] >= N) 152 { 153 int tmp = sa[i] / N; 154 sa[i] = sa[i] % N; 155 cout << myInttoChar(tmp); 156 } 157 //print 158 for (int i = lena - 1; i >= 0; i--) 159 { 160 cout << myInttoChar(sa[i]); 161 } 162 cout << endl; 163 } 164 165 } 166 167 return 0; 168 }
以上是关于任意进制数的加法的主要内容,如果未能解决你的问题,请参考以下文章