Thursday 6 December 2012

Sir Usman Data Structure Assignment No.1 Solution

First assignment given to students from sir usman is Stack Applications.

Program No.1 - Program Conversion from Decimal to Binary Numbers

Program No.2 - Program to perform Balancing Symbol .


#include "stack.h"
#include <stdio.h>
#include <string>
void cnvert()
{
Stack<int> obj(15);
int p;
cout<<"enter number you want to convert into binary"<<endl;
cin>>p;
while(!obj.Is_Full()&&p>0)
{
obj.Push(p%2);
p=p/2;
}

while(!obj.Is_Empty())
{
p=obj.pop();
cout<<p;
}
}

void balance_symbol()
{
int size;
string string;
cout<<"Enter Your Input ";
cin>>string;
size=string.length();
Stack<char> abbasi(size);
for(int i=0; i<size; i++)
{
if(string[i] == '(' || string[i] == '{' || string[i] == '[')
{
abbasi.Push(string[i]);
}
}

for(int i=0; i<size; i++)
{
if(string[i] == ')')
{
if(abbasi.Top() == '(' )
{
abbasi.pop();
if(abbasi.Is_Empty())
{
cout<<"Your Equation Is Balanced:"<<endl;
break;
}
}

else if(abbasi.Top()!= '(')
{
cout<<"Missing Brackets '(':"<<endl;
break;
}
}

else if(string[i] == '}')
{
if(abbasi.Top() == '{' )
{
abbasi.pop();
if(abbasi.Is_Empty())
{
cout<<"Your Equation Is Balanced:"<<endl;
break;
}
goto start;
start:
{

}
}
else if(abbasi.Top()!= '}')
{
cout<<"Missing Parentheses '}'"<<endl;
break;
}
}
else if(string[i] == ']')
{
if(abbasi.Top() == '[' )
{
abbasi.pop();
if(abbasi.Is_Empty())
{
cout<<"Your Equation Is Balanced:"<<endl;
break;
}
}
else if(abbasi.Top()!= '[')
{
cout<<"Missing Square Brackets '[':"<<endl;
break;
}

 
}


}

if(!abbasi.Is_Empty())
{
cout<<"Missing Parentheses '}'"<<endl;
}
system("pause");
}
void main()
{
int n;
cout<<"press '1' to convert decimal no. to binary"<<endl;
cout<<"press '2' to balancing symbol"<<endl;
cin>>n;
if(n==1)
{
cnvert();
}
else if (n==2)
{
balance_symbol();
}

system("pause");
}

0 comments:

Post a Comment