Source code
#include <bits/stdc++.h>
using namespace std;
string prefix;
int op=0,eql=0,brkt=0;
int ok=0;
vector<int> dataSaved;
vector<string> charSaved;
int priority(char c)
{
if(c =='+' || c=='-')
{
return 1;
}
else if(c=='*' || c=='/')
{
return 2;
}
else if( c=='^')
{
return 3;
}
else return 0;
}
void countAll(string str)
{
for(int i=0; i<str.size(); i++)
{
if(str[i]=='+' || str[i]=='-' || str[i]=='*' || str[i]=='/')
{
op++;
}
if(str[i]=='=')
{
eql++;
}
if(str[i]=='(' || str[i]==')')
{
brkt++;
}
}
}
int calculate(int a, int b, char oprt)
{
if(oprt=='+') return a+b;
else if(oprt=='-') return a-b;
else if(oprt=='*') return a*b;
else if(oprt=='/') return a/b;
else if(oprt=='^') return pow(a,b);
}
void save(string str, int data)
{
int update=0;
for(int i=0; i<charSaved.size(); i++)
{
if(str==charSaved[i])
{
dataSaved[i] = data;
update=1;
}
}
if(update==0)
{
charSaved.push_back(str);
dataSaved.push_back(data);
}
}
int searchVar(string str)
{
for(int i=0; i<charSaved.size(); i++)
{
if(charSaved[i]==str)
{
return dataSaved[i];
}
}
cout<<"No Var Saved for : "<<str<<endl;
return -1;
}
int eveluate(string str)
{
char c;
int v=0,lastbrkt=0;
stack <int> value;
stack<char> oprt;
cout<<"Do you want to save the data: "<<str<<" (y/n)"<<endl;
cin>>c;
ofstream file;
file.open("outs.txt",ios_base::app);
countAll(str);
if(c=='y')
{
if(op==0 || (op==0 && eql==1))
{
file<<str<<endl;
file.close();
if(eql==1)
{
string var;
int i=0;
for(i=0; str[i]!='='; i++)
{
var +=str[i];
}
int val=0;
for(i++; i<str.size()-1; i++)
{
val = (val*10) + (str[i]-'0');
}
save(var,val);
}
return 0;
}
}
else if(c=='n')
{
file<<"No Data Saved"<<endl;
file.close();
}
string str1;
string var;
if(eql==1)
{
int i=0;
for(i=0; str[i]!='='; i++)
{
var +=str[i];
}
for(i++; i<str.size(); i++)
{
str1 += str[i];
}
}
else
{
str1=str;
}
while(!value.empty())
{
value.pop();
}
while(!oprt.empty())
{
oprt.pop();
}
for(int i = 0; i < str1.length(); i++)
{
if(str1[i] == ' ')
{
continue;
}
else if(str1[i] == '(')
{
oprt.push(str1[i]);
}
else if(isdigit(str1[i]))
{
int val = 0;
while(i < str1.length() && (isdigit(str1[i])==1))
{
val = (val*10) + (str1[i]-'0');
i++;
if(str1[i]=='+' || str1[i]=='-'|| str1[i]=='*' || str1[i]=='/' || str1[i]=='^' || str1[i]==')' )
{
i--;
break;
}
}
value.push(val);
}
else if(str1[i]>='a' && str1[i]<='z')
{
string s;
v++;
s +=str1[i];
for(i++;i<str1.size();i++)
{
if((str1[i]>='a' && str1[i]<='z'))
{
s+=str1[i];
}
else
{
i--;
break;
}
}
int a = searchVar(s);
if(a==-1)
{
return 0;
}
else
{
str1[i]= (char)a;
value.push(a);
}
}
else if(str1[i] == ')')
{
lastbrkt=i;
while(!oprt.empty() && oprt.top() != '(')
{
int val2 = value.top();
value.pop();
int val1 = value.top();
value.pop();
char op = oprt.top();
oprt.pop();
value.push(calculate(val1, val2, op));
}
oprt.pop();
}
else
{
while(!oprt.empty() && priority(oprt.top())>= priority(str1[i]))
{
int val2 = value.top();
value.pop();
int val1 = value.top();
value.pop();
char op = oprt.top();
oprt.pop();
value.push(calculate(val1, val2, op));
}
oprt.push(str1[i]);
}
}
if(v>1)
{
while(!oprt.empty())
{
char op = oprt.top();
oprt.pop();
}
}
if(brkt<1)
{
while(!oprt.empty())
{
int val2 = value.top();
value.pop();
int val1=value.top();
value.pop();
char op = oprt.top();
oprt.pop();
value.push(calculate(val1, val2, op));
}
}
if(lastbrkt!=str1.size()-2)
{
while(!oprt.empty())
{
int val2 = value.top();
value.pop();
int val1=value.top();
value.pop();
char op = oprt.top();
oprt.pop();
value.push(calculate(val1, val2, op));
}
}
int ans= value.top();
if(eql==1)
{
save(var,ans);
}
cout<<"Ans: "<<ans<<endl;
if(c=='y')
{
if(eql==1)
{
file<<var <<'='<<ans<<endl;
}
else
{
file<<ans<<endl;
}
file.close();
return 0;
}
else if(c=='n')
{
file<<"No Data Saved"<<endl;
file.close();
}
}
int findError(string str)
{
if(str[str.size()-1] !=';')
{
cout<<"Messing ';' For: "<<str<<endl;
return 0;
}
for(int i=0; i<str.size(); i++)
{
if((str[i]=='+' || str[i]=='-'|| str[i]=='*' || str[i]=='/') && (str[i-1]=='+' || str[i-1]=='-'|| str[i-1]=='*' || str[i-1]=='/') )
{
cout<<"Double Operator For: "<<str<<endl;
return 0;
}
if(str[i]==')')
{
if(str[i+1]!=';')
{
if(str[i+1]=='+' || str[i+1]=='-' || str[i+1]=='*' || str[i+1]=='/')
{
continue;
}
else
{
cout<<"Wrong input formate For: "<<str[i+1]<<endl;
return 0;
}
}
if(str[i-1]=='+' || str[i-1]=='-' || str[i-1]=='*' || str[i-1]=='/')
{
cout<<"Wrong input formate For: "<<str<<endl;
return 0;
}
}
if(str[i]=='(')
{
if(i!=0)
{
if(str[i-1]=='+' || str[i-1]=='-' || str[i-1]=='*' || str[i-1]=='/')
{
continue;
}
else
{
cout<<"Wrong input formate For: "<<str<<endl;
return 0;
}
}
if(str[i+1]=='+' || str[i+1]=='-' || str[i+1]=='*' || str[i+1]=='/')
{
cout<<"Wrong input formate For: "<<str<<endl;
return 0;
}
}
}
if(str[str.size()-2]=='+' || str[str.size()-2]=='-' || str[str.size()-2]=='*' || str[str.size()-2]=='/' || str[0]=='+' || str[0]=='-' || str[0]=='*' || str[0]=='/')
{
cout<<"Wrong input formate For: "<<str<<endl;
return 0;
}
ok=1;
}
int main()
{
ifstream file;
file.open("ins.txt");
while(!file.eof())
{
string data;
getline(file, data);
findError(data);
if(ok==1)
{
data[data.size()-1]='