-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
97 lines (82 loc) · 2.19 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
_ "embed"
"flag"
"fmt"
"github.com/mahdin-hc/atomic/elements"
)
//go:embed data/elements.csv
var elementsCSV string
//go:embed data/molecules.csv
var moleculesCSV string
func main() {
ptCmd := flag.Bool("pt", false, "Draw periodic table")
eCmd := flag.Bool("e", false, "Show electron configurations")
flag.Parse()
args := flag.Args()
// Ensure a chemical formula is provided
if len(args) == 0 {
return
}
formula := args[0] // First non-flag argument is the formula
// Load elements data from CSV
err := elements.LoadElements(elementsCSV)
if err != nil {
fmt.Printf("Error loading elements: %v\n", err)
return
}
// Load molecules data from CSV
err = elements.LoadMolecules(moleculesCSV)
if err != nil {
fmt.Println("Error loading molecules:", err)
return
}
// Parse the chemical formula
compound, err := elements.ParseFormula(formula)
if err != nil {
fmt.Println(err)
return
}
molecule := compound.ToMolecule()
fmt.Println()
// Draw the periodic table if the draw command is used
if *ptCmd {
elements.DrawPeriodicTable(molecule)
fmt.Println()
}
// fmt.Printf("%+v\n\n",compound)
// for i, m := range compound.Molecules {
// fmt.Printf("%d %+v\n", i, m.ToString())
// }
// fmt.Println("")
// Output chemical information
name := compound.GetName()
el, exists := elements.ElementTable[formula]
if exists {
name = el.Name
}
fmt.Printf(" Molecule : %s\n", compound.ToString())
fmt.Printf(" Simplify : %s\n", compound.ToMolecule().Simplify())
fmt.Printf(" Name : %s\n", name)
if compound.State != "" {
fmt.Printf(" State : %s\n", compound.State)
}
fmt.Printf(" Mass : %f\n", compound.GetMass())
fmt.Printf(" Charge : %d\n", compound.GetCharge())
if exists {
fmt.Printf(" Number : %d\n", el.Number)
fmt.Printf(" Category : %s\n", el.Category)
fmt.Printf(" Fact : %s\n", el.Fact)
}
fmt.Println()
// Show electron configurations if -e is passed
if *eCmd {
printed := make(map[string]bool)
for _, el := range molecule.Elements {
if !printed[el.Symbol] {
fmt.Printf(" %s(%d): %s\n", el.Symbol, el.Number, el.GetElectronConfiguration().ToString())
printed[el.Symbol] = true
}
}
}
}