Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Molecular Weights - C++ Sort & Search Element data found here: http://pastebin.c

ID: 3677004 • Letter: M

Question

Molecular Weights - C++ Sort & Search Element data found here: http://pastebin.com/f1D2zFtu Formula data found here: http://pastebin.com/cBmvaGd0 The Chemistry Dept. has asked you to develop a program that will calculate molecular weights of compounds given the chemical formula. For example H2O (water) would contain 2 hydrogen atoms weighting 1.008 and 1 oxygen weighting 15.999. The weight of water is 2 x 1.008 + 1 x 15.999 yielding 18.015 atomic weight. The input to you program will be in the following form: element ( number of atoms) element (number of atoms) ... for example H2O would be represented H(2)O. If the ()'s are not present, assume one (1) atom. Another example acetic acid would be represented CH(3)COOH or C(2)H(4)O(2). If an element has two letters symbol representation ie Silver is Ag, the second letter will be lower case indicating it is part of the symbol representation. A list of all the chemical elements can be found in the data file 'Element.dat'. There is one input element per line where the element name appears first followed by its atomic weight. Ex.Al 26.98 Sb 121.75 S 32.06 Ba 137.34 ... A second input file 'Formula.dat' contains the test formulae to use in testing your program. There will be one formula per line. For the output, print out the formula and its Molecular weight in a nice table form (ie line up the columns). Restrictions: You are to use an array of structure to hold the Symbol and its weight. Use BubbleSort to sort the element names alphabetically. Use a Binary search to look up elements in the element table. You are to use Functions/Procedures in your implementation. Format your output in a table form (ie headings and straight columns) Cannot use vectors.

Explanation / Answer

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
#include <sstream>
#include <cctype>

using namespace std;

struct Element
{
   string   eName;
   float   eWeight;
};

struct Formula
{
   char   element1;
   char   element1a;
   int   elAmt;
};
void Array_Zero(Element array[100]);
float Str_float(string eWeight);
void ZeroOutFAry(Formula fAry[100]);

int _tmain(int argc, _TCHAR* argv[])
{
   ifstream inFileE, inFileF;

   inFileE.open("file_name.txt");
   inFileF.open("File_name1.txt");

   Element       array[100];
   Formula       fAry[100];
   char       fry[300], inputCh, firstParen;
   int       i=0, j=0, c=0;
   string       str1, mFor, eAbb;
   int       firstDigit=0, elDigit=0;
   float       strWeight=0;
   string       eName;
   string       eWeight;
   int       len=0;
   char       strArr[20];

   if(!inFileE)
   {
       cout<<"Input File: Element.txt not open."<<endl;
       return 1;
   }

   if(!inFileF)
   {
       cout<<"Input File: Formula.txt not open."<<endl;
       return 1;
   }

   Array_Zero(array);
  
   while(inFileE)
   {
       eName=str1.substr(0,2);
       array[i].eName=eName;
      
       eWeight=str1.substr(3,10);
       str1.clear();
       strWeight=Str_float(eWeight);
       array[i].eWeight=strWeight;
       i++;
   }
   i--;
  
   ZeroOutFAry(fAry);
  
   while (inFileF)
   {
       getline(inFileF,str1);
       len=str1.length();
       int j=0, n=0;
       while( j<len)
       {
           int   pos=0, endPos=0;
           pos=str1.find(')');
           while(n<len)
           {
               inputCh=str1.at(n);
               if(isalpha(inputCh) && isupper(inputCh))
               {
                   fAry[j].element1=str1.at(n);
                   n++;
                   inputCh=str1.at(n);
               }

               if(isalpha(inputCh) && islower(inputCh))
               {
                   fAry[j].element1a=str1.at(n);
                   n++;
                   inputCh=str1.at(n);
               }
              
               if(ispunct(inputCh))
               {
                   n++;
                   inputCh=str1.at(n);
                   elDigit=(inputCh-'0');
               }

               if(isdigit(inputCh))
               {
                   fAry[j].elAmt=elDigit;
                   n++;
               }  
                   inputCh=str1.at(n);
              
               j++;

               if(iscntrl(inputCh))
               {
                   n++;
                   inputCh=str1.at(n);
                   j++;
               }
               n++;
           }
   }
}              
   system("pause");
   return 0;
  
}

void Array_Zero(Element array[100])
{  
   for(int   i=0; i<100; i++)
   {
       array[i].eWeight=0;
   }
}

void ZeroOutFAry(Formula fAry[100])
{  
   for(int   i=0; i<100; i++)
   {
       for(int i=0; i<9; i++)
       {
           fAry[i].elAmt=1;
       }
   }
  
}

float Str_float (string x)
{
   stringstream ss(x);
   float strWeight;
   ss>>strWeight;
  
   return strWeight;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote