Advertisement
Guest User

Untitled

a guest
Jan 11th, 2015
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Drawing;
  7.  
  8. namespace chemistry
  9. {
  10.     /// <summary>
  11.     /// Базовый класс реакции
  12.     /// </summary>
  13.     class Reaction
  14.     {
  15.         protected static Dictionary<string, Metall> metalls = new Dictionary<string, Metall>();
  16.         protected static Dictionary<string, NoMetall> nometalls = new Dictionary<string, NoMetall>();
  17.         protected static Dictionary<string, Molecule> acidsrest = new Dictionary<string, Molecule>();
  18.         private bool enable;
  19.         protected Substance m1;
  20.         protected Substance m2;
  21.         protected Substance m3;
  22.         protected Substance m4;
  23.         protected bool error;
  24.  
  25.         /// <summary>
  26.         /// Свойство поля "Ошибка"
  27.         /// </summary>
  28.         public bool Error
  29.         {
  30.             get { return error; }
  31.             set { error = value; }
  32.         }
  33.         /// <summary>
  34.         /// Свойство первого вещества в реакции
  35.         /// </summary>
  36.         public Substance M1
  37.         {
  38.             get { return m1; }
  39.             set { m1=value; }
  40.         }
  41.         /// <summary>
  42.         /// Свойство второго вещества в реакции
  43.         /// </summary>
  44.         public Substance M2
  45.         {
  46.             get { return m2; }
  47.             set { m2 = value; }
  48.         }
  49.         /// <summary>
  50.         /// Свойство третьего вещества в реакции
  51.         /// </summary>
  52.         public Substance M3
  53.         {
  54.             get { return m3; }
  55.             set { m3 = value; }
  56.         }
  57.         /// <summary>
  58.         /// Свойство четвертого вещества в реакции
  59.         /// </summary>
  60.         public Substance M4
  61.         {
  62.             get { return m4; }
  63.             set { m4 = value; }
  64.         }
  65.         /// <summary>
  66.         /// Свойство доступности проведения реакции
  67.         /// </summary>
  68.         public bool Enable
  69.         {
  70.             get { return enable; }
  71.             set { enable = value; }
  72.         }
  73.         /// <summary>
  74.         /// Свойство списка металлов
  75.         /// </summary>
  76.         public static Dictionary<string, Metall> Metalls
  77.         {
  78.             get { return metalls; }
  79.         }
  80.         /// <summary>
  81.         /// Свойство списка неметаллов
  82.         /// </summary>
  83.         public static Dictionary<string, NoMetall> NoMetalls
  84.         {
  85.             get { return nometalls; }
  86.         }
  87.         /// <summary>
  88.         /// Свойство списка кислотных остатков
  89.         /// </summary>
  90.         public static Dictionary<string, Molecule> Rests
  91.         {
  92.             get { return acidsrest; }
  93.         }
  94.         /// <summary>
  95.         /// Конструктор реакции
  96.         /// </summary>
  97.         public Reaction()
  98.         {
  99.             string s = "";
  100.             bool b = true;
  101.             string kolvo = "";
  102.             StreamReader str = new StreamReader("metalls.txt");
  103.             StreamReader strproton = new StreamReader("proton_metall.txt");
  104.             StreamReader strdegree = new StreamReader("degree.txt");
  105.             while (b)
  106.             {
  107.                 s = str.ReadLine();
  108.                 if (s != null)
  109.                 {
  110.                     string tmp = "";
  111.                     string tmpkol = "";
  112.                     string degree = "";
  113.                     Metall a = new Metall();
  114.                     for (int i = 0; i < s.Length; i++)
  115.                     {
  116.                         if (Char.IsLetter(s[i]))
  117.                             tmp = tmp + s[i];
  118.                         else
  119.                         {
  120.                             a.Charges.Add(s[i] - '0');
  121.                         }
  122.                     }
  123.                     a.Name = tmp;
  124.                     kolvo = strproton.ReadLine();
  125.                     degree = strdegree.ReadLine();
  126.                     for (int i = 0; i < kolvo.Length; i++)
  127.                     {
  128.                         if ((Char.IsDigit(kolvo[i])))
  129.                         {
  130.                             tmpkol = tmpkol + kolvo[i];
  131.                         }
  132.                         else
  133.                         {
  134.                             a.Protons = Convert.ToInt32(tmpkol);
  135.                             tmpkol = "";
  136.                         }
  137.                     }
  138.                     a.Neutrons = Convert.ToInt32(tmpkol);
  139.                     a.Degree = Convert.ToInt32(degree);
  140.                     metalls.Add(tmp, a);
  141.                 }
  142.                 else
  143.                     b = false;
  144.             }
  145.             str.Close();
  146.             strproton.Close();
  147.             b = true;
  148.             StreamReader str1 = new StreamReader("nometalls.txt");
  149.             StreamReader strproton1 = new StreamReader("proton_nometall.txt");
  150.             b = true;
  151.             s = "";
  152.             kolvo = "";
  153.             while (b)
  154.             {
  155.                 s = str1.ReadLine();
  156.                 if (s != null)
  157.                 {
  158.                     string tmp = "";
  159.                     string tmpkol = "";
  160.                     NoMetall a = new NoMetall();
  161.                     for (int i = 0; i < s.Length; i++)
  162.                     {
  163.                         if (Char.IsLetter(s[i]))
  164.                             tmp = tmp + s[i];
  165.                         else
  166.                         {
  167.                             a.Charges.Add((s[i] - '0') * (-1));
  168.                             a.Charges.Add(s[i] - '0');
  169.                         }
  170.                     }
  171.                     a.Name = tmp;
  172.                     kolvo = strproton1.ReadLine();
  173.                     for (int i = 0; i < kolvo.Length; i++)
  174.                     {
  175.                         if ((Char.IsDigit(kolvo[i])))
  176.                         {
  177.                             tmpkol = tmpkol + kolvo[i];
  178.                         }
  179.                         else
  180.                         {
  181.                             a.Protons = Convert.ToInt32(tmpkol);
  182.                             tmpkol = "";
  183.                         }
  184.                     }
  185.                     a.Neutrons = Convert.ToInt32(tmpkol);
  186.                     nometalls.Add(tmp, a);
  187.                 }
  188.                 else
  189.                     b = false;
  190.             }
  191.             nometalls["H"].Charges.Clear();
  192.             nometalls["H"].Charges.Add(1);
  193.             str1.Close();;
  194.             StreamReader str2 = new StreamReader("acids.txt");
  195.             b = true;
  196.             bool d = true;
  197.             s = "";
  198.             while (b)
  199.             {
  200.                 string k = "";
  201.                 d = true;
  202.                 s = str2.ReadLine();
  203.                 Molecule m = new Molecule();
  204.                 m.A1 = new Atom();
  205.                 m.A2 = new Atom();
  206.                 if (s != null)
  207.                 {
  208.                     string tmp = "";
  209.                     for (int i = 0; i < s.Length; i++)
  210.                     {
  211.                         if (d)
  212.                         {
  213.                             if (char.IsLetter(s[i]))
  214.                             {
  215.                                 tmp = tmp + s[i];
  216.                                 k = k + s[i];
  217.                                 if (char.IsUpper(s[i + 1]))
  218.                                 {
  219.                                     m.A1.Name = tmp;
  220.                                     d = false;
  221.                                     tmp = "";
  222.                                 }
  223.                             }
  224.                             else
  225.                             {
  226.                                 if ((i + 1) == s.Length - 1)
  227.                                 {
  228.                                     m.A1.Name = tmp;
  229.                                     m.A1.Count = s[i] - '0';
  230.                                 }
  231.                                 else
  232.                                 {
  233.                                     m.A1.Name = tmp;
  234.                                     m.A2 = null;
  235.                                     m.Ioncharge = (s[i] - '0') * (-1);
  236.                                 }
  237.                             }
  238.                         }
  239.                         else
  240.                         {
  241.                             if (char.IsLetter(s[i]))
  242.                             {
  243.                                 if (Char.IsLetter(s[i + 1]))
  244.                                 {
  245.                                     tmp = tmp + s[i];
  246.                                     k = k + s[i];
  247.                                 }
  248.                                 else
  249.                                 {
  250.                                     tmp = tmp + s[i];
  251.                                     k = k + s[i];
  252.                                     m.A2.Name = tmp;
  253.                                 }
  254.                             }
  255.                             else
  256.                             {
  257.                                 if (i < s.Length - 1)
  258.                                 {
  259.                                     m.A2.Name = tmp;
  260.                                     m.A2.Count = s[i] - '0';
  261.                                     k = k + s[i];
  262.                                 }
  263.                                 else
  264.                                 {
  265.                                     m.Ioncharge = (s[i] - '0') * (-1);
  266.                                 }
  267.                             }
  268.                         }
  269.                     }
  270.                 }
  271.                 else
  272.                     b = false;
  273.                 m.Name = k;
  274.                 acidsrest.Add(k, m);
  275.             }
  276.             str2.Close();
  277.         }
  278.         /// <summary>
  279.         /// Виртуальный метод проведения реакции
  280.         /// </summary>
  281.         public virtual void CarryOut() { }
  282.         /// <summary>
  283.         /// Метод получения металла
  284.         /// </summary>
  285.         /// <param name="s">Название металла</param>
  286.         /// <returns>Металл</returns>
  287.         public static Atom GetMetall(string s)
  288.         {
  289.             return metalls[s];
  290.         }
  291.         /// <summary>
  292.         /// Метод получения неметалла
  293.         /// </summary>
  294.         /// <param name="s">Название неметалла</param>
  295.         /// <returns>Неметалл</returns>
  296.         public static Atom GetAtom(string s)
  297.         {
  298.             return nometalls[s];
  299.         }
  300.         /// <summary>
  301.         /// Метод получения кислотного остатка
  302.         /// </summary>
  303.         /// <param name="s">Название остатка</param>
  304.         /// <returns>Остаток</returns>
  305.         public static Molecule GetAcidRest(string s)
  306.         {
  307.             return acidsrest[s];
  308.         }
  309.         /// <summary>
  310.         /// Метод копирования состояния атома
  311.         /// </summary>
  312.         /// <param name="a">Атом из которого копируют</param>
  313.         /// <param name="b">Атом в который копируют</param>
  314.         public static void CopyAtom(Atom a, Atom b)
  315.         {
  316.             b.Charge = a.Charge;
  317.             b.Charges = a.Charges;
  318.             b.Count = a.Count;
  319.             b.Name = a.Name;
  320.             b.Neutrons = a.Neutrons;
  321.             b.Protons = a.Protons;
  322.             b.Degree = a.Degree;
  323.         }
  324.         /// <summary>
  325.         /// Метод копирования состояния молекулы
  326.         /// </summary>
  327.         /// <param name="m">Молекула из которой копируют</param>
  328.         /// <param name="n">Молекула в которую копируют</param>
  329.         public void CopyMolecule(Molecule m, Molecule n)
  330.         {
  331.             CopyAtom(m.A1, n.A1);
  332.             if (m.A2 != null)
  333.                 CopyAtom(m.A2, n.A2);
  334.             else
  335.                 n.A2 = null;
  336.             n.Coeff = m.Coeff;
  337.             n.Ioncharge = m.Ioncharge;
  338.             n.Name = m.Name;
  339.         }
  340.         /// <summary>
  341.         /// Наименьшее общее кратное
  342.         /// </summary>
  343.         /// <param name="a">Первое число</param>
  344.         /// <param name="b">Второе число</param>
  345.         /// <returns>НОК</returns>
  346.         public static int NOK(int a, int b)
  347.         {
  348.             return (a / NOD(a, b)) * b;
  349.         }
  350.         /// <summary>
  351.         /// Наименьший общий делитель
  352.         /// </summary>
  353.         /// <param name="a">Первое число</param>
  354.         /// <param name="b">Второе число</param>
  355.         /// <returns>НОД</returns>
  356.         public static int NOD(int a, int b)
  357.         {
  358.             if (a != 0) return NOD(b % a, a);
  359.             else return b;
  360.         }
  361.     }
  362. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement