长整数的基本操作

news/2024/7/8 7:08:34
//  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;
}






http://www.niftyadmin.cn/n/3649025.html

相关文章

android SQL 查询操作语句大全

基础 创建数据库 创建之前判断该数据库是否存在 if exists &#xff08;select * from sysdatabases where namedatabaseName&#xff09; drop database databaseName go Create DATABASE database-name 删除数据库 drop database dbname 备份sql server --- 创建 备份数据的 …

andfix热修复

andfix的基本介绍 https://github.com/alibaba/AndFixAndFix优劣 1.原理简单&#xff0c;集成简单&#xff0c;使用简单&#xff0c;即使生效 2.只能修复方法及别的bug,极大的限制了使用场景 AndFix执行流程及核心原理 1.添加依赖&#xff1a;compile com.alipay.euler:andf…

debian10 dns_如何在Debian 10上使用OctoDNS部署和管理DNS

debian10 dnsThe author selected the Electronic Frontier Foundation to receive a donation as part of the Write for DOnations program. 作者选择了电子前沿基金会来接受捐款&#xff0c;这是Write for DOnations计划的一部分。 介绍 (Introduction) OctoDNS is an infr…

《管理的实践》读书笔记(2)

彼得-德鲁克&#xff1a;“我也是第一位认清&#xff0c;或至少是首先指出&#xff0c;有效的管理必须通过目标管理与自我控制的人。”朝晖&#xff1a;“在软件的世界中&#xff0c;一个系统的实现是由多个子系统&#xff0c;到模块&#xff0c;到功能函数等不同的级别而组成的…

GradientDrawable代替Drawable 和 StateListDrawable代替selector工具类

工具类代码&#xff1a; public class DrawUtils {//GradientDrawable代替Drawablepublic static Drawable getDrawable(int rgb, float radius) {GradientDrawable gradientDrawable new GradientDrawable();//gradientDrawable.setColor(rgb);//设置填充颜色gradientDrawabl…

使用ngUpgrade将AngularJS服务迁移到Angular

In our last guide, we covered how to install everything we need to get started with upgrading from AngularJS to Angular. We also covered how to rewrite and downgrade components. 在上一指南中 &#xff0c;我们介绍了如何安装从AngularJS升级到Angular所需的一切。…

数据绑定技术DataBinding

数据绑定技术使用一 第一步添加配置 项目下的build.gradle下的android中defaultConfig下方添加代码: dataBinding{enabled true }第二步添加数据常量并设置set和get方法,因为偷懒就不写了 public class User { private String name; private String nickName; private Strin…

如何在Ubuntu 18.04上使用OpenLiteSpeed安装WordPress

介绍 (Introduction) WordPress is an open-source content management system (CMS). The most popular CMS in the world, WordPress allows you to set up blogs and websites on top of a MySQL database backend, using PHP to execute scripts and process dynamic conte…