Pages

Friday, April 10, 2015

Converting Infix to Postfix using Stacks in C++


#include<iostream>
#include<conio.h>
#include<cstring>
#include<stdlib.h>
using namespace std;
const char size=100;
class stack
{
private:
                char data[size];
                int top;
public:  
                int topnum;
                bool isempty()
                {
                                return top==-1;
                }
                bool isfull()
                {
                                return top==size-1;
                }
                void push(char j)
                {
                                if(isfull())
                                {
                                                cout<<"cannot add new item "<<endl;
                                }
                                else
                                {
                                                top++;
                                                data[top]=j;
                                                topnum=top;
                                }
                }
                void pop(char &j)
                {
                                if (isempty())
                                {
                                                cout<<"stack full"<<endl;
                                }
                                else
                                {
                                                topnum=top;
                                                j=data[top];
                                                top--;
                                }
                }

                void display()
                {
                                cout<<"The Postfix Form Is : ";
                                for(int i=0;i<=top;i++)
                                {
                                                cout<<data[i];
                                }
                }
};

void main()
{
                stack a;
                stack b;
                char c;
                char infex[size];
                cout<<" Enter The  Infex Formula :";
        cin.get(infex,size);
       
        char ch;
        int lenght;
        lenght=strlen(infex);
        for(int i=0;i<lenght;i++)
        {
                if(infex[i]=='+'||infex[i]=='-'||infex[i]=='*'||infex[i]=='/'||infex[i]=='('||infex[i]==')')
                {
                        if(infex[i]=='*'||infex[i]=='/'||infex[i]=='(')
                                a.push(infex[i]);                                                                                                              
                        else if(infex[i]=='+'||infex[i]=='-')
                        {
                                if(a.topnum=='*'||a.topnum=='/')
                                {
                                        a.pop(ch);
                                       c=ch;
                                        while(ch!='('&& ch!=a.isempty())
                                        {
                                                b.push(ch);
                                                a.pop(ch);

                                        }
                                        a.push(infex[i]);
                                }
                                else
                                        a.push(infex[i]);
                                }
                        else if(infex[i]==')')
                        {
                                a.pop(ch);
                                c=ch;
                                while(c!='(')
                                {
                                        b.push(c);
                                        a.pop(ch);
                                        c=ch;
                                }
                        }
                        }
                else
                        b.push(infex[i]);
                }
        while(!a.isempty())
        {
                a.pop(ch);
               c=ch;
                b.push(c);    
        }
        b.display();
        cout<<endl;      
getch();
}

Creating a Dialog Box Dynamically in C#

public static int ShowDialog(string text, string caption)
        {
            Form prompt = new Form();
            prompt.Width = 500;
            prompt.Height = 100;
            prompt.Text = caption;
            Label textLabel = new Label() { Left = 50, Top=20, Text=text };

            NumericUpDown inputBox = new NumericUpDown () { Left = 50, Top=50, Width=400 };

            Button confirmation = new Button() { Text = "Ok", Left=350, Width=100, Top=70 };

            confirmation.Click += (sender, e) => { prompt.Close(); };
            prompt.Controls.Add(confirmation);
            prompt.Controls.Add(textLabel);
            prompt.Controls.Add(inputBox);
            prompt.ShowDialog();
            return (int)inputBox.Value;
        }

It can be tested by calling

int promptValue = Prompt.ShowDialog("Test", "123");



Saturday, April 4, 2015

Matrices Multiplication Using 2D Arrays In C++

