Advertisement
Ashanmaril

Molecular Geometry

Oct 28th, 2014
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.80 KB | None | 0 0
  1. /*****************************************************************************
  2.  
  3.  main.cpp
  4.  Molecular Geometry
  5.  
  6.  Created by Hayden Lueck on 2014-10-27.
  7.  Copyright (c) 2014 Hayden Lueck. All rights reserved.
  8.  
  9.  Program Purpose: Get molecular shape based off of 2 atoms given by input file
  10.                     molecules.txt
  11.  
  12.  main function:
  13.     declares variables for use, sets up input and output files, checks they exist,
  14.     goes through main loop while input file is not at EOF, does math based on given
  15.     information, returns final output
  16.  
  17.  VAtom function:
  18.     takes in string for atom V's symbol, and gives number based on that
  19.  shape function:
  20.     takes in integers for B and N and returns the shape for them
  21.  
  22.  input:
  23.     an input file with the name molecules.txt, includes symbols for elements and
  24.     a number on each line
  25.  output:
  26.     For each line it prints:
  27.     "The geometrical shape of one [atom V] atom surrounded by [num b] [atom B] atoms is [shape]."
  28.  
  29.  limitations:
  30.     needs very specific input from molecules.txt, and if it isn't formatted correctly the program
  31.     won't work.
  32.  
  33.  *****************************************************************************/
  34.  
  35. #include <iostream>
  36. #include <fstream>
  37. #include <string>
  38.  
  39. /******  function protoyping ******/
  40. int VAtom(std::string x);
  41. std::string shape(int numB, int numN);
  42.  
  43. /****** main function ******/
  44. int main() {
  45.    
  46.     std::string atomV, atomB, molecularShape; //declare strings
  47.     int numV, numB, numE, numN; //declare integers
  48.     std::ifstream inFile; //input file
  49.     std::ofstream outFile; //output file
  50.    
  51.     inFile.open("molecules.txt"); //bind inFile to molecules.txt
  52.     outFile.open("geometricalshapes.txt"); //bind outFile to geometricalshapes.txt
  53.    
  54.     /****** check files exist ******/
  55.     if(!inFile){
  56.         std::cout << "Cannot find input file molecules.txt Exiting..." << std::endl;
  57.         return 1;
  58.     }
  59.     if(!outFile){
  60.         std::cout << "Cannot find output file geometricalshapes.txt Exiting..." << std::endl;
  61.         return 2;
  62.     }
  63.    
  64.     /****** main loop ******/
  65.     while(!inFile.eof()){ //while not end of file
  66.         inFile >> atomV; //get string until whitespace
  67.         inFile >> atomB; //get string until whitespace
  68.         inFile >> numB; //get int until whitespace
  69.        
  70.         numV = VAtom(atomV); //get atom V's respective number
  71.         numE = numV - numB; //set numE
  72.         numN = numE / 2; //set numN
  73.         molecularShape = shape(numB, numN); //get final molecular shape for output
  74.        
  75.         outFile << "The geometrical shape of one " << atomV << " atom surrounded by " << numB
  76.         << ' ' << atomB << " atoms is " << molecularShape << "." << std::endl; //output string
  77.        
  78.     }
  79.    
  80.     return 0;
  81. }
  82.  
  83. /****** VAtom function ******/
  84. int VAtom(std::string atomName){
  85.     //check string and return number based off of it
  86.     if(atomName == "Be")
  87.         return 3;
  88.     else if (atomName == "C")
  89.         return 4;
  90.     else if(atomName == "Si")
  91.         return 4;
  92.     else if(atomName == "N")
  93.         return 5;
  94.     else if(atomName == "P")
  95.         return 5;
  96.     else if(atomName == "As")
  97.         return 5;
  98.     else if(atomName == "O")
  99.         return 6;
  100.     else if(atomName == "S")
  101.         return 6;
  102.     else if(atomName == "Se")
  103.         return 6;
  104.     else if(atomName == "F")
  105.         return 7;
  106.     else if(atomName == "Cl")
  107.         return 7;
  108.     else if(atomName == "Br")
  109.         return 7;
  110.     else if(atomName == "I")
  111.         return 7;
  112.     else if(atomName == "Xe")
  113.         return 8;
  114.     else //if none of the names match up
  115.         return 999; //return an extreme outlier (easy to tell if a mistake is made on respective line)
  116.    
  117. }
  118.  
  119. /****** shape function ******/
  120. std::string shape(int numB, int numN){
  121.     //check both numbers and give shaped based off of them
  122.     if(numB == 2 && numN == 0)
  123.         return "linear";
  124.     else if(numB == 2 && numN == 1)
  125.         return "bent";
  126.     else if(numB == 2 && numN == 2)
  127.         return "bent";
  128.     else if(numB == 2 && numN == 3)
  129.         return "linear";
  130.     else if(numB == 3 && numN == 0)
  131.         return "trigonal planar";
  132.     else if(numB == 3 && numN == 1)
  133.         return "trigonal pyramidal";
  134.     else if(numB == 3 && numN == 2)
  135.         return "T-shaped";
  136.     else if(numB == 4 && numN == 0)
  137.         return "tetrahedral";
  138.     else if(numB == 4 && numN == 1)
  139.         return "seesaw";
  140.     else if(numB == 4 && numN == 2)
  141.         return "square planar";
  142.     else if(numB == 5 && numN == 0)
  143.         return "trigonal bipyramidal";
  144.     else if(numB == 5 && numN == 1)
  145.         return "square pyramidal";
  146.     else if(numB == 6 && numN == 0)
  147.         return "octahedral";
  148.     else //if there isn't a case for the 2 numbers gives
  149.         return "unknown"; //return "unknown"
  150.  
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement