MMASS - Mass of Molecule
Tác giả: ladpro98
Ngôn ngữ: Pascal
program mmass; //VNOI
const maxN = 10000;
fi='';
fo='';
type mystack = record
items: array[0..maxN] of longint;
top:longint;
end;
var stack:mystack;
s:ansistring;
ch,num:set of char;
inp,oup:text;
i,temp:longint;
function toVal(c:char):longint;
begin
if c = 'C' then exit(12)
else if c = 'O' then exit(16)
else if c = 'H' then exit(1);
end;
function toNum(c:char):longint;
begin
exit(ord(c)-48);
end;
function isEmpty:boolean;
begin
exit(stack.top=0);
end;
function pop:longint;
begin
dec(stack.top);
exit(stack.items[stack.top+1]);
end;
function get:longint;
begin
exit(stack.items[stack.top]);
end;
procedure push(val:longint);
begin
inc(stack.top);
stack.items[stack.top]:=val;
end;
procedure setTop(val:longint);
begin
stack.items[stack.top]:=val;
end;
procedure input;
begin
assign(inp,fi);
reset(inp);
readln(inp,s);
close(inp);
end;
begin
input;
s:=s+'0';
ch:=['C','H','O'];
num:=['2','3','4','5','6','7','8','9'];
stack.top:=0;
i:=1;
while i<length(s) do
begin
if (s[i] in ch) and (s[i+1] in num) then
begin
setTop(get+toVal(s[i])*toNum(s[i+1]));
inc(i,2);
end
else if s[i] in ch then
begin
setTop(get+toVal(s[i]));
inc(i);
end
else if s[i] = '(' then
begin
push(0);
inc(i);
end
else if (s[i] = ')') and (s[i+1] in num) then
begin
temp:=pop*toNum(s[i+1]);
setTop(get+temp);
inc(i,2);
end
else if (s[i] = ')') then
begin
setTop(pop+get);
inc(i);
end;
end;
assign(oup,fo);
rewrite(oup);
write(oup,get);
close(oup);
end.