#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
void main()
{
//note that for multiplication the columns of first matrix should be equal to rows of second matrix.

//rows1=rows of first matrix and col1=column of first matrix and vice versa.

int rows1,col1,rows2,col2;
int array1[50][50],array2[50][50],product[50][50];
cout<<"Enter number of rows forfirstmatrix:";
cin>>rows1;
cout<<"\n   Enter number of columns for first matrix : ";
cin>>col1;
cout<<"\n   Entering values in first matrix :-";
cout<<endl;
for(int i=0;i<rows1;i++)
{
for(int j=0;j<col1;j++)
{
cout<<"\n   Enter value : ";
cin>>array1[i][j];
}
}
cout<<"\n   First matrix is :";
cout<<endl<<endl;
//"rowsFm" means rows of first matrix, "colFm" means cloumns of first matrix and vice versa.
for(int rowsFm=0;rowsFm<rows1;rowsFm++)
{
for(int colFm=0;colFm<col1;colFm++)
{
cout<<"\t"<<array1[rowsFm][colFm];
}
cout<<endl<<endl;
}
cout<<"\n   Enter number of rows for second matrix : ";
cin>>rows2;
cout<<"\n   Enter number of columns for second matrix : ";
cin>>col2;
cout<<"\n   Entering values in second matrix :-";
cout<<endl;
for(int k=0;k<rows2;k++)
{
for(int l=0;l<col2;l++)
{
cout<<"\n   Enter value :  ";
cin>>array2[k][l];
}
}
cout<<"\n   Second matrix is :";
cout<<endl<<endl;
for(int rowsSm=0;rowsSm<rows2;rowsSm++)
{
for(int colSm=0;colSm<col2;colSm++)
{
cout<<"\t"<<array2[rowsSm][colSm];
}
cout<<endl<<endl;
}
cout<<"\n   The product of matrices is :-";
cout<<endl<<endl;
if(col1==rows2)
{
for(int a=0;a<rows1;a++)
{
for(int b=0;b<col2;b++)
{
product[a][b]=0;
for(int c=0;c<rows2;c++)
{
product[a][b]=product[a][b]+array1[a][c]*array2[c][b];
}
}
}
for(int d=0;d<rows1;d++)
{
for(int e=0;e<col2;e++)
{
cout<<"\t"<<product[d][e];
}
cout<<endl<<endl;
}
}
else 
{
cout<<"\n   Product is not possible.";
}
getch();
}

Friday, April 3, 2015

How To Add Two complex Numbers Using C++ class

#include <iostream>
#include <conio.h>
using namespace std;
class complex
{
public :
int real, img;
};
int main()
{
complex a, b, c;
cout << "Enter a and b where a + ib is the first complex number.";

 cout << "\na = ";
 cin >> a.real;
cout << "b = "; cin >> a.img;
cout << "Enter c and d where c + id is the second complex number.";

 cout << "\nc = "; 
 cin >> b.real;
cout << "d = ";

 cin >> b.img;
c.real = a.real + b.real;
c.img = a.img + b.img;
if ( c.img >= 0 )
cout << "Sum of two complex numbers = " << c.real << " + " << c.img << "i"; 

 else
 cout << "Sum of two complex numbers = " << c.real << " " << c.img << "i";
 return 0; 
}

Friday, March 13, 2015

Selection Sorting in C++

#include<iostream>
#include<conio.h>
using namespace std;

void selectionSort(int numbers[], int array_size) //function for sorting

   int i, j, T, min; 
   for (i = 0; i < array_size; i++) 

      min = i; 
      for (j = i + 1; j < array_size; j++)
 { 
         if (numbers[j] < numbers[min])
 { 
            min = j; 

      } 
      T = numbers[min]; 
      numbers[min] = numbers[i]; 
      numbers[i] = T; 
 cout<<numbers[i]<<"  ";
 
    } 
   


void main()
{
int numbers[5]={5,3,1,2,4};
int b=5;

cout<<"selection sorting : ";
selectionSort(numbers,b);
cout<<"  ";

getch();
}

Wednesday, January 28, 2015

C++ class program to perform rational number arithmetic Operations using Operator Overloading

#include<stdio.h>          
 #include<iostream.h>
 #include<conio.h>
 class rational
 {
                 int numer;
                 int denom;
                 public:
                 void getdata()
                 {
                                 cout<<"\n enter the numerator part of the rational no.";
                                 cin>>numer;
                                 cout<<"\n enter the denominator part of the rational no.";
                                 cin>>denom;
                 }
                 void operator+(rational);
                 void operator-(rational);
                 void operator *(rational);
                 void operator /(rational);
 };
 void rational ::operator+(rational c1)
 {
                 rational temp;
                 temp.numer=(numer*c1.denom)+(c1.numer*denom);
                 temp.denom=denom*c1.denom;
                 cout<<"\nrational no. after addition";
                 cout<<"\n numerator="<<temp.numer<<"\n denominator ="<<temp.denom;
 }
 void raional ::operator -(rational c1)
 {
                 rational temp;
                 temp.numer=(numer*c1.denom)-(c1.numer*denom);
                 temp.denom=denom*c1.denom;
                 cout<<"\n rational no. after subtraction";
                 cout<<"\n numerator="<<temp.numer<,"\n denominator ="<<temp.denom;
 }
 void rational ::operator (rational c1)
 {
                 rational temp;
                 temp.numer=numer*c1.numer;
                 temp.denom=denom*c1.denom;
                 cout<<"\n rational no. after multiplication";
                 cout <<"\n numerator="<temp.numer<<"\n denominator ="<< temp.denom;
 }
 void rational :: operator /(rational c1)
 {
                 rational temp;
                 temp.numer= numer*c1.denom;
                 temp.denom=c1.numer*denom;
                 cout<<"\n rational no. after division";
                 cout <<"\n numerator="<<temp.numer<<"\n denominator ="<<temp.denom;
 }
 void main()
 {
                 clrscr();
                 rational c1, c2;
                 int n;
                 do
                 {
                                 cout<<"\n 1.Input data for rational no. ";
                                 cout<<"\n 2. Addition of rational no. ";
                                 cout<<"\n 3. Subtraction of rational no. ";
                                 cout<<"\n 4. Multiplication of rational no.";
                                 cout<<\n  5. Division of rational no. ";
                                 cout<<"\n 6. Quit";
                                 cout<<"\n Enter your choice";
                                 cin>>n;
                                 switch(n)
                                 {
                                                 case 1:
                                                 cout<<endl<<"\n enter the data for first rational no.";
                                                 c1.getdata();
                                                 cout<<endl<<"\n enter the data for second rational no. ";
                                                 c2.getdata ();
                                                 clrscr();
                                                 break;
                                                 case 2;
                                                 c1+c2;
                                                 getch();
                                                 clrscr();
                                                 break;
                                                 case 3;
                                                 c1-c2;
                                                 getch();
                                                 clrscr();
                                                 case 4:
                                                 c1*c2;
                                                 getch();
                                                 clrscr();
                                                 break;
                                                 case 5:
                                                 c1/c2;
                                                 getch();
                                                 clrscr();
                                                 break;
                                                 case 6:
                                                 exit(1);
                                                 break;
                                 }
                 } while (n!=6);
                 getch();
 }


