⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 operating.cpp

📁 大数的运算的实现程序
💻 CPP
字号:
#include "operation.h"
#include <iostream>
#include<fstream>
using namespace std;
/*void operation::inital(char *a,char *b)
{int i=0,j=0;
while(*a){

	ap[i]=*a-48;
	i++;
	a++;
	cout<<ap[i];
	}
while(*b){

	ap[j]=*b-48;
	j++;
	b++;
	cout<<bp[j];
	}
}*/

void operation:: output(Node *head)
{
    if(!head->next&&!head->data)return;
    output(head->next);
    cout<<head->data;
}
void operation::ad(char *a,char *b)
{
	char*ap=a,*bp=b;
	Node *head=0;
	head=new Node;head->data=0;head->next=0;
	Node *p,*p1;
	int temp=0,temp1,bbit,abit;
		p=head->next;p1=head;
   
	while(*bp||*ap||temp)
	{  
	   
		if(*ap==0) abit=0;
		else abit=*ap-48;
		if(*bp==0) bbit=0;
		else bbit=*bp-48;
		
		if(!p)
		{
		p=new Node;
		p->data=0;
		p->next=0;
		p1->next=p;
		}
		temp1=bbit+abit+temp;
		p1->data=temp1%10;
		temp=temp1/10;
		p1=p;p=p->next;
		bp++;ap++;
	
	}
    p=head;
    output(p); 
   
    while(head)                
    {
            p=head->next;
            delete head;
            head=p;
    }

}
void operation::minu(char *a,char *b)
{
	if(judge(a,b))
	{
		char*ap=a,*bp=b;
		Node *head=0;
		head=new Node;head->data=0;head->next=0;
		Node *p,*p1;
		int temp=0,temp1,bbit,abit;
		p=head->next;p1=head;
		while(*bp||*ap||temp)
		{  
	   
			if(*ap==0) abit=0;
			else abit=*ap-48;
			if(*bp==0) bbit=0;
			else bbit=*bp-48;
		
			if(!p)
			{
			p=new Node;
			p->data=0;
			p->next=0;
			p1->next=p;
			}
			temp1=abit-bbit+temp;		
			if(temp1<0)
			{
				p1->data=temp1+10;
				temp=-1;
			}	
			else
			{
				p1->data=temp1;
				temp=0;
			}
			p1=p;p=p->next;
			bp++;ap++;
	
		}
		 p=head;
		 while(p->data==0&&p->next!=0)
		 {
			p=p->next;
		 }
		 if(p->next==0)cout<<0;
		output(p);    
		while(head)                
		{
            p=head->next;
            delete head;
            head=p;
		}
	}
	else
	{
		cout<<"-";
		minu(b,a);
	}
}

void operation::Mul(char *a,char *b)        
{
     char*ap=a,*bp=b;
    Node *head=0;
    head=new Node;head->data=0,head->next=0;
    Node *p,*q=head,*p1;
    int temp=0,temp1,bbit;
    while(*bp)                 
    {
        p=q->next;p1=q;
        bbit=*bp-48;         
        while(*ap||temp)           
        {
            if(!p)            
            {
                p=new Node;
                p->data=0;
                p->next=0;
                p1->next=p;
            }
            if(*ap==NULL)temp1=temp;
            else {temp1=(p1->data)+(*ap-48)*bbit+temp;ap++;} 
            p1->data=temp1%10;    
            temp=temp1/10;     
            p1=p;p=p->next;              
        }
        ap=a;bp++;q=q->next;              
    }
    p=head;
    output(p);                  
    cout<<endl;
    while(head)                
    {
            p=head->next;
            delete head;
            head=p;
    }
}
bool operation::judge(char *a,char *b)
{
	for(int i=39;i>=0;i--)
	{
		if(a[i]!=b[i]) break;
	}
	if(i==0)return true;
	else if(a[i]<b[i])return false;
	else return true;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -