Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*****************************************************************************
- main.cpp
- Molecular Geometry
- Created by Hayden Lueck on 2014-10-27.
- Copyright (c) 2014 Hayden Lueck. All rights reserved.
- Program Purpose: Get molecular shape based off of 2 atoms given by input file
- molecules.txt
- main function:
- declares variables for use, sets up input and output files, checks they exist,
- goes through main loop while input file is not at EOF, does math based on given
- information, returns final output
- VAtom function:
- takes in string for atom V's symbol, and gives number based on that
- shape function:
- takes in integers for B and N and returns the shape for them
- input:
- an input file with the name molecules.txt, includes symbols for elements and
- a number on each line
- output:
- For each line it prints:
- "The geometrical shape of one [atom V] atom surrounded by [num b] [atom B] atoms is [shape]."
- limitations:
- needs very specific input from molecules.txt, and if it isn't formatted correctly the program
- won't work.
- *****************************************************************************/
- #include <iostream>
- #include <fstream>
- #include <string>
- /****** function protoyping ******/
- int VAtom(std::string x);
- std::string shape(int numB, int numN);
- /****** main function ******/
- int main() {
- std::string atomV, atomB, molecularShape; //declare strings
- int numV, numB, numE, numN; //declare integers
- std::ifstream inFile; //input file
- std::ofstream outFile; //output file
- inFile.open("molecules.txt"); //bind inFile to molecules.txt
- outFile.open("geometricalshapes.txt"); //bind outFile to geometricalshapes.txt
- /****** check files exist ******/
- if(!inFile){
- std::cout << "Cannot find input file molecules.txt Exiting..." << std::endl;
- return 1;
- }
- if(!outFile){
- std::cout << "Cannot find output file geometricalshapes.txt Exiting..." << std::endl;
- return 2;
- }
- /****** main loop ******/
- while(!inFile.eof()){ //while not end of file
- inFile >> atomV; //get string until whitespace
- inFile >> atomB; //get string until whitespace
- inFile >> numB; //get int until whitespace
- numV = VAtom(atomV); //get atom V's respective number
- numE = numV - numB; //set numE
- numN = numE / 2; //set numN
- molecularShape = shape(numB, numN); //get final molecular shape for output
- outFile << "The geometrical shape of one " << atomV << " atom surrounded by " << numB
- << ' ' << atomB << " atoms is " << molecularShape << "." << std::endl; //output string
- }
- return 0;
- }
- /****** VAtom function ******/
- int VAtom(std::string atomName){
- //check string and return number based off of it
- if(atomName == "Be")
- return 3;
- else if (atomName == "C")
- return 4;
- else if(atomName == "Si")
- return 4;
- else if(atomName == "N")
- return 5;
- else if(atomName == "P")
- return 5;
- else if(atomName == "As")
- return 5;
- else if(atomName == "O")
- return 6;
- else if(atomName == "S")
- return 6;
- else if(atomName == "Se")
- return 6;
- else if(atomName == "F")
- return 7;
- else if(atomName == "Cl")
- return 7;
- else if(atomName == "Br")
- return 7;
- else if(atomName == "I")
- return 7;
- else if(atomName == "Xe")
- return 8;
- else //if none of the names match up
- return 999; //return an extreme outlier (easy to tell if a mistake is made on respective line)
- }
- /****** shape function ******/
- std::string shape(int numB, int numN){
- //check both numbers and give shaped based off of them
- if(numB == 2 && numN == 0)
- return "linear";
- else if(numB == 2 && numN == 1)
- return "bent";
- else if(numB == 2 && numN == 2)
- return "bent";
- else if(numB == 2 && numN == 3)
- return "linear";
- else if(numB == 3 && numN == 0)
- return "trigonal planar";
- else if(numB == 3 && numN == 1)
- return "trigonal pyramidal";
- else if(numB == 3 && numN == 2)
- return "T-shaped";
- else if(numB == 4 && numN == 0)
- return "tetrahedral";
- else if(numB == 4 && numN == 1)
- return "seesaw";
- else if(numB == 4 && numN == 2)
- return "square planar";
- else if(numB == 5 && numN == 0)
- return "trigonal bipyramidal";
- else if(numB == 5 && numN == 1)
- return "square pyramidal";
- else if(numB == 6 && numN == 0)
- return "octahedral";
- else //if there isn't a case for the 2 numbers gives
- return "unknown"; //return "unknown"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement