| (Photo credit: Wikipedia) |
Ideas for storing and operating on Big numbers in C++
Hello World!
Today I would like to share my C/C++ Code developed for storing
and performing different operations on a Big Numbers. Big Numbers are those,
which cannot handled by any normal predefined data type (int,float,double,long)
of any available programming language.
I have developed that code to get big numbers and store them
in user defined data type like linked list. Initially big number is stored in a
String of chars. Doubly linked list is used store every single char per node.
Storing a big number in a linked list is just a one thing;
there is no use of storing that number in linked list if you cannot perform
different oppressions on it.
Therefore, I have developed this code in C language to perform
very basic oppressions on it like divide and subtraction.
I have written this code last year. Until now, I did not
update it. I know this code is not perfect but you still get basic idea from
it. I think code is self explanatory
#include<iostream> using namespace std; typedef struct NodeType { int data; struct NodeType *next; struct NodeType *prev; }Node; void createList(Node **head,Node **tail) { *head=NULL; *tail=NULL; } void InsertNode(Node **head,Node **tail,int ele) { Node *temp=(Node*)malloc(sizeof(Node)); temp->data=ele; if(*head==NULL) { temp->next=NULL; temp->prev=NULL; *head=temp; *tail=temp; } else { temp->prev=*tail; temp->prev->next=temp; temp->next=NULL; *tail=temp; } } void InsertAtBeg(Node **head,Node **tail,int ele) { Node *temp=(Node*)malloc(sizeof(Node)); temp->data=ele; temp->prev=NULL; if(*head==NULL) { temp->next=NULL; *head=temp; *tail=temp; } else { temp->next=*head; *head=temp; } } void Subtract(Node *t1,Node *t2,Node **rh,Node **rt) { if(t1==NULL && t2==NULL) printf("List t1 or t2 is NULL"); else { while(t1!=NULL && t2!=NULL) { int a=t1->data; int b=t2->data; if(t1->data<t2->data) { a+=10; t1->prev->data-=1; Node *res=(Node*)malloc(sizeof(Node)); int r=a-b; InsertNode(rh,rt,r); } else { int r=a-b; InsertNode(rh,rt,r); } t1=t1->prev; t2=t2->prev; } if(t1==NULL) { while(t2!=NULL) { InsertNode(rh,rt,t2->data); t2=t2->prev; } } else { while(t1!=NULL) { InsertNode(rh,rt,t1->data); t1=t1->prev; } } } } void divide(Node *t1,int div,Node **rh,Node **rt) { int mod=0; if(t1==NULL) printf("List t1 is NULL"); else { while(t1!=NULL) { int a=t1->data; if(t1->next!=NULL) { while(t1->data<div || t1->next->data==0) { a*=10; a+=t1->next->data; t1=t1->next; if(t1->next==NULL) break; } } { int r=a/div; mod=a%div; InsertNode(rh,rt,r); } t1=t1->next; }//while cout<<"Reminder:"<<mod; } } void display(Node *head,Node *tail,bool rev) { if(head==NULL) return; else { if(rev==false) { Node *temp; temp=head; cout<<"Quotient:"; while(temp!=NULL) { cout<<temp->data; temp=temp->next; } cout<<"\n"; } else { Node *temp; temp=tail; while(temp!=NULL) { cout<<temp->data; temp=temp->prev; } cout<<"\n"; } } } void convertStoI(Node **h1,Node **h2,Node **h3,Node **t1,Node **t2,Node **t3,bool flag) { flushall(); char F_no[10]; char S_no[10]; cout<<"Enter First Value\n"; cin.getline(F_no,10); int len1=strlen(F_no); for(int i=0;i<len1;i++) { InsertNode(h1,t1,F_no[i]-'0'); } if(flag==true)// true means activate for subtract { cout<<"Enter Second Value\n"; cin.getline(S_no,10); int len2=strlen(S_no); for(int i=0;i<len2;i++) { InsertNode(h2,t2,S_no[i]-'0'); } } } void main() { Node *h1,*t1; Node *h2,*t2; Node *h3,*t3; createList(&h1,&t1); createList(&h2,&t2); createList(&h3,&t3); int x=0; cout<<"Enter 1 for Subtraction:\n"; cout<<"Enter 2 for Divide:\n"; cin>>x; if(x==1) { convertStoI(&h1,&h2,&h3,&t1,&t2,&t3,true); Subtract(t1,t2,&h3,&t3); cout<<"\n"; display(h3,t3,true); } else if(x==2) { convertStoI(&h1,&h2,&h3,&t1,&t2,&t3,false); int div=0; cout<<"Enter Dividend:"; cin>>div; if(div!=0) { divide(h1,div,&h3,&t3); cout<<"\n"; display(h3,t3,false); } else cout<<"Dividend should not be Zero(0)"; } }
Comments
Post a Comment