|
10鱼币
原帖地址:http://bbs.fishc.org/thread-80955-1-5.html
01.#include <stdio.h>
02.#include <stdlib.h>
03.#include <string.h>
04.
05.#define INCREMENT 10
06.#define INIT_SIZE INCREMENT
07.
08.char *get_num(void);
09.char *reverse_str(char *str);
10.char *add_two_strs(char *str1, char *str2);
11.char *append_result(char *result, char ch);
12.
13.// 获取用户输入的整数
14.char *get_num(void)
15.{
16. char ch;
17. char *num; // 存储整个数据的首地址
18. char *last; // 最近一次迭代的起始地址
19. int limit = 0; // 每次迭代的限制值
20. long times = 1; // 记录重新申请多少次内存
21.
22. num = (char *)malloc(INIT_SIZE);
23. if(num == NULL)
24. {
25. exit(1);
26. }
27.
28. last = num;
29.
30. printf("请输入一个整数:");
31.
32. while((ch = getchar()) != '\n')
33. {
34. last[limit++] = ch;
35. if(limit >= INCREMENT)
36. {
37. int offset = last - num;
38. num = (char *)realloc(num, INIT_SIZE + INCREMENT * times++);
39. last = num;
40. last += offset;
41. last += INCREMENT;
42. limit = 0;
43. }
44. }
45. last[limit] = '\0';
46.
47. return num;
48.}
49.
50.// 前后翻转字符串
51.char *reverse_str(char *str)
52.{
53. char *start = str;
54. char *left = str;
55. char ch;
56.
57. if(str != NULL)
58. {
59. // 定位到字符串的最后一个字符
60. while(*str++)
61. ;
62. str -= 2;
63.
64. while(left < str)
65. {
66. ch = *left;
67. *left++ = *str;
68. *str-- = ch;
69. }
70. }
71.
72. return start;
73.}
74.
75.char *append_result(char *result, char ch)
76.{
77. char *last = result;
78. int limit = 0;
79. int times = 1;
80.
81. // 定位到字符串的末尾
82. while(last[limit++] != '\0')
83. {
84. if(limit >= INCREMENT)
85. {
86. limit = 0;
87. times++;
88. last += INCREMENT;
89. }
90. }
91.
92. last[limit - 1] = ch;
93. if(limit >= INCREMENT)
94. {
95. int offset = last - result;
96. result = (char *)realloc(result, INIT_SIZE + INCREMENT * times);
97. last = result;
98. last += offset;
99. last += INCREMENT;
100. limit = 0; // reset
101. }
102. last[limit] = '\0';
103.
104. return result;
105.}
106.
107.// 将两个字符串对应的数值相加
108.char *add_two_strs(char *str1, char *str2)
109.{
110. int carry = 0; // 存放进位
111. int num1, num2, num3;
112. char *result;
113.
114. result = (char *)malloc(INIT_SIZE);
115. if(result == NULL)
116. {
117. exit(1);
118. }
119.
120. result[0] = '\0'; // 需要 '\0'
121.
122. // 字符串的存放顺序跟加法规则相反
123. // 需要先反转字符串
124. str1 = reverse_str(str1);
125. str2 = reverse_str(str2);
126.
127. // 两个字符串按位相加
128. while(*str1 != '\0' || *str2 != '\0')
129. {
130. num1 = *str1 - '0';
131. num2 = *str2 - '0';
132.
133. if(*str1 == '\0')
134. {
135. num1 = 0;
136. }
137. else
138. {
139. str1++;
140. }
141.
142. if(*str2 == '\0')
143. {
144. num2 = 0;
145. }
146. else
147. {
148. str2++;
149. }
150.
151. num3 = num1 + num2 + carry;
152. if(num3 > 9)
153. {
154. result = append_result(result, num3 - 10 + '0');
155. carry = 1;
156. }
157. else
158. {
159. result = append_result(result, num3 + '0');
160. carry = 0;
161. }
162. }
163.
164. if(carry)
165. {
166. append_result(result, carry + '0');
167. }
168.
169. result = reverse_str(result);
170.
171. return result;
172.}
173.
174.int main(void)
175.{
176. char *num1;
177. char *num2;
178. char *result;
179.
180. num1 = get_num();
181. num2 = get_num();
182.
183. result = add_two_strs(num1, num2);
184. printf("计算结果是:%s\n", result);
185.
186. return 0;
187.}
|
|