Thursday, January 1, 2015

Operator Overloading in C++ to add,substract,multiply and divide two Complex Numbers

Operator Overloading is a technique of polymorphism by which an operator(+,- etc) can be used to do different types of operations. eg:+ can be used to add two integers say a and b using sum=a+b similarly two floating point numbers say fa,fb by fs=fa+fb. In this example +,-,*,- operators are overloaded to add,subtract, multiply and divide two complex numbers. 

#include<iostream.h> 
 #include<conio.h> 
 class complex 
 { 
 int a,b; 
 public: 
 void read() 
 { 
 cout<<"\n\nEnter the REAL PART : "; 
 cin>>a; 
 cout<<"\n\nEnter the IMAGINARY PART : "; 
 cin>>b; 
 } 
 complex operator +(complex c2) 
 { 
 complex c3; 
 c3.a=a+c2.a; 
 c3.b=b+c2.b; 
 return c3; 
 } 
 complex operator -(complex c2) 
 { 
 complex c3; 
 c3.a=a-c2.a; 
 c3.b=b-c2.b; 
 return c3; 
 } 
 complex operator *(complex c2) 
 { 
 complex c3; 
 c3.a=(a*c2.a)-(b*c2.b); 
 c3.b=(b*c2.a)+(a*c2.b); 
 return c3; 
 } 
 complex operator /(complex c2) 
 { 
 complex c3; 
 c3.a=((a*c2.a)+(b*c2.b))/((c2.a*c2.a)+(c2.b*c2.b)); 
 c3.b=((b*c2.a)-(a*c2.b))/((c2.a*c2.a)+(c2.b*c2.b)); 
 return c3; 
 } 
 void display() 
 { 
 cout<<a<<"+"<<b<<"i"; 
 } 
 }; 
 void main() 
 { 
 complex c1,c2,c3; 
 int choice,cont; 
 do 
 { 
 clrscr(); 
 cout<<"\t\tCOMPLEX NUMBERS\n\n1.ADDITION\n\n2.SUBTRACTION\n\n3.MULTIPLICATION\n\n4.DIVISION"; 
 cout<<"\n\nEnter your choice : "; 
 cin>>choice; 
 if(choice==1||choice==2||choice==3||choice==4) 
 { 
 cout<<"\n\nEnter the First Complex Number"; 
 c1.read(); 
 cout<<"\n\nEnter the Second Complex Number"; 
 c2.read(); 
 } 
 switch(choice) 
 { 
 case 1     : c3=c1+c2; 
            cout<<"\n\nSUM = "; 
            c3.display(); 
            break; 
 case 2     : c3=c1-c2; 
            cout<<"\n\nResult = "; 
            c3.display(); 
            break; 
 case 3 : c3=c1*c2; 
            cout<<"\n\nPRODUCT = "; 
            c3.display(); 
            break; 
 case 4     : c3=c1/c2; 
            cout<<"\n\nQOUTIENT = "; 
            c3.display(); 
            break; 
 default     : cout<<"\n\nUndefined Choice"; 
 } 
 cout<<"\n\nDo You Want to Continue?(1-Y,0-N)"; 
 cin>>cont; 
 }while(cont==1); 
 getch(); 
 }