Brooklyn College C++ 作业!!

kimi876 2009-12-02 03:27:10
Write a program that will calculate the number of molecules of a substance given its
weight in grams and its molecular formula. Assume an unsorted file periodicTable.dat
that contains the atomic symbol, atomic number and mass number of several common
elements. For example the data file may contain the following lines:

H 1 1.0080
He 2 4.00260
C 6 12.011
N 7 14.0067
Na 11 22.9898
….
Create the file periodicTable.dat with a minimum of 25 elements.

Your program should ask the user to input the substance’s molecular formula and weight
and print the number of molecules. A typical dialogue would be:

Enter the molecular formula: CO2
Enter the weight in grams: 0.00220
The number of molecules in 0.00220 grams of CO2 is 3.01E19.
Enter the molecular formula: Fe2O3
….
Try your program on the following problems:
H2 5 grams
H2O 2.5 grams
Fe2O3 1 gram
C6H12O6 25 grams
Make up several more of your own. Be sure your program is clearly documented,
structured, and uses meaningful variables. Output is to be both to a file as well as to the
console. Turn in your program as well as input and output files.

Strategy 『方法』
1. Create a function readPeriodicTable that reads the information in the
periodicTable.dat file into three arrays: atomicSymbol, atomicNumber,
atomicMass. Your function should also return the number of elements in the
arrays.
2. Create a function bubbleSort that sorts the three arrays alphabetically.
3. Create a function printTable that prints a neatly formatted table of the information
that you have sorted.
4. The main function should prompt the user for the molecular formula and the
weight in grams.
5. The main function should call a function numberOfGramsPerMole which is given
the molecular formula as a parameter (string) and returns the number of grams per
mole. This function will have to parse the input string in order to separate each
element and the number of atoms of that element. Note the following rules:
a. An element always begins with a capital letter and may be followed by a
single lower case letter.
b. The absence of a number indicates a single atom of that element.

After determining the identity of the element and the number of atoms of that
element the function calls another function, findMassNumber in order to
determine the mass number of that element.
numberOfGramsPerMole then calculates, and eventually returns, the total number
of grams per mole represented by that formula.
6. The function findMassNumber receives a symbol of an element as a parameter
and returns its atomic mass by searching the arrays you created in steps 1 and 2. If
the element is not in the table, it should print an error message and return a value
which will allow the program to abort.
7. The main function then calls a function calculateNumberOfParticles that accepts
the weight in grams and the number of grams per mole as parameters and returns
the number of particles which is then printed by the main function.
8. The main function should repeat the process until the user indicates that there is
no more data to be processed.



简单得说就是输入化学式和其质量,然后计算其分子量
有能人看得明并且用getline 和 用count的方法 写个程序吗??(小弟只学到排列,其他的不会)

哪位高人能帮上小弟,我不会忘记他的!!
...全文
88 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
kimi876 2009-12-02
  • 打赏
  • 举报
回复
re 1楼
好像没有这么简单 吧
WingForce 2009-12-02
  • 打赏
  • 举报
回复
Strategy 部分确实是。。。很严谨啊。。。。
WingForce 2009-12-02
  • 打赏
  • 举报
回复

#include <fstream>

using namespace std;

int main()
{
ifstream ifs("periodicTable.dat");

while (!ifs.eof())
{
string atomic_symbol;
int atomic_number;
double mass_number;
ifs >> atomic_symbol >> atomic_number >> mass_number;
cout << "Atomic: "
<< atomic_symbol << " "
<< atomic_number << " "
<< mass_number << endl;
}

ifs.close();
return 0;
}

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