Posted on 2014-07-26 08:21
xLsDg 阅读(388)
评论(1) 编辑 收藏 引用 所属分类:
代码库
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #define MAX_BIT ( 255u )
6
7 char* bignum_add( char *a, char *b, char *c )
8 {
9 char *pA = NULL, *pB = NULL, *pC = NULL, d = 0;
10
11 if ( strlen( a ) > strlen( b ) ) {
12 pA = a;
13 pB = b;
14
15 } else {
16 pA = b;
17 pB = a;
18 }
19
20 for ( pC = c; *pA; pA++, pB++, pC++ ) {
21 if ( *pB ) {
22 *pC = ( *pA - '0' ) + ( *pB - '0' ) + d;
23
24 } else {
25 *pC = ( *pA - '0' ) + d;
26 }
27
28 if ( 9 < *pC ) {
29 d = *pC / 10;
30 *pC %= 10;
31
32 } else {
33 d = 0;
34 }
35
36 *pC += '0';
37 }
38
39 if ( 0 < d ) {
40 *pC = d + '0';
41 }
42
43 return c;
44 }
45
46 char* bignum_sub( char *a, char *b, char *c )
47 {
48 char *pA = NULL, *pB = NULL, *pC = NULL, d = 0, flag = 0;
49
50 if ( strlen( a ) >= strlen( b ) ) {
51 pA = a;
52 pB = b;
53 flag = 1;
54
55 } else {
56 pA = b;
57 pB = a;
58 flag = -1;
59 }
60
61 for ( pC = c; *pA; pA++, pB++, pC++ ) {
62 *pC = ( *pA - '0' ) + d;
63
64 if ( *pB ) {
65 *pC = *pC - ( *pB - '0' );
66 }
67
68 if ( 0 > *pC ) {
69 if ( *( pA + 1 ) ) {
70 d = -1;
71 *pC += 10;
72
73 } else {
74 *pC = -*pC;
75 flag = -1;
76 }
77
78 } else {
79 d = 0;
80 }
81
82 *pC += '0';
83 }
84
85 if ( 0 > flag ) {
86 *pC = '-';
87 }
88
89 return c;
90 }
91
92 char* bignum_mul( char *a, char *b, char *c )
93 {
94 char *pA = a, *pB = b, *pC = c, d = 0;
95
96 for ( pB = b; *pB; pB++ ) {
97 for ( pA = a, pC = c + ( pB - b ), d = 0; *pA; pA++, pC++ ) {
98 if ( *pC ) {
99 *pC = ( *pC - '0' ) + ( *pA - '0' ) * ( *pB - '0' ) + d;
100
101 } else {
102 *pC = ( *pA - '0' ) * ( *pB - '0' ) + d;
103 }
104
105 if ( 9 < *pC ) {
106 d = *pC / 10;
107 *pC %= 10;
108
109 } else {
110 d = 0;
111 }
112
113 *pC += '0';
114 }
115
116 if ( 0 < d ) {
117 *pC = d + '0';
118 }
119 }
120
121 return c;
122 }
123
124 char* bignum_reverse( char *a )
125 {
126 char *p1 = a, *p2 = NULL;
127
128 for ( p2 = a + strlen( a ) - 1; p1 < p2; p1++, p2-- ) {
129 *p1 ^= *p2;
130 *p2 ^= *p1;
131 *p1 ^= *p2;
132 }
133
134 return a;
135 }
136
137 int main()
138 {
139 char *pA = NULL, *pB = NULL, *pC = NULL;
140
141 pA = ( char* )malloc( MAX_BIT * sizeof( char ) );
142 pB = ( char* )malloc( MAX_BIT * sizeof( char ) );
143 pC = ( char* )malloc( MAX_BIT * sizeof( char ) * 2 );
144
145 memset( pA, 0, MAX_BIT * sizeof( char ) );
146 printf( "A=" ); scanf( "%s", pA );
147 pA = bignum_reverse( pA );
148
149 memset( pB, 0, MAX_BIT * sizeof( char ) );
150 printf( "B=" ); scanf( "%s", pB );
151 pB = bignum_reverse( pB );
152
153 memset( pC, 0, MAX_BIT * sizeof( char ) * 2 );
154 printf( "--------------------------------\n" );
155 pC = bignum_add( pA, pB, pC );
156
157 pC = bignum_reverse( pC );
158 printf( "A+B=%s\n", pC );
159
160 memset( pC, 0, MAX_BIT * sizeof( char ) * 2 );
161 printf( "--------------------------------\n" );
162 pC = bignum_sub( pA, pB, pC );
163
164 pC = bignum_reverse( pC );
165 printf( "A-B=%s\n", pC );
166
167 memset( pC, 0, MAX_BIT * sizeof( char ) * 2 );
168 printf( "--------------------------------\n" );
169 pC = bignum_mul( pA, pB, pC );
170
171 pC = bignum_reverse( pC );
172 printf( "A*B=%s\n", pC );
173
174 return 0;
175 }
176