Skip to main content

Posts

Showing posts from March, 2010

Binary and Gray Code Interchange

V can convert ny Binary Number in2 its Gray Code @ nytime.V dn't need 2 start 4m 0. Da algo is: Binary to Gray Code Start 4m MSB (Left side of the Number) g(MSB)=b(MSB) g(ith bit)=b(ith bit) XOR b(i+1 th bit) As an Example: Binary= 1 0 0 0 1 1 0 |\|\|\|\|\|\| Gray = 1 1 0 0 1 0 1 Gray Code to Binary Start from MSB b(MSB)=g(MSB) b(ith bit)=g(ith bit) if the number of 1's preceding g(ith bit) is Even else b(ith bit)=NOT of g(ith bit) As an Example: Gray 1 1 0 0 1 0 1 Binary 1 0 0 0 1 1 0 Comments MSB Prec 1 is odd Prec 1 is Even Prec 1 is Even Prec 1 is Even Prec 1 is odd Prec 1 is Odd Here is a Conversion Program:: // Header Files Inclusion #include<stdio.h> #include<conio.h> #include<string.h> // User defined Functions Prototype List void printcenter(char *s,int c); // Function to print text in Center of Screen void printtabbed(char *s,int t,int c); // Function to print text with preceding space from the

Complex Number Class (C++ with Exception Handling)

This is a Complex Number Class Definition in C++. The basic Operations on Complex Numbers are defined here also, from taking input to Algebra. And it has a tiny example of Exception Handling for new operator #include <iostream.h> class complex{ private: float *x,*y; public: complex(float a=0.0, float b=0.0) { try{ x=new float; *x=a; y=new float; *y=b; } catch(...) { cout<<"Memory is not sufficient"; } } complex operator + (complex c){ complex z; *(z.x)=(*x) + *(c.x); *(z.y)=(*y) + *(c.y); return z; } complex operator - (complex c){ // Binary Minus complex z; *(z.x)=(*x) - *(c.x); *(z.y)=(*y) - *(c.y); return z; } complex operator - (){ // Unary Minus complex z; *(z.x)=-(*x); *(z.y)=-(*y); return z; } complex operator * (complex c){ complex z; *(z.x)=(*x)*(*(c.x))-(*y)*(*(c.y)); *(z.y)=(*y)*(*(c.x))+(*x)*(*(c.y)); return z; } complex operator / (complex c){ complex z; float t; t=(*(c.x))*(*(c.x)) + (*(c.y))*(*(c.y)); *(z.x)=(