当前位置:网站首页>Large numbers (C language)

Large numbers (C language)

2022-06-26 04:38:00 bu_ xiang_ tutou

Add big numbers

#include<stdio.h>
#include<string.h>
int main()
{// Using string calculation A+B Because A,B It's too long  
    char a[501],b[501],c[509]="\0";
    gets(a);
    gets(b);
    int x,y,d=0;
    for( x=strlen(a)-1,y=strlen(b)-1; x>=0&&y>=0; x--,y--)
        c[d++]=a[x]+b[y]-48;//-48( because '0' Of ASCII Code is 48), Minus one 48 Because c It is also a character expression    '7'='0'-48+'7'
		                   // c The first digit of the array is the single digit ... 
    if(x>=0)// At the end of the cycle ,y<0 了 , however x>=0. 
	//b The character length of the array does not have a Long , namely a It's five digits ,b It's a four digit situation  
    {
        for(; x>=0; x--)
            c[d++]=a[x];
    }
    if(y>=0)// ditto . 
    {
        for(; y>=0; y--)
            c[d++]=b[y];
    }
    for(x=0;x<strlen(c);x++)
    if(c[x]>'9')// full 10 Into the 1 
    {
        c[x]-=10;
                if(c[x+1]==0)// Judge x+1 Is there an initial value on the bit  
                    c[x+1]='0';
                    c[x+1]+=1;// Into the 1 
    }
   for(x=strlen(c)-1;x>=0;x--)// Output in reverse order , Output from the highest bit to the first digit . 
    printf("%c",c[x]);
    return 0;
}
#include<stdlib.h>
#include<stdio.h>
#include<string.h> 
char s1[100],s2[100];
int a[100],b[100];
int max(int a,int b)
{
	if(a>b)
	return a;
	else
	return b;
}
int main()
{
	scanf("%s",s1);
	scanf("%s",s2);
	int n,i=0;
	for(n=strlen(s1)-1;n>=0;n--)
	{
		a[i++]=s1[n]-'0';
	}
	i=0;
	for(n=strlen(s2)-1;n>=0;n--)
	{
		b[i++]=s2[n]-'0';
	}
	for(n=0;n<max(strlen(s1),strlen(s2));n++)
	{
		a[n]+=b[n];
	} 
	for(n=0;n<=100;n++)
	{
	if(a[n]>9)
	{
		a[n]-=10;
		a[n+1]+=1;
	}
	}
	for(n=100;a[n]==0&&n>=0;n--);
	if(n>=0)
	for(;n>=0;n--)
	printf("%d",a[n]);
    return 0;
}

Big number subtraction

#include"stdio.h"
#include"string.h"
char s1[405],s2[405];
int s[405];
void jian(char a[],char b[])//a String ratio b Long , Or both  
{
	int n,m,d=0;
	for(n=strlen(a),m=strlen(b);n>=0&&m>=0;n--,m--)
	{
		s[d++]=a[n]-'0'-(b[m]-'0');
	}
	if(n>=0)
	{
		for(;n>=0;n--)
		s[d++]=a[n]-'0';
	}
	for(n=0;n<=405;n++)
	{
		if(s[n]<0)
		{
			s[n]+=10;
			s[n+1]-=1;	
		}	
	} 
	for(n=405;n>=0&&s[n]==0;n--);
	if(n>=0)
	for(;n>0;n--)
	printf("%d",s[n]);
}
int main()
{
	gets(s1),gets(s2);
	if(strlen(s1)<strlen(s2)||(strlen(s1)==strlen(s2)&&strcmp(s1,s2)<0))// character string s2>s1 
	{
		printf("-");
		jian(s2,s1);
	}
	else if(strlen(s1)==strlen(s2)&&strcmp(s1,s2)==0)
	printf("0");
	else
	jian(s1,s2);
	return 0;
}

Multiplication of large numbers

#include <stdio.h>
#include<string.h>
#include<math.h>
int main() {
	char a[1000],b[1000];
	scanf("%s",a);
	scanf("%s",b);
	int len1,len2,n,m,t;
	int s1[1000],s2[1000],c[10000];
	len1=strlen(a),len2=strlen(b);
	memset(s1,0,sizeof(s1));
	memset(s2,0,sizeof(s2));
	memset(c,0,sizeof(c));// hold  s1,s2,c The initial value of the array in 0 
	for(m=0,n=len1-1; n>=0; n--)
		s1[m++]=a[n]-'0';// Invert the array , And replace the characters with numbers , Easy to calculate 
	for(t=0,n=len2-1; n>=0; n--)
		s2[t++]=b[n]-'0';
	for(m=0; m<=len1-1; m++) {
		for(t=0; t<=len2-1; t++)
			c[m+t]+=s1[m]*s2[t];// Multiplication 
	}
	for(m=0; m<=len1+len2; m++) {// full 10 Into the 1
		if(c[m]>9) {
			c[m+1]+=c[m]/10;
			c[m]=c[m]%10;
		}
	}
	for(m=len1+len2; (m>=0)&&(c[m]==0); m--); // hold m>=0 Put it in the front , It can avoid crossing the boundary ,(m=-1), First judge m>=0 
	// Skip high 0, Finding the first one doesn't mean 0 The location of , Pay attention to the semicolon . 
		if(m>=0) {
			for(; m>=0; m--)
				printf("%d",c[m]);}
		else
			printf("0");
	return 0;
}

原网站

版权声明
本文为[bu_ xiang_ tutou]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180512196300.html