1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
#include <string>
#include <ctype.h>
#include <iostream>
#include <iomanip>
using namespace std;
struct Element
{
public:
double atomicWeight;
string name;
string symbol;
int atomicNumber;
Element()
{
atomicWeight = 0.0;
name = " ";
symbol = " ";
atomicNumber = 0;
};
Element(double Weight, string Name, string Symbol, int AtomicNum)
{
atomicWeight = Weight;
name = Name;
symbol = Symbol;
atomicNumber = AtomicNum;
}
};
//Prototypes
void DisplayElements(Element *ChemicalElements, int size);
void bubbleSort_Name(Element *ChemicalElements, int size, string name);
void bubbleSort_Symbol(string name, Element *ChemicalElements, int size);
void bubbleSort_AtomicWeight(Element *ChemicalElements, int size, double atomicweight);
void bubbleSort_Number(Element *ChemicalElements, int size, int AtominNum);
void ClearScreen();
int main()
{
int select = 0;
char doAgain;
const int SIZE = 10;
//Pointer to Element
Element ChemicalElements;
Element *pChemicalElements = new Element[10];
pChemicalElements = &ChemicalElements;
pChemicalElements->atomicNumber = 1;
pChemicalElements->name = "Hydrogen";
pChemicalElements->atomicWeight = 1.00790;
pChemicalElements->symbol = "H";
pChemicalElements++;
//took out other 9 that are initialized here
pChemicalElements = &ChemicalElements;
do
{
do
{
ClearScreen(); //clear from 'Y' || 'y'
//atomic weight, name, symbol, and atomic number
cout << "How would you like data sorted?" << endl
<< "1. Atomic weight " << endl
<< "2. Name" << endl
<< "3. Symbol " << endl
<< "4. Atomic Number " << endl
<< endl;
cin >> select;
ClearScreen();
}while (select < 1 || select > 4);
ClearScreen();
if (select == 1)
bubbleSort_AtomicWeight(&ChemicalElements, SIZE, 0.0);
else if (select == 2)
bubbleSort_Name(&ChemicalElements, SIZE, " ");
else if (select == 3)
bubbleSort_Symbol(" ", &ChemicalElements, SIZE);
else if (select == 4)
bubbleSort_Number(&ChemicalElements, SIZE, 0);
cout << "\nOriginal Element List: \n"
<< "Weight Name Sym Num \n"
<< "___________________________\n";
DisplayElements(&ChemicalElements, SIZE);
cout << "\nSorted Element List: \n"
<< "Weight Name Sym Num \n"
<< "___________________________\n";
DisplayElements(&ChemicalElements, SIZE);
cout << "\nSort another? (Y/N) ";
cin >> doAgain;
} while (doAgain == 'Y' || doAgain == 'y');
return 0;
};
void DisplayElements(Element *ChemicalElements, int size)
{
for (int index = 0; index < size; index++)
{
cout << setw(8) << left << ChemicalElements[index].atomicWeight
<< setw(12)<< left << ChemicalElements[index].name
<< setw(4) << left << ChemicalElements[index].symbol
<< setw(4) << left << ChemicalElements[index].atomicNumber
<< endl;
}
}
void bubbleSort_Name(Element *ChemicalElements, int size, string name)
{
Element *temp = new Element;
//Element *temp = new Element;
bool swap;
do
{
swap = false;
for (int count = 0; count < (size -1); count++)
{
if (ChemicalElements[count].name > ChemicalElements[count + 1].name)
{
temp = ChemicalElements[count];
ChemicalElements[count] = ChemicalElements[count + 1];
ChemicalElements[count + 1] = temp;
swap = true;
}
}
}while (swap);
} //end BubbleSort
|