當時還只是一個讀化工專業的大四學生
基本上對什么編程規范、什么算法,什么性能優化沒什么了解
只是簡單地用字符串模擬實現了任意大整數的四則運算
現在看來當然很幼稚了
不過為保持原來面目,一直沒改,留做紀念。
供有需要的朋友參考
敬請指教
1
//long integer operation , inclue addition,substracttion,multiplicaton
2
//and division
3
#include <stdio.h>
4
#include <conio.h>
5
#include <ctype.h>
6
#include <string.h>
7
#define MAX 1000
8
#define MARK ('0'-1)
9
10
//The Prototypes of the functions
11
char* Initialiaze(char [],int);
12
int Read(char [],char [],char * );
13
int Print(char [],char [],char [],char []);
14
int Calculate(char[],char[],char[],char []);
15
char* Addition(char[],char[]);
16
char* Substraction(char[],char[]);
17
char* Multiplication(char[],char[]);
18
char* Division(char[],char[]);
19
char Div_per_bit(char [],int , char []);
20
int Sub_per_bit(char [],char [],int *);
21
int Copy(char [],int ,char []);
22
int Compare(char [],char []);
23
int Data_Process(char []);
24
int Value(char);
25
int Check(char []);
26
int Compare(char [],char []);
27
int Judge(int);
28
int Convert(char [],char [],int);
29
30
//The main function,calling Read , Calculate and Print function
31
int main(void)
32

{ char a[MAX],b[MAX],c[2*MAX],action[MAX],ch='\t';
33
while (ch!=EOF)
34
{
35
clrscr();
36
Read(a,b,action);
37
if (Calculate(a,b,c,action))
38
Print(a,b,c,action);
39
ch=getchar();
40
}
41
return 0;
42
}
43
//The check function check if the input number is legal
44
int Check(char s[])
45

{ int i;
46
if (strlen(s)==0)
47
return 0;
48
if (strlen(s)>=MAX)
49
return 0;
50
51
for (i=0; s[i]; i++)
52
if (!isdigit(s[i]))
53
return 0;
54
return 1;
55
}
56
//The Iniatilize function ,initialize the result number before calculation
57
char* Initialize(char s[],int length)
58

{ int i;
59
for (i=0; i<length; i++)
60
s[i]=MARK;
61
return s;
62
63
}
64
//The Read function,Read the operands and the operation
65
int Read(char a[],char b[],char action[])
66

{
67
printf("************This Program calculats two long integer action!************\n");
68
printf("Input long integer A,max length is %d:\n",MAX);
69
gets(a);
70
while (!Check(a))
71
{ printf("Input error , Please input a correct value :\n");
72
gets(a);
73
}
74
printf("Iuput the operation over a & b \n");
75
printf("addition is '+' ,substraction is '-',multiplication is '*',division is '/'\n");
76
printf(" Warning:\n");
77
printf("If you do the division,A must bigger than B ,and B must not equal to zero !\n");
78
gets(action);
79
while ((Compare(action,"+")!=0) && (Compare(action,"-")!=0) && (Compare(action,"*")!=0) && (Compare(action,"/")!=0))
80
{ printf("Input error , Please input a correct action :\n");
81
gets(action);
82
}
83
printf("Input long integer b,max length is %d:\n",MAX);
84
gets(b);
85
while (!Check(b))
86
{ printf("Input error , Please input a correct value :\n");
87
gets(b);
88
}
89
return 1;
90
}
91
//The Calculate function,calling Addition,Substraction,Multiplication or Division function in accordance with the action
92
int Calculate(char a[],char b[],char c[],char action[])
93

{
94
if (Compare(action,"+")==0)
95
{ strcpy(c,Addition(a,b));
96
return 1;
97
}
98
if (Compare(action,"-")==0)
99
{ if ((Substraction(a,b))!=NULL)
100
strcpy(c,Substraction(a,b));
101
else
102
{ strcpy(c,"-");
103
strcat(c,Substraction(b,a));
104
}
105
return 1;
106
}
107
if (Compare(action,"*")==0)
108
{ strcpy(c,Multiplication(a,b));
109
return 1;
110
}
111
112
if (Compare(action,"/")==0)
113
{ if ((Division(a,b))!=NULL)
114
strcpy(c,Division(a,b));
115
else
116
{ printf("Press Ctrl-Z to end, Press Enter to recalculate!\n");
117
return 0;
118
}
119
return 1;
120
}
121
122
}
123
//The Print function , print the result
124
int Print(char a[],char b[],char c[],char action[])
125

