高精度加1
#include <stdio.h>
#include <string.h>
int main()
{
char a[310], b[310];
int c[310], d[310], e[310];
scanf("%s %s", a, b);
int x = strlen(a), y = strlen(b);
for (int i = 0; i < x; i++) {
c[x-1-i] = a[i] - '0';
}
for (int i = 0; i < y; i++) {
d[y-1-i] = b[i] - '0';
}
int l = (x > y) ? x : y;
memset(e, 0, sizeof(e));
for (int i = 0; i < l; i++) {
e[i] += c[i] + d[i];
if(e[i] > 9) e[i] -= 10,e[i+1] = 1;
}
if(e[l]) l++;
for(int i = l-1; i >= 0; i--) printf("%d", e[i]);
printf("\n");
return 0;
}
高精度减法
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 300 + 5;
int main(int argc, char **argv) {
int a[maxn], b[maxn], c[maxn];
char s1[maxn], s2[maxn];
char *ss1 = s1, *ss2 = s2;
memset(a, 0, sizeof(a) );
memset(b, 0, sizeof(b) );
memset(c, 0, sizeof(c) );
scanf("%s%s", s1, s2);
if( strlen(s1)<strlen(s2) || (strlen(s1)==strlen(s2)&&strcmp(s1,s2)<0) ){
ss1 = s2;
ss2 = s1;
printf("-");
}else if( !strcmp(s1,s2) ){
printf("0");
return 0;
}
int len1 = strlen(ss1);
int len2 = strlen(ss2);
for( int i=0; i<len1; i++ )
a[len1-1-i] = ss1[i]-'0';
for( int i=0; i<len2; i++ )
b[len2-1-i] = ss2[i]-'0';
int j;
for( j=0; j<len1||j<len2; j++ ){
c[j] += a[j]-b[j];
if( c[j]<0 ){
c[j] += 10;
c[j+1] --;
}
}
int lenc = j;
for( ; lenc>=0 && !c[lenc]; lenc-- );
for( ; lenc>=0; lenc-- )
printf("%d", c[lenc]);
/*scanf("%s%s", s1, s2);
printf("%d", strcmp(ss1, ss2));*/
return 0;
}
高精度乘法
#include<cstdio>
#include<cstring>
using namespace std;
char a1[55],b1[55];
int a[55],b[55],c[125];
int main(){
scanf("%s%s",a1,b1);
int lena=strlen(a1);
int lenb=strlen(b1);
for(int i=1;i<=lena;i++)
a[i]=a1[lena-i]-'0';
for(int i=1;i<=lenb;i++)
b[i]=b1[lenb-i]-'0';
for(int i=1;i<=lena;i++){
int x=0;
for(int j=1;j<=lenb;j++){
c[i+j-1]+=a[i]*b[j]+x;
x=c[i+j-1]/10;
c[i+j-1]%=10;
}
c[i+lenb]=x;
}
int lenc=lena+lenb;
while(c[lenc]==0&&lenc>1)
lenc--;
while(lenc)
printf("%d",c[lenc--]);
return 0;
}
高精乘除单精法
#include<stdlib.h>
#include<stdio.h>
char s[501],ch;
int a[501],b,ka,k=0;
void f()
{
int i,c=0;
for(i=0;i<k;i++)
{
a[i]=a[i]*b+c;
if(a[i]>=10)
{
c=a[i];
a[i]=a[i]%10;
c=c/10;
}else c=0;
}
if(c!=0) printf("%d",c);
for(i=k-1;i>=0;i--)
printf("%d",a[i]);
}
void x()
{
int d=0,i,c;
for(i=k-1;i>=0;i--)
{
c=a[i]+d*10;
a[i]=(a[i]+d*10)/b;
d=c%b;
}
while((a[k-1]==0)&&(k>0)) k--;
for(i=k-1;i>=0;i--)
printf("%d",a[i]);
if(d!=0)
printf("---%d",d);
}
int main()
{
int i,temp;
scanf("%s",s);
while((s[k]!='*')&&(s[k]!='/'))
{
a[k]=s[k]-'0';
k++;
}
ch=s[k];
b=s[strlen(s)-1]-'0';
ka=k;//读入
for(i=0;i<(ka>>1);i++)
{
temp=a[i];
a[i]=a[ka-1-i];
a[ka-1-i]=temp;
}
if(ch=='*') f(); else x();
system("pause");
return 0;
}