计算formulas.txt文件中每个分子的分子量

时间:2018-03-18 22:47:01

标签: c++

为什么我的程序无法正确计算公式文件的分子量?我知道它与double get_total_weight(table& hash_table,string line_input,int i)和double total_weight有关。但我不知道自己做错了什么。我知道如果我将total_weight更改为5,那就是所有人的权重。

这是formulas.txt文件:

H2SO4
Al2(SO4)3
H2O
CH4
C6H12O6
(CH3)3  
C3H7
AuI3
Bi2O3
Ga(C2H3O2)3
Cu3(PO4)2
In(OH)3
Li(AlSi2O6)
Sb2OS2

以下是代码:

#include <cctype>      // Provides toupper
#include <cstdlib>     // Provides EXIT_SUCCESS and size_t
#include <iostream>    // Provides cin, cout
#include "table.h"    // Provides the table class
#include <fstream>
#include <cstring>    // Provides strchr
#include <string>
#include <sstream>

using namespace std;

void read_in_table(table& hash_table, string line_input);
double get_total_weight(table& hash_table, string line_input, int i);

int main()
{
    string line_input;
    table hash_table;
    char dataFileName[] = "PeriodicTableElements.txt";

    // I've got the file name, now try to open the file for reading
    ifstream fileData;
    fileData.open(dataFileName, 0);
    if (fileData.good() == false)
    {
        cout << "ERROR: can't open data file: " << dataFileName << endl;

        // wait for the user to press enter to quit
        cout << endl << "Press the [Enter] key to quit...";
        getchar();
        return -1;
    }

    char dataFileName1[] = "formulas.txt";

    // I've got the file name, now try to open the file for reading
    ifstream fileData1;
    fileData1.open(dataFileName1, 0);
    if (fileData1.good() == false)
    {
        cout << "ERROR: can't open data file: " << dataFileName1 << endl;

        // wait for the user to press enter to quit
        cout << endl << "Press the [Enter] key to quit...";
        getchar();
        return -1;
    }


    // I've got the data file open.
    // read and use the data
    string hash;
    while (getline(fileData, hash))
    {
        cout << hash << endl;   
        cout << endl;
    }
    while (getline(fileData1, hash))
    {
        cout << hash << endl;
        getline(fileData, line_input, '\n');
        double total_weight = get_total_weight(hash_table, line_input, 0);
        cout << line_input << "=" << total_weight << endl;

    }

    fileData.close();
    cout << endl << "Press the [Enter] key to quit...";
    getchar();
}

void read_in_table(table& hash_table, string line_input)
{
    double weight;
    int i = 0;
    while (line_input[i] != ' ')
        ++i;
    string element = line_input.substr(0, i);
    int element_number = element[0] - 0;
    int weight_length = line_input.size() - i;
    string weight_string = line_input.substr(i, weight_length);
    istringstream convert(weight_string);
    if (!(convert >> weight))
        weight = 0;
}
double get_total_weight(table& hash_table, string line_input, int i)
{
    int j;
    int multiplier;
    double total_weight = 0.0;
    double weight;
    while (line_input[i] != '\0')
    {
        j = i;
        if (line_input[i] == '(')
        {
            ++i;
            int k = i;
            while (line_input[k + 1] != ')')
                k++;
            string line_help = line_input.substr(i, k - i + 1);
            weight = get_total_weight(hash_table, line_help, 0);
            i = k + 2;
            if (line_input[i] == '\0')
                total_weight = total_weight + weight * 1;
        }
        else
        {
            while (islower(line_input[i + 1]))
                i++;
            int k = i - j + 1;
            string element = line_input.substr(j, k);
            double element_number = element[0] - 0;
            weight = hash_table.retrieve(element_number, element);
            ++i;
            if (!(isdigit(line_input[i])))
                total_weight = total_weight + weight * 1;
        }
        j = i;
        while (isdigit(line_input[i]))
            i++;
        int k = i - j;
        string line_input_passer = line_input.substr(j, k);
        istringstream convert(line_input_passer);
        if (!(convert >> multiplier)) //give the value to weight using the characters in the stream
            multiplier = 0;
        total_weight = total_weight + weight * multiplier;
    }
    return total_weight;
}

2 个答案:

答案 0 :(得分:0)

始终提供文件位置的完整路径。更改以下两个声明,这将解决您的问题。

string pathway = "PeridicTableElements.txt";//change this code like below

string pathway = "/path/to/this/file/location/PeridicTableElements.txt";//In Unix/Linux

string pathway = "C:\\path\\to\\file\\location\\PeridicTableElements.txt";//In Windows

 string pathway1 = "formulas.txt";// change this code like below

 string pathway1 = "/path/to/this/file/location/formulas.txt";//In Unix/Linux

 string pathway1 = "C:\\path\\to\\file\\location\\formulas.txt";// In Windows 

答案 1 :(得分:0)

我能够让它工作,但我不得不改变字符串路径和字符串途径1到

char dataFileName[] = "PeriodicTableElements.txt"; `then put` ifstream fileData; fileData.open(dataFileName, 0); if(fileData.good() == false)
{
cout << "ERROR: cant open data file:" << dataFileName << endl;
cout << endl << "Press[enter] to quit...";
getchar();
return -1;
}