大数乘法
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN (100)
#define RSTMAX (1000000)
int main(int ac, char **av)
{
char Mp1[MAXLEN] = {0}, Mp2[MAXLEN] = {0};
char temp[MAXLEN + 3] = {0}, rst[RSTMAX] = {0};
int i = 0, j = 0, t = 0, s = 0;
int len1 = 0, len2 = 0;
int bit = 0;
printf("============= Welcome to Mutip Calculate ============\n");
printf("Please enter two number you want to calculate : \n");
scanf("%s%s", Mp1, Mp2);
len1 = strlen(Mp1);
len2 = strlen(Mp2);
for(j = len2 - 1; j >=0; --j){
for(i = len1 - 1, t = len1; i >= 0; --i, --t){
// let two number not two character to multiply
temp[t] = (Mp1[i] - 0x30) * (Mp2[j] - 0x30);
// 0x30 == 48 == '0'
}
// adjust temp's each bit number which more than 9 to between 0 to 9
for(t = len1; t >0; --t){
if(temp[t] > 9){
temp[t-1] += temp[t]/10;
temp[t] %= 10;
}
}
// sum the new result to the original result;
for(s = len1 + len2 - bit, t = len1; t >= 0; --s, --t){
rst[s] += temp[t];
}
// ajust the new result which more than 9
for(s = len1 + len2; s > 0; --s){
if(rst[s] > 9){
rst[s-1] += rst[s]/10;
rst[s] %= 10;
}
}
// bzero the temp array
for(t = len1; t >= 0; --t){
temp[t] = 0;
}
bit++;
}
// in order to narmal output the result as a string not a interge
rst[len1 + len2 + 1] = '\0';
// switch rst's each bit to character save into the result array.
for(s = 0; s <= len1 + len2; ++s){
rst[s] += 0x30;
}
// delete the first zero before output the result.
for(s = 0; s < len1 + len2; ++s){
if(0x30 == rst[0]){
for(t = 0; t <= len1 + len2 - s; ++t){
rst[t] = rst[t+1];
}
}else{
break;
}
}
// output the result;
printf("%s * %s = %s\n", Mp1, Mp2, rst);
printf("========== Bye Bye ==========\n");
return 0;
}
//大数除法
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MaxLen 200
//函数SubStract功能:
//用长度为len1的大整数p1减去长度为len2的大整数p2
// 结果存在p1中,返回值代表结果的长度
//不够减 返回-1 正好够 返回0
int SubStract( int *p1, int *p2, int len1, int len2 )
{
int i;
if( len1 < len2 )
return -1;
if( len1 == len2 )
{ //判断p1 > p2
for( i=len1-1; i>=0; i-- )
{
if( p1[i] > p2[i] ) //若大,则满足条件,可做减法
break;
else if( p1[i] < p2[i] ) //否则返回-1
return -1;
}
}
for( i=0; i<=len1-1; i++ ) //从低位开始做减法
{
p1[i] -= p2[i];
if( p1[i] < 0 ) //若p1<0,则需要借位
{
p1[i] += 10; //借1当10
p1[i+1]–; //高位减1
}
}
for( i=len1-1; i>=0; i-- ) //查找结果的最高位
if( p1[i] ) //最高位第一个不为0
return (i+1); //得到位数并返回
return 0; //两数相等的时候返回0
}
int main()
{
int n, k, i, j; //n:测试数据组数
int len1, len2; //大数位数
int nTimes; //两大数相差位数
int nTemp; //Subtract函数返回值
int num_a[MaxLen]; //被除数
int num_b[MaxLen]; //除数
int num_c[MaxLen]; //商
char str1[MaxLen + 1]; //读入的第一个大数
char str2[MaxLen + 1]; //读入的第二个大数
scanf("%d",&n);
while ( n-->0 )
{
scanf("%s", str1); //以字符串形式读入大数
scanf("%s", str2);
for ( i=0; i<MaxLen; i++ ) //初始化清零操作
{
num_a[i] = 0;
num_b[i] = 0;
num_c[i] = 0;
}
len1 = strlen(str1); //获得大数的位数
len2 = strlen(str2);
for( j=0, i=len1-1; i>=0; j++, i-- )
num_a[j] = str1[i] - '0'; //将字符串转换成对应的整数,颠倒存储
for( j=0, i=len2-1; i>=0; j++, i-- )
num_b[j] = str2[i] - '0';
if( len1 < len2 ) //如果被除数小于除数,结果为0
{
printf("0\n");
continue; //利用continue直接跳出本次循环。 进入下一组测试
}
nTimes = len1 - len2; //相差位数
for ( i=len1-1; i>=0; i-- ) //将除数扩大,使得除数和被除数位数相等
{
if ( i>=nTimes )
num_b[i] = num_b[i-nTimes];
else //低位置0
num_b[i] = 0;
}
len2 = len1;
for( j=0; j<=nTimes; j++ ) //重复调用,同时记录减成功的次数,即为商
{
while((nTemp = SubStract(num_a,num_b + j,len1,len2 - j)) >= 0)
{
len1 = nTemp; //结果长度
num_c[nTimes-j]++;//每成功减一次,将商的相应位加1
}
}
//输出结果
for( i=MaxLen-1; num_c[i]==0 && i>=0; i-- );//跳过高位0
if( i>=0 )
for( ; i>=0; i-- )
printf("%d", num_c[i]);
else
printf("0");
printf("\n");
}
return 0;
}