windows forms input continuously showing - c#

I cannot for the life of me figure out how to continuously show the input and then output of the numbers for this.
What do you do to store the numbers somewhere and then show them? I've gone in so many circles that I have confused myself to oblivion.
I know what needs to be done but not how or exactly where to do it?
public partial class Form1 : Form
{
char c;
double num1;
double num2;
public Form1()
{
InitializeComponent();
}
private void btn0_Click(object sender, EventArgs e)
{
txtBox.Text += 0;
}
private void btn1_Click(object sender, EventArgs e)
{
txtBox.Text += 1;
}
private void btn2_Click(object sender, EventArgs e)
{
txtBox.Text += 2;
}
private void btn3_Click(object sender, EventArgs e)
{
txtBox.Text += 3;
}
private void btn4_Click(object sender, EventArgs e)
{
txtBox.Text += 4;
}
private void btn5_Click(object sender, EventArgs e)
{
txtBox.Text += 5;
}
private void btn6_Click(object sender, EventArgs e)
{
txtBox.Text += 6;
}
private void btn7_Click(object sender, EventArgs e)
{
txtBox.Text += 7;
}
private void btn8_Click(object sender, EventArgs e)
{
txtBox.Text += 8;
}
private void btn9_Click(object sender, EventArgs e)
{
txtBox.Text += 9;
}
private void btnDecimal_Click(object sender, EventArgs e)
{
if (!txtBox.Text.Contains('.'))
txtBox.Text += '.';
}
private void btnAddition_Click(object sender, EventArgs e)
{
c = '+';
num1 = double.Parse(txtBox.Text);
txtBox.Text = string.Empty;
}
private void btnSubtraction_Click(object sender, EventArgs e)
{
c = '-';
num1 = double.Parse(txtBox.Text);
txtBox.Text = string.Empty;
}
private void btnMultiplication_Click(object sender, EventArgs e)
{
c = '*';
num1 = double.Parse(txtBox.Text);
txtBox.Text = string.Empty;
}
private void btnDivision_Click(object sender, EventArgs e)
{
c = '/';
num1 = double.Parse(txtBox.Text);
txtBox.Text = string.Empty;
}
private void btnClear_Click(object sender, EventArgs e)
{
txtBox.Clear();
}
private void btnEqual_Click(object sender, EventArgs e)
{
num2 = double.Parse(txtBox.Text);
double result;
switch (c)
{
case '+':
result = num1 + num2;
txtBox.Text = result.ToString();
break;
case '-':
result = num1 - num2;
txtBox.Text = result.ToString();
break;
case '/':
if (num2 != 0)
{
result = num1 / num2;
txtBox.Text = result.ToString();
}
else
{
txtBox.Text = "You can't divide by zero... sign up for Math 100 please =)";
}
break;
case '*':
result = num1 * num2;
txtBox.Text = result.ToString();
break;
}
}
}
}

