长整数的基本操作

// LongInt.cpp:Definestheentrypointfortheconsoleapplication.
//

#include
" stdafx.h "
#include
" string.h "
#define MAXLEN50
/**/ /*
functionInputLongInt()长整数输入函数
*/

int InputLongInt( int * a, char * para)
... {
intlen,i;
charnumber[MAXLEN];
printf(
"请输入[%s]长整数: ",para);
scanf(
"%s",number);
len
=strlen(number);
for(i=len;i>=1;i--)
a[i]
=number[len-i]-'0';
a[
0]=len;
return(a[0]);
}

/**/ /*
functionOutputLongInt()长整数输出函数
*/

void OutputLongInt( int * a, char * para)
... {
inti;
printf(
"输出的[%s]长整数: ",para);
for(i=a[0];i>=1;i--)
printf(
"%d",a[i]);
printf(
" ");
}

/**/ /*
functionFormatLongInt()长整数规范化函数
*/

int FormatLongInt( int * a)
... {
intp;
for(p=1;p<a[0]||a[p]>=10;p++)
...{
if(p>=a[0])
a[p
++]=0;
a[p
++]+=a[p]/10;
a[p]
=a[p]%10;
}

if(p>a[0])
a[
0]=p;
return(a[0]);
}

/**/ /*
functionLongIntAddLongInt()长整数加长整数函数
相加的和放在数组A中
*/

void LongIntAddLongInt( int * a, int * b)
... {
inti;
while(a[0]<b[0])
a[
++a[0]]=0;
for(i=1;i<=b[0];i++)
a[i]
+=b[i];
FormatLongInt(a);
}

/**/ /*
functionLongIntDivInt()长整数除普通整数函数
除得的商放在数组A中,余数放在返回值中
*/

int LongIntDivInt( int * a, int divisor)
... {
intp,//存储相除后的余数下标
k;//存储数字个数(长整数有效数字个数)
k=p=a[0];
a[
0]=0;
while(p>0)
...{
a[p
-1]+=a[p]%divisor*10;
a[p]
=a[p]/divisor;
if(a[k]==0)
k
--;
p
--;
}

p
=a[0]/10;//保存余数
a[0]=k;//回写有效数字个数
FormatLongInt(a);
return(p);
}

/**/ /*
functionLongIntDivInt()长整数转换成二进制数函数
转换的二进制数存储数组B中
*/

void LongIntToBin( int * a, int * b)
... {
intp;
b[
0]=0;
while(a[0]>0)
...{
b[
0]++;
b[b[
0]]=a[1]%2;
p
=a[0];
while(p>0)
...{
if((a[p]%2)&&(p>1))
a[p
-1]+=10;
a[p]
/=2;
if(a[a[0]]==0)
a[
0]--;
p
--;
}

}

}


int main( int argc, char * argv[])
... {
inta[MAXLEN],b[MAXLEN];
intlen,residue;
/**//*
len=InputLongInt(a);
printf("源长整数的长度是:%d ",len);
str="源";
OutputLongInt(a,str);
len=FormatLongInt(a);
printf("规范化后长整数的长度是:%d ",len);
str="规范化后";
OutputLongInt(a,str);
residue=LongIntDivInt(a,11);
str="除以普通整数11后";
OutputLongInt(a,str);
printf("长整数除以普通整数11后的余数是:%d ",residue);
*/

len
=InputLongInt(a,"第一个加数");
//len=InputLongInt(b,"第二个加数");
//LongIntAddLongInt(a,b);
//OutputLongInt(a,"两数相加的和");
LongIntToBin(a,b);
OutputLongInt(b,
"转换后的二进制数");
printf(
" 应用程序正在运行! ");
return0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值