// 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;
}


Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=935663