You are on page 1of 8

#include <iostream>

#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <sstream>
#include <math.h>
#define PI 3.1415926535897932
#define e 2.71828182845904523
using namespace std;
class Calculator
{
public:
double add(string exp);
double subtract(string exp,char sign);
double multiply(string exp);
double divide(string exp);
void repadd(string &exp);
void repsubtract(string &exp);
void repmultiply(string &exp);
void repdivide(string &exp);
void reptrigo(string &exp);
void repexp(string &exp);
void replog(string &exp);
};
double Calculator::add(string exp)
{
double val=0;
for(unsigned int i=0;i<exp.length();i++)
{
if(exp.at(i)=='+')
val=atof(exp.substr(0,i+1).c_str()) + atof(exp.substr(i+1).c_str());
}
return val;
}
double Calculator::subtract(string exp,char sign)
{
double val=0;
for(unsigned int i=0;i<exp.length();i++)
{
if(sign=='+')
{
if(exp.at(i)=='-')
val=atof(exp.substr(0,i+1).c_str()) - atof(exp.substr(i+1).c_str());
}
else if(sign=='-')
{
if(exp.at(i)=='+')
val=-atof(exp.substr(0,i+1).c_str()) + atof(exp.substr(i+1).c_str());
}
}
return val;
}
double Calculator::multiply(string exp)
{

double val=0;
for(unsigned int i=0;i<exp.length();i++)
{
if(exp.at(i)=='*')
val=atof(exp.substr(0,i+1).c_str()) * atof(exp.substr(i+1).c_str());
}
return val;
}
double Calculator::divide(string exp)
{
double val=0;
for(unsigned int i=0;i<exp.length();i++)
{
if(exp.at(i)=='/')
val =atof(exp.substr(0,i+1).c_str()) / atof(exp.substr(i+1).c_str());
}
return val;
}
void Calculator::repadd(string &exp)
{
for(unsigned int i=0;i<exp.length();i++)
{
if(exp.at(i)=='+')
{
int fpos=i,bpos=i;
char sgn='+';

//for ADDITION

for(int j=i;j>0;j--)
{
if(isdigit(exp.at(j-1))||exp.at(j-1)=='.')
fpos--;
else
break;
}
for(unsigned int j=i;j<exp.length()-1;j++)
{
if(isdigit(exp.at(j+1))||exp.at(j+1)=='.')
bpos++;
else
break;
}
if(fpos==0||exp.at(fpos-1)=='+'||isdigit(exp.at(fpos-1)))
sgn='+';
else sgn='-';
ostringstream ss;
if(sgn=='+')
{
ss<<add(exp.substr(fpos,bpos-fpos+1));
exp.replace(fpos,bpos-fpos+1,ss.str());
}
else
{
if(fpos==1)
{
ss<<subtract(exp.substr(fpos,bpos-fpos+1),'-');
exp.replace(fpos-1,bpos-fpos+2,ss.str());

}
else
if(subtract(exp.substr(fpos,bpos-fpos+1),'-')<0)
{
ss<<subtract(exp.substr(fpos,bpos-fpos+1),'-');
exp.replace(fpos-1,bpos-fpos+2,ss.str());
}
else
{
ss<<subtract(exp.substr(fpos,bpos-fpos+1),'-');
exp.replace(fpos-1,bpos-fpos+2,"+"+ss.str());
}
}
i=0;
}
}
}
void Calculator::repsubtract(string &exp)
{
for(unsigned int i=0;i<exp.length();i++)
{
if(exp.at(i)=='-')
{
int fpos=i,bpos=i;

// for SUBTRACTION

for(int j=i;j>0;j--)
{
if(isdigit(exp.at(j-1))||exp.at(j-1)=='.')
fpos--;
else
break;
}
for(unsigned int j=i;j<exp.length()-1;j++)
{
if(isdigit(exp.at(j+1))||exp.at(j+1)=='.')
bpos++;
else
break;
}
ostringstream ss;
ss<<subtract(exp.substr(fpos,bpos-fpos+1),'+');
exp.replace(fpos,bpos-fpos+1,ss.str());
i=0;
}
}
}
void Calculator::repmultiply(string &exp)
{
for(unsigned int i=0;i<exp.length();i++)
{
if(exp.at(i)=='*')
{
int fpos=i,bpos=i;

// for MULTIPLICATION

for(int j=i;j>0;j--)
{
if(isdigit(exp.at(j-1))||exp.at(j-1)=='.')

fpos--;
else
break;
}
for(unsigned int j=i;j<exp.length()-1;j++)
{
if(isdigit(exp.at(j+1))||exp.at(j+1)=='.')
bpos++;
else
break;
}
ostringstream ss;
ss<<multiply(exp.substr(fpos,bpos-fpos+1));
exp.replace(fpos,bpos-fpos+1,ss.str());
i=0;
}
}
}
void Calculator::repdivide(string &exp)
{
for(unsigned int i=0;i<exp.length();i++)
{
if(exp.at(i)=='/')
{
int fpos=i,bpos=i;

// for MULTIPLICATION

for(int j=i;j>0;j--)
{
if(isdigit(exp.at(j-1))||exp.at(j-1)=='.')
fpos--;
else
break;
}
for(unsigned int j=i;j<exp.length()-1;j++)
{
if(isdigit(exp.at(j+1))||exp.at(j+1)=='.')
bpos++;
else
break;
}
ostringstream ss;
ss<<divide(exp.substr(fpos,bpos-fpos+1));
exp.replace(fpos,bpos-fpos+1,ss.str());
i=0;
}
}
}
void Calculator::reptrigo(string &exp)
{
for(unsigned int i=0;i<exp.length();i++)
{
if(exp.substr(i,3)=="sin")
{
int bpos=i+3;
for(unsigned int j=bpos;j<exp.length()-1;j++)
{

if(isdigit(exp.at(j+1))||exp.at(j+1)=='.')
bpos++;
else
break;
}
ostringstream ss;
ss<<sin(atof(exp.substr(i+3,i+3-bpos-1).c_str())*PI/180);
exp.replace(i,bpos-i+1,ss.str());
i=0;
}
else if(exp.substr(i,5)=="cosec")
{
int bpos=i+5;
for(unsigned int j=bpos;j<exp.length()-1;j++)
{
if(isdigit(exp.at(j+1))||exp.at(j+1)=='.')
bpos++;
else
break;
}
ostringstream ss;
ss<<1/sin(atof(exp.substr(i+5,i+5-bpos-1).c_str())*PI/180);
exp.replace(i,bpos-i+1,ss.str());
i=0;
}
else if(exp.substr(i,3)=="cos")
{
int bpos=i+3;
for(unsigned int j=bpos;j<exp.length()-1;j++)
{
if(isdigit(exp.at(j+1))||exp.at(j+1)=='.')
bpos++;
else
break;
}
ostringstream ss;
ss<<cos(atof(exp.substr(i+3,i+3-bpos-1).c_str())*PI/180);
exp.replace(i,bpos-i+1,ss.str());
i=0;
}
else if(exp.substr(i,3)=="tan")
{
int bpos=i+3;
for(unsigned int j=bpos;j<exp.length()-1;j++)
{
if(isdigit(exp.at(j+1))||exp.at(j+1)=='.')
bpos++;
else
break;
}
ostringstream ss;
ss<<tan(atof(exp.substr(i+3,i+3-bpos-1).c_str())*PI/180);
exp.replace(i,bpos-i+1,ss.str());

i=0;
}
else if(exp.substr(i,3)=="sec")
{
int bpos=i+3;
for(unsigned int j=bpos;j<exp.length()-1;j++)
{
if(isdigit(exp.at(j+1))||exp.at(j+1)=='.')
bpos++;
else
break;
}
ostringstream ss;
ss<<1/cos(atof(exp.substr(i+3,i+3-bpos-1).c_str())*PI/180);
exp.replace(i,bpos-i+1,ss.str());
i=0;
}
else if(exp.substr(i,3)=="cot")
{
int bpos=i+3;
for(unsigned int j=bpos;j<exp.length()-1;j++)
{
if(isdigit(exp.at(j+1))||exp.at(j+1)=='.')
bpos++;
else
break;
}
ostringstream ss;
ss<<1/tan(atof(exp.substr(i+3,i+3-bpos-1).c_str())*PI/180);
exp.replace(i,bpos-i+1,ss.str());
i=0;
}
}
}
void Calculator::repexp(string &exp)
{
for(unsigned int i=0;i<exp.length();i++)
{
if(exp.at(i)=='^')
{
int fpos=i,bpos=i;
for(int j=i;j>0;j--)
{
if(isdigit(exp.at(j-1))||exp.at(j-1)=='.')
fpos--;
else
break;
}
for(unsigned int j=i;j<exp.length()-1;j++)
{
if(isdigit(exp.at(j+1))||exp.at(j+1)=='.')
bpos++;
else
break;

}
ostringstream ss;
if(exp.at(i-1)=='e')
{
ss<<pow(e,atof(exp.substr(i+1,i+1-bpos+2).c_str()));
exp.replace(i-1,i-bpos,ss.str());
}
else
{
ss<<pow(atof(exp.substr(fpos,i-fpos).c_str()),atof(exp.substr(i+1,bpos-i).c_
str()));
exp.replace(fpos,bpos-fpos+1,ss.str());
}
i=0;
}
}
}
void Calculator::replog(string &exp)
{
for(unsigned int i=0;i<exp.length();i++)
{
if(exp.substr(i,3)=="log")
{
int bpos=i+3;
for(unsigned int j=bpos;j<exp.length()-1;j++)
{
if(isdigit(exp.at(j+1))||exp.at(j+1)=='.')
bpos++;
else
break;
}
ostringstream ss;
ss<<log10(atof(exp.substr(i+3,i+3-bpos-1).c_str()));
exp.replace(i,bpos-i+1,ss.str());
i=0;
}
else
if(exp.substr(i,2)=="ln")
{
int bpos=i+2;
for(unsigned int j=bpos;j<exp.length()-1;j++)
{
if(isdigit(exp.at(j+1))||exp.at(j+1)=='.')
bpos++;
else
break;
}
ostringstream ss;
ss<<log(atof(exp.substr(i+2,i+2-bpos-1).c_str()));
exp.replace(i,bpos-i+1,ss.str());
i=0;
}
}
}

int main()
{
string es;
Calculator calc;
cin>>es;
calc.replog(es);
calc.reptrigo(es);
calc.repexp(es);
calc.repdivide(es);
calc.repmultiply(es);
calc.repadd(es);
calc.repsubtract(es);
cout<<es;
return 0;
}

You might also like