Skip to main content

Operations on Big Numbers

Numbers 1-0
 (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

Popular posts from this blog

Font identifier and Unicode converter for Hindi

Font identifier and Unicode converter for Hindi Fonts are used to represent text in document. Fonts are mainly two kind non-Unicode and Unicode fonts. Complex scripts like Hindi and other Asian languages well represented in Unicode fonts. There are some other ways to write these languages for e.g we can use ASCII/ISCII codes to represent different characters of Hindi, but there are large numbers of characters in Hindi script as compared to English. Therefore, we always need multiple ASCII/ISCII encoded characters combination to represent a single character of Hindi Script. One major problem in these ASCII encoding based fonts is that we cannot easily transfer text from one system to another. The system must have these text fonts. There is hundreds of ASCII/ISCII encoding based fonts which are used to write Hindi text. New software systems are based on Unicode fonts.                   ...

Binary Search Tree in ASP .Net

Binary Search Tree in ASP .Net To create Binary Search Tree(BST) in Asp.net application   first you need to create a Node class. Something like following : class Node {     public String data;     public int freq = 0;     public Node left, right;     public Node()     { }     public Node( String data)     {         this .data = data;         left = null ;         right = null ;     } } Next You need to create a class including different functions.Like class BinaryTreeImp {     Node root;     String outputfreq = "" ;     static int count = 0;     public BinaryTreeImp()     {      ...

Hindi to Punjabi Machine Translation System

The Hindi To Punjabi Machine Translation System has been developed using Direct/Rule based Approach by Dr.Vishal Goyal and Dr. G.S Lehal. Various large size Lexicon resources  have been used to map Source and Target language words.  In general, if the two languages are structurally similar, in particular as regards lexical correspondences, morphology and word order, the case for abstract syntactic analysis seems less convincing. Since the present research work deals with a pair of closely related language, so the direct translation system is the obvious choice. The overall system architecture shown below, is adopted for Hindi to Punjabi Machine Translation System. The system is divided into three stages: Preprocessing, Translation Engine, and Post Processing stage. Following is the description of various steps of this architecture.  PreProcessing   The pre-processing stage is a collection of operations that are applied on input  data to make it pr...