{ printf("The result of \n%s \n%s \n%s \nis :\n",a,action,b);
126
puts(c);
127
printf("Press Ctrl-Z to end , Press Enter to recalculate
..\n");
128
return 1;
129
}
130
//The Addition function , add two operands and return the result
131
char* Addition(char a[],char b[])
132

{ char c[2*MAX],d[2*MAX];
133
int i,j,k,a_length,b_length;
134
Initialize(c,2*MAX);
135
a_length=strlen(a);
136
b_length=strlen(b);
137
for (i=a_length-1,j=b_length-1,k=2*MAX-1; ( i>=0 || j>=0 ) ; i--,j--,k--)
138
{ if ( i>=0 && j>=0 )
139
c[k]=Value(a[i])+Value(b[j])+'0';
140
else
141
if (i>=0 && j<0 )
142
c[k]=Value(a[i])+'0';
143
else
144
if ( i<0 && j>=0 )
145
c[k]=Value(b[j])+'0';
146
}
147
Data_Process(c);
148
Convert(c,d,2*MAX);
149
return d;
150
}
151
//The Substraction function , substract one operand from another operand , and return the result
152
char* Substraction(char a[],char b[])
153

{ char c[2*MAX],d[2*MAX];
154
int i,j,k,a_length,b_length,sub_result,symbol,flag[2*MAX]=
{0};
155
Initialize(c,2*MAX);
156
a_length=strlen(a);
157
b_length=strlen(b);
158
if (strcmp(a,b)==0)
159
return ("0");
160
for (i=a_length-1,j=b_length-1,k=2*MAX-1; ( i>=0 || j>=0 ) ; i--,j--,k--)
161
{ sub_result=a[i]-b[j];
162
symbol=Judge(sub_result);
163
if (i>=1 && j>=0)
164
{ if (flag[k]==0)
165
{ if (a[i]>=b[j])
166
c[k]=sub_result+'0';
167
else
168
{ c[k]=sub_result+10+'0';
169
flag[k-1]=1;
170
}
171
}
172
else
173
{ if (a[i]-b[j]>=1)
174
c[k]=sub_result-1+'0';
175
else
176
{ c[k]=sub_result+9+'0';
177
flag[k-1]=1;
178
}
179
}
180
}
181
else
182
if (i==0 && j<0)
183
{
184
if (flag[k]==0)
185
c[k]=a[i];
186
else
187
{ if (a[i]==1)
188
;
189
else
190
c[k]=a[i]-1;
191
}
192
}
193
else
194
{ if ((i==0) && (j==0))
195
{ if (flag[k]==0)
196
{ switch (symbol)
197
{ case 0: ;
198
break;
199
case 1: c[k]=sub_result+'0';
200
break;
201
case -1: return NULL;
202
break;
203
}
204
}
205
else
206
{ switch (Judge(sub_result-1))
207
{ case 0: ;
208
break;
209
case 1: c[k]=sub_result-1+'0';
210
break;
211
case -1: return NULL;
212
break;
213
}
214
}
215
}
216
else
217
if ((i<0) && (j>=0))
218
return NULL;
219
else
220
if ((i>0) && (j<0))
221
{ if (flag[k]==0)
222
c[k]=a[i];
223
else
224
{ if (a[i]>'0')
225
c[k]=a[i]-1;
226
else
227
{ c[k]='9';
228
flag[k-1]=1;
229
}
230
}
231
}
232
}
233
}
234
Convert(c,d,2*MAX);
235
return d;
236
}
237
//The Multiplication function,multipy two operands and return the result
238
char* Multiplication(char a[],char b[])
239

{ char c[2*MAX],d[2*MAX];
240
int i,j,k,p,a_length,b_length;
241
Initialize(c,2*MAX);
242
a_length=strlen(a);
243
b_length=strlen(b);
244
for (j=b_length-1; j>=0 ; j--)
245
{ k=2*MAX-(b_length-j);
246
for (i=a_length-1,p=k; i>=0; i--,p--)
247
if (c[p]==MARK)
248
{ c[p]=(Value(a[i]))*(Value(b[j]))%10+'0';
249
if (c[p-1]==MARK)
250
c[p-1]=(Value(a[i]))*(Value(b[j]))/10+'0';
251
else
252
c[p-1]+=(Value(a[i]))*(Value(b[j]))/10;
253
}
254
else
255
{ c[p]+=(Value(a[i]))*(Value(b[j]))%10;
256
if (c[p-1]==MARK)
257
c[p-1]=(Value(a[i]))*(Value(b[j]))/10+'0';
258
else
259
c[p-1]+=(Value(a[i]))*(Value(b[j]))/10;
260
}
261
Data_Process(c);
262
}
263
Convert(c,d,2*MAX);
264
return d;
265
266
}
267
//The Division function,divise one operand from another operand, and return the result
268
char* Division(char a[],char b[])
269

{ char a1[MAX],c[MAX],d[MAX];
270
strcpy(a1,a);
271
int i,k,a_length=strlen(a1),b_length=strlen(b);
272
for (i=0; b[i]; i++)
273
if (b[i]!='0')
274
break;
275
if (i==strlen(b))
276
277
{ printf("Error!\nIf you do division,the dividend must not equal to zero!\n");
278
return 0;
279
}
280
if (Compare(a,b)==-1)
281
{ printf("Error!\nIf you do division, A must bigger than B!\n");
282
return 0;
283
}
284
for ( i=0,k=0; k<a_length-b_length+1; i++,k++)
285
c[k]=Div_per_bit(a1,b_length+i,b);
286
c[k]='\0';
287
Convert(c,d,MAX);
288
return d;
289
290
}
291
//The Div_per_bit function , calculate quotient per digit ,and return the result to Division function
292
char Div_per_bit(char a[],int a_l,char b[])
293

{ int i,j;
294
char c[MAX];
295
for (i=0,j=0; i<a_l; i++)
296
if ( a[i]!=MARK)
297
{ c[j]=a[i];
298
j++;
299
}
300
c[j]='\0';
301
for (i=0; c[i]; i++)
302
if (c[i]!='0')
303
break;
304
if (i==strlen(c))
305
return '0';
306
if (Compare(c,b)<0)
307
return '0';
308
if (Compare(c,b)==0)
309
{ for ( i=0; i<a_l; i++)
310
a[i]=MARK;
311
return '1';
312
}
313
i=0;
314
while (Compare(c,b)>=0)
315
Sub_per_bit(c,b,&i);
316
Copy(a,a_l,c);
317
return ('0'+i);
318
319
}
320
//The Sub_per_bit function, do the division by using substraction time after time
321
int Sub_per_bit(char a[],char b[],int *count)
322

{ char c[MAX],d[MAX];
323
int i,j,k,a_length,b_length,sub_result,symbol,flag[MAX]=
{0};
324
Initialize(c,MAX);
325
a_length=strlen(a);
326
b_length=strlen(b);
327
if (strcmp(a,b)==0)
328
strcpy(c,"0");
329
for (i=a_length-1,j=b_length-1,k=MAX-1; ( i>=0 || j>=0 ) ; i--,j--,k--)
330
{ sub_result=a[i]-b[j];
331
symbol=Judge(sub_result);
332
if (i>=1 && j>=0)
333
{ if (flag[k]==0)
334
{ if (a[i]>=b[j])
335
c[k]=sub_result+'0';
336
else
337
{ c[k]=sub_result+10+'0';
338
flag[k-1]=1;
339
}
340
}
341
else
342
{ if (a[i]-b[j]>=1)
343
c[k]=sub_result-1+'0';
344
else
345
{ c[k]=sub_result+9+'0';
346
flag[k-1]=1;
347
}
348
}
349
}
350
else
351
if (i==0 && j<0)
352
{
353
if (flag[k]==0)
354
c[k]=a[i];
355
else
356
{ if (a[i]==1)
357
;
358
else
359
c[k]=a[i]-1;
360
}
361
}
362
else
363
{ if ((i==0) && (j==0))
364
{ if (flag[k]==0)
365
{ switch (symbol)
366
{ case 0: ;
367
break;
368
case 1: c[k]=sub_result+'0';
369
break;
370
case -1: return 0;
371
break;
372
}
373
}
374
else
375
{ switch (Judge(sub_result-1))
376
{ case 0: ;
377
break;
378
case 1: c[k]=sub_result-1+'0';
379
break;
380
case -1: return 0;
381
break;
382
}
383
}
384
}
385
else
386
if ((i<0) && (j>=0))
387
{ return 0;
388
}
389
else
390
if ((i>0) && (j<0))
391
{ if (flag[k]==0)
392
c[k]=a[i];
393
else
394
{ if (a[i]>'0')
395
c[k]=a[i]-1;
396
else
397
{ c[k]='9';
398
flag[k-1]=1;
399
}
400
}
401
}
402
}
403
}
404
Convert(c,d,MAX);
405
strcpy(a,d);
406
(*count)++;
407
return 1;
408
}
409
//The Copy function , copy a number_string to another , wipe off the inutility symbol
410
int Copy(char a[],int a_l,char c[])
411

{ int i,j,c_l=strlen(c);
412
for (i=0; i<a_l-c_l; i++)
413
a[i]=MARK;
414
for (i,j=0; j<c_l; i++,j++)
415
a[i]=c[j];
416
return 1;
417
}
418
//The Compare function ,compare two numbers and return the result
419
int Compare(char a[],char b[])
420

{ char c[MAX];
421
if ((strlen(a))>(strlen(b)))
422
return 1;
423
if ((strlen(a))==(strlen(b)))
424
return (strcmp(a,b));
425
if ((strlen(a))<(strlen(b)))
426
return -1;
427
}
428
429
//The Value function , receiver a digit_char, and return the number
430
int Value(char c)
431

{ if (isdigit(c))
432
return (c-'0');
433
else return 0;
434
}
435
//The Data_Process function,
436
int Data_Process(char s[])
437

{ int p,head,tail=2*MAX-1;
438
for (head=0; s[head]==MARK ; head++)
439
;
440
for (p=tail; p>=head ; p--)
441
if (!isdigit(s[p]))
442
if (s[p-1]!=MARK)
443
{ s[p-1]+=(s[p]-'0')/10;
444
s[p]=(s[p]-'0')%10+'0';
445
}
446
else
447
{ s[p-1]=(s[p]-'0')/10+'0';
448
s[p]=(s[p]-'0')%10+'0';
449
}
450
return 1;
451
}
452
//The Judeg function , judge the symbol of the number
453
int Judge(int i)
454

{ if (i==0)
455
return 0;
456
else
457
if (i>0)
458
return 1;
459
else
460
return -1;
461
}
462
//The Convert function , convert the original result to the final result ,wipe off the inuility symbol and number
463
int Convert(char c[],char d[],int length)
464

{ int i,j;
465
for (i=0; i<length; i++)
466
if (c[i]==MARK || c[i]=='0')
467
;
468
else
469
break;
470
for (i,j=0; i<length; i++,j++)
471
d[j]=c[i];
472
d[j]='\0';
473
return 1;
474
}
475

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32



33

34



35

36

37

38

39

40

41

42

43

44

45



46

47

48

49

50

51

52

53

54

55

56

57

58



59

60

61

62

63

64

65

66



67

68

69

70

71



72

73

74

75

76

77

78

79

80



81

82

83

84

85

86



87

88

89

90

91

92

93



94

95



96

97

98

99



100

101

102



103

104

105

106

107

108



109

110

111

112

113



114

115

116



117

118

119

120

121

122

123

124

125



126

127


128

129

130

131

132



133

134

135

136

137

138



139

140

141

142

143

144

145

146

147

148

149

150

151

152

153



154



155

156

157

158

159

160

161



162

163

164



165



166

167

168



169

170

171

172

173



174

175

176



177

178

179

180

181

182

183



184

185

186

187



188

189

190

191

192

193

194



195



196



197



198

199

200

201

202

203

204

205

206



207



208

209

210

211

212

213

214

215

216

217

218

219

220

221



222

223

224



225

226

227



228

229

230

231

232

233

234

235

236

237

238

239



240

241

242

243

244

245



246

247

248



249

250

251

252

253

254

255



256

257

258

259

260

261

262

263

264

265

266

267

268

269



270

271

272

273

274

275

276

277



278

279

280

281



282

283

284

285

286

287

288

289

290

291

292

293



294

295

296

297



298

299

300

301

302

303

304

305

306

307

308

309



310

311

312

313

314

315

316

317

318

319

320

321

322



323



324

325

326

327

328

329

330



331

332

333



334



335

336

337



338

339

340

341

342



343

344

345



346

347

348

349

350

351

352



353

354

355

356



357

358

359

360

361

362

363



364



365



366



367

368

369

370

371

372

373

374

375



376



377

378

379

380

381

382

383

384

385

386

387



388

389

390

391



392

393

394



395

396

397



398

399

400

401

402

403

404

405

406

407

408

409

410

411



412

413

414

415

416

417

418

419

420



421

422

423

424

425

426

427

428

429

430

431



432

433

434

435

436

437



438

439

440

441

442

443



444

445

446

447



448

449

450

451

452

453

454



455

456

457

458

459

460

461

462

463

464



465

466

467

468

469

470

471

472

473

474

475
