数据结构大型实验(大整数)附源代码 (2)

骤雨初歇 分享 2020-06-21 下载文档

cout<

?void print2()打印并存储二进制数据。

void Long_Num::print2() //打印二进制数并存储数据 {

if(head==NULL) {

cout<<\数据为空!********\ } else {

ofstream outfile;

outfile.open(\ node*p=head; while(p->next) {

if(p->value==0) p=p->next; else

break; }

while(p) {

cout<value; outfile<value; p=p->next; }

outfile<

?bool compare(Long_Num temp)实现两个长度相等的大整数的比较。

bool Long_Num::compare(Long_Num temp) //初级比较,只用于长度相等的大正间的比较,大于等于返回true {

bool flag=false;

}

node*temp1=head,*temp2=temp.head; while(temp1) {

if(temp1->value!=temp2->value) {

flag=temp1->value>temp2->value?true:false; break; } else {

temp1=temp1->next; temp2=temp2->next; } }

if(!temp1) flag=true; return flag;

?bool compare2(Long_Num temp)实现两个大整数的比较。

bool Long_Num::compare2(Long_Num temp) //升级版比较,可比较长度不等的大整数,大于等于返回true {

if(len>temp.len) return true; else {

if(len==temp.len)

return compare(temp); else

return false; } }

?void set_Long_Num(string s) 创建大整数,把字符串的内容转为int赋给大

整数。

void Long_Num::set_Long_Num(string s) //将输入的字符串转成大整数 {

int str_len=s.length();

head=new node((int)s[0]-48); len++; back=head; if(str_len>1) {

for(int i=1;i

node*Newnode=new node((int)s[i]-48); len++;

back->next=Newnode; Newnode->pre=back; back=back->next; } }

length(); }

void Long_Num::set_Long_Num2(int temp) //建立大整数,将参数建立新节点加在尾部 {

node*Newnode=new node(temp); if(back) {

back->next=Newnode; Newnode->pre=back; back=back->next; } else {

head=Newnode; back=head; }

len++; }

?void set_Long_Num2(int temp) 如果当前大整数为空,则创建大整数,并把

值赋给它,如果非空,则在后面添加节点,把值赋给节点。

void Long_Num::set_Long_Num2(int temp) //建立大整数,将参数建立新节点加在尾部 {

node*Newnode=new node(temp); if(back) {

back->next=Newnode; Newnode->pre=back; back=back->next; } else {

head=Newnode; back=head; }

len++; }

Long_Num Long_Num::sub_Num(Long_Num temp,node* &p) //截取一定长度,用于试商法 {

Long_Num tempp;

node*p1=head,*p2=temp.head; string s=\ while(p2) {

s+=p1->value+'0'; p1=p1->next; p2=p2->next; p=p->next; }

tempp.set_Long_Num(s);

if(!tempp.compare(temp)) //如果截取的子串还是比参数小,再向后截一位 {

p=p->next;

tempp.set_Long_Num2(p1->value); }

tempp.length(); return tempp; }

?Long_Num sub_Num(Long_Num temp,node* &p) 截取被除数的字串,用于

除法与除数的比较,如果小,则再截取后面一位。

Long_Num Long_Num::sub_Num(Long_Num temp,node* &p) //截取一定长度,用于试商法 {

Long_Num tempp;

node*p1=head,*p2=temp.head;

string s=\ while(p2) {

s+=p1->value+'0'; p1=p1->next; p2=p2->next; p=p->next; }

tempp.set_Long_Num(s);

if(!tempp.compare(temp)) //如果截取的子串还是比参数小,再向后截一位 {

p=p->next;

tempp.set_Long_Num2(p1->value); }

tempp.length(); return tempp; } int Long_Num::result(Long_Num temp) //除法中的试商法得到当前位的商 {

length(); int result=0;

while(compare2(temp)) {

decrease(temp); result++; }

length();

return result; }

?int result(Long_Num temp)得到当前大整数除以除数的商。

int Long_Num::result(Long_Num temp) //除法中的试商法得到当前位的商 {

length(); int result=0;

while(compare2(temp)) {


数据结构大型实验(大整数)附源代码 (2).doc 将本文的Word文档下载到电脑

下一篇:统计13期中试卷

相关推荐
相关阅读
本类排行
× 游客快捷下载通道(下载后可以自由复制和排版)

下载本文档需要支付 7

支付方式:

开通VIP包月会员 特价:29元/月

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:xxxxxx QQ:xxxxxx