I'll let you wire this up, but here is what I threw together.
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private string op;
private string num1;
private string num2;
private void handleNumberButtonClick(object sender, System.EventArgs e)
{
var num = Convert.ToInt16(((Button)sender).Tag);
if (op == null)
num1 += num;
else
num2 += num;
PrintEquation(num1, op, num2);
}
private void PrintEquation(string first, string oper = null, string second = null, string equals = null, string result = null)
{
txtBox.Text = first + oper + second + equals + result;
}
private void handleOperatorButtonClick(object sender, System.EventArgs e)
{
op = ((Button)sender).Tag.ToString();
PrintEquation(num1, op);
}
private void btnDecimal_Click(object sender, EventArgs e)
{
if (op == null && !num1.Contains(".")) num1 += ".";
if (op != null && !num2.Contains(".")) num2 += ".";
this.PrintEquation(num1, op, num2);
}
private void btnClear_Click(object sender, EventArgs e)
{
txtBox.Clear();
op = null;
num1 = null;
num2 = null;
}
private void btnEqual_Click(object sender, EventArgs e)
{
double result = 0;
var first = Convert.ToDouble(num1);
var second = Convert.ToDouble(num2);
switch (op)
{
case "+":
result = first + second;
break;
case "-":
result = first - second;
break;
case "/":
if (second != 0)
{
result = first / second;
}
else
{
errorLbl.Text = "You can't divide by zero... sign up for Math 100 please =)";
}
break;
case "*":
result = first * second;
break;
}
this.PrintEquation(num1, op, num2, "=", result.ToString());
}
}
To give you a clue on how things go together, this is what the button should look like in the .designer.cs file:
this.btn0.Location = new System.Drawing.Point(12, 60);
this.btn0.Name = "btn0";
this.btn0.Size = new System.Drawing.Size(75, 23);
this.btn0.TabIndex = 1;
this.btn0.Tag = "0";
this.btn0.Text = "btn0";
this.btn0.UseVisualStyleBackColor = true;
this.btn0.Click += new System.EventHandler(this.handleNumberButtonClick);
Notice the .Tag value and the eventhandler.
The Operators should look like this(this is add, notice the tag and the Click Event handler):
this.addButton.Location = new System.Drawing.Point(178, 60);
this.addButton.Name = "addButton";
this.addButton.Size = new System.Drawing.Size(75, 23);
this.addButton.TabIndex = 11;
this.addButton.Tag = "+";
this.addButton.Text = "add";
this.addButton.UseVisualStyleBackColor = true;
this.addButton.Click += new System.EventHandler(this.handleOperatorButtonClick);
And the decimal, like this:
this.button4.Location = new System.Drawing.Point(178, 176);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(75, 23);
this.button4.TabIndex = 15;
this.button4.Tag = ".";
this.button4.Text = "decimal";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.btnDecimal_Click);
Oh, and throw this in your clear button definition
this.button5.Click += new System.EventHandler(this.btnClear_Click);
Special thanks to #Mark Hall and #Martin James for their insights

Related

How to fix the code for Subtaction, multiplication and division C#

