#include <iostream>
#include <string>
using namespace std;
inline int compare(string str1, string str2)
{
if(str1.size() > str2.size()) //长度长的整数大于长度小的整数
return 1;
else if(str1.size() < str2.size())
return -1;
else
return str1.compare(str2); //若长度相等,从头到尾按位比较,compare函数:相等返回0,大于返回1,小于返回-1
}
//高精度加法
string ADD_INT(string str1, string str2)
{
string MINUS_INT(string str1, string str2);
int sign = 1; //sign 为符号位
string str;
if(str1[0] == '-') {
if(str2[0] == '-') {
sign = -1;
str = ADD_INT(str1.erase(0, 1), str2.erase(0, 1));
}else {
str = MINUS_INT(str2, str1.erase(0, 1));
}
}else {
if(str2[0] == '-')
str = MINUS_INT(str1, str2.erase(0, 1));
else {
//把两个整数对齐,短整数前面加0补齐
string::size_type l1, l2;
int i;
l1 = str1.size(); l2 = str2.size();
if(l1 < l2) {
for(i = 1; i <= l2 - l1; i++)
str1 = "0" + str1;
}else {
for(i = 1; i <= l1 - l2; i++)
str2 = "0" + str2;
}
int int1 = 0, int2 = 0; //int2 记录进位
for(i = str1.size() - 1; i >= 0; i--) {
int1 = (int(str1[i]) - 48 + int(str2[i]) - 48 + int2) % 10; //48 为 '0' 的ASCII 码
int2 = (int(str1[i]) - 48 + int(str2[i]) - 48 +int2) / 10;
str = char(int1 + 48) + str;
}
if(int2 != 0) str = char(int2 + 48) + str;
}
}
//运算后处理符号位
if((sign == -1) && (str[0] != '0'))
str = "-" + str;
return str;
}
//高精度减法
string MINUS_INT(string str1, string str2)
{
string MULTIPLY_INT(string str1, string str2);
int sign = 1; //sign 为符号位
string str;
if(str2[0] == '-')
str = ADD_INT(str1, str2.erase(0, 1));
else {
if (str1[0]=='-')
{
str =ADD_INT(str1.erase(0,1),str2);
str = "-" + str;
return str;
}
else
{
int res = compare(str1, str2);
if(res == 0) return "0";
if(res < 0) {
sign = -1;
string temp = str1;
str1 = str2;
str2 = temp;
}
string::size_type tempint;
tempint = str1.size() - str2.size();
for(int i = str2.size() - 1; i >= 0; i--) {
if(str1[i + tempint] < str2[i]) {
str1[i + tempint - 1] = char(int(str1[i + tempint - 1]) - 1);
str = char(str1[i + tempint] - str2[i] + 58) + str;
}
else
str = char(str1[i + tempint] - str2[i] + 48) + str;
}
for(int i = tempint - 1; i >= 0; i--)
str = str1[i] + str;
}
}
//去除结果中多余的前导0
str.erase(0, str.find_first_not_of('0'));
if(str.empty()) str = "0";
if((sign == -1) && (str[0] != '0'))
str = "-" + str;
return str;
}
//高精度乘法
string MULTIPLY_INT(string str1, string str2)
{
int sign = 1; //sign 为符号位
string str;
if(str1[0] == '-') {
sign *= -1;
str1 = str1.erase(0, 1);
}
if(str2[0] == '-') {
sign *= -1;
str2 = str2.erase(0, 1);
}
int i, j;
string::size_type l1, l2;
l1 = str1.size(); l2 = str2.size();
for(i = l2 - 1; i >= 0; i --) { //实现手工乘法
string tempstr;
int int1 = 0, int2 = 0, int3 = int(str2[i]) - 48;
if(int3 != 0) {
for(j = 1; j <= (int)(l2 - 1 - i); j++)
tempstr = "0" + tempstr;
for(j = l1 - 1; j >= 0; j--) {
int1 = (int3 * (int(str1[j]) - 48) + int2) % 10;
int2 = (int3 * (int(str1[j]) - 48) + int2) / 10;
tempstr = char(int1 + 48) + tempstr;
}
if(int2 != 0) tempstr = char(int2 + 48) + tempstr;
}
str = ADD_INT(str, tempstr);
}
//去除结果中的前导0
str.erase(0, str.find_first_not_of('0'));
if(str.empty()) str = "0";
if((sign == -1) && (str[0] != '0'))
str = "-" + str;
return str;
}
//高精度除法
string DIVIDE_INT(string str1, string str2, int flag)
{
//flag = 1时,返回商; flag = 0时,返回余数
string quotient, residue; //定义商和余数
int sign1 = 1, sign2 = 1;
if(str2 == "0") { //判断除数是否为0
quotient = "ERROR!";
residue = "ERROR!";
if(flag == 1) return quotient;
else return residue;
}
if(str1 == "0") { //判断被除数是否为0
quotient = "0";
residue = "0";
}
if(str1[0] == '-') {
str1 = str1.erase(0, 1);
sign1 *= -1;
sign2 = -1;
}
if(str2[0] == '-') {
str2 = str2.erase(0, 1);
sign1 *= -1;
}
int res = compare(str1, str2);
if(res < 0) {
quotient = "0";
residue = str1;
}else if(res == 0) {
quotient = "1";
residue = "0";
}else {
string::size_type l1, l2;
l1 = str1.size(); l2 = str2.size();
string tempstr;
tempstr.append(str1, 0, l2 - 1);
//模拟手工除法
for(int i = l2 - 1; i < l1; i++) {
tempstr = tempstr + str1[i];
for(char ch = '9'; ch >= '0'; ch --) { //试商
string str;
str = str + ch;
if(compare(MULTIPLY_INT(str2, str), tempstr) <= 0) {
quotient = quotient + ch;
tempstr = MINUS_INT(tempstr, MULTIPLY_INT(str2, str));
<span style="white-space:pre"> </span> tempstr.erase(0, tempstr.find_first_not_of('0'));
break;
}
}
}
residue = tempstr;
}
//去除结果中的前导0
quotient.erase(0, quotient.find_first_not_of('0'));
if(quotient.empty()) quotient = "0";
if((sign1 == -1) && (quotient[0] != '0'))
quotient = "-" + quotient;
if((sign2 == -1) && (residue[0] != '0'))
residue = "-" + residue;
if(flag == 1) return quotient;
else return residue;
}
//高精度除法,返回商
string DIV_INT(string str1, string str2)
{
return DIVIDE_INT(str1, str2, 1);
}
//高精度除法,返回余数
string MOD_INT(string str1, string str2)
{
return DIVIDE_INT(str1, str2, 0);
}
int main()
{
char ch;
string s1, s2, res;
while(cin >> ch) {
cin >> s1 >> s2;
switch(ch) {
case '+': res = ADD_INT(s1, s2); break; //高精度加法
case '-': res = MINUS_INT(s1, s2); break; //高精度减法
case '*': res = MULTIPLY_INT(s1, s2); break; //高精度乘法
case '/': res = DIV_INT(s1, s2); break; //高精度除法, 返回商
case 'm': res = MOD_INT(s1, s2); break; //高精度除法, 返回余数
default : break;
}
cout << res << endl;
}
return(0);
}
大数模版
/*大数加法*/
# include<stdio.h>
# include<string.h>
# include<malloc.h>
void add(char* a,char* b,char* c)
{
int i,j,k,max,min,n,temp;
char *s,*pmax,*pmin;
max=strlen(a);
min=strlen(b);
if (max<min)
{
temp=max;
max=min;
min=temp;
pmax=b;
pmin=a;
}
else
{
pmax=a;
pmin=b;
}
s=(char*)malloc(sizeof(char)*(max+1));
s[0]='0';
for (i=min-1,j=max-1,k=max;i>=0;i--,j--,k--)
s[k]=pmin[i]-'0'+pmax[j];
for (;j>=0;j--,k--)
s[k]=pmax[j];
for (i=max;i>=0;i--)
if (s[i]>'9')
{
s[i]-=10;
s[i-1]++;
}
if (s[0]=='0')
{
for (i=0;i<=max;i++)
c[i-1]=s[i];
c[i-1]='\0';
}
else
{
for (i=0;i<=max;i++)
c[i]=s[i];
c[i]='\0';
}
free(s);
}
/*大数凿法*/
void subtract(char* a,char* b,char* c)
{
int i,j,ca,cb;
ca=strlen(a);
cb=strlen(b);
if (ca>cb||(ca==cb&&strcmp(a,b)>=0))
{
for (i=ca-1,j=cb-1;j>=0;i--,j--)
a[i]-=(b[j]-'0');
for (i=ca-1;i>=0;i--)
if (a[i]<'0')
{
a[i]+=10;
a[i-1]--;
}
i=0;
while (a[i]=='0')
i++;
if (a[i]=='\0')
{
c[0]='0';
c[1]='\0';
}
else
{
for (j=0;a[i]!='\0';i++,j++)
c[j]=a[i];
c[j]='\0';
}
}
else
{
for (i=ca-1,j=cb-1;i>=0;i--,j--)
b[j]-=(a[i]-'0');
for (j=cb-1;j>=0;j--)
if (b[j]<'0')
{
b[j]+=10;
b[j-1]--;
}
j=0;
while (b[j]=='0')
j++;
i=1;
c[0]='-';
for (;b[j]!='\0';i++,j++)
c[i]=b[j];
c[i]='\0';
}
}
/* 大数乘法*/
void multiply(char* a,char* b,char* c)
{
int i,j,ca,cb,* s;
ca=strlen(a);
cb=strlen(b);
s=(int*)malloc(sizeof(int)*(ca+cb));
for (i=0;i<ca+cb;i++)
s[i]=0;
for (i=0;i<ca;i++)
for (j=0;j<cb;j++)
s[i+j+1]+=(a[i]-'0')*(b[j]-'0');
for (i=ca+cb-1;i>=0;i--)
if (s[i]>=10)
{
s[i-1]+=s[i]/10;
s[i]%=10;
}
i=0;
while (s[i]==0)
i++;
for (j=0;i<ca+cb;i++,j++)
c[j]=s[i]+'0';
c[j]='\0';
free(s);
}
/*大数除法,返回余数*/
int dividor(char* a,int b,char* c)
{
int i,j,temp=0,n;
char* s;
n=strlen(a);
s=(char*)malloc(sizeof(char)*(n+1));
for (i=0;a[i]!=0;i++)
{
temp=temp*10+a[i]-'0';
s[i]=temp/b+'0';
temp%=b;
}
s[i]='\0';
for (i=0;s[i]=='0'&&s[i]!='\0';i++);
if (s[i]=='\0')
{
c[0]='0';
c[1]='\0';
}
else
{
for (j=0;s[i]!='\0';i++,j++)
c[j]=s[i];
c[j]='\0';
}
free(s);
return temp;
}