I have written this parsing algorithm in c++ and compiled in visual studio.On runtime after giving the value it gives an assertion error.
The logic is correct bcuz ive dry runned it but i think there's a problem with memory allocation.I need urgent help.Please.
// sas.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
const int LEN=80;
const int MAX=40;
//////////////////////////////////////////////////////////////////////////////
class Type
{
public:
virtual float getnumber()=0;
virtual char getoperator()=0;
virtual int get()=0;
};
/////////////////////////////////////////////////////////////////////////////
class Operator : public Type
{
private:
char oper;
int d;
public:
Operator(char p,int y):oper(p),d(y){}
char getoperator();
float getnumber(){ return NULL; }
int get(){return d;}
};
char Operator::getoperator()
{
return oper;
}
/////////////////////////////////////////////////////////////////////////////
class Number : public Type
{
private:
float fnum;
int d;
public:
Number(float f,int y):fnum(f),d(y){}
float getnumber();
char getoperator(){ return NULL; }
int get(){return d;}
};
float Number::getnumber()
{
return fnum;
}
//////////////////////////////////////////////////////////////////////////////
class Stack
{
private:
Type* st[MAX];
int top;
public:
Stack():top(0){}
void push(Type* var)
{ st[++top]=var; }
Type* pop()
{ return st[top--]; }
int gettop()
{ return top; }
};
//////////////////////////////////////////////////////////////////////////////
class express{
private:
char*pstr;
Stack s;
Type* arr[80];
int len;
public:
express(char* ptr):pstr(ptr){
len=static_cast<int>(strlen(ptr));}
void parse();
float solve();
};
void express::parse(){
int i=0,x=0;
char ch;
while(i<len){
if(ch>='0'&&ch<='9'){
char* a;
float f;
int j=i;
while(true){
strcat_s(a,20,&ch);
j++;
ch=pstr[j];
if(ch<'0'||ch>'9'){
f=static_cast<float>(atof(a));
i=j;
arr[x]=new Number(f,1);
break;
}
}
}
else{
arr[x]=new Operator(ch,2);
i++;
}
x++;
}
int d;
for(int k=0;k<x;k++){
d=arr[k]->get();
if(d==1){
s.push(arr[k]);
}
else{
char ch2=arr[k]->getoperator();
if(s.gettop()==1)
s.push(arr[k]);
else{
float lastval;
char lastop;
Type* a1=s.pop();
lastval=a1->getnumber();
Type* a2=s.pop();
lastop=a2->getoperator();
delete a2;
delete a1;
if((ch2=='*'||ch2=='/')&&(lastop=='+'||lastop=='-')){
s.push(&Operator(lastop,2));
s.push(&Number(lastval,1));
}
else{
Type* a3=s.pop();
float val1=a3->getnumber();
switch(lastop){
case'+':
s.push(&Number((val1+lastval),1));break;
case'-':
s.push(&Number((val1-lastval),1));break;
case'*':
s.push(&Number((val1*lastval),1));break;
case'/':
s.push(&Number((val1/lastval),1));break;
}
}
s.push(arr[k]);
}
}
}
}
float express::solve(){
float v1,v2;
char a3;
while(s.gettop()>1){
Type* n=s.pop();
Type* m=s.pop();
Type* o=s.pop();
v1=n->getnumber();
a3=m->getoperator();
v2=o->getnumber();
switch(a3){
case'+':
s.push(&Number((v1+v2),1));break;
case'-':
s.push(&Number((v1-v2),1));break;
case'*':
s.push(&Number((v1*v2),1));break;
case'/':
s.push(&Number((v1/v2),1));break;
}
delete o;
delete m;
delete n;
}
Type* x=s.pop();
v1=x->getnumber();
delete x;
return v1;
}
void main(){
char string[LEN];
cout<<"Enter Expression: ";
cin>>string;
express* e=new express(string);
e->parse();
cout<<"The value is: ";
cout<<e->solve();
}