// C++.cpp : main project file.
#include "stdafx.h"
using namespace System;
#include "iostream"
#include "fstream"
#include "string"
#include "ctype.h"
#include "stdlib.h"
#include "math.h"
using namespace std;
using namespace IO;
void brackets(float[],char*,int,int&);
void operate(float[],char*,int);
void squeeze(float[],char*,int&,int);
void specfunc(float[],char*,int&,int,int);
void strconvert(float*&,char*&,int&,int&);
void strconvert(float*&nexpres,char*&newo,int &nicount,int &oicount)
{
// a:
ifstream istring;
ofstream ostring;
ostring.open("cal.dat",ios::out);
char ch;
nicount=0;
int njcount=1;
oicount=0;
//counts all the numbers and operators
do
{
cin.get(ch);
ostring<<ch;
if(isdigit(ch) || ch=='.')
{
nicount++;
cin.get(ch);
ostring<<ch;
for(int tempnj=2;isdigit(ch) || ch=='.' ;tempnj++)
{
if(tempnj>njcount)
{
njcount=tempnj;
}
cin.get(ch);
ostring<<ch;
}
}
if(isalpha(ch))
{
oicount++;
cin.get(ch);
ostring<<ch;
for(;isalpha(ch);)
{
cin.get(ch);
ostring<<ch;
}
}
if(isalnum(ch)==0 && ch!='\n' && ch!='.')
oicount++;
}
while(ch!='\n');
ostring.close();
char **oexpres=new char*[oicount+1]; //+1 for safety
for (int index=0;index<oicount;index++)
{
oexpres[index]=new char[5]; //max asin+1
}
char **numch=new char*[nicount+1];
for (int index=0;index<nicount;index++)
{
numch[index]=new char[njcount+1];
}
istring.open("cal.dat",ios::in);
//puts numbers and operators in seperate 2-d strings
int ni=0,oi=0;
do
{
istring.get(ch);
if(isdigit(ch) || ch=='.' && ni<nicount)
{
numch[ni][0]=ch;
istring.get(ch);
for(int nj=1;isdigit(ch) || ch=='.' && nj<njcount;nj++)
{
numch[ni][nj]=ch;
istring.get(ch);
}
ni++;
}
if(isalpha(ch))
{
oexpres[oi][0]=ch;
istring.get(ch);
for(int oj=1;isalpha(ch) && oj<4;oj++)
{
oexpres[oi][oj]=ch;
istring.get(ch);
}
oi++;
}
if(isalnum(ch)==0 && ch!='.' && ch!='\n' && oi<oicount)
{
oexpres[oi][0]=ch;
oi++;
}
}
while(ch!='\n');
//converts number string to float a string
nexpres=new float[nicount];
for(int index=0;index<nicount;index++)
{
nexpres[index]=atof(numch[index]);
}
newo=new char[oicount+1];
for(int index=0;index<oicount;index++)
{
if (isalpha(oexpres[index][0]))
{
if(strncmp(oexpres[index],"sin",3)==0)
{oexpres[index][0]='s';}
else if(strncmp(oexpres[index],"cos",3)==0)
{oexpres[index][0]='c';}
else if(strncmp(oexpres[index],"tan",3)==0)
{oexpres[index][0]='t';}
else if(strncmp(oexpres[index],"asin",4)==0)
{oexpres[index][0]='i';}
else if(strncmp(oexpres[index],"acos",4)==0)
{oexpres[index][0]='o';}
else if(strncmp(oexpres[index],"atan",4)==0)
{oexpres[index][0]='a';}
else if(strncmp(oexpres[index],"log",3)==0)
{oexpres[index][0]='g';}
else if(strncmp(oexpres[index],"ln",2)==0)
{oexpres[index][0]='n';}
else if(strncmp(oexpres[index],"exp",3)==0)
{oexpres[index][0]='e';}
else if(strncmp(oexpres[index],"abs",3)==0)
{oexpres[index][0]='b';}
else if(strncmp(oexpres[index],"sqrt",4)==0)
{oexpres[index][0]='q';}
}
newo[index]=oexpres[index][0];
}
}
void brackets(float nexpres[],char *oexpres,int nxindex,int &oxindex)
{
int bcount=0;
for(int i=0;i<oxindex;i++) //counts times to perform funtion
{
if (oexpres[i]==')')
bcount++;
}
for(int braks=0;braks<bcount;braks++)
{
int end=0;
for(;oexpres[end]!=')';end++)
{}
int begin=end;
for(;oexpres[begin]!='(';begin--)
{}
float *number=new float[end-begin];
char *operators=new char[end-begin]; // chars+ '/0'
int numpos=0;
int i;
for(int i=0;i<begin;i++) //for nested brackets
{
if (oexpres[i]=='(' || isalpha(oexpres[i]))
numpos++;
}
i=0;
for(int k=(begin-numpos);k<(end-numpos);k++) //load up number with bracket elements
number[i++]=nexpres[k];
for(int k=begin+1,i=0;k<end;k++)
operators[i++]=oexpres[k]; //load up with operators
operate(number,operators,(end-begin-1));
nexpres[begin-numpos]=number[0];
int j=(begin-numpos+1);
for(i=(end-numpos);i<nxindex;i++)
nexpres[j++]=nexpres[i];
for(i=end+1,j=begin;i<oxindex;i++)
oexpres[j++]=oexpres[i];
nxindex-=(end-begin-1);
oxindex-=(end-begin+1);
if(isalpha(oexpres[begin-1]))
specfunc(nexpres,oexpres,oxindex,begin,numpos);
}
}
void operate(float number[],char*operators,int oindex)
{
const char* orderof="^/*-+";
for(int k=0;k<5;k++) //changes operator according to precedence
{
for(int i=0;i<oindex;i++) //marches through the statement
{
if(operators[i]==orderof[k]) //checks for desired operator
{
switch(operators[i]) //conducts the operation
{
case '^':number[i]=pow(number[i],number[i+1]);break;
case '*':number[i]*=number[i+1];break;
case '/':number[i]/=number[i+1];break;
case '+':number[i]+=number[i+1];break;
case '-':number[i]-=number[i+1];break;
}
squeeze(number,operators,oindex,(i+1)); //squeezeout (i+1)
if(oindex==1)
i--;
if(i!=(oindex-1)) //re-test same index with new 'squeezed' values
i--;
}
}
}
}
void squeeze(float number[],char*operators,int &oindex,int squeezeout)
{//sqeezes remaining operation together, maintains integrity of loop
for(int j=squeezeout;j<oindex;j++)
{
number[j]=number[j+1];
operators[j-1]=operators[j];
}
oindex--;
}
void specfunc
(float nexpres[],char *oexpres,int &oxindex,int begin,int numpos)
{
switch(oexpres[begin-1])
{
case 's':nexpres[begin-numpos]=sin(nexpres[begin-numpos]);break;
case 'c':nexpres[begin-numpos]=cos(nexpres[begin-numpos]);break;
case 't':nexpres[begin-numpos]=tan(nexpres[begin-numpos]);break;
case 'i':nexpres[begin-numpos]=asin(nexpres[begin-numpos]);break;
case 'o':nexpres[begin-numpos]=acos(nexpres[begin-numpos]);break;
case 'a':nexpres[begin-numpos]=atan(nexpres[begin-numpos]);break;
case 'g':nexpres[begin-numpos]=log10(nexpres[begin-numpos]);break;
case 'n':nexpres[begin-numpos]=log(nexpres[begin-numpos]);break;
case 'e':nexpres[begin-numpos]=exp(nexpres[begin-numpos]);break;
case 'b':nexpres[begin-numpos]=abs(nexpres[begin-numpos]);break;
case 'q':nexpres[begin-numpos]=sqrt(nexpres[begin-numpos]);break;
}
for(int j=begin;j<oxindex;j++)
oexpres[j-1]=oexpres[j];
oxindex--;
}
void main()
{
float *nexpres;
char *oexpres;
int nxindex,oxindex;
cout<<"//all calculations are in radians"<<endl<<endl;
cout<<"calculate->";
strconvert(nexpres,oexpres,nxindex,oxindex);
brackets(nexpres,oexpres,nxindex,oxindex);
operate(nexpres,oexpres,oxindex);
cout<<"="<<nexpres[0];
//to view all elements
//for(int i=0;i<nxindex;i++)
//cout<<nexpres[i]<<" ";
//cout<<endl;
//for(int i=0;i<oxindex;i++)
//cout<<oexpres[i];
//cout<<endl;
cin.ignore(128, '\n');
}