Hello everyone,
So far I finally got it with some help from a friend, here is the code below, i have made a class equations, for all 4 equations, and class number to store the numbers temperarily. the good news is i found online a example that sorta helped except they used a temp array, for me i just used 3 variables (two double and 1 string for equation) the only problem is sometimes it will add, subtract, multiply or divide and sometimes it wont, also my backspace and clear button doesnt work as thought as it would, can anyone check what am i doing wrong and if you can correct and post the answer? Thank you in advance i will continue my answer on this but so far nothing.
main form
class equationsCode:using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { numbers num = new numbers(); public Form1() { InitializeComponent(); } enum operation { add, subtract, divide, multiply } private bool hasDecimal = false; private void exitToolStripMenuItem_Click(object sender, EventArgs e) { this.Close(); } private void Form1_Load(object sender, EventArgs e) { } private void btnclear_Click(object sender, EventArgs e) { num.numbering = 0; num.numbering2 = 0; firstNumberSet = false; txtbox1.Clear(); } private void txtbox1_TextChanged(object sender, EventArgs e) { } private bool firstNumberSet = false; private void btn1_Click(object sender, EventArgs e) { try { if (!firstNumberSet) { num.numbering = 1; } else { num.numbering2 = 1; } txtbox1.Text += "1"; } catch(Exception) { MessageBox.Show("WHAT DID YOU DO!!"); } } private void btn2_Click(object sender, EventArgs e) { try { if (!firstNumberSet) { num.numbering = 2; } else { num.numbering2 = 2; } txtbox1.Text += "2"; } catch (Exception) { MessageBox.Show("WHAT DID YOU DO!!"); } } private void btn3_Click(object sender, EventArgs e) { try { if (!firstNumberSet) { num.numbering = 3; } else { num.numbering2 = 3; } txtbox1.Text += "3"; } catch (Exception) { MessageBox.Show("WHAT DID YOU DO!!"); } } private void btn4_Click(object sender, EventArgs e) { try { if (!firstNumberSet) { num.numbering = 4; } else { num.numbering2 = 4; } txtbox1.Text += "4"; } catch (Exception) { MessageBox.Show("WHAT DID YOU DO!!"); } } private void btn5_Click(object sender, EventArgs e) { try { if (!firstNumberSet) { num.numbering = 5; } else { num.numbering2 = 5; } txtbox1.Text += "5"; } catch (Exception) { MessageBox.Show("WHAT DID YOU DO!!"); } } private void btn6_Click(object sender, EventArgs e) { try { if (!firstNumberSet) { num.numbering = 6; } else { num.numbering2 = 6; } txtbox1.Text += "6"; } catch (Exception) { MessageBox.Show("WHAT DID YOU DO!!"); } } private void btn7_Click(object sender, EventArgs e) { try { if (!firstNumberSet) { num.numbering = 7; } else { num.numbering2 = 7; } txtbox1.Text += "7"; } catch (Exception) { MessageBox.Show("WHAT DID YOU DO!!"); } } private void btn8_Click(object sender, EventArgs e) { try { if (!firstNumberSet) { num.numbering = 8; } else { num.numbering2 = 8; } txtbox1.Text += "8"; } catch (Exception) { MessageBox.Show("WHAT DID YOU DO!!"); } } private void btn9_Click(object sender, EventArgs e) { try { if (!firstNumberSet) { num.numbering = 9; } else { num.numbering2 = 9; } txtbox1.Text += "9"; } catch (Exception) { MessageBox.Show("WHAT DID YOU DO!!"); } } private void btn0_Click(object sender, EventArgs e) { try { if (!firstNumberSet) { num.numbering = 0; } else { num.numbering2 = 0; } txtbox1.Text += "0"; } catch (Exception) { MessageBox.Show("WHAT DID YOU DO!!"); } } operation currentOperation; private void btnplus_Click(object sender, EventArgs e) { txtbox1.Text += "+"; currentOperation = operation.add; firstNumberSet = true; // this is where we decide that the first number is done and we should //start on the second one } private void btnsubtract_Click(object sender, EventArgs e) { txtbox1.Text += "-"; // this is where we decide that the first number is done and we should currentOperation = operation.subtract; //start on the second one firstNumberSet = true; } private void btnmultiply_Click(object sender, EventArgs e) { txtbox1.Text += "*"; currentOperation = operation.multiply; // this is where we decide that the first number is done and we should firstNumberSet = true; //start on the second one } private void btndivide_Click(object sender, EventArgs e) { txtbox1.Text += "/"; currentOperation = operation.divide; // this is where we decide that the first number is done and we should firstNumberSet = true; //start on the second one } private void aboutToolStripMenuItem_Click(object sender, EventArgs e) { aboutfrm bout = new aboutfrm(); bout.Show(); } private void btnequal_Click(object sender, EventArgs e) { Equations equat = new Equations(); switch (currentOperation) { case operation.add: // get info from the buttons either divide, multiply, add, and subtract txtbox1.Text = equat.add(num.numbering, num.numbering2).ToString(); break; case operation.subtract: txtbox1.Text = equat.subtract(num.numbering, num.numbering2).ToString(); break; case operation.multiply: txtbox1.Text = equat.mulitply(num.numbering, num.numbering2).ToString(); break; case operation.divide: txtbox1.Text = equat.divide(num.numbering, num.numbering2).ToString(); break; } } private void btnback_Click(object sender, EventArgs e) { //Declare locals needed string str; int loc; //Make sure the text length is > 1 if (txtbox1.Text.Length > 0) { //Get the next to last character str = txtbox1.Text.Substring(txtbox1.Text.Length - 1); //Check if its a decimal if (str == ".") { //If it is toggle the hasDecimal flag hasDecimal = false; } //Get the length of the string loc = txtbox1.Text.Length; //Remove the last character, incrementing by 1 txtbox1.Text = txtbox1.Text.Remove(loc - 1, 1); } } private void btndecimal_Click(object sender, EventArgs e) { if (firstNumberSet) { //Check if it already has a decimal (if it does then do nothing) if (!hasDecimal) { //Check to make sure the length is > than 1 //Dont want user to add decimal as first character if (txtbox1.Text.Length != 0) { //Make sure 0 isnt the first number if (txtbox1.Text != "0") { //It met all our requirements so add the zero txtbox1.Text += btndecimal.Text; //Toggle the flag to true (only 1 decimal per calculation) hasDecimal = true; } } else { //Since the length isnt > 1 //make the text 0. txtbox1.Text = "0."; } } } } } }
class numbersCode:using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WindowsFormsApplication1 { class Equations { public double add(double num1, double num2) { return (num1 + num2); } public double subtract(double num1, double num2) { return num1 - num2; } public double mulitply(double num1, double num2) { return num1 * num2; } public double divide(double num1, double num2) { return num1 / num2; } } }
Thanks again in advanceCode:using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WindowsFormsApplication1 { class numbers { private double number; private double number2; public double numbering { get { return number; } set { number = number * 1; // this will move the other digits left one number += value; // this puts the new one in the ones column } } public double numbering2 { get { return number2; } set { number2 = number2 * 1; // this will move the other digits left one number2 += value; // this puts the new one in the ones column } } } }
why does it blank out the *** on cl***?
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks