Question

Write a C++ code to prase the records list below--it's from a text file. Example of...

Write a C++ code to prase the records list below--it's from a text file. Example of the output

First-line output: 1. Polylactic acid,8244, 541-25-3,C2H2Cl3As, 207.32;

Second-line output: 2. LEWISITE, 6393,541-25-3, C2H2AsCl3,(C3-H6-O3)x, 1250.032;

The User will be asked to input--including the semicolon- "NAME:" "ID:" "RO:" "Formula:" "Molar mass:" The Formula can you two data.

Record 1 & 2 are below-- as they appear in the text file.

Thanks!  

1. record

NAME: Polylactic acid

The monomer name is typically from fermented plant starch such as from corn, cassava, sugarcane or sugar beet pulp.

It was once manufactured in the U.S

ID: 8244

RO: 26100-51-6

Formula: C2H2AsCl3 [Lewis, R.J. Sr. (ed) Sax's Dangerous Properties of Industrial
Materials. 11th Edition. Wiley-Interscience, Wiley & Sons, Inc.
Hoboken, NJ. 2004., p. 768]

Formula: C2H2Cl3As

Molar mass: 207.32 g/mol


2. record

NAME: LEWISITE

Lewisite is a type of chemical warfare agent. This kind of agent is called a vesicant or blistering agent, because it causes blistering of the skin and mucous membranes on contact.

ID: 6393

RO: 541-25-3

Formula: C2H2AsCl3

Formula: (C3-H6-O3)x

Molar mass: 1250.032 g/mol

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ code for the above problem is given along with explanation in comments:

------------------------------------------------------------------CODE-----------------------------------------------------------

#include<iostream>
#include<fstream>
#include<vector>
#include<sstream>
using namespace std;
vector<string> split(string s,char del);
int main()
{
   ifstream fin;
   string line,file;
   cout<<"Enter Input File:";
   cin>>file;
   //Open the Given file
   fin.open(file);
   vector<string> parsed;
   vector<vector<string>> res;
   //Read the file untill file ends
  
   while(fin)
   {
       getline(fin,line);
       if(line=="")
           continue;
       vector<string> v=split(line,' ');
       //First we split the line with : as delimiter
       //and check the first string if it is equal to NAME,ID,RO
       //then we append it to the vector
       if(v[0]=="NAME:" || v[0]=="ID:" || v[0]=="RO:"){
           parsed.push_back(v[1]);
       }
       //If the first word is Formula then we split it with ' '(space)
       //then we append second word to the vector parsed
       if(v[0]=="Formula:" && v.size()==2){
           parsed.push_back(v[1]);

       }
       if(v[0]=="Molar"){
           parsed.push_back(v[2]);
           res.push_back(parsed);
           parsed.clear();
       }

   }
   //Printing the output into the given
   //File and save it
   int k=1;
   cout<<"Enter Output File to Save:";
   cin>>file;
   ofstream out;
   out.open(file);
   for(vector<string> v:res){
           out<<k<<".";
       for(int i=0;i<v.size()-1;i++){
           out<<v[i]<<",";
       }
       out<<v[v.size()-1]<<";"<<endl;
       k++;
   }

}

//Splits the given string with the given delimiter
vector<string> split(string s,char del)
{
   vector <string> v;
   stringstream ss(s);
   string temp;
   while(getline(ss,temp,del))
   {
       v.push_back(temp);
   }
   return v;
}

-------------------------------------------------------------------END-------------------------------------------------------------

Code in sublime text:

sample.txt looks like:

Output:

Add a comment
Know the answer?
Add Answer to:
Write a C++ code to prase the records list below--it's from a text file. Example of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT