长整数的基本操作
来源:互联网 发布:淘宝怎么看行业大盘 编辑:程序博客网 时间:2024/05/01 17:28
// LongInt.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "string.h"
#define MAXLEN 50
/*
function InputLongInt()长整数输入函数
*/
int InputLongInt(int *a,char *para)
{
int len,i;
char number[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]);
}
/*
function OutputLongInt()长整数输出函数
*/
void OutputLongInt(int *a,char *para)
{
int i;
printf("输出的[%s]长整数:",para);
for(i=a[0];i>=1;i--)
printf("%d",a[i]);
printf(" ");
}
/*
function FormatLongInt()长整数规范化函数
*/
int FormatLongInt(int *a)
{
int p;
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]);
}
/*
function LongIntAddLongInt()长整数加长整数函数
相加的和放在数组A中
*/
void LongIntAddLongInt(int *a,int *b)
{
int i;
while(a[0] < b[0])
a[++a[0]] = 0;
for(i=1;i<=b[0];i++)
a[i] += b[i];
FormatLongInt(a);
}
/*
function LongIntDivInt()长整数除普通整数函数
除得的商放在数组A中,余数放在返回值中
*/
int LongIntDivInt(int *a,int divisor)
{
int p, //存储相除后的余数下标
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);
}
/*
function LongIntDivInt()长整数转换成二进制数函数
转换的二进制数存储数组B中
*/
void LongIntToBin(int *a,int *b)
{
int p;
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[])
{
int a[MAXLEN],b[MAXLEN];
int len,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(" 应用程序正在运行! ");
return 0;
}
//
#include "stdafx.h"
#include "string.h"
#define MAXLEN 50
/*
function InputLongInt()长整数输入函数
*/
int InputLongInt(int *a,char *para)
{
int len,i;
char number[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]);
}
/*
function OutputLongInt()长整数输出函数
*/
void OutputLongInt(int *a,char *para)
{
int i;
printf("输出的[%s]长整数:",para);
for(i=a[0];i>=1;i--)
printf("%d",a[i]);
printf(" ");
}
/*
function FormatLongInt()长整数规范化函数
*/
int FormatLongInt(int *a)
{
int p;
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]);
}
/*
function LongIntAddLongInt()长整数加长整数函数
相加的和放在数组A中
*/
void LongIntAddLongInt(int *a,int *b)
{
int i;
while(a[0] < b[0])
a[++a[0]] = 0;
for(i=1;i<=b[0];i++)
a[i] += b[i];
FormatLongInt(a);
}
/*
function LongIntDivInt()长整数除普通整数函数
除得的商放在数组A中,余数放在返回值中
*/
int LongIntDivInt(int *a,int divisor)
{
int p, //存储相除后的余数下标
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);
}
/*
function LongIntDivInt()长整数转换成二进制数函数
转换的二进制数存储数组B中
*/
void LongIntToBin(int *a,int *b)
{
int p;
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[])
{
int a[MAXLEN],b[MAXLEN];
int len,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(" 应用程序正在运行! ");
return 0;
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=935663
- 长整数的基本操作
- 长整数的基本操作
- 长整数的基本操作
- 长整数的基本操作
- 长整数的基本操作
- 长整数的基本操作
- 长整数的基本操作
- 长整数的基本操作
- 长整数的基本操作
- 长整数的基本操作
- 长整数的基本操作
- 长整数的基本操作
- 长整数的基本操作
- 长整数的基本操作
- 不定长顺序表的基本操作
- 长整数的乘法
- 长整数的加减法
- 不用JAVA基本类库把一字符串转成长整数的基本方法
- Buildix 构建敏捷开发平台
- 微软过桥问题的图论解法
- 程序员如何缓解压力,保持心理健康
- Closures for Java
- 坐南京13路公交车,体验《头文字D》感觉!
- 长整数的基本操作
- 并行版本控制的好帮手--WinMerge
- 并行版本控制的好帮手--WinMerge
- 写给即将步入大学,主修计算机专业的青年们的一封信
- 从一个中学的高考录取名单看中国高校的排名
- 日广告巨头看上博客 电通ADK合资专投博客
- 插花问题的“动态规划法”算法
- Blog分析师颠覆传统IT研究市场
- Google打不开的解决方法和IP地址表