这个程序应该是我在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
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