I'm Making a Calculator. I have created the code when I input two numbers and press equal it does its job such as subtracting, adding, multiplying and dividing. help me to make the code for subtracting, multiplying and dividing continuously without pressing equal for addition i have done the code please help me for the buttons. I will include the whole code below please help me fix it.
namespace WindowsFormsApp1
{
public partial class Windows10Calcoo : Form
{
string Input, NumStore1, NumStore2 = string.Empty ;
char Operation;
double Answ;
public Windows10Calcoo()
{
InitializeComponent();
}
private void PlusBtn_Click(object sender, EventArgs e)
{
//Creating + and Storing Num1
NumStore1 = Input;
Operation = '+';
Input = string.Empty;
//Storing Number 2
NumStore2 = Input;
double num1, num2;
double.TryParse(NumStore1, out num1);
double.TryParse(NumStore2, out num2);
// Adding Code
Answ += num1 + num2;
textBox1.Text = Answ.ToString();
}
private void SubBtn_Click(object sender, EventArgs e)
{
NumStore1 = Input;
Operation = '-';
Input = string.Empty;
//Storing Number 2
NumStore2 = Input;
double num1, num2;
double.TryParse(NumStore1, out num1);
double.TryParse(NumStore2, out num2);
// Adding Code
Answ += num1 - num2 ;
textBox1.Text = Answ.ToString();
}
private void DivideBtn_Click(object sender, EventArgs e)
{
NumStore1 = Input;
Operation = '/';
Input = string.Empty;
}
private void MultiBtn_Click(object sender, EventArgs e)
{
NumStore1 = Input;
Operation = '*';
Input = string.Empty;
}
private void BackSpaceBtn_Click(object sender, EventArgs e)
{
int textlength = textBox1.Text.Length;
textBox1.Text = textBox1.Text.Substring(0, textlength - 1);
}
private void OneBtn_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
Input += "1";
this.textBox1.Text += Input;
}
private void TwoBtn_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
Input += "2";
this.textBox1.Text += Input;
}
private void ThreeBtn_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
Input += "3";
this.textBox1.Text += Input;
}
private void FourBtn_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
Input += "4";
this.textBox1.Text += Input;
}
private void FiveBtn_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
Input += "5";
this.textBox1.Text += Input;
}
private void SixBtn_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
Input += "6";
this.textBox1.Text += Input;
}
private void SevenBtn_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
Input += "7";
this.textBox1.Text += Input;
}
private void EightBtn_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
Input += "8";
this.textBox1.Text += Input;
}
private void NineBtn_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
Input += "9";
this.textBox1.Text += Input;
}
private void ZeroBtn_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
Input += "0";
this.textBox1.Text += Input;
}
private void DecimalBtn_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
Input += ".";
this.textBox1.Text += Input;
}
private void ClearBtn_Click(object sender, EventArgs e)
{
Input += "C";
this.textBox1.Text = "";
this.Input = string.Empty;
this.NumStore1 = string.Empty;
this.NumStore2 = string.Empty;
Answ = 0;
}
private void EqualBtn_Click(object sender, EventArgs e)
{
NumStore2 = Input;
double num1, num2;
double.TryParse(NumStore1, out num1);
double.TryParse(NumStore2, out num2);
this.textBox1.Text = "";
this.Input = string.Empty;
this.NumStore1 = string.Empty;
this.NumStore2 = string.Empty;
if (Operation == '+')
{
Answ = num1 + num2;
textBox1.Text = Answ.ToString();
}
else if (Operation == '-')
{
Answ = num1 - num2;
textBox1.Text = Answ.ToString();
}
else if (Operation == '*')
{
Answ = num1 * num2;
textBox1.Text = Answ.ToString();
}
else if (Operation == '/')
{
if (num2 != 0)
{
Answ = num1 / num2;
textBox1.Text = Answ.ToString();
}
else
{
textBox1.Text = "DIV/Zero!";
}
}
}
private void Windows10Calcoo_Load(object sender, EventArgs e)
{
}
}
}
for subtraction button its not subtracting sits continuesly adding when i use [Answ += num1 - num2 ;], if i use [Answ -= num1 - num2 ;] its adding negatively .

Calculator Trouble: Only getting second value I enter

