注意num1, num2, newnum中的数位都是倒着写的。比如1234要写成4321,方便对其数位。 【代码】 #include <iostream>
using namespace std;
int main(void) { char *num1 = "982799999"; char *num2 = "4563"; char newnum[20]; char *pnum = newnum; bool carry = false; memset(newnum, '0', sizeof(newnum)); memcpy(newnum, num1, strlen(num1)); while (*num2 != '\0') { if (carry) { (*pnum)++; carry = false; } *pnum += *num2 - '0'; if (*pnum > '9') { *pnum -= 10; carry = true; } num2++; pnum++; } while (carry) { (*pnum)++; if (*pnum > '9') *pnum -= 10; else carry = false; pnum++; } *pnum = '\0'; cout << newnum << endl; system("pause"); return 0; } 【运行结果】 3490000001
|