添加两个大数字作为字符串
Add two large numbers as strings
我不会在此处添加 "real" 代码片段,因为我尝试了很多变体 - 并取得了不同程度的成功 - 所以我将使用类似 C 的伪代码。
===
我想加两个超过ANSI C
最大long long
的数(比如两个50位的数)
我的想法是,我将使用两个 char[]
数组,并通过将两个加数的每个字符转换为整数,添加并携带十位,然后分配来进行经典的纸笔式加法结果再次作为 char
到 char[]
数组。
我 运行 遇到的问题是将 char
转换为 int
(总是失败)...然后将结果添加到另一个文本数组。尝试使用 result[i] = "5"
或什至其 ascii 值 result[i] = 53
向 char result[]
添加字符总是失败。
伪代码
int add(char *n1, char *n2){
// examples (Intentionally not the same length)
// n1 = "12345678901234567890"
// n2 = "987654321098765"
char result[100]; // array for resulting added ints as char
int r = 100; // The index (set to end) for the result array
result[r] = 0; // Assign the char-halt to END of the result
int carry = 0; // for carrying the 10s
maxlength = <length of largest addend> // (sizeof(n)/sizeof(n[0])) doesnt work because pointers
// start at end (right end) of arrays and move towards start (left end)
// each loop takes one character, starting at the far RIGHT (end) of the string array
// i = (maxlength - 1) to skip stop "0"
for (int i = (maxlength - 1); i >= 0; i--) {
a1 = int()n1[i] // doesnt return correct value in tests. Neither does a1 = n1[i]-0
a2 = int()n1[i] // doesnt return correct value in tests. Neither does a1 = n1[i]-0
int asum = a1 + a2 + carry
// carry all the tens
carry = 0; // reset carry
while (asum > 10){
carry += 10;
asum -= 10;
}
result[r] = char()asum
r -= 1 // Move result index one to the LEFT
}
}
The problems I'm running into is converting the char
to int
...
C 标准保证 '0'
到 '9'
的 10 个字符保持连续递增的值:
[...]
3 Both the basic source and basic execution character sets shall have the following members:
the 10 decimal digits
0 1 2 3 4 5 6 7 8 9
[...] the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous.
看看这个,了解一下概念:
#include stdio.h>
int main(void)
{
char c = '7';
int i = c - '0';
printf("c = %c, i = %d\n", c, i);
}
我不会在此处添加 "real" 代码片段,因为我尝试了很多变体 - 并取得了不同程度的成功 - 所以我将使用类似 C 的伪代码。
===
我想加两个超过ANSI C
最大long long
的数(比如两个50位的数)
我的想法是,我将使用两个 char[]
数组,并通过将两个加数的每个字符转换为整数,添加并携带十位,然后分配来进行经典的纸笔式加法结果再次作为 char
到 char[]
数组。
我 运行 遇到的问题是将 char
转换为 int
(总是失败)...然后将结果添加到另一个文本数组。尝试使用 result[i] = "5"
或什至其 ascii 值 result[i] = 53
向 char result[]
添加字符总是失败。
伪代码
int add(char *n1, char *n2){
// examples (Intentionally not the same length)
// n1 = "12345678901234567890"
// n2 = "987654321098765"
char result[100]; // array for resulting added ints as char
int r = 100; // The index (set to end) for the result array
result[r] = 0; // Assign the char-halt to END of the result
int carry = 0; // for carrying the 10s
maxlength = <length of largest addend> // (sizeof(n)/sizeof(n[0])) doesnt work because pointers
// start at end (right end) of arrays and move towards start (left end)
// each loop takes one character, starting at the far RIGHT (end) of the string array
// i = (maxlength - 1) to skip stop "0"
for (int i = (maxlength - 1); i >= 0; i--) {
a1 = int()n1[i] // doesnt return correct value in tests. Neither does a1 = n1[i]-0
a2 = int()n1[i] // doesnt return correct value in tests. Neither does a1 = n1[i]-0
int asum = a1 + a2 + carry
// carry all the tens
carry = 0; // reset carry
while (asum > 10){
carry += 10;
asum -= 10;
}
result[r] = char()asum
r -= 1 // Move result index one to the LEFT
}
}
The problems I'm running into is converting the
char
toint
...
C 标准保证 '0'
到 '9'
的 10 个字符保持连续递增的值:
[...]
3 Both the basic source and basic execution character sets shall have the following members: the 10 decimal digits
0 1 2 3 4 5 6 7 8 9
[...] the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous.
看看这个,了解一下概念:
#include stdio.h>
int main(void)
{
char c = '7';
int i = c - '0';
printf("c = %c, i = %d\n", c, i);
}