Home > Back-end >  Super calculator
Super calculator

Time:09-15

#include
using namespace std;
Int main ()
{
For (int I; i<1000000; I++)
{
Long long a, b;
Cin & gt;> a> b;
Cout & lt; & lt; Endl<& lt; Endl
& lt; & lt; Endl<& lt; Endl<& lt; Endl<& lt; Endl<& lt; Endl<& lt; Endl
& lt; & lt; Endl<& lt; Endl<& lt; Endl<& lt; Endl<& lt; Endl<& lt; Endl
& lt; & lt; Endl<& lt; Endl<& lt; Endl<& lt; Endl<& lt; Endl<& lt; Endl
& lt; & lt; Endl<& lt; Endl<& lt; Endl<& lt; Endl<& lt; Endl<& lt; Endl
& lt; & lt; Endl<& lt; Endl<& lt; Endl<& lt; Endl<& lt; Endl<& lt; Endl
& lt; & lt;" Results: "& lt; & lt; A + b & lt; & lt; Endl;
}
return 0;
}

CodePudding user response:

 # include & lt; Iostream> 
#include
using namespace std;
The inline int the compare (string str1, string str2) {//return equal 0, is greater than the return 1, less than the return 1
If (str1. The size () & gt; Str2. The size ()) return 1;//the length of the long integer greater than the length of the integer
Else if (str1. The size () & lt; Str2. The size ()) return 1;
The else pare said return str1.com (str2);//if equal length, head to tail according to the comparison of
}
String SUB_INT (string str1, string str2);
String ADD_INT (string str1, string str2) {//high precision addition
Int sign=1;//sign for the sign bit
String STR.
If (str1 [0]=='-') {
If (str2 [0]=='-') {
Sign=1;
STR=ADD_INT (str1. Erase (0, 1), str2. Erase (0, 1));
} else {
STR=SUB_INT (str2, str1. Erase (0, 1));
}
} else {
If (str2 [0]=='-') {
STR=SUB_INT (str1, str2. Erase (0, 1));
} else {//alignment, the two integers up short integer adding 0
String: : size_type L1, L2,
int i;
L1=str1. The size ();
L2=str2. The size ();
If (L1 & lt; 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;//record int2 carry
For (I=str1. The size () - 1; I>=0; I -) {
Int1=(int (str1 [I]) - '0' + int (str2 [I]) - '0' + int2) % 10;
Int2=(int (str1 [I]) - '0' + int (str2 [I]) - '0' + int2)/10;
STR=char (int1 + '0') + STR.
}
If (int2! STR==0) char (int2 + '0') + STR;
}
}
//operation post-processing the sign bit
If ((sign==1) & amp; & (STR [0]! STR='0'))="-" + STR;
return str;
}
String SUB_INT (string str1, string str2) {//high precision subtraction
Int sign=1;//sign for the sign bit
String STR.
int i,j;
If (str2 [0]=='-') {
STR=ADD_INT (str1, str2. Erase (0, 1));
} 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. The size () - str2. The size ();
For (I=str2. The size () - 1; I>=0; I -) {
If (str1 + tempint [I] j=1;
While (1) {//zhao4zhong1 add
If (str1 [I + tempint - j]=='0') {
Str1 [I + tempint - j]='9';
j++;
} else {
Str1 [I + tempint - j]=char (int (str1 [I + tempint - j]) - 1);
break;
}
}
STR=char (str1 + tempint [I] - str2 [I] + ':') + STR.
} else {
STR=char (str1 + tempint [I] - str2 [I] + '0') + STR.
}
}
For (I=tempint - 1; I>=0; I -) STR=str1 [I] + STR;
}
//remove the excess of leading 0
STR. Erase (0, STR. Find_first_not_of (' 0 '));
If (STR) empty ()) STR="0";
If ((sign==1) & amp; & (STR [0]! STR='0'))="-" + STR;
return str;
}
String MUL_INT (string str1, string str2) {//high precision multiplication
Int sign=1;//sign for the sign bit
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. The size ();
L2=str2. The size ();
For (I=L2-1; I>=0; I -) {//analog multiplication by hand upright
String tempstr;
Int int1=0, int2=0, int3=int (str2 [I]) - '0'.
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]) - '0') + int2) % 10;
Int2=(int3 * (int (str1 [j]) - '0') + int2)/10;
Tempstr=char (int1 + '0') + tempstr;
}
If (int2! Tempstr==0) char (int2 + '0') + tempstr;
}
STR=ADD_INT (STR, tempstr);
}
//remove the results of the leading 0
STR. Erase (0, STR. Find_first_not_of (' 0 '));
If (STR) empty ()) STR="0";
If ((sign==1) & amp; & (STR [0]! STR='0'))="-" + STR;
return str;
}
String str2 string DIVIDE_INT (string str1, int flag) {//high precision division, the flag==1, return to business; Flag==0, return to the remainder
String quotient, residue;//define the quotient and remainder
Int sign1=1, sign2=1;
If (str2=="0") {//whether divisor of 0
Quotient="ERROR!" ;
Residue="ERROR!" ;
If (flag==1) return quotient;
The else return residue;
}
If (str1=="0") {//whether the dividend of 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. The size ();
L2=str2. The size ();
String tempstr;
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related