Skip to main content

[vu-students] [**Virtual University Of Pakistan**Student Cafe] .::VULMSIT::.eNoxel CS301 Second Assignment Solution



#include<iostream>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<string>

#define MAX_SIZE 20

using namespace std;
template<class T>
class Stack{
private:
T item[MAX_SIZE];
int top;
public:
Stack(){
top = -1;
}
void push(T data){
if(!this->is_full())
item[++top] = data;
else{
cout<<"Stack Error"<<endl;
exit(10);
}
}
T pop(){
if(!this->is_empty())
return item[top--];
else{
cout<<"Stack is Empty"<<endl;
exit(11);
}
}

int size(){
return top+1;
}

bool is_empty(){
if(top==-1)
return true;
else
return false;
}

bool is_full(){
if(top==MAX_SIZE-1)
return true;
else
return false;
}

void display(){
for(int i=0; i<this->size(); i++){
cout<<item[i]<<" ";
}
cout<<endl;
}
T return_top(){
return item[top];
}
};

class Convert{
private:
bool num_flag;
bool tow_digit_flag;
public:
Convert();
string return_with_bracket(string infix);
void to_Postfix(string infix,char postfix[]);
bool prcd(char op1, char op2);
int isOperand(char op);
int isOperator(char op);
bool return_flag(){
return num_flag;
}
};

Convert::Convert(){
this->num_flag = false;
this->tow_digit_flag = false;
}
string Convert::return_with_bracket(string infix){
return("(" + infix + ")");
}

bool Convert::prcd(char op1, char op2){
if((op1=='+' || op1=='-' || op1=='*' || op1=='/') && op2=='(' )
return true;
if(op1=='+' && op2=='+')
return true;
if(op1=='-' && op2=='-')
return false;
if(op1=='-' && op2=='+')
return false;
if(op1=='+' && op2=='-')
return false;
if(op1=='/' && op2=='/')
return false;
if(op1=='/' && (op2=='-' || op2=='+'))
return true;
if(op1=='*' && (op2=='+' || op2=='-'))
return true;
if((op1 == '-' || op1 == '+') && (op2 =='*' || op2 == '/'))
return false;
if((op1=='$' || op1 == '+') && (op2 =='*' || op2 == '/' || op2=='-'))
return true;
if((op1 == '-' || op1 == '+' || op1 =='*' || op1 == '/')&& op2=='^')
return false;
if(op1 == '^' && ( op2 == '+' || op2 =='*' || op2 == '/' || op2=='-'))
return false;
}

int Convert::isOperand(char op){
return(op>= '0' && op <= '9');
}

int Convert::isOperator(char op){
return(op=='+' || op=='-' || op == '/' || op=='*' || op=='^');
}

void Convert::to_Postfix(string infix, char postfix[]){
int position, outpos=0;
char c;
int count = 0;
char temp;
char stacktop ;
Stack<char> stack;
for(position = 0; (c = infix[position])!='\0'; position++){
if(this->isOperand(c)){
postfix[outpos++] = c;
this->num_flag = true;
count++;
if(count>=2){
this->tow_digit_flag = true;
}
}else if(this->isOperator(c)){
count = 0;
if(isOperator(infix[position]) && isOperator(infix[position+1])){
cout<<"\aMissing argument in between "<<infix[position]<<" and "<<infix[position+1]
<<" in column "<< position+1<<endl;
exit(9);
}
if(this->prcd(c, stacktop)){
stacktop=stack.return_top();
stack.push(c);
stacktop = c;
}else{
while(true){
temp = stack.pop();
postfix[outpos++] =temp;
stacktop = stack.return_top();
if(prcd(c, stacktop) || stacktop=='(')
break;
}
stack.push(c);
stacktop = stack.return_top();
}
}
else if(c=='('){
count = 0;
stack.push(c);
stacktop = stack.return_top();
}else if(c==')'){
count = 0;
while(1){
if(stack.size()==0){
cout<<"Warning!! Number of ')' is greater than '('" <<endl;
exit(2);
}
temp = stack.pop();
if(temp!='('){
postfix[outpos++] = temp;
}else{
break;
}
}
stacktop =stack.return_top();
}
else{
cout<<"Invalid input";
exit(3);
}
if(infix[position]==')' && infix[position+1]=='('){
stack.push('*');
stacktop = stack.return_top();
}
}
if(stack.size()!=0){
cout<<"Warning!!Number of '(' is greater than ')'"<<endl;
// exit(6);
}
if(!this->return_flag()){
cout<<"You must Enter Numeric value for calculation"<<endl;
cout<<"This program cannot perform operations on variables";
exit(5);
}
if(this->tow_digit_flag){
cout<<"Sory! Althoug u may have entered right string"<<endl;
cout<<"this program is only for single digit operation"<<endl;
exit(8);
}
postfix[outpos] = '\0';
}

