Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LogicCalculator : Form
- {
- private TextBox display;
- private Button[] buttons;
- private string operand1 = "";
- private string operand2 = "";
- private string operation = "";
- private bool operationCompleted = false;
- public LogicCalculator()
- {
- display = new TextBox { Top = 10, Left = 10, Width = 200, ReadOnly = true };
- buttons = new Button[8];
- string[] labels = { "0", "1", "AND", "OR", "NOT", "XOR", "IMPLIES", "EQUIVALENT" };
- Size = new Size(250, 200);
- FormBorderStyle = FormBorderStyle.FixedSingle;
- MaximizeBox = false;
- for (int i = 0; i < 8; i++)
- {
- buttons[i] = new Button
- {
- Text = labels[i],
- Top = 50 + (i / 2) * 30,
- Left = 10 + (i % 2) * 60,
- Width = 50
- };
- buttons[i].Click += new EventHandler(Button_Click);
- Controls.Add(buttons[i]);
- }
- Controls.Add(display);
- }
- private void Button_Click(object sender, EventArgs e)
- {
- Button button = (Button)sender;
- string buttonText = button.Text;
- if (operationCompleted && (buttonText == "0" || buttonText == "1"))
- {
- operand1 = "";
- operand2 = "";
- operation = "";
- operationCompleted = false;
- buttons[0].Enabled = true; // Enable button "0"
- buttons[1].Enabled = true; // Enable button "1"
- }
- if (buttonText == "0" || buttonText == "1")
- {
- if (operation == "" && operand1 == "")
- {
- operand1 = buttonText;
- display.Text = operand1;
- buttons[0].Enabled = false; // Disable button "0"
- buttons[1].Enabled = false; // Disable button "1"
- }
- else if (operation != "" && operand2 == "")
- {
- operand2 = buttonText;
- display.Text = operand1 + " " + operation + " " + operand2;
- }
- }
- else if (buttonText == "AND" || buttonText == "OR" || buttonText == "XOR" || buttonText == "IMPLIES" || buttonText == "EQUIVALENT")
- {
- if (operand1 != "" && operation == "")
- {
- operation = buttonText;
- display.Text = operand1 + " " + operation;
- buttons[0].Enabled = true; // Enable button "0"
- buttons[1].Enabled = true; // Enable button "1"
- }
- }
- else if (buttonText == "NOT")
- {
- if (operand1 != "")
- {
- Convert(ref operand1, out bool a);
- operand1 = (!a).ToString();
- display.Text = operand1;
- operationCompleted = true;
- buttons[0].Enabled = true; // Enable button "0"
- buttons[1].Enabled = true; // Enable button "1"
- }
- }
- if (operand1 != "" && operand2 != "" && operation != "")
- {
- Convert(ref operand1, out bool a);
- Convert(ref operand2, out bool b);
- bool result = false;
- switch (operation)
- {
- case "AND":
- result = a && b;
- break;
- case "OR":
- result = a || b;
- break;
- case "XOR":
- result = a ^ b;
- break;
- case "IMPLIES":
- result = !a || b;
- break;
- case "EQUIVALENT":
- result = a == b;
- break;
- }
- display.Text = operand1 + " " + operation + " " + operand2 + " = " + result.ToString();
- operand1 = result.ToString();
- operand2 = "";
- operation = "";
- operationCompleted = true;
- }
- }
- public static void Convert(ref string str, out bool a)
- {
- a = str.Equals("1");
- }
- [STAThread]
- static void Main()
- {
- Application.Run(new LogicCalculator());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment