lpiazza Posted January 31, 2022 Share Posted January 31, 2022 I am to do a simple calculator in C# windows forum. This is my first time doing C# and I've scoured what I could to figure this out. I'm not looking to copy and paste, I want to understand what I'm doing. My biggest issue at this moment is figuring out how to have the calculator solve a line of numbers. My calculator you can hit the buttons to enter numbers and the expression or whatever, but I can only figure out how to do it with two numbers. Any Ideas? Link to comment Share on other sites More sharing options...
lpiazza Posted January 31, 2022 Author Share Posted January 31, 2022 (edited) using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Piazza_Simple_Calculator { public partial class Form1 : Form { string input = string.Empty; string math1 = string.Empty; string math2 = string.Empty; char operation; double result = 0.0; public Form1() { InitializeComponent(); } private void Equal_Click(object sender, EventArgs e) { math2 = input; double num1, num2; double.TryParse(math1, out num1); double.TryParse(math2, out num2); if(operation=='+') { result = num1 + num2; Screen.Text = result.ToString(); } else if (operation == '-') { result = num1 - num2; Screen.Text = result.ToString(); } else if (operation == '*') { result = num1 * num2; Screen.Text = result.ToString(); } else if (operation == '/') { if(num2!=0) { result = num1 / num2; Screen.Text = result.ToString(); } else { Screen.Text = "You Cannot Divide By 0!"; } } } private void B0_Click(object sender, EventArgs e) { this.Screen.Text = ""; input += "0"; this.Screen.Text += input; Lbl_eq.Text += input; } private void B1_Click(object sender, EventArgs e) { this.Screen.Text = ""; input += "1"; this.Screen.Text += input; Lbl_eq.Text += input; } private void button7_Click(object sender, EventArgs e) { this.Screen.Text = ""; input += "2"; this.Screen.Text += input; Lbl_eq.Text += input; } private void B3_Click(object sender, EventArgs e) { input += "3"; Screen.Text = "3"; Lbl_eq.Text = Lbl_eq.Text + "3"; } private void B4_Click(object sender, EventArgs e) { input += "4"; Screen.Text = "4"; Lbl_eq.Text = Lbl_eq.Text + "4"; } private void B5_Click(object sender, EventArgs e) { input += "5"; Screen.Text = "5"; Lbl_eq.Text = Lbl_eq.Text + "5"; } private void B6_Click(object sender, EventArgs e) { input += "6"; Screen.Text = "6"; Lbl_eq.Text = Lbl_eq.Text + "6"; } private void B7_Click(object sender, EventArgs e) { input += "7"; Screen.Text = "7"; Lbl_eq.Text = Lbl_eq.Text + "7"; } private void B8_Click(object sender, EventArgs e) { input += "8"; Screen.Text = "8"; Lbl_eq.Text =Lbl_eq.Text+"8"; } private void B9_Click(object sender, EventArgs e) { input += "9"; Screen.Text = "9"; Lbl_eq.Text = Lbl_eq.Text + "9"; } private void Clear_Click(object sender, EventArgs e) { Screen.Text = ""; Lbl_eq.Text = ""; } private void Back_Click(object sender, EventArgs e) { int length = Screen.TextLength - 1; string text = Screen.Text; Screen.Clear(); for(int i=0; i<length;i++) { Screen.Text=Screen.Text + text[i]; } } private void Dec_Click(object sender, EventArgs e) { input += "."; Screen.Text = Screen.Text + "."; } private void Add_Click(object sender, EventArgs e) { math1 = input; operation = '+'; Screen.Text ="" + "+"; Lbl_eq.Text = Lbl_eq.Text + "+"; input = string.Empty; } private void Form1_Load(object sender, EventArgs e) { } private void Sub_Click(object sender, EventArgs e) { math1 = input; operation = '-'; Screen.Text = "-"; Lbl_eq.Text = Lbl_eq.Text + "-"; input = string.Empty; } private void Mult_Click(object sender, EventArgs e) { math1 = input; operation = '*'; Screen.Text = "*"; Lbl_eq.Text = Lbl_eq.Text + "*"; input = string.Empty; } private void Div_Click(object sender, EventArgs e) { math1 = input; operation = '/'; Screen.Text = "/"; Lbl_eq.Text = Lbl_eq.Text + "/"; input = string.Empty; } } } Yea I know half of the code doesn't work right, I've been stuck for a bit and it's hard to find things. Edited January 31, 2022 by lpiazza Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now