大整数的简单运算

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 100 /*一个大整数用个字节保存,最多表示100位大整数*/
/*大整数的定义*/
typedef struct BigNum /*大整数字符结构*/
{
char data[SIZE]; /*空间为SIZE个字节*/
}BigNum;
typedef struct BigInt/*大整数整数数组结构*/
{
int data[SIZE+1];
}BigInt;
/*基本函数的声明部分如下:*/
void input_BigNum(BigNum &BigNum_a);/*大整数的输入函数*/
void Printf_BigNum(BigNum BigNum_a);/*输出大整数的函数*/
BigNum abs_BigNum(BigNum BigNum_a);/*求大整数BigNum_a绝对值函数*/
int Length_BigNum(BigNum BigNum_a);/*求大整数BigNum_a的实际位数即为长度length*/
BigNum add_BigNum(BigNum &BigNum_a,BigNum &BigNum_b);/*求大整数BigNum_a,BigNum_b的和值*/
BigNum sub_BigNum(BigNum &BigNum_a,BigNum &BigNum_b);/*求大整数BigNum_a,BigNum_b的差值*/
BigNum negate_BigNum(BigNum BigNum_a);/*求大整数BigNum_a的负数*/
BigNum inc_BigNum(BigNum BigNum_a);/*求大整数BigNum_a的自增运算*/
BigNum dec_BigNum(BigNum BigNum_a);/*求大整数BigNum_a的自减运算*/
char toChar(int a);/*数字转数字字符*/
int toInt(char a);/*数字字符转数字*/
void clear_BigNum(BigNum &BigNum_a);/*清空函数*/
/*各个函数实现部分*/
void input_BigNum(BigNum &BigNum_a)
{/*大整数的输入函数*/
printf("Enter BigNums and end of Enter botton!\n");
getchar();
gets(BigNum_a.data);
}
void Printf_BigNum(BigNum BigNum_a)
{/*输入大整数的函数*/
int i=1;
printf("%c",BigNum_a.data[0]);
if(BigNum_a.data[0]=='-')
while(BigNum_a.data[i]!='\0'&&i<Length_BigNum(BigNum_a)+1)
{
printf("%c",BigNum_a.data[i]);
i++;
}
else
if(BigNum_a.data[0]!='-')
while(BigNum_a.data[i]!='\0'&&i<Length_BigNum(BigNum_a))
{
printf("%c",BigNum_a.data[i]);
i++;
}
printf("\n");
}
BigNum abs_BigNum(BigNum BigNum_a)
{/*求大整数BigNum_a绝对值函数*/
int i;
BigNum BigNum_b;
if(BigNum_a.data[0]=='-')
for(i=0;i<SIZE;i++)
BigNum_b.data[i]=BigNum_a.data[i+1];
else
for(i=0;i<SIZE;i++)
BigNum_b.data[i]=BigNum_a.data[i];
return BigNum_b;
}
int Length_BigNum(BigNum BigNum_a)
{/*求大整数BigNum_a的实际位数即为长度length不包括符号位*/
int i=0;
char *B;
B=BigNum_a.data;
if(*B=='-')
{
i=0;
B++;
}
while(*B!='\0')
{
i++;
B++;
}
return i;
}
BigNum add_BigNum(BigNum &BigNum_a,BigNum &BigNum_b)
{/*求大整数BigNum_a,BigNum_b的和值*/
int i,La,Lb,Lc;
int *pa_last,*pb_last,*pc_last;
BigInt BigInt_a,BigInt_b,BigInt_c;
BigNum BigNum_c;
for(i=0;i<SIZE;i++)
{/*初始化数组不易输出错误的信息*/
BigNum_c.data[i]='\0';
BigInt_a.data[i]=0;
BigInt_b.data[i]=0;
BigInt_c.data[i]=0;
}
La=strlen(BigNum_a.data);
Lb=strlen(BigNum_b.data);
if(BigNum_a.data[0]!='-'&&BigNum_b.data[0]!='-')
{/*BigNum_a,BigNum_b都为大正整数情况*/
Lc=La>Lb?(La+1):(Lb+1);
for(i=0;i<La;i++) BigInt_a.data[i]=toInt(BigNum_a.data[i]);/*将BigNum_a,BigNum_b转化成BigInt_a,BigInt_b进行相加运算*/
for(i=0;i<Lb;i++) BigInt_b.data[i]=toInt(BigNum_b.data[i]);
pa_last=BigInt_a.data+La-1;/*三指针初始化*/
pb_last=BigInt_b.data+Lb-1;
pc_last=BigInt_c.data+Lc-1;
while(pa_last>=BigInt_a.data&&pb_last>=BigInt_b.data&&pc_last>=BigInt_c.data)
{
*pc_last+=*pa_last+*pb_last;
if(*pc_last>=10)
{/*进位代码*/
*pc_last-=10;
pc_last--;
(*pc_last)++;
pc_last++;
}
pa_last--;
pb_last--;
pc_last--;
}
while(pa_last>=BigInt_a.data)
{/*La>Lb*/
*pc_last+=*pa_last;
pa_last--;
pc_last--;
}
while(pb_last>=BigInt_b.data)
{/*La<Lb*/
*pc_last+=*pb_last;
pb_last--;
pc_last--;
}
for(i=0;i<Lc;i++) BigNum_c.data[i]=toChar(BigInt_c.data[i]);/*将BigInt_c转化成BigNum_c*/
}
else
if(BigNum_a.data[1]=='-'&&BigNum_b.data[1]=='-')
{/*BigNum_a,BigNum_b都为大负整数情况*/
printf("akdhkajhfdkj");
La=Length_BigNum(BigNum_a);
Lb=Length_BigNum(BigNum_b);
Lc=La>Lb?(La+1):(Lb+1);
for(i=2;i<=La;i++) BigInt_a.data[i]=toInt(BigNum_a.data[i]);/*将BigNum_a,BigNum_b转化成BigInt_a,BigInt_b进行相加运算*/
for(i=2;i<=Lb;i++) BigInt_b.data[i]=toInt(BigNum_b.data[i]);
pa_last=BigInt_a.data+La;/*三指针初始化*/
pb_last=BigInt_b.data+Lb;
pc_last=BigInt_c.data+Lc;
while(pa_last>BigInt_a.data+1&&pb_last>BigInt_b.data+1&&pc_last>BigInt_c.data+1)
{
*pc_last+=*pa_last+*pb_last;
if(*pc_last>=10)
{
*pc_last-=10;
pc_last--;
(*pc_last)++;
pc_last++;
}
pa_last--;
pb_last--;
pc_last--;
}
while(pa_last>BigInt_a.data)
{/*La>Lb*/
*pc_last+=*pa_last;
if(*pc_last>9)
{
*pc_last-=10;
pc_last--;
(*pc_last)++;
pc_last++;
}
pa_last--;
pc_last--;
}
while(pb_last>BigInt_b.data)
{/*La<Lb*/
*pc_last+=*pb_last;
if(*pc_last>9)
{
*pc_last-=10;
pc_last--;
(*pc_last)++;
pc_last++;
}
pb_last--;
pc_last--;
}
BigNum_c.data[1]='-';
for(i=0;i<Lc;i++)
BigNum_c.data[i]=toChar(BigInt_c.data[i]);/*将BigInt_c转化成BigNum_c*/
}
return BigNum_c;
}
BigNum sub_BigNum(BigNum &BigNum_a,BigNum &BigNum_b)
{/*求大整数BigNum_a-BigNum_b的差值*/
int i=0,La=0,Lb=0,Lc=0;
int *pa_last=NULL,*pb_last=NULL,*pc_last=NULL;
BigInt BigInt_a,BigInt_b,BigInt_c;
BigNum BigNum_c;
for(i=0;i<SIZE;i++)
{/*初始化数组不易输出错误的信息*/
BigNum_c.data[i]='\0';
BigInt_a.data[i]=0;
BigInt_b.data[i]=0;
BigInt_c.data[i]=0;
}
if(BigNum_a.data[1]!='-' && BigNum_b.data[1]=='-')
{/*(+BigNum_a)-(-BigNum_b)*/
Lb=strlen(BigNum_b.data);
printf("\tThe length %d of the sub_BigNUm_b ",Lb-1);
for(i=2;i<Lb;i++)
{
BigNum_b.data[i-1]=BigNum_b.data[i];
}
BigNum_c=add_BigNum(BigNum_a,BigNum_b);
}
if(BigNum_a.data[1]=='-' && BigNum_b.data[1]!='-')
{/*(-BigNum_a)-(+BigNum_b)*/
Lb=strlen(BigNum_b.data);
BigNum_b.data[0]='-';
BigNum_b.data[Lb+1]='\0';
for(i=0;i<Lb;i++)
{
BigNum_b.data[i+1]=BigNum_b.data[i];
}
BigNum_c=add_BigNum(BigNum_a,BigNum_b);
}
if(BigNum_a.data[0]!='-' && BigNum_b.data[0]!='-')/*two positive number*/
{/*(BigNum_a)-(BigNum_b)*/
La=strlen(BigNum_a.data);
Lb=strlen(BigNum_b.data);
Lc=La>Lb?La:Lb;
for(i=0;i<La;i++) BigInt_a.data[i]=toInt(BigNum_a.data[i]);/*将BigNum_a,BigNum_b转化成BigInt_a,BigInt_b进行相加运算*/
for(i=0;i<Lb;i++) BigInt_b.data[i]=toInt(BigNum_b.data[i]);
pa_last=BigInt_a.data+La-1;/*三指针初始化*/
pb_last=BigInt_b.data+Lb-1;
pc_last=BigInt_c.data+Lc-1;
while(pa_last>=BigInt_a.data && pb_last>=BigInt_b.data && pc_last>=BigInt_c.data)
{
*pc_last+=(*pa_last)-(*pb_last);
if(*pc_last<0)
{
*pc_last+=10;
pc_last--;
(*pc_last)--;
pc_last++;
}
pc_last--;
pc_last--;
pc_last--;
}
while(pa_last>=BigInt_a.data)
{
*pc_last=*pa_last-*pc_last;
pc_last--;
pc_last--;
}
while(pa_last>=BigInt_a.data)
{
*pc_last=*pa_last-*pc_last;
pc_last--;
pc_last--;
}
for(i=0;i<Lc;i++)
BigNum_c.data[i]=toChar(BigInt_c.data[i]);
}
if(BigNum_a.data[0]=='-' && BigNum_b.data[0]=='-')/*two negative number*/
{/*(-BigNum_a)-(-BigNum_b)*/
La=strlen(BigNum_a.data);
Lb=strlen(BigNum_b.data);
for(i=1;i<Lb;i++)
{
BigNum_b.data[i-1]=BigNum_b.data[i];
}
BigNum_b.data[Lb-1]='\0';
for(i=1;i<La;i++)
{
BigNum_a.data[i-1]=BigNum_a.data[i];
}
BigNum_a.data[La-1]='\0';
BigNum_c=add_BigNum(BigNum_b,BigNum_a);
}
return BigNum_c;
}
BigNum negate_BigNum(BigNum BigNum_a)
{/*求大整数BigNum_a的负数*/
int i=0;
BigNum BigNum_b;
for(i=0;i<SIZE;i++)
BigNum_b.data[i]='\0';
if(BigNum_a.data[0]=='-')
for(i=1;i<=Length_BigNum(BigNum_a);i++)
BigNum_b.data[i-1]=BigNum_a.data[i];
else
{
BigNum_b.data[0]='-';
for(i=0;i<Length_BigNum(BigNum_a);i++)
BigNum_b.data[i+1]=BigNum_a.data[i];
}
return BigNum_b;
}
BigNum inc_BigNum(BigNum BigNum_a)
{/*求大整数BigNum_a的自增运算*/
int i;
BigNum BigNum_b;
char *BigNum_last;
for(i=0;i<SIZE;i++)
BigNum_b.data[i]='\0';
if(BigNum_a.data[0]!='-')
{/*正大整数++*/
BigNum_last=BigNum_a.data+Length_BigNum(BigNum_a)-1;
while(*BigNum_last=='9')
{
*BigNum_last='0';
BigNum_last--;
}
if(BigNum_last<BigNum_a.data)
{
/*printf("\tThe BigNum are all 9!\n");*/
BigNum_a.data[0]='1';
BigNum_b.data[Length_BigNum(BigNum_a)]='0';
}
else
(*BigNum_last)++;/*最左边的9的数字字符加1*/
for(i=0;i<Length_BigNum(BigNum_a);i++)
BigNum_b.data[i]=BigNum_a.data[i];
}
else
if(BigNum_a.data[0]=='-')
{/*负大整数++*/
BigNum_last=BigNum_a.data+Length_BigNum(BigNum_a);
while(*BigNum_last=='0')
{
*BigNum_last='9';
BigNum_last--;
}
if(BigNum_last<=BigNum_a.data)
{/*全部为0除了第一个'-'号*/
for(i=0;i<Length_BigNum(BigNum_a);i++)
{
BigNum_a.data[i]='0';
}
BigNum_a.data[Length_BigNum(BigNum_a)]='1';
}
else
(*BigNum_last)--;
for(i=0;i<=Length_BigNum(BigNum_a);i++)
BigNum_b.data[i]=BigNum_a.data[i];
}
//free(BigNum_last);
return BigNum_b;
}
BigNum dec_BigNum(BigNum BigNum_a)
{/*求大整数BigNum_a的自减运算*/
int i;
char *BigNum_last;
BigNum BigNum_b;
for(i=0;i<SIZE;i++)
BigNum_b.data[i]='\0';
if(BigNum_a.data[0]!='-')
{/*正大整数--*/
BigNum_last=BigNum_a.data+Length_BigNum(BigNum_a)-1;
while(*BigNum_last=='0')
{
*BigNum_last='9';
BigNum_last--;
}
if(BigNum_last>=BigNum_a.data)
{/*大整数不全为'0'*/
(*BigNum_last)--;
}
else
if(BigNum_last<BigNum_a.data)
{/*大整数全为'0'*/
BigNum_a.data[0]='-';
for(i=1;i<Length_BigNum(BigNum_a);i++)
{
BigNum_a.data[i]='0';
}
BigNum_a.data[Length_BigNum(BigNum_a)]='1';
}
for(i=0;i<=Length_BigNum(BigNum_a);i++)
BigNum_b.data[i]=BigNum_a.data[i];
}
if(BigNum_a.data[0]=='-')
{/*负数--*/
BigNum_last=BigNum_a.data+Length_BigNum(BigNum_a);
while(*BigNum_last=='9')
{
*BigNum_last='0';
BigNum_last--;
}
if(BigNum_last>BigNum_a.data)
{/*不全为'9'*/
(*BigNum_last)++;
}
else
if(BigNum_last<=BigNum_a.data)
{/*如:'-99999999'*/
BigNum_a.data[1]='1';
BigNum_last=BigNum_a.data+Length_BigNum(BigNum_a)+1;
*BigNum_last='0';
BigNum_last++;
*BigNum_last='\0';
}
for(i=0;i<=Length_BigNum(BigNum_a);i++)
BigNum_b.data[i]=BigNum_a.data[i];
}
return BigNum_b;
}

char toChar(int a)
{/*数字转数字字符*/
return (char)(a+48);
}
int toInt(char a)
{/*数字字符转数字*/
return (int)(a-48);
}
int menu()
{
int sign=10;
printf("\t************************\n");
printf("\t***1.Input BigNum!****\n");
printf("\t***2.Printf BigNum!****\n");
printf("\t***3.Abs of BigNum!****\n");
printf("\t***4.Length BigNum!****\n");
printf("\t***5.Negate BigNum!****\n");
printf("\t***6.Inc the BigNum!****\n");
printf("\t***7.Dec the BigNum!****\n");
printf("\t***8.Add two BigNums!***\n");
printf("\t***9.Sub two BigNums!***\n");
printf("\t***0.Exit the system!***\n");
printf("\t************************\n");
printf("Input the number in(1-9)...\n");
scanf("%d",&sign);
return sign;
}
void clear_BigNum(BigNum &BigNum_a)
{
int i=0,La;
La=strlen(BigNum_a.data);
for(i=0;i<La;i++)
{
BigNum_a.data[i]='\0';
}
}

int main()
{
int i=SIZE;
BigNum BigNum_a,BigNum_b,BigNum_c;
Loop:i=menu();
switch(i)
{
case 1:
{
input_BigNum(BigNum_a);
goto Loop;
}
case 2:
{
Printf_BigNum(BigNum_a);
goto Loop;
}
case 3:
{
BigNum_b=abs_BigNum(BigNum_a);
Printf_BigNum(BigNum_b);
goto Loop;
}
case 4:
{
clear_BigNum(BigNum_a);
input_BigNum(BigNum_a);
printf("Length is:\t%d\n",Length_BigNum(BigNum_a));
goto Loop;
}
case 5:
{
BigNum_b=negate_BigNum(BigNum_a);
Printf_BigNum(BigNum_b);
goto Loop;
}
case 6:
{
BigNum_b=inc_BigNum(BigNum_a);
Printf_BigNum(BigNum_b);
goto Loop;
}
case 7:
{
BigNum_b=dec_BigNum(BigNum_a);
Printf_BigNum(BigNum_b);
goto Loop;
}
case 8:
{
printf("Input zero before input BigNum!\n");
clear_BigNum(BigNum_a);
clear_BigNum(BigNum_b);
input_BigNum(BigNum_a);
input_BigNum(BigNum_b);
BigNum_c=add_BigNum(BigNum_a,BigNum_b);
Printf_BigNum(BigNum_c);
goto Loop;
}
case 9:
{
printf("Input zero before input BigNum!\n");
clear_BigNum(BigNum_a);
clear_BigNum(BigNum_b);
clear_BigNum(BigNum_b);
input_BigNum(BigNum_a);
input_BigNum(BigNum_b);
BigNum_c=sub_BigNum(BigNum_a,BigNum_b);
Printf_BigNum(BigNum_c);
goto Loop;
}
case 0:exit(1);
}
return 0;
}

你可能感兴趣的:(c,绝对值,大整数,BigNum)