// 大数的四则运算及求模。数字无位数限制,除法的商支持小数。
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
class BIGINTEGEROPERATIONS
{
private:
static int COMPARE(string number1, string number2)
{
int j;
int length1 = number1.size();
int length2 = number2.size();
if (number1.size() == )
number1 = "";
if (number2.size() == )
number2 = "";
j = ;
for (int i = ; i < length1; ++i)
{
if (number1[i] == '')
++j;
else
break;
}
number1 = number1.substr(j);
j = ;
for (int i = ; i < length2; ++i)
{
if (number2[i] == '')
++j;
else
break;
}
number2 = number2.substr(j);
length1 = number1.size();
length2 = number2.size();
if (length1 > length2)
{
return ;
}
else if (length1 == length2)
{
if (number1.compare(number2) > )
{
return ;
}
else if (number1.compare(number2) == )
{
return ;
}
else
{
return -;
}
}
else
{
return -;
}
return ;
}
public:
static string PLUS(string number1, string number2)
{
int i;
int length1 = number1.size();
int length2 = number2.size();
string result = "";
reverse(number1.begin(), number1.end());
reverse(number2.begin(), number2.end());
for (i = ; i < length1 && i < length2; i++)
{
char c = (char)(number1[i] + number2[i] - );
result = result + c;
}
while (i < length1)
{
result = result + number1[i];
++i;
}
while (i < length2)
{
result = result + number2[i];
++i;
}
int carry = ;
for (i = ; i < (int)result.size(); ++i)
{
int value = result[i] - + carry;
result[i] = (char)(value % + );
carry = value / ;
}
if (carry != )
{
result = result + (char)(carry + );
}
for (i = result.size() - ; i >= ; i--)
{
if (result[i] != '') break;
}
result = result.substr(, i + );
reverse(result.begin(), result.end());
if (result.length() == )
result = "";
return result;
}
static string MINUS(string number1, string number2)
{
int i;
string result = "";
int length1 = number1.size();
int length2 = number2.size();
if (COMPARE(number2, number1) > )
{
return "-" + MINUS(number2, number1);
}
reverse(number1.begin(), number1.end());
reverse(number2.begin(), number2.end());
for (i = ; i < length1 && i < length2; i++)
{
char c = number1[i] - number2[i] + ;
result = result + c;
}
if (i < length1)
{
for (; i < length1; i++)
{
result = result + number1[i];
}
}
int carry = ;
for (i = ; i < (int)result.length(); i++)
{
int value = result[i] - + carry;
if (value < )
{
value = value + ;
carry = -;
}
else carry = ;
result[i] = (char)(value + );
}
for (i = result.size() - ; i >= ; i--)
{
if (result[i] != '')break;
}
result = result.substr(, i + );
reverse(result.begin(), result.end());
if (result.length() == ) result = "";
return result;
}
static string MULTIPLY(string number1, string number2)
{
int i, j;
int *iresult;
int length1 = number1.size();
int length2 = number2.size();
string result = "";
reverse(number1.begin(), number1.end());
reverse(number2.begin(), number2.end());
iresult = (int*)malloc(sizeof(int) * (length1 + length2 + ));
memset(iresult, , sizeof(int) * (length1 + length2 + ));
for (i = ; i < length1; i++)
{
for (j = ; j < length2; j++)
{
iresult[i + j] += ((number1[i] - ) * (number2[j] - ));
}
}
int carry = ;
for (i = ; i < length1 + length2; i++)
{
int value = iresult[i] + carry;
iresult[i] = value % ;
carry = value / ;
}
for (i = length1 + length2 - ; i >= ; i--)
{
if (iresult[i] != )break;
}
for (; i >= ; i--)
{
result = result + (char)(iresult[i] + );
}
free(iresult);
if (result == "") result = "";
return result;
}
// 缺省地,商数向下取整,floatpoint用于指定保留小数点的位数
static string DIVIDE(string number1, string number2, int floatpoint = )
{
int i, j, pos;
string result = "";
string tempstr = "";
int length1 = number1.size();
int length2 = number2.size();
if ((COMPARE(number2, number1) > ) && (floatpoint == ))
{
return "";
}
tempstr = number1.substr(, length2);
pos = length2 - ;
while (pos < length1)
{
int quotient = ;
while (COMPARE(tempstr, number2) >= )
{
quotient++;
tempstr = MINUS(tempstr, number2);
}
result = result + (char)(quotient + );
pos++;
if (pos < length1)
{
tempstr += number1[pos];
}
}
if (floatpoint > )
{
result += '.';
string stmp = "";
int itmp = ;
for (int k = ; k < floatpoint; ++k)
{
stmp += '';
if (COMPARE(MULTIPLY(MINUS(number1, MULTIPLY(DIVIDE(number1, number2), number2)), stmp), number2) < )
{
result += '';
++itmp;
}
}
string temp = DIVIDE(MULTIPLY(MINUS(number1, MULTIPLY(DIVIDE(number1, number2), number2)), stmp), number2);
if (temp[] != '') result += temp;
}
j = result.size();
for (i = ; i < j; i++)
{
if (result[i] != '') break;
}
result = result.substr(i, j);
return result;
}
static string MOD(string number1, string number2)
{
if (COMPARE(number2, number1) > )
{
return number1;
}
else if (COMPARE(number2, number1) == )
{
return "";
}
else
{
return MINUS(number1, MULTIPLY(DIVIDE(number1, number2), number2));
}
}
};
int main(int argc, char* argv[])
{
clock_t start, end;
// 两个操作数均为600位
string number1 = ""
""
""
""
""
""
""
""
""
""
""
"";
string number2 = ""
""
""
""
""
""
""
""
""
""
""
"";
string result;
int interval = ;
start = clock();
result = BIGINTEGEROPERATIONS::PLUS(number1, number2);
end = clock();
interval = (end - start) * / CLOCKS_PER_SEC;
cout << "Result of PLUS operation:" << endl;
cout << result << endl;
cout << "It took " << interval << "ms" << endl;
cout << endl;
start = clock();
result = BIGINTEGEROPERATIONS::MINUS(number1, number2);
end = clock();
interval = (end - start) * / CLOCKS_PER_SEC;
cout << "Result of MINUS operation:" << endl;
cout << result << endl;
cout << "It took " << interval << "ms" << endl;
cout << endl;
start = clock();
result = BIGINTEGEROPERATIONS::MULTIPLY(number1, number2);
end = clock();
interval = (end - start) * / CLOCKS_PER_SEC;
cout << "Result of MULTIPLY operation:" << endl;
cout << result << endl;
cout << "It took " << interval << "ms" << endl;
cout << endl;
start = clock();
result = BIGINTEGEROPERATIONS::DIVIDE(number1, number2);
end = clock();
interval = (end - start) * / CLOCKS_PER_SEC;
cout << "Result of DIVIDE operation:" << endl;
cout << result << endl;
cout << "It took " << interval << "ms" << endl;
cout << endl;
start = clock();
result = BIGINTEGEROPERATIONS::MOD(number1, number2);
end = clock();
interval = (end - start) * / CLOCKS_PER_SEC;
cout << "Result of MOD operation:" << endl;
cout << result << endl;
cout << "It took " << interval << "ms" << endl;
cout << endl;
return ;
}
/* 计算结果如下(Release 模式):
Result of PLUS operation:
1999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989997
It took 0ms
Result of MINUS operation:
100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001
It took 0ms
Result of MULTIPLY operation:
999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989998999899989997000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010002
It took 15ms
Result of DIVIDE operation:
1.0001
It took 0ms
Result of MOD operation:
100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001
It took 0ms
Debug模式下,加、减、乘、除以及取模的执行时间分别为:
0ms
0ms
63ms
78ms
0ms
*/
void remove_duplicate(char* str)
{
if(str==NULL)
return;
int len=strlen(str);
if(len<)
return;
char flags[];
int i,j;
int index=;
memset(flags,,sizeof(flags));
flags[str[]]=;
for(i=;i<len;++i)
{
if(flags[str[i]]!=)
{
str[index++]=str[i];
flags[str[i]]=;
}
}
str[index]='\0';
}