hi guys... the same problem .getting WA.
could someone please tell me the cases , my code does not work for?
#include<iostream>
#include<stack>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
int main()
{
stack<int> str;
int weight[6]={12,0,0,0,16,1}; //FOR ASCII VALUES OF C,H AND O I AM USING (ASCII_VAL-67%8) TO MAP THE CHAR ONTO MASS.
char *formula=(char *)calloc(1,100),c;
int mass=0,nvar=0,i=0,val,index,p;
fgets(formula,100,stdin);
formula[strlen(formula)-1]='\0';
for(i=0;i<strlen(formula);i++)
{
if(formula[i]==' ')
continue;
if(formula[i]=='(')
{
str.push(mass);
str.push(-1);
mass=0;
}
else if(isdigit(formula[i]))
{
p=i-1;
while(formula[p]==' ')
p--;
if(formula[p]==')')
{
mass= mass*atoi(&formula[i]);
while(!str.empty() && str.top() != -1 )
{
mass= mass + str.top();
str.pop();
}
if(!str.empty())
str.pop();
}
else
{ p=i-1;
while(formula[p]==' ')
p--;
mass += weight[(formula[p]-67)%8]*(atoi(&formula[i])-1);
}
}
else if(formula[i]==')')
{
while(!str.empty() && str.top() != -1 )
{
mass= mass + str.top();
str.pop();
}
if(!str.empty())
str.pop();
}
else
{
index=(formula[i]-67);
mass =mass + weight[index%8];
}
}
while(!str.empty())
{
mass = mass+ str.top();
str.pop();
}
// cout<<"FORMULA:"<<formula<<" mass: "<<mass<<"i:"<<i<<endl;
cout<<mass;
return 0;
}