Click here to Skip to main content
12,691,231 members (30,222 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
Hello codeproject

My problem is that i currently making a calculator in c#, and this is some of the problems i have:

It crashes when i divide by 0.

When i wanna make it show the whole calculation, it will reverse the signs used
(plus = minus, minus = plus and so on)


How can i solve them??
I hope you will understand my problem, and try help me.
Sorry if my english is bad.
PS some of it is in Danish so just ignore the comments after //
Thanks for the help


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 Calculator{
    public partial class CalForm : Form {
        bool plus = false;
        bool minus = false;
        bool multiply = false;
        bool divide = false;
        bool equal = false;
        public CalForm() {
            InitializeComponent();
            Text = "Calculator";
        }

        private void CalForm_Load(object sender, EventArgs e) {
        }
        // 1
        private void button1_Click(object sender, EventArgs e) {
            if (equal) {
                textBox1.Text = "";
                equal = false;
            }
            textBox1.Text = textBox1.Text + "1";
        }
        // 2
        private void button2_Click(object sender, EventArgs e) {
            if (equal) {
                textBox1.Text = "";
                equal = false;
            }
            textBox1.Text = textBox1.Text + "2";
        }
        // 3
        private void button3_Click(object sender, EventArgs e) {
            if (equal) {
                textBox1.Text = "";
                equal = false;
            }
            textBox1.Text = textBox1.Text + "3";
        }
        // 4
        private void button4_Click(object sender, EventArgs e) {
            if (equal) {
                textBox1.Text = "";
                equal = false;
            }
            textBox1.Text = textBox1.Text + "4";
        }
        // 5
        private void button5_Click(object sender, EventArgs e) {
            if (equal)
            {
                textBox1.Text = "";
                equal = false;
            }
            textBox1.Text = textBox1.Text + "5";
        }
        // 6
        private void button6_Click(object sender, EventArgs e) {
            if (equal) {
                textBox1.Text = "";
                equal = false;
            }
            textBox1.Text = textBox1.Text + "6";
        }
        // 7
        private void button7_Click(object sender, EventArgs e) {
            if (equal) {
                textBox1.Text = "";
                equal = false;
            }
            textBox1.Text = textBox1.Text + "7";
        }
        // 8
        private void button8_Click(object sender, EventArgs e) {
            if (equal) {
                textBox1.Text = "";
                equal = false;
            }
            textBox1.Text = textBox1.Text + "8";
        }
        // 9
        private void button9_Click(object sender, EventArgs e) {
            if (equal) {
                textBox1.Text = "";
                equal = false;
            }
            textBox1.Text = textBox1.Text + "9";
        }
        // 0
        private void button14_Click(object sender, EventArgs e) {
            if (equal) {
                textBox1.Text = "";
                equal = false;
            }
            textBox1.Text = textBox1.Text + "0";
        }
        // ,
        private void button15_Click(object sender, EventArgs e) {
            if (equal) {
                textBox1.Text = "";
                equal = false;
            }
            if (textBox1.Text.Contains(",")) {
                return;
            }
            else {
                textBox1.Text = textBox1.Text + ",";
            }
        }
        //Dette er plus
        private void button11_Click(object sender, EventArgs e) {
            if (textBox1.Text == "") {
                return;
            }
            else {
                plus = true;
                textBox1.Tag = textBox1.Text;
                textBox1.Text = "+";
            }
        }
        //Dette er "lig med" for regne reglerne i lommeregneren
        private void button13_Click(object sender, EventArgs e) {
            equal = true;
            if (plus) {
                decimal deo = Convert.ToDecimal(textBox1.Tag) + Convert.ToDecimal(textBox1.Text);
                textBox1.Text = deo.ToString();
            }
            if (minus) {
                decimal deo = Convert.ToDecimal(textBox1.Tag) - Convert.ToDecimal(textBox1.Text);
                textBox1.Text = deo.ToString();
            }
            if (multiply) {
                decimal deo = Convert.ToDecimal(textBox1.Tag) * Convert.ToDecimal(textBox1.Text);
                textBox1.Text = deo.ToString();
            }
            if (divide) {
                decimal deo = Convert.ToDecimal(textBox1.Tag) / Convert.ToDecimal(textBox1.Text);
                textBox1.Text = deo.ToString();
            }
        }
        //Dette er minus
        private void button12_Click(object sender, EventArgs e) {
            if (textBox1.Text == "") {
                return;
            }
            else {
                minus = true;
                textBox1.Tag = textBox1.Text;
                textBox1.Text = "-";
        }
    }
        //Dette er gange
        private void button16_Click(object sender, EventArgs e) {
            if (textBox1.Text == "") {
                return;
            }
            else {
                multiply = true;
                textBox1.Tag = textBox1.Text;
                textBox1.Text = "";
            }
        }
        //Dette er clear
        private void button17_Click(object sender, EventArgs e) {
            plus = minus = multiply = divide = false;
            textBox1.Tag = textBox1.Text;
            textBox1.Text = "";
        }
        // Dette er divider
        private void button10_Click(object sender, EventArgs e) {
            if (textBox1.Text == "") {
                return;
            }
            else {
                divide = true;
                textBox1.Tag = textBox1.Text;
                textBox1.Text = "";
            }
        }
        //Dette er pi
        private void button19_Click(object sender, EventArgs e) {
            if (equal) {
                textBox1.Text = "";
                equal = false;
            }
            textBox1.Text = textBox1.Text + "3,14";
        }
        //Dette er tau
        private void button21_Click(object sender, EventArgs e) {
            if (equal) {
                textBox1.Text = "";
                equal = false;
            }
            textBox1.Text = textBox1.Text + "6,28";
        }
        //Dette er e
        private void button20_Click(object sender, EventArgs e) {
            if (equal) {
                textBox1.Text = "";
                equal = false;
            }
            textBox1.Text = textBox1.Text + "2,71";
            // Dette er så man ikke kan dividere med 0
            
            }
        }
    }
}
Posted 10-Nov-12 10:03am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

