Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- #include <cmath>
- #include <iomanip>
- #include <fstream>
- #include <cstdlib>
- #include <stdlib.h>
- #include <time.h>
- #include <ctime>
- ofstream outf;
- ifstream inf;
- ifstream inf2;
- struct ele
- {
- string elem;
- float weight;
- };
- ele elements[30];
- int read(ele []);
- void sort(ele [], int);
- int locate(ele [], int, string);
- int find2(ele a[], int size, string word);
- int main()
- {
- outf.open("elements.txt");
- inf.open("element.txt");
- inf2.open("formula.txt");
- if (!inf)
- {
- cout << "Error opening file" << endl;
- return 0;
- }
- if(!inf2)
- {
- cout << "Error opening file" << endl;
- return 0;
- }
- int max, pos, e;
- float sum = 0;
- string sym;
- char a, b;
- string c, d;
- max = read(elements);
- sort(elements, max);
- while(inf2 >> a){
- cout << a;
- b = inf2.peek();
- c = a;
- d = b;
- if(b == '\n'){
- if(isupper(a)){
- sym = a;
- pos = find2(elements, max, sym);
- sum = sum + elements[pos].weight;
- }
- cout << " = " << sum;
- cout << endl;
- sum = 0;
- }
- if(islower(b)){
- sym = c + d;
- }
- if(isdigit(b)){
- inf2 >> e;
- cout << e;
- pos = find2(elements, max, sym);
- sum = sum + (e * elements[pos].weight);
- }
- if(ispunct(b)){
- if(isupper(a)){
- sym = a;
- }
- }
- if(isupper(b)){
- if(islower(a)){
- pos = find2(elements, max, sym);
- sum = sum + elements[pos].weight;
- }
- }
- if(isupper(a)){
- if(isupper(b)){
- sym = a;
- pos = find2(elements, max, sym);
- sum = sum + elements[pos].weight;
- }
- }
- }
- outf.close();
- inf.close();
- return 0;
- }
- int read(ele a[]){
- int x = 0;
- while(inf >> a[x].elem >> a[x].weight){
- x++;
- }
- return x;
- }
- void sort(ele a[], int max){
- float temp;
- string temp2;
- for(int i = 0; i < max - 1; i++){
- for(int x = 0; x < max - 1; x++){
- if(a[x].weight > a[x + 1].weight){
- temp = a[x].weight;
- a[x].weight = a[x + 1].weight;
- a[x + 1].weight = temp;
- temp2 = a[x].elem;
- a[x].elem = a[x + 1].elem;
- a[x + 1].elem = temp2;
- }
- }
- }
- }
- int find2(ele a[], int size, string word){
- int pos;
- for(int x = 0; x < size; x++){
- if(word == a[x].elem){
- return x;
- }
- }
- return -1;
- }
- int locate(ele a[], int size, string word){
- int low = 0;
- int high = size;
- int mid;
- int pos;
- while(low <= high)
- {
- mid = (low + high )/2;
- if (word == a[mid].elem){
- return mid;
- }
- else if (a[mid].elem > word){
- high = mid - 1;
- }
- else{
- low = mid + 1;
- }
- }
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement