为什么我的程序没有正确计算我的公式文件的分子量?我知道它与双get_total_weight(表& hash_table,字符串line_input,int )和双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;
}发布于 2018-03-18 23:08:46
始终提供文件位置的完整路径。更改以下两个语句,这将解决您的问题。
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 发布于 2018-03-19 14:28:52
我能够让它正常工作,但我不得不将字符串路径和字符串pathway1更改为
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;
}https://stackoverflow.com/questions/49353596
复制相似问题