博士
最后登录1970-1-1
在线时间 小时
注册时间2020-5-14
|
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef int bool;
- #define false (-1)
- #define true (0)
- static bool char_check(char c) {
- if (c > '9') {
- return false;
- }
- if (c < '0') {
- return false;
- }
- return true;
- }
- static bool str_check(const char* src) {
- for ( ; *src; src++) {
- if (false == char_check(*src)) {
- return false;
- }
- }
- return true;
- }
- static void str_reverse(char* s) {
- char temp;
- char* r = s + strlen(s) - 1;
- while (s < r) {
- temp = *s;
- *s++ = *r;
- *r-- = temp;
- }
- }
- static bool char_add(char a, char b, char* c, char* d) {
- if (false == char_check(a)) {
- return false;
- }
- if (false == char_check(b)) {
- return false;
- }
- a -= '0';
- b -= '0';
- *d = a + b + (*c);
- if (*d > 9) {
- *d -= 10;
- *c = 1;
- }
- else {
- *c = 0;
- }
- *d += '0';
- return true;
- }
- static bool str_add(const char* a, const char* b, char* d) {
- char aa, bb, c = 0;
- int len_a = strlen(a), len_b = strlen(b);
- char* p_a = a + len_a - 1;
- char* p_b = b + len_b - 1;
- char* p_d = d;
- if (false == str_check(a)) {
- return false;
- }
- if (false == str_check(b)) {
- return false;
- }
- for ( ; (len_a > 0) || (len_b > 0); ) {
- aa = len_a > 0? *p_a-- : '0';
- bb = len_b > 0? *p_b-- : '0';
- char_add(aa, bb, &c, p_d++);
- len_a--;
- len_b--;
- }
- str_reverse(d);
- return true;
- }
- int main()
- {
- char* a = "16664129129421912941294";
- char* b = "34666666666543854397591";
- char c[100] = "";
- str_add(a, b, c);
- printf("%s!\n", c);
- return 0;
- }
复制代码
|
|