I created a basic calculator, but each time I enter the second value and press the equal button, I get no result back. Only the value I entered 2nd.
My methods for +-*/ are in a separate class. What will the best way be to resolve this problem? It is probably a simple error, but I can't find it. Can you also please give me an explanation on what I did wrong. Thanks in Advance.
public sealed partial class Calculator : Page
{
public double num01, num02;
int operater;
public Calculator()
{
this.InitializeComponent();
}
private void btn1_Click(object sender, RoutedEventArgs e)
{
txtcalcdisplay.Text =txtcalcdisplay.Text+ "1";
}
private void btn2_Click(object sender, RoutedEventArgs e)
{
txtcalcdisplay.Text = txtcalcdisplay.Text + "2";
}
private void btn3_Click(object sender, RoutedEventArgs e)
{
txtcalcdisplay.Text = txtcalcdisplay.Text + "3";
}
private void btn4_Click(object sender, RoutedEventArgs e)
{
txtcalcdisplay.Text = txtcalcdisplay.Text + "4";
}
private void btn5_Click(object sender, RoutedEventArgs e)
{
txtcalcdisplay.Text = txtcalcdisplay.Text + "5";
}
private void btn6_Click(object sender, RoutedEventArgs e)
{
txtcalcdisplay.Text = txtcalcdisplay.Text + "6";
}
private void btn7_Click(object sender, RoutedEventArgs e)
{
txtcalcdisplay.Text = txtcalcdisplay.Text + "7";
}
private void btn8_Click(object sender, RoutedEventArgs e)
{
txtcalcdisplay.Text = txtcalcdisplay.Text + "8";
}
private void btn9_Click(object sender, RoutedEventArgs e)
{
txtcalcdisplay.Text = txtcalcdisplay.Text + "9";
}
private void Clear_Click(object sender, RoutedEventArgs e)
{
txtcalcdisplay.Text = string.Empty;
}
private void btnsubtract_Click(object sender, RoutedEventArgs e)
{
num01 = Convert.ToDouble(txtcalcdisplay.Text);
txtcalcdisplay.Text = "";
operater = '1';
}
private void btnadd_Click(object sender, RoutedEventArgs e)
{
num01 = Convert.ToDouble(txtcalcdisplay.Text);
txtcalcdisplay.Text = "";
operater = '2';
}
private void btnmultiply_Click(object sender, RoutedEventArgs e)
{
num01 = Convert.ToDouble(txtcalcdisplay.Text);
txtcalcdisplay.Text = "";
operater = '3';
}
private void btndivide_Click(object sender, RoutedEventArgs e)
{
num01 = Convert.ToDouble(txtcalcdisplay.Text);
txtcalcdisplay.Text = "";
operater = '4';
}
private void btnequals_Click(object sender, RoutedEventArgs e)
{
switch (operater)
{
case 1:
num02 = Convert.ToDouble(txtcalcdisplay.Text);
CalculationClass sub = new CalculationClass();
double answer= sub.Subtract(num01, num02);
txtcalcdisplay.Text = answer.ToString();
break;
case 2:
num02 = Convert.ToDouble(txtcalcdisplay.Text);
CalculationClass add = new CalculationClass();
answer= add.Addition(num01, num02);
txtcalcdisplay.Text = answer.ToString();
break;
case 3:
num02 = Convert.ToDouble(txtcalcdisplay.Text);
CalculationClass mult = new CalculationClass();
answer = mult.Multiply(num01, num02);
txtcalcdisplay.Text = answer.ToString();
break;
case 4:
num02 = Convert.ToDouble(txtcalcdisplay.Text);
CalculationClass div = new CalculationClass();
answer = div.Div(num01, num02);
txtcalcdisplay.Text = Convert.ToString(answer);
break;
}
}
private void btnback_Click(object sender, RoutedEventArgs e)
{
}
private void btnplusdivideminus_Click(object sender, RoutedEventArgs e)
{
}
private void btncomma_Click(object sender, RoutedEventArgs e)
{
}
private void btngallery_Click(object sender, RoutedEventArgs e)
{
}
private void btncontact_Click(object sender, RoutedEventArgs e)
{
}
private void num0_Click(object sender, RoutedEventArgs e)
{
txtcalcdisplay.Text += "0";
num01 = Convert.ToDouble(txtcalcdisplay.Text);
}
}
class CalculationClass
{
double answer;
public double Addition(double x, double y)
{
answer = x + y;
return answer;
}
public double Subtract(double x, double y)
{
answer = x - y;
return answer;
}
public double Multiply(double x, double y)
{
answer = x * y;
return answer;
}
public double Div(double x, double y)
{
answer = x / y;
return answer;
}
}
I'm not 100% certain, but I'm fairly sure this has to do with the fact that your variable operater is an int but you're assigning a character to it (this works - characters can be assigned to ints) and then comparing it back to the integer (eg doing 1 == '1')
int x = '1';
Console.WriteLine(x); // outputs 49
Console.WriteLine(x == 1); // outputs false
So to fix it either use the characters in your switch:
switch(operater){
case '1': ...
}
Or assign the integer 1,2,3,4 rather than the character '1','2,'3','4'
private void btnadd_Click(object sender, RoutedEventArgs e)
{
num01 = Convert.ToDouble(txtcalcdisplay.Text);
txtcalcdisplay.Text = "";
operater = 2; // here
}
So in your operator click handlers (the ones for +,-,*,/ etc.) you are setting your operater variable to a character '1','2', etc. Well, your operater variable is an int. But wait, you shouldn't be able to assign a char to an int?! Well, the compiler does an implicit conversion here (language spec says it should).
So what ends up happening when you do operater = '1' is that operater gets assigned 49 ('1''s ASCII value). Then when you get to your equals button click handler it hits that switch statement. And guess what? You don't have a case for 49. So nothing happens and you keep seeing your second number as the text on screen.
So to fix it, remove the single quotes around your numbers you assign to operater in your operator handlers. IE:
private void btnsubtract_Click(object sender, RoutedEventArgs e)
{
num01 = Convert.ToDouble(txtcalcdisplay.Text);
txtcalcdisplay.Text = "";
operater = 1; // <-- change this line to be like this, removed the single quotes
}

Simple Calculator Woes(Can't get the equals button to work more than once)

I am a programming noob and I can't seem to get this calculator working right. I have already coded all the buttons and allowed keyboard input(which i have not put into the code excerpt because its irrelevent) from the user but like the title suggests, the equals button will not work for more than one sum. I'm not sure what's causing the problem. Any suggestions would be appreciated.
Here's my code.
public partial class MainWindow : Window
{
string input = string.Empty; //String storing user input
string operand1 = string.Empty; //String storing first operand
string operand2 = string.Empty; //String storing second operand
char operation; //char for operarion
double result = 0.0; //calculated result
bool operationCompleted = false;
public MainWindow()
{
InitializeComponent();
}
private void btn_Zero_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
input += "0";
this.textBox.Text += input;
if(operationCompleted)
{
btn_Clear_Click(sender, e);
operationCompleted = false;
}
btn_Equals.Focus();
}
private void btn_One_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
input += "1";
this.textBox.Text += input;
if (operationCompleted)
{
btn_Clear_Click(sender, e);
operationCompleted = false;
}
btn_Equals.Focus();
}
private void btn_Two_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
input += "2";
this.textBox.Text += input;
if (operationCompleted)
{
btn_Clear_Click(sender, e);
operationCompleted = false;
}
btn_Equals.Focus();
}
private void btn_Three_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
input += "3";
this.textBox.Text += input;
if (operationCompleted)
{
btn_Clear_Click(sender, e);
operationCompleted = false;
}
btn_Equals.Focus();
}
private void btn_Four_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
input += "4";
this.textBox.Text += input;
if (operationCompleted)
{
input = string.Empty;
textBox.Text = string.Empty;
operand1 = string.Empty;
operand2 = string.Empty;
operationCompleted = false;
}
btn_Equals.Focus();
}
private void btn_Five_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
input += "5";
this.textBox.Text += input;
if (operationCompleted)
{
btn_Clear_Click(sender, e);
operationCompleted = false;
}
btn_Equals.Focus();
}
private void btn_Six_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
input += "6";
this.textBox.Text += input;
if (operationCompleted)
{
btn_Clear_Click(sender, e);
operationCompleted = false;
}
btn_Equals.Focus();
}
private void btn_Seven_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
input += "7";
this.textBox.Text += input;
if (operationCompleted)
{
btn_Clear_Click(sender, e);
operationCompleted = false;
}
btn_Equals.Focus();
}
private void btn_Eight_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
input += "8";
this.textBox.Text += input;
if (operationCompleted)
{
btn_Clear_Click(sender, e);
operationCompleted = false;
}
btn_Equals.Focus();
}
private void btn_Nine_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
input += "9";
this.textBox.Text += input;
if (operationCompleted)
{
btn_Clear_Click(sender, e);
operationCompleted = false;
}
btn_Equals.Focus();
}
private void btn_Dot_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
input += ".";
this.textBox.Text += input;
btn_Equals.Focus();
}
private void btn_Minus_Click(object sender, RoutedEventArgs e)
{
operand1 = input;
operation += '-';
input = string.Empty;
textBox.Text = string.Empty;
btn_Equals.Focus();
}
private void btn_Plus_Click(object sender, RoutedEventArgs e)
{
operand1 = input;
operation += '+';
input = string.Empty;
textBox.Text = string.Empty;
btn_Equals.Focus();
}
private void btn_Multiply_Click(object sender, RoutedEventArgs e)
{
operand1 = input;
operation += '*';
input = string.Empty;
textBox.Text = string.Empty;
btn_Equals.Focus();
}
private void btn_Divide_Click(object sender, RoutedEventArgs e)
{
operand1 = input;
operation += '/';
input = string.Empty;
textBox.Text = string.Empty;
btn_Equals.Focus();
}
// The equals works for the first sum but not for any after it
private void btn_Equals_Click(object sender, RoutedEventArgs e)
{
{
operand2 = input;
double num1, num2;
double.TryParse(operand1, out num1);
double.TryParse(operand2, out num2);
if (operation == '+')
{
result = num1 + num2;
textBox.Text = result.ToString();
}
else if (operation == '-')
{
result = num1 - num2;
textBox.Text = result.ToString();
}
else if (operation == '*')
{
result = num1 * num2;
textBox.Text = result.ToString();
}
else if (operation == '/')
{
if (num1 != 0 || num2 != 0)
{
result = num1 / num2;
textBox.Text = result.ToString();
}
else
{
textBox.Text = "Cannot divide by zero";
}
}
operationCompleted = true;
this.Focus();
}
}
private void btn_Clear_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
this.input = string.Empty;
this.operand1 = string.Empty;
this.operand2 = string.Empty;
this.result = 0.0;
operationCompleted = false;
}
}
You need to clear the operation. You have operation += '/'. So every time someone clicks one of the operation buttons you are appending that operation to the string. When you click the equals button, you do not have a catch for bad operations so nothing happens.
Suggestion:
Clear the operation on finish.
Add a default else if to account for bad operations so that you can handle the error.

