我有一个高精度加减乘除取余都包含的程序(目前还不能负数或小数)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int compare(char *str_a,char *str_b)
{
int len_a, len_b;
len_a=strlen(str_a);
len_b=strlen(str_b);
if(strcmp(str_a,str_b)==0)
return 0;
if(len_a>len_b)
return 1;
else if(len_a==len_b)
return strcmp(str_a, str_b);
else
return -1;
}
char* ps(char str_a[],char str_b[])
{
int f=0,n,i,k,add=0,len_a=strlen(str_a),len_b=strlen(str_b),num_a[20000]={0},num_b[20000]={0},num_c[20000];
k=len_a>len_b?len_a:len_b;
char st[20000]="";
num_c[0]=0;
n=compare(str_a,str_b);
for(i=0;i<len_a;i++)
num_a[i]=str_a[len_a-i-1]-'0';
for(i=0;i<len_b;i++)
num_b[i]=str_b[len_b-i-1]-'0';
for(i=0;i<k;i++)
{
if(n>=0)
{
if(num_a[i]>=num_b[i])
num_c[i]=num_a[i]-num_b[i];
else
{
num_c[i]=num_a[i]-num_b[i]+10;
num_a[i+1]--;
}
}
else
{
if (num_b[i]>=num_a[i])
num_c[i]=num_b[i]-num_a[i];
else
{
num_c[i]=num_b[i]-num_a[i]+10;
num_b[i+1]--;
}
}
}
if(n<0)
{st[add]='-';add++;}
for(i=k-1;i>=0;i--)
{
if (num_c[i])
f=1;
if (f||i==0)
{st[add]=num_c[i]+'0';add++;}
}
for(i=0;i<k;i++)
{
num_a[i] = 0;
num_b[i] = 0;
}
char* p=st;
return p;
}
char* add(char s[],char s1[])
{
int a[20000+1]={0},i,j,len;
len=strlen(s);
for(i=len-1;i>=0;i--)
a[20000-len+i+1]=s[i]-48;
j=1;
int a1[20000+1]={0},i1,j1,len1;
len1=strlen(s1);
for(i1=len1-1;i1>=0;i1--)
a1[20000-len1+i1+1]=s1[i1]-48;
j1=1;
int ca=0;
for(i=20000;i>=0;i--)
{
a[i]=a[i]+a1[i]+ca;
ca=a[i]/10;
a[i]%=10;
}
char mm[20000+1]="";
int f=0;
while(a[j]==0)j++;
for(i=j;i<=20000;i++){mm[f]=a[i]+'0';f++;}
char* p=mm;
return p;
}
char* ts(char str_a[],char str_b[])
{
char str_c[2*20000];
int num_a[20000]={0}, num_b[20000]={0}, num_c[2*20000];
int i,j,k,d,len_a,len_b;
for (i=0; i<2*20000; i++)
num_c[i]=0;
len_a=strlen(str_a);
i=len_a-1;
k=0;
while(i>=0)
num_a[k++]=str_a[i--]-'0';
len_b=strlen(str_b);
i=len_b-1;
k=0;
while (i>=0)
num_b[k++]=str_b[i--]-'0';
for(i=0;i<len_a;i++)
for(j=0;j<len_b;j++)
num_c[i+j]+=num_a[i]*num_b[j];
k=39999;
while (k>=0&&num_c[k]==0)k--;
i=0;d=0;
while(i<=k)
{
num_c[i] += d;
d = num_c[i]/10;
num_c[i]%=10;
i++;
}
while(d>0)
{
num_c[i]=d%10;
d=d/10;
i++;
}
k=i;
for(i=k-1;i>=0;i--)
str_c[k-i-1]=num_c[i]+'0';
str_c[k]='\0';
char* p=str_c;
return p;
}
char* pps(char str1[],char str2[])
{
if(!strcmp(str2,"0"))
{
char *p="e";
return p;
}
int i;
char doc[20000],str[20000],strr[20000],cstr1[20000],aoc[20000],coc[20000];
strcpy(str,str1);
strcpy(strr,str2);
coc[0]='0';
strcpy(doc,ps(str,strr));
while(doc[0]!='-')
{
for(i=0;i<20000;i++)str[i]='\0';
strcpy(coc,add(coc,"1"));
strcpy(str,doc);
strcpy(doc,ps(str,strr));
}
char *p=coc;
return p;
}
char* mod(char str1[],char str2[])
{
if(!strcmp(str2,"0"))
{
char *p="e";
return p;
}
int i;
char doc[20000],str[20000],strr[20000],cstr1[20000],aoc[20000];
strcpy(str,str1);
strcpy(strr,str2);
strcpy(doc,ps(str,strr));
while(doc[0]!='-')
{
for(i=0;i<20000;i++)str[i]='\0';
strcpy(str,doc);
strcpy(doc,ps(str,strr));
}
for(i=1;i<20000;i++)doc[i-1]=doc[i];
doc[19999]=0;
char *p=ps(str2,doc);
return p;
}
char* maths(char str1[],char sign,char str2[])
{
int i,j=1;
for(i=0;i<strlen(str1);i++)
if((str1[i]<'0'||str1[i]>'9'))j=0;
for(i=0;i<strlen(str2);i++)
if((str2[i]<'0'||str2[i]>'9'))j=0;
if(sign=='+'&&j==1)return add(str1,str2);
else if(sign=='-'&&j==1)return ps(str1,str2);
else if(sign=='*'&&j==1)return ts(str1,str2);
else if(sign=='/'&&j==1)return pps(str1,str2);
else if(sign=='%'&&j==1)return mod(str1,str2);
else {system("cls");printf("maths()只能做两个字符串型非负整数之间的加(+)、减(-)、乘(*)、除(/)、取余(%%)运算");return 0;}
}
main()
{
puts(maths("111",'%',"11"));
puts(maths("111",'/',"11"));
puts(maths("11",'-',"111"));
puts(maths("111",'-',"11"));
puts(maths("111",'+',"11"));
puts(maths("111",'*',"11"));
return 0;
}