MonsterAar
Programmer
I've written a program for calculating the molecular mass of any inputted molecule. I've constructed it using 118 if statements as you can see:
is there a nicer way to do this?
Code:
#!/usr/bin/perl -w
#perl2exe_include Math::BigInt
#perl2exe_include Math::BigInt::Calc
#molwt.pl : Calculates molecular weights
#Written by Luke Aaron
#Version B 30/01/08
use bignum;
print "\nFormat example: Water written H-2O-1\n";
print "Enter Compound: ";
$compound = <STDIN>;
chomp($compound);
#split compound into elements
@elements = split /(?=[A-Z])/, $compound;
#find the number of elements
$elenum = $#elements;
#initialise element counter
$num = 0;
#initialise molecular weight
$molwt = 0;
#calculation loop
while(1) {
#split element to element and number:
@elesplit = split(/-/, $elements[$num]);
#find what element it is and save atomic mass to $wt
if($elesplit[0] eq 'H') {
$wt = 1.00794;
}
if($elesplit[0] eq 'He') {
$wt = 4.002602;
}
if($elesplit[0] eq 'Li') {
$wt = 6.941;
}
if($elesplit[0] eq 'Be') {
$wt = 9.012182;
}
if($elesplit[0] eq 'B') {
$wt = 10.811;
}
if($elesplit[0] eq 'C') {
$wt = 12.0107;
}
if($elesplit[0] eq 'N') {
$wt = 14.0067;
}
if($elesplit[0] eq 'O') {
$wt = 15.9994;
}
if($elesplit[0] eq 'F') {
$wt = 18.9984032;
}
if($elesplit[0] eq 'Ne') {
$wt = 20.1797;
}
if($elesplit[0] eq 'Na') {
$wt = 22.98976928;
}
if($elesplit[0] eq 'Mg') {
$wt = 24.3050;
}
if($elesplit[0] eq 'Al') {
$wt = 26.9815386;
}
if($elesplit[0] eq 'Si') {
$wt = 28.0855;
}
if($elesplit[0] eq 'P') {
$wt = 30.973762;
}
if($elesplit[0] eq 'S') {
$wt = 32.065;
}
if($elesplit[0] eq 'Cl') {
$wt = 35.453;
}
if($elesplit[0] eq 'K') {
$wt = 39.0983;
}
if($elesplit[0] eq 'Ar') {
$wt = 39.948;
}
if($elesplit[0] eq 'Ca') {
$wt = 40.078;
}
if($elesplit[0] eq 'Sc') {
$wt = 44.955912;
}
if($elesplit[0] eq 'Ti') {
$wt = 47.867;
}
if($elesplit[0] eq 'V') {
$wt = 50.9415;
}
if($elesplit[0] eq 'Cr') {
$wt = 51.9961;
}
if($elesplit[0] eq 'Mn') {
$wt = 54.938045;
}
if($elesplit[0] eq 'Fe') {
$wt = 55.845;
}
if($elesplit[0] eq 'Ni') {
$wt = 58.6934;
}
if($elesplit[0] eq 'Co') {
$wt = 58.933195;
}
if($elesplit[0] eq 'Cu') {
$wt = 63.546;
}
if($elesplit[0] eq 'Zn') {
$wt = 65.409;
}
if($elesplit[0] eq 'Ga') {
$wt = 69.723;
}
if($elesplit[0] eq 'Ge') {
$wt = 72.64;
}
if($elesplit[0] eq 'As') {
$wt = 74.92160;
}
if($elesplit[0] eq 'Se') {
$wt = 78.96;
}
if($elesplit[0] eq 'Br') {
$wt = 79.904;
}
if($elesplit[0] eq 'Kr') {
$wt = 83.798;
}
if($elesplit[0] eq 'Rb') {
$wt = 85.4678;
}
if($elesplit[0] eq 'Sr') {
$wt = 97.62;
}
if($elesplit[0] eq 'Y') {
$wt = 88.90585;
}
if($elesplit[0] eq 'Zr') {
$wt = 91.224;
}
if($elesplit[0] eq 'Nb') {
$wt = 92.906;
}
if($elesplit[0] eq 'Mo') {
$wt = 95.94;
}
if($elesplit[0] eq 'Tc') {
$wt = 98;
}
if($elesplit[0] eq 'Ru') {
$wt = 101.07;
}
if($elesplit[0] eq 'Rh') {
$wt = 102.90550;
}
if($elesplit[0] eq 'Pd') {
$wt = 106.42;
}
if($elesplit[0] eq 'Ag') {
$wt = 107.8682;
}
if($elesplit[0] eq 'Cd') {
$wt = 112.411;
}
if($elesplit[0] eq 'In') {
$wt = 114.818;
}
if($elesplit[0] eq 'Sn') {
$wt = 118.710;
}
if($elesplit[0] eq 'Sb') {
$wt = 121.760;
}
if($elesplit[0] eq 'Te') {
$wt = 127.60;
}
if($elesplit[0] eq 'I') {
$wt = 126.90447;
}
if($elesplit[0] eq 'Xe') {
$wt = 131.293;
}
if($elesplit[0] eq 'Cs') {
$wt = 132.9054519;
}
if($elesplit[0] eq 'Ba') {
$wt = 137.327;
}
if($elesplit[0] eq 'La') {
$wt = 138.90547;
}
if($elesplit[0] eq 'Ce') {
$wt = 140.116;
}
if($elesplit[0] eq 'Pr') {
$wt = 140.90765;
}
if($elesplit[0] eq 'Nd') {
$wt = 144.242;
}
if($elesplit[0] eq 'Pm') {
$wt = 145;
}
if($elesplit[0] eq 'Sm') {
$wt = 150.36;
}
if($elesplit[0] eq 'Eu') {
$wt = 151.964;
}
if($elesplit[0] eq 'Gd') {
$wt = 157.25;
}
if($elesplit[0] eq 'Tb') {
$wt = 158.92535;
}
if($elesplit[0] eq 'Dy') {
$wt = 162.500;
}
if($elesplit[0] eq 'Ho') {
$wt = 164.930;
}
if($elesplit[0] eq 'Er') {
$wt = 167.259;
}
if($elesplit[0] eq 'Tm') {
$wt = 168.93421;
}
if($elesplit[0] eq 'Yb') {
$wt = 173.04;
}
if($elesplit[0] eq 'Lu') {
$wt = 174.967;
}
if($elesplit[0] eq 'Hf') {
$wt = 178.49;
}
if($elesplit[0] eq 'Ta') {
$wt = 180.94788;
}
if($elesplit[0] eq 'W') {
$wt = 183.84;
}
if($elesplit[0] eq 'Re') {
$wt = 186.207;
}
if($elesplit[0] eq 'Os') {
$wt = 190.23;
}
if($elesplit[0] eq 'Ir') {
$wt = 192.217;
}
if($elesplit[0] eq 'Pt') {
$wt = 195.084;
}
if($elesplit[0] eq 'Au') {
$wt = 196.966569;
}
if($elesplit[0] eq 'Hg') {
$wt = 200.59;
}
if($elesplit[0] eq 'Tl') {
$wt = 204.3833;
}
if($elesplit[0] eq 'Pb') {
$wt = 207.2;
}
if($elesplit[0] eq 'Bi') {
$wt = 208.98040;
}
if($elesplit[0] eq 'Po') {
$wt = 210;
}
if($elesplit[0] eq 'At') {
$wt = 210;
}
if($elesplit[0] eq 'Rn') {
$wt = 220;
}
if($elesplit[0] eq 'Fr') {
$wt = 223;
}
if($elesplit[0] eq 'Ra') {
$wt = 226;
}
if($elesplit[0] eq 'Ac') {
$wt = 227;
}
if($elesplit[0] eq 'Pa') {
$wt = 231.03588;
}
if($elesplit[0] eq 'Th') {
$wt = 232.03806;
}
if($elesplit[0] eq 'Np') {
$wt = 237;
}
if($elesplit[0] eq 'U') {
$wt = 238.02891;
}
if($elesplit[0] eq 'Am') {
$wt = 243;
}
if($elesplit[0] eq 'Pu') {
$wt = 244;
}
if($elesplit[0] eq 'Cm') {
$wt = 247;
}
if($elesplit[0] eq 'Bk') {
$wt = 247;
}
if($elesplit[0] eq 'Cf') {
$wt = 251;
}
if($elesplit[0] eq 'Es') {
$wt = 252;
}
if($elesplit[0] eq 'Fm') {
$wt = 257;
}
if($elesplit[0] eq 'Md') {
$wt = 258;
}
if($elesplit[0] eq 'No') {
$wt = 259;
}
if($elesplit[0] eq 'Lr') {
$wt = 262;
}
if($elesplit[0] eq 'Rf') {
$wt = 261;
}
if($elesplit[0] eq 'Db') {
$wt = 262;
}
if($elesplit[0] eq 'Sg') {
$wt = 266;
}
if($elesplit[0] eq 'Bh') {
$wt = 264;
}
if($elesplit[0] eq 'Hs') {
$wt = 277;
}
if($elesplit[0] eq 'Mt') {
$wt = 268;
}
if($elesplit[0] eq 'Ds') {
$wt = 271;
}
if($elesplit[0] eq 'Rg') {
$wt = 272;
}
if($elesplit[0] eq 'Uub') {
$wt = 285;
}
if($elesplit[0] eq 'Uut') {
$wt = 284;
}
if($elesplit[0] eq 'Uuq') {
$wt = 289;
}
if($elesplit[0] eq 'Uup') {
$wt = 288;
}
if($elesplit[0] eq 'Uuh') {
$wt = 292;
}
if($elesplit[0] eq 'Uuo') {
$wt = 294;
}
#perform element mass calcualtion
$ewt = $wt * $elesplit[1];
#add to molecular weight
$molwt = $molwt + $ewt;
if($num == $elenum) {
die "Molecular weight = $molwt\n";
}
$num = $num+1;
}
is there a nicer way to do this?