C# Calculator Logic

when I press 9 and then press times the display shows 9 and when I press 9 again it'll add it to the first number (9) instead of clearing the first 9. so it thinks its 9*99. how do I clear the display on the first input after an operation?
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 WindowsFormsApplication2
{
public partial class Form1 : Form
{
string input = string.Empty;
string operand1 = string.Empty;
string operand2 = string.Empty;
char operation;
double result = 0.0;
public Form1()
{
InitializeComponent();
}
private void Multiply_Click(object sender, EventArgs e)
{
operation = '*';
if (operand1 != "")
{
calculate();
}
else
{
operand1 = input;
}
}
private void Minus_Click(object sender, EventArgs e)
{
operation = '-';
if (operand1 != "")
{
calculate();
}
else
{
operand1 = input;
}
}
private void Addition_Click(object sender, EventArgs e)
{
operation = '+';
if (operand1 != "")
{
calculate();
}
else
{
operand1 = input;
}
}
private void Divide_Click(object sender, EventArgs e)
{
operation = '/';
if (operand1 != "")
{
calculate();
}
else
{
operand1 = input;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void five_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "5";
this.textBox1.Text += input;
}
private void Zero_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "0";
this.textBox1.Text += input;
}
private void one_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "1";
this.textBox1.Text += input;
}
private void two_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "2";
this.textBox1.Text += input;
}
private void three_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "3";
this.textBox1.Text += input;
}
private void four_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "4";
this.textBox1.Text += input;
}
private void six_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "6";
this.textBox1.Text += input;
}
private void seven_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "7";
this.textBox1.Text += input;
}
private void eight_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "8";
this.textBox1.Text += input;
}
private void nine_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "9";
this.textBox1.Text += input;
}
private void Equal_Click(object sender, EventArgs e)
{
calculate();
}
private void Decimal_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += ".";
this.textBox1.Text += input;
}
private void Clear_Click(object sender, EventArgs e)
{
textBox1.Clear();
}
public void calculate()
{
Console.WriteLine("performing operation" + operand1 + " " + operation + " " + operand2 + "= ???" + "the input is?" + input);
operand2 = input;
double num1, num2;
double.TryParse(operand1, out num1);
double.TryParse(operand2, out num2);
if (operation == '*')
{
result = num1 * num2;
}
if (operation == '-')
{
result = num1 * num2;
}
if (operation == '+')
{
result = num1 * num2;
}
if (operation == '/')
{
result = num1 * num2;
}
textBox1.Text = result.ToString();
input = result.ToString();
}
}
}
Add the following to your class Form1:
bool operationExcecuted = false;
And in your calculate method add the following line:
operationExcecuted = true;
And in every number input check if an operation is excecuted. If so clear the input and add a new number. For example the number 9:
private void nine_Click(object sender, EventArgs e)
{
if(operationExcecuted)
{
input = String.Empty;
operationExcecuted = false;
}
this.textBox1.Text = "";
input += "9";
this.textBox1.Text += input;
}
I think, that's because of you adding
input += "9"
this.textBox1.text += input;
You're always adding the 9 to the variable input. Try cleaning input. Before adding a new number.

