Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Drawing;
- namespace chemistry
- {
- /// <summary>
- /// Базовый класс реакции
- /// </summary>
- class Reaction
- {
- protected static Dictionary<string, Metall> metalls = new Dictionary<string, Metall>();
- protected static Dictionary<string, NoMetall> nometalls = new Dictionary<string, NoMetall>();
- protected static Dictionary<string, Molecule> acidsrest = new Dictionary<string, Molecule>();
- private bool enable;
- protected Substance m1;
- protected Substance m2;
- protected Substance m3;
- protected Substance m4;
- protected bool error;
- /// <summary>
- /// Свойство поля "Ошибка"
- /// </summary>
- public bool Error
- {
- get { return error; }
- set { error = value; }
- }
- /// <summary>
- /// Свойство первого вещества в реакции
- /// </summary>
- public Substance M1
- {
- get { return m1; }
- set { m1=value; }
- }
- /// <summary>
- /// Свойство второго вещества в реакции
- /// </summary>
- public Substance M2
- {
- get { return m2; }
- set { m2 = value; }
- }
- /// <summary>
- /// Свойство третьего вещества в реакции
- /// </summary>
- public Substance M3
- {
- get { return m3; }
- set { m3 = value; }
- }
- /// <summary>
- /// Свойство четвертого вещества в реакции
- /// </summary>
- public Substance M4
- {
- get { return m4; }
- set { m4 = value; }
- }
- /// <summary>
- /// Свойство доступности проведения реакции
- /// </summary>
- public bool Enable
- {
- get { return enable; }
- set { enable = value; }
- }
- /// <summary>
- /// Свойство списка металлов
- /// </summary>
- public static Dictionary<string, Metall> Metalls
- {
- get { return metalls; }
- }
- /// <summary>
- /// Свойство списка неметаллов
- /// </summary>
- public static Dictionary<string, NoMetall> NoMetalls
- {
- get { return nometalls; }
- }
- /// <summary>
- /// Свойство списка кислотных остатков
- /// </summary>
- public static Dictionary<string, Molecule> Rests
- {
- get { return acidsrest; }
- }
- /// <summary>
- /// Конструктор реакции
- /// </summary>
- public Reaction()
- {
- string s = "";
- bool b = true;
- string kolvo = "";
- StreamReader str = new StreamReader("metalls.txt");
- StreamReader strproton = new StreamReader("proton_metall.txt");
- StreamReader strdegree = new StreamReader("degree.txt");
- while (b)
- {
- s = str.ReadLine();
- if (s != null)
- {
- string tmp = "";
- string tmpkol = "";
- string degree = "";
- Metall a = new Metall();
- for (int i = 0; i < s.Length; i++)
- {
- if (Char.IsLetter(s[i]))
- tmp = tmp + s[i];
- else
- {
- a.Charges.Add(s[i] - '0');
- }
- }
- a.Name = tmp;
- kolvo = strproton.ReadLine();
- degree = strdegree.ReadLine();
- for (int i = 0; i < kolvo.Length; i++)
- {
- if ((Char.IsDigit(kolvo[i])))
- {
- tmpkol = tmpkol + kolvo[i];
- }
- else
- {
- a.Protons = Convert.ToInt32(tmpkol);
- tmpkol = "";
- }
- }
- a.Neutrons = Convert.ToInt32(tmpkol);
- a.Degree = Convert.ToInt32(degree);
- metalls.Add(tmp, a);
- }
- else
- b = false;
- }
- str.Close();
- strproton.Close();
- b = true;
- StreamReader str1 = new StreamReader("nometalls.txt");
- StreamReader strproton1 = new StreamReader("proton_nometall.txt");
- b = true;
- s = "";
- kolvo = "";
- while (b)
- {
- s = str1.ReadLine();
- if (s != null)
- {
- string tmp = "";
- string tmpkol = "";
- NoMetall a = new NoMetall();
- for (int i = 0; i < s.Length; i++)
- {
- if (Char.IsLetter(s[i]))
- tmp = tmp + s[i];
- else
- {
- a.Charges.Add((s[i] - '0') * (-1));
- a.Charges.Add(s[i] - '0');
- }
- }
- a.Name = tmp;
- kolvo = strproton1.ReadLine();
- for (int i = 0; i < kolvo.Length; i++)
- {
- if ((Char.IsDigit(kolvo[i])))
- {
- tmpkol = tmpkol + kolvo[i];
- }
- else
- {
- a.Protons = Convert.ToInt32(tmpkol);
- tmpkol = "";
- }
- }
- a.Neutrons = Convert.ToInt32(tmpkol);
- nometalls.Add(tmp, a);
- }
- else
- b = false;
- }
- nometalls["H"].Charges.Clear();
- nometalls["H"].Charges.Add(1);
- str1.Close();;
- StreamReader str2 = new StreamReader("acids.txt");
- b = true;
- bool d = true;
- s = "";
- while (b)
- {
- string k = "";
- d = true;
- s = str2.ReadLine();
- Molecule m = new Molecule();
- m.A1 = new Atom();
- m.A2 = new Atom();
- if (s != null)
- {
- string tmp = "";
- for (int i = 0; i < s.Length; i++)
- {
- if (d)
- {
- if (char.IsLetter(s[i]))
- {
- tmp = tmp + s[i];
- k = k + s[i];
- if (char.IsUpper(s[i + 1]))
- {
- m.A1.Name = tmp;
- d = false;
- tmp = "";
- }
- }
- else
- {
- if ((i + 1) == s.Length - 1)
- {
- m.A1.Name = tmp;
- m.A1.Count = s[i] - '0';
- }
- else
- {
- m.A1.Name = tmp;
- m.A2 = null;
- m.Ioncharge = (s[i] - '0') * (-1);
- }
- }
- }
- else
- {
- if (char.IsLetter(s[i]))
- {
- if (Char.IsLetter(s[i + 1]))
- {
- tmp = tmp + s[i];
- k = k + s[i];
- }
- else
- {
- tmp = tmp + s[i];
- k = k + s[i];
- m.A2.Name = tmp;
- }
- }
- else
- {
- if (i < s.Length - 1)
- {
- m.A2.Name = tmp;
- m.A2.Count = s[i] - '0';
- k = k + s[i];
- }
- else
- {
- m.Ioncharge = (s[i] - '0') * (-1);
- }
- }
- }
- }
- }
- else
- b = false;
- m.Name = k;
- acidsrest.Add(k, m);
- }
- str2.Close();
- }
- /// <summary>
- /// Виртуальный метод проведения реакции
- /// </summary>
- public virtual void CarryOut() { }
- /// <summary>
- /// Метод получения металла
- /// </summary>
- /// <param name="s">Название металла</param>
- /// <returns>Металл</returns>
- public static Atom GetMetall(string s)
- {
- return metalls[s];
- }
- /// <summary>
- /// Метод получения неметалла
- /// </summary>
- /// <param name="s">Название неметалла</param>
- /// <returns>Неметалл</returns>
- public static Atom GetAtom(string s)
- {
- return nometalls[s];
- }
- /// <summary>
- /// Метод получения кислотного остатка
- /// </summary>
- /// <param name="s">Название остатка</param>
- /// <returns>Остаток</returns>
- public static Molecule GetAcidRest(string s)
- {
- return acidsrest[s];
- }
- /// <summary>
- /// Метод копирования состояния атома
- /// </summary>
- /// <param name="a">Атом из которого копируют</param>
- /// <param name="b">Атом в который копируют</param>
- public static void CopyAtom(Atom a, Atom b)
- {
- b.Charge = a.Charge;
- b.Charges = a.Charges;
- b.Count = a.Count;
- b.Name = a.Name;
- b.Neutrons = a.Neutrons;
- b.Protons = a.Protons;
- b.Degree = a.Degree;
- }
- /// <summary>
- /// Метод копирования состояния молекулы
- /// </summary>
- /// <param name="m">Молекула из которой копируют</param>
- /// <param name="n">Молекула в которую копируют</param>
- public void CopyMolecule(Molecule m, Molecule n)
- {
- CopyAtom(m.A1, n.A1);
- if (m.A2 != null)
- CopyAtom(m.A2, n.A2);
- else
- n.A2 = null;
- n.Coeff = m.Coeff;
- n.Ioncharge = m.Ioncharge;
- n.Name = m.Name;
- }
- /// <summary>
- /// Наименьшее общее кратное
- /// </summary>
- /// <param name="a">Первое число</param>
- /// <param name="b">Второе число</param>
- /// <returns>НОК</returns>
- public static int NOK(int a, int b)
- {
- return (a / NOD(a, b)) * b;
- }
- /// <summary>
- /// Наименьший общий делитель
- /// </summary>
- /// <param name="a">Первое число</param>
- /// <param name="b">Второе число</param>
- /// <returns>НОД</returns>
- public static int NOD(int a, int b)
- {
- if (a != 0) return NOD(b % a, a);
- else return b;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement