From d0620b2488eec630f6cb1ea04a721700244332b7 Mon Sep 17 00:00:00 2001 From: code2828 Date: Sat, 21 Dec 2024 17:47:21 +0000 Subject: [PATCH] Add support for multiple formulae and self-defined group --- Makefile | 2 +- cocp2.cpp | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-------- util.cpp | 15 ++++++++++--- util.h | 2 +- 4 files changed, 80 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index e5a630b..2689d0c 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,6 @@ LDFLAGS=-lc -lstdc++ EXE=cobaltocene run: db_exe - ./$(EXE) "Fe(CF3SO3)2" + ./$(EXE) "(NH4)2(Ce(NO3)6)" db_exe: $(CXX) $(CXXFLAGS) $(LDFLAGS) -g3 $(SRC) -o $(EXE) diff --git a/cocp2.cpp b/cocp2.cpp index 2b5e54d..5ba1ce4 100644 --- a/cocp2.cpp +++ b/cocp2.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include"util.h" #include"parse.h" using namespace std; @@ -10,34 +11,90 @@ vector e; vector eleamount; -int main(int argc, const char** argv) +queue q; + +double relmass(vector v, vector e) { - register_elements(e); - if (argc <= 1 - || (argc > 1 - && (!strcmp(argv[1], "--") - || !strcmp(argv[1], "-")))) // read from stdin + double relmass = 0; + for (int i = 0; i < e.size(); i++) { - cout << "plz input formula as parameter.\n"; - return 0; + relmass += v[i] * e[i].mass; } - string form = argv[1]; + return relmass; +} + +void calc_and_output(string form) +{ cout << "Input formula: " << form << endl; vector v; v.resize(e.size()); parse(form, e, v); ostringstream oss; int cnt = 0; + double relmass = 0; for (int i = 0; i < e.size(); i++) { if(v[i] != 0) { oss << e[i].symbol << ": " << v[i] << endl; cnt++; + relmass += v[i] * e[i].mass; } } cout << "There are " << cnt << " elements.\n"; cout << oss.str(); + cout << "Molecular mass = " << relmass << " g/mol\n"; +} + +int main(int argc, const char** argv) +{ + register_elements(e); + if (argc <= 1 + || (argc > 1 + && (!strcmp(argv[1], "--") + || !strcmp(argv[1], "-")))) // read from stdin, not implemented now + { + cout << "Please input formula as the first parameter.\n"; + return 0; + } + int k = -0x05; + for (k = 1; k < argc; k++) + { + string form = argv[k]; + if(form == "where") + { + k++; + break; + } + q.push(form); + } + for (; k < argc; k++) + { + string x, y; + char c; + int j = 0; + while((c = argv[k][j++]) > 0) + { + if (c == '=') + { + break; + } + x.push_back(c); + } + while((c = argv[k][j++]) > 0) + { + y.push_back(c); + } + vector resss; + parse(y, e, resss); + double grouprelmass = relmass(resss, e); + s(x, "Custom Group", grouprelmass, e); + } + while(!q.empty()) + { + calc_and_output(q.front()); + q.pop(); + } return 0; } diff --git a/util.cpp b/util.cpp index 0d72f22..2b4f785 100644 --- a/util.cpp +++ b/util.cpp @@ -3,15 +3,24 @@ using namespace std; -// quick element add to vector -inline void s(string _, string a, double b, vector& e) +// quick element add to vector (or override) +void s(string _, string a, double b, vector& e) { element c; c.name = a; c.n = e.size() + 1; c.symbol = _; c.mass = b; - e.push_back(c); + int ind = find_symbol(e, _); + if(ind != -1) + { + e[ind] = c; + } + else + { + e.push_back(c); + } + return; } void register_elements(vector& e) diff --git a/util.h b/util.h index 980844c..383e6ee 100644 --- a/util.h +++ b/util.h @@ -13,7 +13,7 @@ struct element double mass; }; -inline void s(string, string, double, vector&); +void s(string, string, double, vector&); void register_elements(vector&); -- 2.11.4.GIT