how to convert from double with decimal point number to binary in c# visual studio

i'm wrtting a code in visual studio c# that converts integers from binary to decimal mode and from decimal mode to binary but i want it to converts numbers with decimal points from decimal mode to binary how can i do this please help me and tell me what modifies i must put in my code
this is my code for the calculator :
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 assignment2
{
public partial class Form1 : Form
{
const int asciiDiff = 48;
double num1 = 0, num2 = 0, result = 0;
double fact = 1;
int[] iHexaNumeric = new int[] { 10, 11, 12, 13, 14, 15 };
char[] cHexa = new char[] { 'A', 'B', 'C', 'D', 'E', 'F' };
String a = "";
char op;
bool b = false;
const int base10 = 10;
public Form1()
{
//
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
a = DisplayResult.Text.ToString();
DisplayResult.Text = a + "1";
}
private void buttonTow_Click(object sender, EventArgs e)
{
a = DisplayResult.Text.ToString();
DisplayResult.Text = a + "2";
}
private void buttonThree_Click(object sender, EventArgs e)
{
a = DisplayResult.Text.ToString();
DisplayResult.Text = a + "3";
}
private void buttonFour_Click(object sender, EventArgs e)
{
a = DisplayResult.Text.ToString();
DisplayResult.Text = a + "4";
}
private void buttonFive_Click(object sender, EventArgs e)
{
a = DisplayResult.Text.ToString();
DisplayResult.Text = a + "5";
}
private void buttonSix_Click(object sender, EventArgs e)
{
a = DisplayResult.Text.ToString();
DisplayResult.Text = a + "6";
}
private void buttonSeven_Click(object sender, EventArgs e)
{
a = DisplayResult.Text.ToString();
DisplayResult.Text = a + "7";
}
private void buttonEight_Click(object sender, EventArgs e)
{
a = DisplayResult.Text.ToString();
DisplayResult.Text = a + "8";
}
private void buttonNine_Click(object sender, EventArgs e)
{
a = DisplayResult.Text.ToString();
DisplayResult.Text = a + "9";
}
private void buttonZero_Click(object sender, EventArgs e)
{
a = DisplayResult.Text.ToString();
DisplayResult.Text = a + "0";
}
private void buttonPlus_Click(object sender, EventArgs e)
{
num1 = Convert.ToDouble(DisplayResult.Text.ToString());
op = '+';
DisplayResult.Text = string.Empty;
}
private void buttonMinus_Click(object sender, EventArgs e)
{
num1 = Convert.ToDouble(DisplayResult.Text.ToString());
op = '-';
DisplayResult.Text = string.Empty;
}
private void buttonMultipler_Click(object sender, EventArgs e)
{
num1 = Convert.ToDouble(DisplayResult.Text.ToString());
op = '*';
DisplayResult.Text = string.Empty;
}
private void buttonDivider_Click(object sender, EventArgs e)
{
num1 = Convert.ToDouble(DisplayResult.Text.ToString());
op = '/';
DisplayResult.Text = string.Empty;
}
private void buttonEqual_Click(object sender, EventArgs e)
{
if (DisplayResult.Text == "")
return;
else
{
try
{
num2 = Convert.ToDouble(DisplayResult.Text.ToString());
switch (op)
{
case '+': //suma
result = (num1 + num2);
DisplayResult.Text = result.ToString();
break;
case '-': //resta
result = (num1 - num2);
DisplayResult.Text = result.ToString();
break;
case '*': //multiply
result = (num1 * num2);
DisplayResult.Text = result.ToString();
break;
case '/': //division
if (num2 != 0)
{
result = (num1 / num2);
DisplayResult.Text = result.ToString();
}
else
{
DisplayResult.Text = "Can't divide by 0";
}
break;
}
}
catch (Exception ex)
{
MessageBox.Show("Unexpected error occured. Details: " +
ex.Message);
}
}
}
private void buttonBackSpace_Click(object sender, EventArgs e)
{
try
{
String Value = DisplayResult.Text.ToString();
int temp = Value.Length;
if (temp == 1)
DisplayResult.Text = String.Empty;
else
{
DisplayResult.Text = DisplayResult.Text.Substring(0, temp - 1);
}
}
catch (Exception ex)
{
MessageBox.Show("Unexpected error in buttonBackSpace occured. Details: " +
ex.Message);
}
}
private void buttonClear_Click(object sender, EventArgs e)
{
DisplayResult.Text = String.Empty;
}
private void buttonDecimal_Click(object sender, EventArgs e)
{
if (DisplayResult.Text.Contains("."))
{
return;
}
DisplayResult.Text += ".";
}
private void DecimalRadioButton_CheckedChanged(object sender, EventArgs e)
{
if (DecimalRadioButton.Checked == true)
{
DisplayResult.Text= BaseToDecimal(DisplayResult.Text.ToString(), 2).ToString();
}
buttonTow.Enabled = true;
buttonThree.Enabled = true;
buttonFour.Enabled = true;
buttonFive.Enabled = true;
buttonSix.Enabled = true;
buttonSeven.Enabled = true;
buttonEight.Enabled = true;
buttonNine.Enabled = true;
}
private void BinaryRadioButton_CheckedChanged(object sender, EventArgs e)
{
if (BinaryRadioButton.Checked == true)
{
DisplayResult.Text = DecimalToBase(Convert.ToInt16(DisplayResult.Text.ToString()), 2);
buttonTow.Enabled = false;
buttonThree.Enabled = false;
buttonFour.Enabled = false;
buttonFive.Enabled = false;
buttonSix.Enabled = false;
buttonSeven.Enabled = false;
buttonEight.Enabled = false;
buttonNine.Enabled = false;
}
}
string DecimalToBase(int iDec, int numbase)
{
string strBin = "";
int[] result = new int[32];
int MaxBit = 32;
for (; iDec > 0; iDec /= numbase)
{
int rem = iDec % numbase;
result[--MaxBit] = rem;
}
for (int i = 0; i < result.Length; i++)
if ((int)result.GetValue(i) >= base10)
strBin += cHexa[(int)result.GetValue(i) % base10];
else
strBin += result.GetValue(i);
strBin = strBin.TrimStart(new char[] { '0' });
return strBin;
}
int BaseToDecimal(string sBase, int numbase)
{
int dec = 0;
int b;
int iProduct = 1;
string sHexa = "";
if (numbase > base10)
for (int i = 0; i < cHexa.Length; i++)
sHexa += cHexa.GetValue(i).ToString();
for (int i = sBase.Length - 1; i >= 0; i--, iProduct *= numbase)
{
string sValue = sBase[i].ToString();
if (sValue.IndexOfAny(cHexa) >= 0)
b = iHexaNumeric[sHexa.IndexOf(sBase[i])];
else
b = (int)sBase[i] - asciiDiff;
dec += (b * iProduct);
}
return dec;
}
}
}
When converting a number with a decimal point from decimal to binary, this is what you do: first you take the part before the decimal point and convert it (in the usual way) to binary; and the part after the decimal point you multiply by 2 and see if it is >= 1; if it is not, write 0 and keep multiplying. You are done when it is = 1.00. For example:
2.25 -
You take the 0.25;
0.25 * 2 = 0.50 --> 0,
0.50 * 2 = 1.00 --> 1, and you just read the numbers.
So, 0.25 would be 0.01 (2.25 would be 10.01 binary.)

Categories