6677

Untitled

Dec 8th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.68 KB | None | 0 0
  1. using System;
  2.  
  3. using System.Collections.Generic;
  4.  
  5. using System.ComponentModel;
  6.  
  7. using System.Data;
  8.  
  9. using System.Data.SqlClient;
  10.  
  11. using System.Drawing;
  12.  
  13. using System.Linq;
  14.  
  15. using System.Text;
  16.  
  17. using System.Windows.Forms;
  18.  
  19.  
  20.  
  21. namespace Coursework
  22.  
  23. {
  24.  
  25. public partial class Main_Form : Form
  26.  
  27. {
  28.  
  29. public static DataSet myDataSet = new DataSet();
  30.  
  31. public static SqlConnectionStringBuilder connection = new SqlConnectionStringBuilder();
  32.  
  33. public static int circuitID = 0;
  34.  
  35. public static List<Classes.Gate> GateList = new List<Classes.Gate>();
  36.  
  37. //public DataTable Table1 = new DataTable("CircuitData");
  38.  
  39. int userID = 0;
  40.  
  41.  
  42.  
  43. int OutputID = 0;
  44.  
  45. bool MakingSelection = false;
  46.  
  47.  
  48.  
  49. public Main_Form()
  50.  
  51. {
  52.  
  53. InitializeComponent();
  54.  
  55. connection.UserID = "callumk";
  56.  
  57. connection.Password = "king-underwood";
  58.  
  59. connection.IntegratedSecurity = false;
  60.  
  61. connection.InitialCatalog = "callumk";
  62.  
  63. connection.DataSource = "213.171.206.37";
  64.  
  65.  
  66.  
  67. Classes.Gate AND = new Classes.Gate();
  68.  
  69. AND.SetComponentId(0);
  70.  
  71.  
  72.  
  73. AND.Input1 = 0;
  74.  
  75. AND.Input2 = 0;
  76.  
  77. AND.Output = 0;
  78.  
  79. AND.InToolBox = true;
  80.  
  81. AND.ControlRemovedFromToolbox += new EventHandler(gate1_ControlRemovedFromToolbox);
  82.  
  83. AND.SetLocation(3, 3);
  84.  
  85. AND.Location = new Point(3, 3);
  86.  
  87. AND.SetGateType(1);
  88.  
  89. AND.ControlHasBeenMoved += new EventHandler(ControlGateMoved);
  90.  
  91. splitContainer1.Panel1.Controls.Add(AND);
  92.  
  93.  
  94.  
  95. Classes.Gate OR = new Classes.Gate();
  96.  
  97. OR.SetComponentId(0);
  98.  
  99.  
  100.  
  101. OR.Input1 = 0;
  102.  
  103. OR.Input2 = 0;
  104.  
  105. OR.Output = 0;
  106.  
  107. OR.InToolBox = true;
  108.  
  109. OR.ControlRemovedFromToolbox += new EventHandler(gate1_ControlRemovedFromToolbox);
  110.  
  111. OR.SetLocation(82, 3);
  112.  
  113. OR.Location = new Point(82, 3);
  114.  
  115. OR.SetGateType(2);
  116.  
  117. OR.ControlHasBeenMoved += new EventHandler(ControlGateMoved);
  118.  
  119. splitContainer1.Panel1.Controls.Add(OR);
  120.  
  121.  
  122.  
  123. Classes.Gate NOT = new Classes.Gate();
  124.  
  125. NOT.SetComponentId(0);
  126.  
  127.  
  128.  
  129. NOT.Input1 = 0;
  130.  
  131. NOT.Input2 = 0;
  132.  
  133. NOT.Output = 0;
  134.  
  135. NOT.InToolBox = true;
  136.  
  137. NOT.ControlRemovedFromToolbox += new EventHandler(gate1_ControlRemovedFromToolbox);
  138.  
  139. NOT.SetLocation(161, 3);
  140.  
  141. NOT.Location = new Point(161, 3);
  142.  
  143. NOT.SetGateType(3);
  144.  
  145. NOT.ControlHasBeenMoved += new EventHandler(ControlGateMoved);
  146.  
  147. splitContainer1.Panel1.Controls.Add(NOT);
  148.  
  149.  
  150.  
  151. Classes.Gate XOR = new Classes.Gate();
  152.  
  153. XOR.SetComponentId(0);
  154.  
  155.  
  156.  
  157. XOR.Input1 = 0;
  158.  
  159. XOR.Input2 = 0;
  160.  
  161. XOR.Output = 0;
  162.  
  163. XOR.InToolBox = true;
  164.  
  165. XOR.ControlRemovedFromToolbox += new EventHandler(gate1_ControlRemovedFromToolbox);
  166.  
  167. XOR.SetLocation(239, 3);
  168.  
  169. XOR.Location = new Point(239, 3);
  170.  
  171. XOR.SetGateType(4);
  172.  
  173. XOR.ControlHasBeenMoved += new EventHandler(ControlGateMoved);
  174.  
  175. splitContainer1.Panel1.Controls.Add(XOR);
  176.  
  177.  
  178.  
  179. Classes.Gate NAND = new Classes.Gate();
  180.  
  181. NAND.SetComponentId(0);
  182.  
  183.  
  184.  
  185. NAND.Input1 = 0;
  186.  
  187. NAND.Input2 = 0;
  188.  
  189. NAND.Output = 0;
  190.  
  191. NAND.InToolBox = true;
  192.  
  193. NAND.ControlRemovedFromToolbox += new EventHandler(gate1_ControlRemovedFromToolbox);
  194.  
  195. NAND.SetLocation(318, 3);
  196.  
  197. NAND.Location = new Point(318, 3);
  198.  
  199. NAND.SetGateType(5);
  200.  
  201. NAND.ControlHasBeenMoved += new EventHandler(ControlGateMoved);
  202.  
  203. splitContainer1.Panel1.Controls.Add(NAND);
  204.  
  205.  
  206.  
  207. Classes.Gate NOR = new Classes.Gate();
  208.  
  209. NOR.SetComponentId(0);
  210.  
  211.  
  212.  
  213. NOR.Input1 = 0;
  214.  
  215. NOR.Input2 = 0;
  216.  
  217. NOR.Output = 0;
  218.  
  219. NOR.InToolBox = true;
  220.  
  221. NOR.ControlRemovedFromToolbox += new EventHandler(gate1_ControlRemovedFromToolbox);
  222.  
  223. NOR.SetLocation(396, 3);
  224.  
  225. NOR.Location = new Point(396, 3);
  226.  
  227. NOR.SetGateType(6);
  228.  
  229. NOR.ControlHasBeenMoved += new EventHandler(ControlGateMoved);
  230.  
  231. splitContainer1.Panel1.Controls.Add(NOR);
  232.  
  233. }
  234.  
  235.  
  236.  
  237. void ControlGateMoved(object sender, EventArgs e)
  238.  
  239. {
  240.  
  241. splitContainer1.Panel2.Invalidate();
  242.  
  243. }
  244.  
  245.  
  246.  
  247. void Input1Selection(object sender, EventArgs e)
  248.  
  249. {
  250.  
  251. Classes.Gate s = (Classes.Gate)sender;
  252.  
  253. if ((MakingSelection == true) & (s.GetComponentID() != OutputID))
  254.  
  255. {
  256.  
  257. //make new link
  258.  
  259.  
  260.  
  261. foreach (Classes.Gate Gate in GateList)
  262.  
  263. {
  264.  
  265. if (Gate.Input1 == OutputID)
  266.  
  267. {
  268.  
  269. Gate.Input1 = 0;
  270.  
  271. }
  272.  
  273. if (Gate.Input2 == OutputID)
  274.  
  275. {
  276.  
  277. Gate.Input2 = 0;
  278.  
  279. }
  280.  
  281. if (Gate.Output == s.GetComponentID())
  282.  
  283. {
  284.  
  285. Gate.Output = 0;
  286.  
  287. }
  288.  
  289. if (Gate.GetComponentID() == OutputID)
  290.  
  291. {
  292.  
  293. Gate.Output = s.GetComponentID();
  294.  
  295. }
  296.  
  297. }
  298.  
  299. s.Input1 = OutputID;
  300.  
  301. MakingSelection = false;
  302.  
  303. OutputID = 0;
  304.  
  305. }
  306.  
  307. splitContainer1.Panel2.Invalidate();
  308.  
  309. }
  310.  
  311.  
  312.  
  313. void Input2Selection(object sender, EventArgs e)
  314.  
  315. {
  316.  
  317. Classes.Gate s = (Classes.Gate)sender;
  318.  
  319. if ((MakingSelection == true) & (s.GetComponentID() != OutputID))
  320.  
  321. {
  322.  
  323. foreach (Classes.Gate Gate in GateList)
  324.  
  325. {
  326.  
  327. if (Gate.Input1 == OutputID)
  328.  
  329. {
  330.  
  331. Gate.Input1 = 0;
  332.  
  333. }
  334.  
  335. if (Gate.Input2 == OutputID)
  336.  
  337. {
  338.  
  339. Gate.Input2 = 0;
  340.  
  341. }
  342.  
  343. if (Gate.Output == s.GetComponentID())
  344.  
  345. {
  346.  
  347. Gate.Output = 0;
  348.  
  349. }
  350.  
  351. if (Gate.GetComponentID() == OutputID)
  352.  
  353. {
  354.  
  355. Gate.Output = s.GetComponentID();
  356.  
  357. }
  358.  
  359. }
  360.  
  361. MakingSelection = false;
  362.  
  363. s.Input2 = OutputID;
  364.  
  365. OutputID = 0;
  366.  
  367. }
  368.  
  369. splitContainer1.Panel2.Invalidate();
  370.  
  371. }
  372.  
  373.  
  374.  
  375. void OutputSelection(object sender, EventArgs e)
  376.  
  377. {
  378.  
  379. Classes.Gate s = (Classes.Gate)sender;
  380.  
  381. if (MakingSelection == true)
  382.  
  383. {
  384.  
  385. MakingSelection = false;
  386.  
  387. }
  388.  
  389. else
  390.  
  391. {
  392.  
  393. MakingSelection = true;
  394.  
  395. OutputID = s.GetComponentID();
  396.  
  397. }
  398.  
  399. splitContainer1.Panel2.Invalidate();
  400.  
  401. }
  402.  
  403.  
  404.  
  405. void GateRemovedFromCircuit(object sender, EventArgs e)
  406.  
  407. {
  408.  
  409. Classes.Gate GateToRemove = (Classes.Gate)sender;
  410.  
  411. int IDToDelete = GateToRemove.GetComponentID();
  412.  
  413. foreach (Classes.Gate Gate in GateList)
  414.  
  415. {
  416.  
  417. if (Gate.Input1 == IDToDelete)
  418.  
  419. {
  420.  
  421. Gate.Input1 = 0;
  422.  
  423. }
  424.  
  425. if (Gate.Input2 == IDToDelete)
  426.  
  427. {
  428.  
  429. Gate.Input2 = 0;
  430.  
  431. }
  432.  
  433. if (Gate.Output == IDToDelete)
  434.  
  435. {
  436.  
  437. Gate.Output = 0;
  438.  
  439. }
  440.  
  441. }
  442.  
  443. GateList.Remove(GateToRemove);
  444.  
  445. splitContainer1.Panel2.Controls.Remove(GateToRemove);
  446.  
  447. splitContainer1.Panel2.Invalidate();
  448.  
  449. }
  450.  
  451.  
  452.  
  453. void AND_DoubleClick(object sender, EventArgs e)
  454.  
  455. {
  456.  
  457. throw new NotImplementedException();
  458.  
  459. }
  460.  
  461.  
  462.  
  463. public int CurrentUser()
  464.  
  465. {
  466.  
  467. return userID;
  468.  
  469. }
  470.  
  471.  
  472.  
  473. /*
  474.  
  475. public int GetNewComponentID()
  476.  
  477. {
  478.  
  479. int currentNum = 1;
  480.  
  481. foreach (Classes.Gate gate in GateList)
  482.  
  483. {
  484.  
  485. if (gate.GetComponentID() >= currentNum)
  486.  
  487. {
  488.  
  489. currentNum = gate.GetComponentID() + 1;
  490.  
  491. }
  492.  
  493. }
  494.  
  495. return currentNum;
  496.  
  497. }
  498.  
  499. */
  500.  
  501.  
  502.  
  503. static int power(int x, int y)
  504.  
  505. {
  506.  
  507. if (y < 0)
  508.  
  509. {
  510.  
  511. return 1;
  512.  
  513. }
  514.  
  515. switch (y){
  516.  
  517. case 0:
  518.  
  519. return 1;
  520.  
  521.  
  522.  
  523. default:
  524.  
  525. int result = x;
  526.  
  527. for (int i = 1; i <= (y - 1); i++)
  528.  
  529. {
  530.  
  531. result = result * 2;
  532.  
  533. }
  534.  
  535. return result;
  536.  
  537. }
  538.  
  539. }
  540.  
  541.  
  542.  
  543. static List<List<bool>> table(int n)
  544.  
  545. {
  546.  
  547. int columnsize = power(2, n);
  548.  
  549. List<List<bool>> truthtable = new List<List<bool>>();
  550.  
  551. for (int inputs = 1; inputs <= n; inputs++)
  552.  
  553. {
  554.  
  555. int length = power(2, inputs - 1);
  556.  
  557. List<bool> sublist = new List<bool>();
  558.  
  559. while (sublist.Count < columnsize)
  560.  
  561. {
  562.  
  563. for (int zero = 0; zero < length; zero++)
  564.  
  565. {
  566.  
  567. sublist.Add(false);
  568.  
  569. }
  570.  
  571. for (int zero = 0; zero < length; zero++)
  572.  
  573. {
  574.  
  575. sublist.Add(true);
  576.  
  577. }
  578.  
  579. }
  580.  
  581. truthtable.Add(sublist);
  582.  
  583. }
  584.  
  585.  
  586.  
  587. return truthtable;
  588.  
  589. }
  590.  
  591.  
  592.  
  593. //File>Open/Save/New
  594.  
  595. //New
  596.  
  597. private void NewClicked(object sender, EventArgs e)
  598.  
  599. {
  600.  
  601. Console.WriteLine("File>New Clicked");
  602.  
  603. string MessageBoxText = "Are You Sure You Want To Create A New File?" + Environment.NewLine + "Any Unsaves Changes Will Be Lost";
  604.  
  605. Console.Write("Dialog Shown, Result: ");
  606.  
  607. if (MessageBox.Show(MessageBoxText, "Are You Sure?", MessageBoxButtons.YesNo, MessageBoxIcon.None, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
  608.  
  609. {
  610.  
  611. Console.WriteLine("Yes Clicked");
  612.  
  613.  
  614.  
  615. Console.Write("Clearing " + splitContainer1.Panel2.Controls.Count.ToString() + " items>");
  616.  
  617. splitContainer1.Panel2.Controls.Clear();
  618.  
  619. //myDataSet.Tables["CircuitData"].Clear();
  620.  
  621. GateList.Clear();
  622.  
  623. Console.WriteLine("DONE");
  624.  
  625.  
  626.  
  627. circuitID = 0;
  628.  
  629. }
  630.  
  631. else
  632.  
  633. {
  634.  
  635. Console.WriteLine("No Clicked");
  636.  
  637. }
  638.  
  639. splitContainer1.Panel2.Invalidate();
  640.  
  641. }
  642.  
  643. //Save
  644.  
  645. private void SaveClicked(object sender, EventArgs e)
  646.  
  647. {
  648.  
  649. Console.WriteLine("File>Save Clicked");
  650.  
  651. if ((userID != 0) & (circuitID != 0))
  652.  
  653. {
  654.  
  655. using (SqlConnection oConn = new SqlConnection(connection.ConnectionString))
  656.  
  657. {
  658.  
  659. oConn.Open();
  660.  
  661. MessageBox.Show(circuitID.ToString());
  662.  
  663. SqlCommand oCommand = new SqlCommand("delete from circuitContents where circuitID=" + circuitID.ToString(), oConn);
  664.  
  665. oCommand.ExecuteNonQuery();
  666.  
  667.  
  668.  
  669.  
  670.  
  671. foreach (Classes.Gate gate in GateList)
  672.  
  673. {
  674.  
  675. oCommand = new SqlCommand("insert into circuitcontents values (" + circuitID.ToString() + ", " + gate.GetComponentID().ToString() + ", " + gate.GetGateType() + ", " + gate.GateLocation.X.ToString() + ", " + gate.GateLocation.Y.ToString() + ", " + gate.Input1 + ", " + gate.Input2 + ", " + gate.Output + ")",oConn);
  676.  
  677. oCommand.ExecuteNonQuery();
  678.  
  679. }
  680.  
  681. }
  682.  
  683. }
  684.  
  685. else if (userID == 0)
  686.  
  687. {
  688.  
  689. MessageBox.Show("Must Be Logged In To Complete This Action");
  690.  
  691. }
  692.  
  693. }
  694.  
  695.  
  696.  
  697. //Save As
  698.  
  699. private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
  700.  
  701. {
  702.  
  703. Console.WriteLine("File>SaveAs Clicked");
  704.  
  705. if (userID != 0)
  706.  
  707. {
  708.  
  709. SaveAsForm SaveAs = new SaveAsForm(CurrentUser());
  710.  
  711. SaveAs.ShowDialog();
  712.  
  713.  
  714.  
  715. SaveClicked(sender, e);
  716.  
  717. }
  718.  
  719. else
  720.  
  721. {
  722.  
  723. MessageBox.Show("Must Be Logged In To Complete This Action");
  724.  
  725. }
  726.  
  727. }
  728.  
  729.  
  730.  
  731. //Open
  732.  
  733. private void OpenClicked(object sender, EventArgs e)
  734.  
  735. {
  736.  
  737. Console.WriteLine("File>Open Clicked");
  738.  
  739. bool DataAvailable = false;
  740.  
  741. using (SqlConnection oConn = new SqlConnection(Main_Form.connection.ConnectionString))
  742.  
  743. {
  744.  
  745. oConn.Open();
  746.  
  747. SqlCommand oCommand = new SqlCommand("SELECT circuitName, circuitID From circuit WHERE userID='" + userID + "'", oConn);
  748.  
  749. SqlDataReader dr = oCommand.ExecuteReader();
  750.  
  751. if (dr.Read())
  752.  
  753. {
  754.  
  755. DataAvailable = true;
  756.  
  757. }
  758.  
  759. oConn.Close();
  760.  
  761. }
  762.  
  763. if (DataAvailable)
  764.  
  765. {
  766.  
  767. OpenForm Open = new OpenForm(userID);
  768.  
  769. Open.ShowDialog();
  770.  
  771. if (Open.fileChosen == true)
  772.  
  773. {
  774.  
  775. //circuitID = Open.selectedCircuitID;
  776.  
  777. GateList.Clear();
  778.  
  779. splitContainer1.Panel2.Controls.Clear();
  780.  
  781. Main_Form.circuitID = Open.selectedCircuitID;
  782.  
  783.  
  784.  
  785. using (SqlConnection oConn = new SqlConnection(Main_Form.connection.ConnectionString))
  786.  
  787. {
  788.  
  789. oConn.Open();
  790.  
  791. SqlCommand oCommand = new SqlCommand("SELECT componentID, gateType, gateX, gateY, input1, input2, output From circuitContents where circuitID='" + Main_Form.circuitID + "'", oConn);
  792.  
  793. SqlDataReader dr = oCommand.ExecuteReader();
  794.  
  795.  
  796.  
  797. while (dr.Read())
  798.  
  799. {
  800.  
  801. Classes.Gate newControl = new Classes.Gate();
  802.  
  803. newControl.SetComponentId(dr.GetInt32(0));
  804.  
  805. newControl.SetGateType(dr.GetInt32(1));
  806.  
  807. int x = dr.GetInt32(2);
  808.  
  809. int y = dr.GetInt32(3);
  810.  
  811. newControl.SetLocation(x, y);
  812.  
  813. newControl.Location = new Point(x, y);
  814.  
  815. newControl.Input1 = dr.GetInt32(4);
  816.  
  817. newControl.Input2 = dr.GetInt32(5);
  818.  
  819. newControl.Output = dr.GetInt32(6);
  820.  
  821. newControl.InToolBox = false;
  822.  
  823. newControl.ControlHasBeenMoved += new EventHandler(ControlGateMoved);
  824.  
  825. newControl.Input1Selected += new EventHandler(Input1Selection);
  826.  
  827. newControl.Input2Selected += new EventHandler(Input2Selection);
  828.  
  829. newControl.OutputSelected += new EventHandler(OutputSelection);
  830.  
  831. newControl.DeleteComponent += new EventHandler(GateRemovedFromCircuit);
  832.  
  833. splitContainer1.Panel2.Controls.Add(newControl);
  834.  
  835. GateList.Add(newControl);
  836.  
  837. }
  838.  
  839. dr.Close();
  840.  
  841. dr.Dispose();
  842.  
  843.  
  844.  
  845. oConn.Close();
  846.  
  847. }
  848.  
  849. }
  850.  
  851. }
  852.  
  853. splitContainer1.Panel2.Invalidate();
  854.  
  855. }
  856.  
  857.  
  858.  
  859. private void ExitClicked(object sender, EventArgs e)
  860.  
  861. {
  862.  
  863. Console.WriteLine("Exiting...");
  864.  
  865. Application.Exit();
  866.  
  867. }
  868.  
  869.  
  870.  
  871.  
  872.  
  873. private void gate1_ControlRemovedFromToolbox(object sender, EventArgs e)
  874.  
  875. {
  876.  
  877. Classes.Gate s = (Classes.Gate)sender;
  878.  
  879. Console.WriteLine("Casting Successful");
  880.  
  881. Classes.Gate newControl = new Classes.Gate();
  882.  
  883. Console.Write("New gate generated: ");
  884.  
  885. Console.WriteLine(newControl.ToString());
  886.  
  887. newControl.CreateNewComponentID(ref GateList);
  888.  
  889. newControl.SetGateType(s.GetGateType());
  890.  
  891. newControl.SetLocation(15, 15);
  892.  
  893. newControl.Location = new Point(15, 15);
  894.  
  895. newControl.InToolBox = false;
  896.  
  897. newControl.ControlHasBeenMoved += new EventHandler(ControlGateMoved);
  898.  
  899. newControl.Input1Selected += new EventHandler(Input1Selection);
  900.  
  901. newControl.Input2Selected += new EventHandler(Input2Selection);
  902.  
  903. newControl.OutputSelected += new EventHandler(OutputSelection);
  904.  
  905. newControl.DeleteComponent += new EventHandler(GateRemovedFromCircuit);
  906.  
  907. splitContainer1.Panel2.Controls.Add(newControl);
  908.  
  909. s.Reset();
  910.  
  911. GateList.Add(newControl);
  912.  
  913. }
  914.  
  915.  
  916.  
  917. private void loginToolStripMenuItem_Click(object sender, EventArgs e)
  918.  
  919. {
  920.  
  921. Console.WriteLine("Login");
  922.  
  923. if (userID != 0)
  924.  
  925. {
  926.  
  927. Console.WriteLine("Already logged in, logging out");
  928.  
  929. logoutToolStripMenuItem_Click(sender, e);
  930.  
  931. }
  932.  
  933. using (Login l = new Login())
  934.  
  935. {
  936.  
  937. l.ShowDialog();
  938.  
  939. if (l.loggedIn == true)
  940.  
  941. {
  942.  
  943. userID = l.lastId;
  944.  
  945. Console.WriteLine("Logging In");
  946.  
  947. }
  948.  
  949. }
  950.  
  951. }
  952.  
  953.  
  954.  
  955. private void logoutToolStripMenuItem_Click(object sender, EventArgs e)
  956.  
  957. {
  958.  
  959. Console.WriteLine("Logout");
  960.  
  961. userID = 0;
  962.  
  963. splitContainer1.Panel2.Controls.Clear();
  964.  
  965. circuitID = 0;
  966.  
  967. splitContainer1.Panel2.Invalidate();
  968.  
  969. }
  970.  
  971.  
  972.  
  973.  
  974.  
  975. private List<Classes.Gate> GetGatesWithNoInput()
  976.  
  977. {
  978.  
  979. List<Classes.Gate> Gates = new List<Classes.Gate>();
  980.  
  981. foreach (Classes.Gate Gate in GateList)
  982.  
  983. {
  984.  
  985. if (Gate.GetGateType() == 3)
  986.  
  987. {
  988.  
  989. //NOT gate must be handle seperately
  990.  
  991. if (Gate.Input1 == 0)
  992.  
  993. {
  994.  
  995. Gates.Add(Gate);
  996.  
  997. }
  998.  
  999. }
  1000.  
  1001. else
  1002.  
  1003. {
  1004.  
  1005. if ((Gate.Input1 == 0) | (Gate.Input2 == 0))
  1006.  
  1007. {
  1008.  
  1009. Gates.Add(Gate);
  1010.  
  1011. }
  1012.  
  1013. }
  1014.  
  1015.  
  1016.  
  1017. }
  1018.  
  1019. return Gates;
  1020.  
  1021. }
  1022.  
  1023.  
  1024.  
  1025. private int GetNumberOfInputs(List<Classes.Gate> Gates)
  1026.  
  1027. {
  1028.  
  1029. int NumberInputs = 0;
  1030.  
  1031. foreach (Classes.Gate Gate in Gates)
  1032.  
  1033. {
  1034.  
  1035. if (Gate.GetGateType() == 3)
  1036.  
  1037. {
  1038.  
  1039. NumberInputs += 1;
  1040.  
  1041. }
  1042.  
  1043. else
  1044.  
  1045. {
  1046.  
  1047. if (Gate.Input1 == 0)
  1048.  
  1049. {
  1050.  
  1051. NumberInputs += 1;
  1052.  
  1053. }
  1054.  
  1055. if (Gate.Input2 == 0)
  1056.  
  1057. {
  1058.  
  1059. NumberInputs += 1;
  1060.  
  1061. }
  1062.  
  1063. }
  1064.  
  1065.  
  1066.  
  1067. }
  1068.  
  1069. return NumberInputs;
  1070.  
  1071. }
  1072.  
  1073.  
  1074.  
  1075. private Classes.Gate GetGate(int ID)
  1076.  
  1077. {
  1078.  
  1079. Classes.Gate Result = new Classes.Gate();
  1080.  
  1081. foreach (Classes.Gate Gate in GateList)
  1082.  
  1083. {
  1084.  
  1085. if (Gate.GetComponentID() == ID)
  1086.  
  1087. {
  1088.  
  1089. Result = Gate;
  1090.  
  1091. }
  1092.  
  1093. }
  1094.  
  1095. return Result;
  1096.  
  1097. }
  1098.  
  1099.  
  1100.  
  1101. private int GetIdOfOutput(int StartID)
  1102.  
  1103. {
  1104.  
  1105. bool OutputFound = false;
  1106.  
  1107. int currentID = StartID;
  1108.  
  1109. while (OutputFound == false)
  1110.  
  1111. {
  1112.  
  1113. Classes.Gate CurrentGate = GetGate(currentID);
  1114.  
  1115. if (CurrentGate.Output != 0)
  1116.  
  1117. {
  1118.  
  1119. //it has an output
  1120.  
  1121. currentID = CurrentGate.Output;
  1122.  
  1123. }
  1124.  
  1125. else
  1126.  
  1127. {
  1128.  
  1129. //found our gate
  1130.  
  1131. OutputFound = true;
  1132.  
  1133. }
  1134.  
  1135. }
  1136.  
  1137. return currentID;
  1138.  
  1139. }
  1140.  
  1141.  
  1142.  
  1143. private void CalculateTruthTable(object sender, EventArgs e)
  1144.  
  1145. {
  1146.  
  1147. Console.WriteLine("Calculating Truth Table");
  1148.  
  1149. List<Classes.Gate> ListOfInputGates = GetGatesWithNoInput();
  1150.  
  1151. int NumberOfInputs = GetNumberOfInputs(ListOfInputGates);
  1152.  
  1153. Console.WriteLine("Number Of Inputs: " + NumberOfInputs.ToString());
  1154.  
  1155. if (ListOfInputGates.Count > 0)
  1156.  
  1157. {
  1158.  
  1159. int OutputID = GetIdOfOutput(ListOfInputGates[0].GetComponentID());
  1160.  
  1161. Console.WriteLine("Output ID: " + OutputID.ToString());
  1162.  
  1163. List<List<bool>> TruthTable = table(NumberOfInputs);
  1164.  
  1165. OutputForm OutputWindow = new OutputForm(ref TruthTable, ref GateList, ref OutputID, ref NumberOfInputs);
  1166.  
  1167. OutputWindow.ShowDialog();
  1168.  
  1169. OutputWindow.Dispose();
  1170.  
  1171. }
  1172.  
  1173. else
  1174.  
  1175. {
  1176.  
  1177. MessageBox.Show("No Circuit To Calculate Table For");
  1178.  
  1179. }
  1180.  
  1181. }
  1182.  
  1183.  
  1184.  
  1185. //Draw Connections
  1186.  
  1187. private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
  1188.  
  1189. {
  1190.  
  1191. Graphics g = e.Graphics;
  1192.  
  1193. g.SetClip(e.ClipRectangle);
  1194.  
  1195. float f = 4;
  1196.  
  1197. Pen p = new Pen(Color.White, f);
  1198.  
  1199. //g.DrawLine(p, new Point(20, 20), new Point(45, 34));
  1200.  
  1201. foreach (Classes.Gate Gate in GateList)
  1202.  
  1203. {
  1204.  
  1205. //draw lines
  1206.  
  1207. //draw line 1
  1208.  
  1209. if (Gate.Input1 != 0)
  1210.  
  1211. {
  1212.  
  1213. try
  1214.  
  1215. {
  1216.  
  1217. Classes.Gate InputGate = GetGate(Gate.Input1);
  1218.  
  1219. Point Point1 = new Point((InputGate.Location.X + 72), (InputGate.Location.Y + 48));
  1220.  
  1221. Point Point2 = new Point(Gate.Location.X, (Gate.Location.Y + 36));;
  1222.  
  1223. if (Gate.GetGateType() == 3)
  1224.  
  1225. {
  1226.  
  1227. Point2 = new Point(Gate.Location.X, (Gate.Location.Y + 48));
  1228.  
  1229. }
  1230.  
  1231. g.DrawLine(p, Point1, Point2);
  1232.  
  1233. }
  1234.  
  1235. catch
  1236.  
  1237. {
  1238.  
  1239. Gate.Input1 = 0;
  1240.  
  1241. }
  1242.  
  1243. }
  1244.  
  1245. if (Gate.GetGateType() != 3)
  1246.  
  1247. {
  1248.  
  1249. //The second line is not applicable to a NOT gate
  1250.  
  1251. if (Gate.Input2 != 0)
  1252.  
  1253. {
  1254.  
  1255. try
  1256.  
  1257. {
  1258.  
  1259. Classes.Gate InputGate = GetGate(Gate.Input2);
  1260.  
  1261. Point Point1 = new Point((InputGate.Location.X + 72), (InputGate.Location.Y + 48));
  1262.  
  1263. Point Point2 = new Point(Gate.Location.X, (Gate.Location.Y + 60));
  1264.  
  1265. g.DrawLine(p, Point1, Point2);
  1266.  
  1267. }
  1268.  
  1269. catch
  1270.  
  1271. {
  1272.  
  1273. Gate.Input1 = 0;
  1274.  
  1275. }
  1276.  
  1277. }
  1278.  
  1279. }
  1280.  
  1281. }
  1282.  
  1283. }
  1284.  
  1285. }
  1286.  
  1287. }
Advertisement
Add Comment
Please, Sign In to add comment