大数加减乘除及模运算

自己写的代码,测试了几组测试样例,都没什么问题,代码如下:


#define _CRT_SECURE_NO_DEPRECATE

#include <iostream>  
#include <cstring>  
#include<algorithm>
#include<string.h>

using namespace std;

const int MAXN = 3000000 + 80;
const int M = 2000000;
const int wei = 100;

void get(int *a, int *b)
{
	for (int i = 0; i < wei; i++)
		b[i] = a[i];
}

//统计数有多少位
int cal(long long n)
{
	int sum = 0;
	while (n != 0)
	{
		n /= 10;
		sum++;
	}
	return sum;
}
//大数有多少位
int cal(int *n)
{
	int i=wei-1;
	while (n[i] == 0&&i!=0)
		i--;
	return i;
}
//打印大数
void prin(int *an1)
{
	for (int i = cal(an1); i >= 0; i--)
		cout << an1[i];
//	cout << endl;
}
//大数大小比较
int compare(int *an1, int *bn1)
{
	for (int i = wei-1; i >= 0; i--)
	{
		if (an1[i] > bn1[i])
			return 1;
		else
		{
			if (an1[i] < bn1[i])
			{
				return -1;
			}
		}
	}
	return 0;
}
//把大数初始化为零
void chushihua(int *n)
{
	for (int i = 0; i < wei; i++)
		n[i] = 0;
}
//大数加正常数
void jia(int *an1, int bn1, int *cn1)
{
//	chushihua(cn1);
	for (int i = 0; i < wei; i++)
		cn1[i] = an1[i];
	cn1[0] += bn1;
	for (int i = 0; i<wei - 1; i++)
		if (cn1[i] >= 10)
		{
			cn1[i + 1] += cn1[i] / 10;
			cn1[i] %= 10;
		}
	for (int i = 0; i<wei - 1; i++)
		if (cn1[i] < 0)
		{
			cn1[i + 1] --;
			cn1[i] += 10;
		}
}
//大数加大数
void jia(int *an1, int *bn1,int *cn1)
{
//	chushihua(cn1);
	for (int i = 0; i < wei; i++)
		cn1[i] = an1[i] + bn1[i];
	for (int i = 0; i<wei-1; i++)
		if (cn1[i] >= 10)
		{
			cn1[i + 1] += cn1[i] / 10;
			cn1[i] %= 10;
		}
	for (int i = 0; i<wei-1; i++)
		if (cn1[i] < 0)
		{
			cn1[i + 1] --;
			cn1[i] += 10;
		}
}
//大数减大数
void jian(int *an1, int *bn1,int *cn1)
{
//	chushihua(cn1);
	//如果an1小于bn1
	if (compare(an1, bn1) == -1)
	{
		jian(bn1, an1, cn1);
		cn1[cal(cn1)] *= -1;
		return;
	}
	for (int i = 0; i < wei; i++)
		cn1[i] = an1[i] - bn1[i];
	for (int i = 0; i<wei-1; i++)
		if (cn1[i] >= 10)
		{
			cn1[i + 1] += cn1[i] / 10;
			cn1[i] %= 10;
		}
	for (int i = 0; i<wei-1; i++)
		while (cn1[i] < 0)
		{
			cn1[i + 1] --;
			cn1[i] += 10;
		}
}
//大数减正常数
void jian(int *an1, int bn1, int *cn1)		//未考虑an1小于bn1的情况
{
//	chushihua(cn1);
	for (int i = 0; i < wei; i++)
		cn1[i] = an1[i] ;
	cn1[0] -= bn1;
	for (int i = 0; i<wei - 1; i++)
		if (cn1[i] >= 10)
		{
			cn1[i + 1] += cn1[i] / 10;
			cn1[i] %= 10;
		}
	for (int i = 0; i<wei - 1; i++)
		while (cn1[i] < 0)
		{
			cn1[i + 1] --;
			cn1[i] += 10;
		}
}
//大数乘正常数
void chen(int *an1, int k, int *cn1)
{
//	chushihua(cn1);
	for (int i = 0; i < wei; i++)
		cn1[i] = an1[i] * k;
	for (int i = 0; i < wei - 1; i++)
		if (cn1[i] >= 10)
		{
			cn1[i + 1] += cn1[i] / 10;
			cn1[i] %= 10;
		}
}
//大数乘大数
void chen(int *an1, int *bn1, int *cn1)
{
	for (int i = 0; i < wei; i++)
		for (int j = 0; i + j < wei; j++)
		{
			cn1[i + j] += an1[i] * bn1[j];
		}
	for (int i = 0; i < wei - 1; i++)
		if (cn1[i] >= 10)
		{
			cn1[i + 1] += cn1[i] / 10;
			cn1[i] %= 10;
		}
}
//大数除大数
void chu(int *a, int *b, int *c)
{
	int temp[wei];
	get(b, temp);
	while (compare(a, b) != -1)
	{
		while (compare(a, temp) != -1)
			chen(temp, 10, temp);
		while (compare(a, temp) == -1)
		{
			for (int i = 1; i < wei - 1; i++)
				temp[i - 1] = temp[i];
			temp[wei - 1] = 0;
		}
		while (compare(a, temp) != -1)
		{
			jian(a, temp, a);
			c[cal(temp) - cal(b)]++;
		}
	}
//	cout << "1asd   " << c[0] << "  " << c[1] << "  " << c[2] << endl;
	for (int i = 0; i<wei - 1; i++)
		if (c[i] >= 10)
		{
			c[i + 1] += c[i] / 10;
			c[i] %= 10;
		}
//	cout << "1asd   " << c[0] << "  " << c[1] << "  " << c[2] << endl;
}
//大数模大数
void mod(int *a, int *b, int *c)
{
	int temp[wei];
	chushihua(temp);
	chu(a, b, temp);
	chen(b, temp, temp);
	jian(a, temp, c);
}


int main()
{
	
	int a1[wei], b1[wei], c1[wei];
	char ch1[wei], ch2[wei];
	while (cin >> ch1>>ch2 )
	{
		for (int i = 0; i < wei; i++)
			a1[i] = b1[i] = c1[i] = 0;
		for (int i = 0; i<strlen(ch1); i++)
		{
			a1[strlen(ch1) - 1 - i] = ch1[i]-'0';
		}
		for (int i = 0; i<strlen(ch2); i++)
		{
			b1[strlen(ch2) - 1 - i] = ch2[i] - '0';
		}
		prin(a1); 
		prin(b1);
		jia(a1, b1, c1);
		prin(a1); cout << " + " << b1 << " = "; prin(c1); cout << endl;
		chushihua(c1);
		jian(a1, b1, c1);
		prin(a1); cout << " - " << b1 << " = "; prin(c1); cout << endl;
		chushihua(c1);
		chen(a1, b1, c1);
		prin(a1); cout << " * " << b1 << " = "; prin(c1); cout << endl;
		chushihua(c1);
		chu(a1, b1, c1);
		prin(a1); cout << " / " << b1 << " = "; prin(c1); cout << endl;
		chushihua(c1);
		mod(a1, b1, c1);
		prin(a1); cout << " % " << b1 << " = "; prin(c1); cout << endl;

	}
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值