Dandomi

Logical Calculator 2

Dec 10th, 2023
795
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.18 KB | Source Code | 0 0
  1. public class LogicCalculator : Form
  2. {
  3.     private TextBox display;
  4.     private Button[] buttons;
  5.     private string operand1 = "";
  6.     private string operand2 = "";
  7.     private string operation = "";
  8.     private bool operationCompleted = false;
  9.  
  10.     public LogicCalculator()
  11.     {
  12.         display = new TextBox { Top = 10, Left = 10, Width = 200, ReadOnly = true };
  13.         buttons = new Button[8];
  14.         string[] labels = { "0", "1", "AND", "OR", "NOT", "XOR", "IMPLIES", "EQUIVALENT" };
  15.  
  16.         Size = new Size(250, 200);
  17.         FormBorderStyle = FormBorderStyle.FixedSingle;
  18.         MaximizeBox = false;
  19.  
  20.         for (int i = 0; i < 8; i++)
  21.         {
  22.             buttons[i] = new Button
  23.             {
  24.                 Text = labels[i],
  25.                 Top = 50 + (i / 2) * 30,
  26.                 Left = 10 + (i % 2) * 60,
  27.                 Width = 50
  28.             };
  29.             buttons[i].Click += new EventHandler(Button_Click);
  30.             Controls.Add(buttons[i]);
  31.         }
  32.  
  33.         Controls.Add(display);
  34.     }
  35.  
  36.     private void Button_Click(object sender, EventArgs e)
  37.     {
  38.         Button button = (Button)sender;
  39.         string buttonText = button.Text;
  40.  
  41.         if (operationCompleted && (buttonText == "0" || buttonText == "1"))
  42.         {
  43.             operand1 = "";
  44.             operand2 = "";
  45.             operation = "";
  46.             operationCompleted = false;
  47.             buttons[0].Enabled = true; // Enable button "0"
  48.             buttons[1].Enabled = true; // Enable button "1"
  49.         }
  50.  
  51.         if (buttonText == "0" || buttonText == "1")
  52.         {
  53.             if (operation == "" && operand1 == "")
  54.             {
  55.                 operand1 = buttonText;
  56.                 display.Text = operand1;
  57.                 buttons[0].Enabled = false; // Disable button "0"
  58.                 buttons[1].Enabled = false; // Disable button "1"
  59.             }
  60.             else if (operation != "" && operand2 == "")
  61.             {
  62.                 operand2 = buttonText;
  63.                 display.Text = operand1 + " " + operation + " " + operand2;
  64.             }
  65.         }
  66.         else if (buttonText == "AND" || buttonText == "OR" || buttonText == "XOR" || buttonText == "IMPLIES" || buttonText == "EQUIVALENT")
  67.         {
  68.             if (operand1 != "" && operation == "")
  69.             {
  70.                 operation = buttonText;
  71.                 display.Text = operand1 + " " + operation;
  72.                 buttons[0].Enabled = true; // Enable button "0"
  73.                 buttons[1].Enabled = true; // Enable button "1"
  74.             }
  75.         }
  76.         else if (buttonText == "NOT")
  77.         {
  78.             if (operand1 != "")
  79.             {
  80.                 Convert(ref operand1, out bool a);
  81.                 operand1 = (!a).ToString();
  82.                 display.Text = operand1;
  83.                 operationCompleted = true;
  84.                 buttons[0].Enabled = true; // Enable button "0"
  85.                 buttons[1].Enabled = true; // Enable button "1"
  86.             }
  87.         }
  88.  
  89.         if (operand1 != "" && operand2 != "" && operation != "")
  90.         {
  91.             Convert(ref operand1, out bool a);
  92.             Convert(ref operand2, out bool b);
  93.             bool result = false;
  94.  
  95.             switch (operation)
  96.             {
  97.                 case "AND":
  98.                     result = a && b;
  99.                     break;
  100.                 case "OR":
  101.                     result = a || b;
  102.                     break;
  103.                 case "XOR":
  104.                     result = a ^ b;
  105.                     break;
  106.                 case "IMPLIES":
  107.                     result = !a || b;
  108.                     break;
  109.                 case "EQUIVALENT":
  110.                     result = a == b;
  111.                     break;
  112.             }
  113.  
  114.             display.Text = operand1 + " " + operation + " " + operand2 + " = " + result.ToString();
  115.  
  116.             operand1 = result.ToString();
  117.             operand2 = "";
  118.             operation = "";
  119.             operationCompleted = true;
  120.         }
  121.     }
  122.  
  123.     public static void Convert(ref string str, out bool a)
  124.     {
  125.         a = str.Equals("1");
  126.     }
  127.  
  128.     [STAThread]
  129.     static void Main()
  130.     {
  131.         Application.Run(new LogicCalculator());
  132.     }
  133. }
  134.  
Advertisement
Add Comment
Please, Sign In to add comment