class Evaluate{
public:
double eval(char expr[], Convert &);
double oper(int symb, double op1, double op2);
};
double Evaluate::oper(int symb, double op1, double op2){
switch(symb){
case '+': return (op1 + op2);
case '-': return (op1 - op2);
case '*': return (op1 * op2);
case '/': return (op1 / op2);
case '^': return (pow(op1, op2));
}
}
double Evaluate::eval(char expr[],Convert &convert){
int c, position;
char temp1;
int count = 0;
double opnd1, opnd2, value;
Stack<double> stack;
for(position = 0; (c = expr[position])!='\0'; position++){
if(convert.isOperand(c)){
temp1 = double(c-'0');
stack.push(temp1);
}else{
opnd2 = stack.pop();
if(stack.size()==0){
cout<<"This program cannot process unary operation";
exit(1);
}
opnd1 = stack.pop();
value = oper(c, opnd1, opnd2);
stack.push(value);
}
}
if(stack.size()>=2){
cout<<"Sory! this program cannot calculate this"<<endl;
cout<<"Enter +, *, /, - or ^ between bracket"<<endl;
exit(4);
}

return (stack.pop());
}
int main(){
Convert convert;
Evaluate evaluate;
string bracketted_infix;
char infix[50], postfix[50];
char choice;
while(1){
cout<<"Enter operation: ";
cin>>infix;
cout<<endl;
cout<<"Entered operation: "<<infix<<endl;
bracketted_infix = convert.return_with_bracket(infix);
convert.to_Postfix(bracketted_infix, postfix);
cout<<"Equivalent Postfix operation: "<<postfix<<endl;
cout<<"RESULT: ";
cout<<evaluate.eval(postfix, convert);
cout<<"\nCalculate another operation?(y/n) ";
cin>>choice;
cout<<endl;
if(choice=='n')
break;
}
return 0;
}


--

     

Phr Yaad-e-Khuda Se Ghafil Iss Duniya Ki Hawass Mein

Iss Mukhtasir Si Zindgi Ka Ik Aor Din Beet Gaya...!!!

                <<<<<<<<<< sAd pAncHi >>>>>>>>>>


--
Virtual University of Pakistan*** IT n CS Blog
================================
http://www.geniusweb.tk
 
http://itncs.tk
 
 
You received this message because you are subscribed to the Google
Groups "vulms" group.
To post to this group, send email to vulmsit@googlegroups.com
To unsubscribe from this group, send email to
vulmsit+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/vulmsit?hl=en?hl=en


--
Posted By Sheroo to **Virtual University Of Pakistan**Student Cafe at 11/17/2011 09:32:00 AM

--
You received this message because you are subscribed to the Google
Groups "VU Students" group.
To post to this group, send email to vu-students@googlegroups.com
To unsubscribe from this group, send email to
vu-students+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/vu-students?hl=en_PK?hl=en

Comments

Popular posts from this blog

[vu-students] Urdu Love Letters ............ ahahahahahhahahah

  The One & Only .......... IRFAN. Italy. http://groups.yahoo.com/group/p_se_poetry   -- You received this message because you are subscribed to the Google Groups "VU Students" group. To post to this group, send email to vu-students@googlegroups.com To unsubscribe from this group, send email to vu-students+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/vu-students?hl=en_PK?hl=en

Re: ::: vuaskari.com ::: Fwd: Cancer treatment- Shocking news.

plz send me the soloution of Assignment MGT-603 and MKT-624 plzzzzzzzzz today last day i need urgent 2011/4/23 ∂υα ωαqαя < dua4566@gmail.com > ---------- Forwarded message ---------- From: Virtual Brain < mc090400181@gmail.com > Date: Sat, Apr 23, 2011 at 10:30 AM Subject: Cancer treatment- Shocking news. AS  one of my friend tell me all this PLEASE FORWARD THIS MAIL.  Dear All, One of my cousin got   lung cancer   few months back. That was really shocking to all his family members. Cancer diagnosed was of stage three. Immediately treatment was started from Pakistan most of the art hospital for cancer i.e. Shoukat Khanum Hospital in Lahore. But there were little chances to reverse the disease as it's already reached to stage 3. Than one day somebody told us about a person in Distt Nowshera who is treating cancer by herbs. We went there and he gave us some   herbal medicines   for ten days. It was really amazing when we saw an improvement in the condition of my...

Re: ::: vuaskari.com ::: MCS Students Group

please send me solution of mth 202 quiz 1 On Sun, May 11, 2014 at 8:51 PM, Muhammad Asif < itsvividmirror@gmail.com > wrote: cs 604 605 607 610 catring stock On Thu, May 8, 2014 at 4:32 PM, Malik Aslam < malik693012@gmail.com > wrote: I am also student of MCS.  On Wed, May 7, 2014 at 3:50 PM, Zadee Ali < abdullah.313.27@gmail.com > wrote: i have also these subjects. On Tue, May 6, 2014 at 1:26 PM, Shahzad Ali < saim2.vti@gmail.com > wrote: i am mcs student 3rd semester On Tue, Apr 29, 2014 at 12:02 PM, Jasmine Khan < jasmine.khan3939@gmail.com > wrote: I can help. On Apr 29, 2014 9:36 AM, "Nadeem Abbas (BP/Acc)" < nadeem.abbas@haleebfoods.com > wrote: > > Is there any or more students of the following subjects of MCS and want to do combine study and help each others? > >   > > CS304-OOP > > CS401-Assembly > > CS607-AI > > CS614-Data W/H > > ...