- 高精度乘法
- 2015-11-04 12:58:40 @
#include <iostream>
#include <string>
using namespace std;
void Proc1();
class Num
{
public:
Num();
Num(short);
Num(string&);
Num(Num&);
~Num();
Num operator*(const Num&num_p) const;
friend std::ostream& operator<<(std::ostream&os, const Num&n);
private:
int* num_indigit;
short digit;
};
Num::Num()
{
digit = 1;
num_indigit = new int[digit];
num_indigit[0] = 0;
}
Num::Num(short digit_num)
{
digit = digit_num;
num_indigit = new int[digit];
for (int i = 0; i < digit; i++)
{
num_indigit[i] = 0;
}
}
Num::Num(string&str_p)
{
digit = str_p.size();
num_indigit = new int[digit];
for (short i = 0; i < digit; i++)
num_indigit[i] = (str_p[digit-1-i]-'0');
}
Num::Num(Num& num_cpy)
{
digit = num_cpy.digit;
num_indigit = new int[digit];
for (short i = 0; i < num_cpy.digit; i++)
num_indigit[i] = num_cpy.num_indigit[i];
}
Num::~Num()
{
}
Num Num::operator*(const Num & num_p) const
{
short digit1 = digit, digit2 = num_p.digit;
Num result(digit1 + digit2);
for (int i = 0; i < digit2; i++)
for (int j = 0; j < digit1; j++)
result.num_indigit[i + j] += num_indigit[j] * num_p.num_indigit[i];
for (int i = 0; i < digit1 + digit2-1; i++)
{
result.num_indigit[i + 1] += result.num_indigit[i] / 10;
result.num_indigit[i] %= 10;
}
while (!result.num_indigit[result.digit - 1]) result.digit--;
return result;
}
std::ostream& operator<<(std::ostream&os, const Num&num_show)
{
char *ctmp = new char[num_show.digit+1];
for (int i = 0; i < num_show.digit; i++)
{
ctmp[i] = char(num_show.num_indigit[num_show.digit-1-i]+'0');
}
*(ctmp + num_show.digit) = '\0';
os << ctmp;
return os;
}
//-Main Procedure-感觉自己萌萌哒
int main()
{
Proc1();
return 0;
}
void Proc1()
{
using namespace std;
string p, q;
cin >> p;
cin >> q;
Num num_p = Num(p);
Num num_q = Num(q);
Num result;
result = num_p*num_q;
cout << result<<endl;
}
2 条评论
-
magetrom LV 8 @ 2015-11-04 23:00:23
是c++11吧。。
-
2015-11-04 20:43:25@
编译器说啥
- 1