divide by zero:
private void button13_Click(object sender, EventArgs e) {
            equal = true;
            if (plus) {
                decimal deo = Convert.ToDecimal(textBox1.Tag) + Convert.ToDecimal(textBox1.Text);
                textBox1.Text = deo.ToString();
            }
            if (minus) {
                decimal deo = Convert.ToDecimal(textBox1.Tag) - Convert.ToDecimal(textBox1.Text);
                textBox1.Text = deo.ToString();
            }
            if (multiply) {
                decimal deo = Convert.ToDecimal(textBox1.Tag) * Convert.ToDecimal(textBox1.Text);
                textBox1.Text = deo.ToString();
            }
            if (divide) {
                try
                {
                    decimal deo = Convert.ToDecimal(textBox1.Tag) / Convert.ToDecimal(textBox1.Text);
                    textBox1.Text = deo.ToString();
                }
                catch (DivideByZeroException)
                {
                  MessageBox.Show ("Cannot Divide by zero!")
                }
            }
        }


and the second problem is much more complicated... You have operators type boolean, so when you declare Plus_Click, Minus_Click and so on, you have to set the others to false.

Example:
//Dette er plus
        private void button11_Click(object sender, EventArgs e) {
            if (textBox1.Text == "") {
                return;
            }
            else {
                plus = true;
                minus = false;
                multiply = false;
                divide = false;
                equal = false;
                textBox1.Tag = textBox1.Text;
                textBox1.Text = "+";
            }
        }


In this code I can't see any more errors, maybe it could be in different class.

I hope this helps.

-Pepin z Hane
  Permalink  
v2
Comments
Nelek 10-Nov-12 18:46pm
   
OP's commet to you moved from non-solution below
WOW it worked!!
Thank you so much ^^
Pepin z Hane 12-Nov-12 10:27am
   
for little... so accept the solution.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

just put that on try catch block
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170117.1 | Last Updated 10 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100