迄今为止的一个稍大的程序(实现一个大整数类(+,*))
[code=C/C++][/code]
#include<iostream>
#include<string>
#include<istream>
#include<ostream>
#include<vector>
using namespace std;
int weishu(int a)//计算整数位数。
{
int sum = 0;
while(a)
{
a /= 10;
sum ++;
}
return sum;
}
class HugeInt
{
private:
int *main_int;
string mark_int;
int sum;
public:
HugeInt( HugeInt& b)
{
sum=b.sum;
main_int=(new int[b.sum]);
for(int i=0;i<b.sum;i++)
{
main_int[i]=b.main_int[i];
}
mark_int=b.mark_int;
}
HugeInt(int n):sum(n),mark_int(),main_int(new int[weishu(n)]){}
HugeInt():sum(0),main_int(new int[0]),mark_int(){}//能不能加入新元素呢?
~HugeInt(){delete main_int;};
HugeInt(string fuzhi)
{
int a;
string s;
string mark;
main_int=new int[fuzhi.length()];
mark=fuzhi.substr(0,1);
if(mark!="-"&&mark!="+")
a=0;
else
{mark_int=mark;
a=1;}
for(int i=a;i<(fuzhi.length())-a;i++)
*(main_int+i)=atoi((fuzhi.substr(i,1)).c_str());//expression cannot be evaluated
sum=fuzhi.length()-a;
}
class bad_op
{
public :
int type;
bad_op(int bi)
{
type=bi;
}
};
void operator=(HugeInt& b)
{
mark_int=b.mark_int;
sum=b.sum;
for(int i=0;i<sum;i++)
{
main_int[i]=b.main_int[i];
}}
friend HugeInt&operator+(HugeInt&,HugeInt&);
friend HugeInt&operator+(HugeInt&,int);
friend HugeInt&operator*(HugeInt&,HugeInt&);
friend ostream&operator<<(ostream&,HugeInt&);
friend istream&operator>>(istream&,HugeInt&);
friend HugeInt&operator*(HugeInt& ,int );
friend HugeInt&operator*(int,HugeInt& );
};
HugeInt&operator+(HugeInt& ii1,HugeInt& ii2)
{
int min,max,flag=0;
HugeInt temp;
if(ii1.sum<ii2.sum)
{min=ii1.sum;max=ii2.sum;flag=1;}
else {min=ii2.sum;max=ii1.sum;}
for(int i=0;i<min;i++)
{
int a;
a=0;
temp.main_int[i]=ii1.main_int[i]+ii2.main_int[i]+a;
if(temp.main_int[i]>9)
{temp.main_int[i]= temp.main_int[i]-10;
a=1;}
}
for(int i=min;i<max;i++)
{
if(flag==0)
temp.main_int[i]=ii1.main_int[i];
else temp.main_int[i]=ii2.main_int[i];
}
temp.sum=max;
return temp;
}
HugeInt&operator+(HugeInt&i1,int i2)
{
HugeInt temp=HugeInt(i2);
return i1+temp;
}
HugeInt&operator+(HugeInt&i1,string i2)
{
HugeInt temp=HugeInt(i2);
return temp+i1;
}
ostream&operator<<(ostream& os,HugeInt& hu)
{
os<<hu.mark_int;
for(int i=0;i<hu.sum;i++)
{
os<<hu.main_int[i];//有问题.
}
return os;
}
istream& operator>>(istream& is,HugeInt& hu)
{
is>>hu.mark_int;
for(int i=0;i<hu.sum;i++)
{
hu.main_int=new int[1];
is>>hu.main_int[i];
}
return is;
}
HugeInt&operator*(HugeInt& ii1,HugeInt&ii2)
{
HugeInt temp;
int min,max;
if(ii1.sum<ii2.sum)
{min=ii1.sum;max=ii2.sum;}
else {min=ii2.sum;max=ii1.sum;}
vector<vector<int> > temp1;
for(int i=0;i<ii1.sum;i++)
for(int j=0;j<ii2.sum;j++)
{
temp1[i][j]=ii1.main_int[i]*ii2.main_int[j];
}
for(int i=0;i<2*max;i++)
{
for(int j=0;j<max;j++)
temp.main_int[i]+=temp1[j][i-j];
}
for(int i=0;i<min;i++)
{
int a;
a=0;
temp.main_int[i]=ii1.main_int[i]+ii2.main_int[i]+a;
if(temp.main_int[i]>9)
{temp.main_int[i]= temp.main_int[i]-10;
a=1;}
}
if(ii1.mark_int==ii2.mark_int)
temp.mark_int=" ";
else temp.mark_int="-";
return temp;
}
HugeInt&operator*(HugeInt&i1 ,int i2)
{
HugeInt temp=HugeInt(i2);
return temp*i1;
}
HugeInt&operator*(int i2,HugeInt& i1 )
{
HugeInt temp=HugeInt(i2);
return temp*i1;
}
只能说它build成功。。。。。。
测试程序如下:
//***************************************************************
// Test Program for Assignment 8-1.
//***************************************************************
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>
using namespace std;
#include "hugeint.h"
int main()
try
{
HugeInt n1(76543210), n2(781231),
n3("12312345678901234567890123456789098999999999999999999999999999"),
n4("1"), n5=0;
cout << "n1 is " << n1 << "\nn2 is " << n2
<< "\nn3 is " << n3 << "\nn4 is " << n4
<< "\nn5 is " << n5 << "\n\n";
HugeInt n11(-767854321), n12(-767891234),
n13("-1234567890123456789670123456789098999999999999999999999999999"),
n14("-1"), n15;
cout << "n11 is " << n11 << "\nn12 is " << n12
<< "\nn13 is " << n13 << "\nn14 is " << n14
<< "\nn15 is " << n15 << "\n\n";
n5 = n1 * n2;
cout << n1 << " * " << n2 << " = " << n5 << "\n\n";
cout << n3 << " * " << n3 << " = " << n3*n3 << "\n\n";
cout << n13 << " * " << n13 << " = " << n13*n13 << "\n\n";
cout << n3 << " * " << n13 << " = " << n3*n13 << "\n\n";
cout << n13 << " * " << n3 << " = " << n13*n3 << "\n\n";
cout << n3 << " + " << n4 << " = " << n3+n4 << "\n\n";
n5 = n1 + 9;
cout << n1 << " + " << 9 << " = " << n5 << "\n\n";
n5 = n2 + "10000";
cout << n2 << " + " << "10000" << " = " << n5 << "\n";
n5 = n2 * 1111;
cout << n2 << " * " << "1111" << " = " << n5 << endl;
cout << "1111" << " * " << n2 << " = " << 1111*n2 << endl;
// Compute factorial(n):
cout << "Compute factorial(n) : " << endl;
{ HugeInt product =1;
long N; cout << "enter n : " ; cin>>N;
long startTime=clock();
for (long idx=1; idx<=N;idx++)
{
cout << setw(4) << setfill(' ') << idx <<flush;
product = product*idx;
cout << "\b\b\b\b";
}
cout << endl << N << "! : " << product << endl;
long endTime=clock();
cout << endl << "Time Elasped : "
<< 1000*(double)(endTime-startTime) / CLOCKS_PER_SEC
<< " milliseconds." << endl;
}
// compute n^n using trivial method:
cout << "Compute n^n : " << endl;
{ HugeInt product =1;
long N; cout << "enter n : " ; cin>>N;
long startTime=clock();
for (long idx=1; idx<=N;idx++)
{
cout << setw(4) << setfill(' ') << idx <<flush;
product = product*N;
cout << "\b\b\b\b";
}
cout << endl << N << "**" << N << " : "<< product << endl;
long endTime=clock();
cout << endl << "Time Elasped : "
<< 1000*(double)(endTime-startTime) / CLOCKS_PER_SEC
<< " milliseconds." << endl;
}
system("pause"); return 0;
}
catch(...) { cerr<<"ERROR!"<<endl; system("pause"); }
[解决办法]
congrats!
[解决办法]
祝贺!
[解决办法]
代码 放在code=C/C++ /code] 里面,才有效果。 放在外面,就没有高亮了
[解决办法]
有什么可祝贺的?
看了楼主的代码,简直不知所云。
sum 这个成员变量到底是干什么用的。从以下两个函数看, sum应该是这个大整数10进制表示的位数。如
数76543210赋值 或者 用76543210初始化HugeInt后,sum应为8.
ostream&operator<<(ostream& os,HugeInt& hu)istream& operator>>(istream& is,HugeInt& hu)
[解决办法]
#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 SUB_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 = SUB_INT(str2, str1.erase(0, 1)); } } else { if(str2[0] == '-') str = SUB_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]) - '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 != 0) str = char(int2 + '0') + str; } } //运算后处理符号位 if((sign == -1) && (str[0] != '0')) str = "-" + str; return str;}string SUB_INT(string str1, string str2) {//高精度减法 string MUL_INT(string str1, string str2); int sign = 1; //sign 为符号位 string str; int i; 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.size() - str2.size(); for(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] + ':') + str; } else str = char(str1[i + tempint] - str2[i] + '0') + str; } for(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 MUL_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]) - '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 != 0) tempstr = char(int2 + '0') + 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]; tempstr.erase(0, tempstr.find_first_not_of('0'));//zhao4zhong1添加 if(tempstr.empty()) tempstr = "0";//zhao4zhong1添加 for(char ch = '9'; ch >= '0'; ch --) { //试商 string str; str = str + ch; if(compare(MUL_INT(str2, str), tempstr) <= 0) { quotient = quotient + ch; tempstr = SUB_INT(tempstr, MUL_INT(str2, str)); 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 = SUB_INT(s1, s2); break; case '*': res = MUL_INT(s1, s2); break; case '/': res = DIV_INT(s1, s2); break; case '%': res = MOD_INT(s1, s2); break; default : break; } cout << res << endl; } return(0);}