这个程序应该是我在2004年写的
当时还只是一个读化工专业的大四学生
基本上对什么编程规范、什么算法,什么性能优化没什么了解
只是简单地用字符串模拟实现了
任意大整数的四则运算
现在看来当然很幼稚了
不过为保持原来面目,一直没改,留做纪念。
供有需要的朋友参考
敬请指教
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![](/Images/OutliningIndicators/None.gif)
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![](/Images/OutliningIndicators/None.gif)
30
//The main function,calling Read , Calculate and Print function
31
int main(void)
32![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{ char a[MAX],b[MAX],c[2*MAX],action[MAX],ch='\t';
33
while (ch!=EOF)
34![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{ int i;
46
if (strlen(s)==0)
47
return 0;
48
if (strlen(s)>=MAX)
49
return 0;
50![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{ int i;
59
for (i=0; i<length; i++)
60
s[i]=MARK;
61
return s;
62![](/Images/OutliningIndicators/InBlock.gif)
63
}
64
//The Read function,Read the operands and the operation
65
int Read(char a[],char b[],char action[])
66![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ 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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ 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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ 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![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
94
if (Compare(action,"+")==0)
95![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ strcpy(c,Addition(a,b));
96
return 1;
97
}
98
if (Compare(action,"-")==0)
99![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ if ((Substraction(a,b))!=NULL)
100
strcpy(c,Substraction(a,b));
101
else
102![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ strcpy(c,"-");
103
strcat(c,Substraction(b,a));
104
}
105
return 1;
106
}
107
if (Compare(action,"*")==0)
108![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ strcpy(c,Multiplication(a,b));
109
return 1;
110
}
111![](/Images/OutliningIndicators/InBlock.gif)
112
if (Compare(action,"/")==0)
113![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ if ((Division(a,b))!=NULL)
114
strcpy(c,Division(a,b));
115
else
116![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ printf("Press Ctrl-Z to end, Press Enter to recalculate!\n");
117
return 0;
118
}
119
return 1;
120
}
121![](/Images/OutliningIndicators/InBlock.gif)
122
}
123
//The Print function , print the result
124
int Print(char a[],char b[],char c[],char action[])
125![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{ 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![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{ 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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ 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![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{ char c[2*MAX],d[2*MAX];
154![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ sub_result=a[i]-b[j];
162
symbol=Judge(sub_result);
163
if (i>=1 && j>=0)
164![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ if (flag[k]==0)
165![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ if (a[i]>=b[j])
166
c[k]=sub_result+'0';
167
else
168![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ c[k]=sub_result+10+'0';
169
flag[k-1]=1;
170
}
171
}
172
else
173![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ if (a[i]-b[j]>=1)
174
c[k]=sub_result-1+'0';
175
else
176![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ c[k]=sub_result+9+'0';
177
flag[k-1]=1;
178
}
179
}
180
}
181
else
182
if (i==0 && j<0)
183![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
184
if (flag[k]==0)
185
c[k]=a[i];
186
else
187![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ if (a[i]==1)
188
;
189
else
190
c[k]=a[i]-1;
191
}
192
}
193
else
194![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ if ((i==0) && (j==0))
195![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ if (flag[k]==0)
196![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ switch (symbol)
197![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ 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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ switch (Judge(sub_result-1))
207![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ 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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ if (flag[k]==0)
222
c[k]=a[i];
223
else
224![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ if (a[i]>'0')
225
c[k]=a[i]-1;
226
else
227![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ 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![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{ 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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ k=2*MAX-(b_length-j);
246
for (i=a_length-1,p=k; i>=0; i--,p--)
247
if (c[p]==MARK)
248![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ 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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ 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![](/Images/OutliningIndicators/InBlock.gif)
266
}
267
//The Division function,divise one operand from another operand, and return the result
268
char* Division(char a[],char b[])
269![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{ 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![](/Images/OutliningIndicators/InBlock.gif)
277![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ 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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ 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![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{ int i,j;
294
char c[MAX];
295
for (i=0,j=0; i<a_l; i++)
296
if ( a[i]!=MARK)
297![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ 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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ 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![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{ char c[MAX],d[MAX];
323![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ sub_result=a[i]-b[j];
331
symbol=Judge(sub_result);
332
if (i>=1 && j>=0)
333![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ if (flag[k]==0)
334![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ if (a[i]>=b[j])
335
c[k]=sub_result+'0';
336
else
337![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ c[k]=sub_result+10+'0';
338
flag[k-1]=1;
339
}
340
}
341
else
342![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ if (a[i]-b[j]>=1)
343
c[k]=sub_result-1+'0';
344
else
345![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ c[k]=sub_result+9+'0';
346
flag[k-1]=1;
347
}
348
}
349
}
350
else
351
if (i==0 && j<0)
352![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
353
if (flag[k]==0)
354
c[k]=a[i];
355
else
356![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ if (a[i]==1)
357
;
358
else
359
c[k]=a[i]-1;
360
}
361
}
362
else
363![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ if ((i==0) && (j==0))
364![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ if (flag[k]==0)
365![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ switch (symbol)
366![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ 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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ switch (Judge(sub_result-1))
376![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ 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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ return 0;
388
}
389
else
390
if ((i>0) && (j<0))
391![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ if (flag[k]==0)
392
c[k]=a[i];
393
else
394![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ if (a[i]>'0')
395
c[k]=a[i]-1;
396
else
397![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ 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![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{ 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![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{ 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![](/Images/OutliningIndicators/None.gif)
429
//The Value function , receiver a digit_char, and return the number
430
int Value(char c)
431![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{ if (isdigit(c))
432
return (c-'0');
433
else return 0;
434
}
435
//The Data_Process function,
436
int Data_Process(char s[])
437![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{ 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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ s[p-1]+=(s[p]-'0')/10;
444
s[p]=(s[p]-'0')%10+'0';
445
}
446
else
447![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ 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![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{ 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![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{ 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![](/Images/OutliningIndicators/None.gif)
posted on 2005-12-04 22:33
乖狗狗 阅读(6710)
评论(11) 编辑 收藏 引用