How to convert this to a string? [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am trying to make a simple visual calculator and if you press 1 on it it currently just adds the number to it that would mean that 1+1 is 2. I want it to be 11. How do I do this? How will my code look after I did it?
{
double value1 = 0;
double value2 = 0;
double finalResult = 0;
double output = 0;
bool second = false;
bool plus = false;
bool minus = false;
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
if (second == false)
{
value1 = value1 + 1;
output = value1 * 10 + 1;
textresult.Text = output.ToString();
}
else if (second == true)
{
value2 = value2 + 1;
output = value2;
textresult.Text = output.ToString();
}
}
private void button2_Click(object sender, RoutedEventArgs e)
{
if (second == false)
{
value1 = value1 + 2;
output = value1;
textresult.Text = output.ToString();
}
else if (second == true)
{
value2 = value2 + 2;
output = value2;
textresult.Text = output.ToString();
}
}
private void button3_Click(object sender, RoutedEventArgs e)
{
if (second == false)
{
value1 = value1 + 3;
output = value1;
textresult.Text = output.ToString();
}
else if (second == true)
{
value2 = value2 + 3;
output = value2;
textresult.Text = output.ToString();
}
}
private void button4_Click(object sender, RoutedEventArgs e)
{
if (second == false)
{
value1 = value1 + 4;
output = value1;
textresult.Text = output.ToString();
}
else if (second == true)
{
value2 = value2 + 4;
output = value2;
textresult.Text = output.ToString();
}
}
private void button5_Click(object sender, RoutedEventArgs e)
{
if (second == false)
{
value1 = value1 + 5;
output = value1;
textresult.Text = output.ToString();
}
else if (second == true)
{
value2 = value2 + 5;
output = value2;
textresult.Text = output.ToString();
}
}
private void button6_Click(object sender, RoutedEventArgs e)
{
if (second == false)
{
value1 = value1 + 6;
output = value1;
textresult.Text = output.ToString();
}
else if (second == true)
{
value2 = value2 + 6;
output = value2;
textresult.Text = output.ToString();
}
}
private void button7_Click(object sender, RoutedEventArgs e)
{
if (second == false)
{
value1 = value1 + 7;
output = value1;
textresult.Text = output.ToString();
}
else if (second == true)
{
value2 = value2 + 7;
output = value2;
textresult.Text = output.ToString();
}
}
private void button8_Click(object sender, RoutedEventArgs e)
{
if (second == false)
{
value1 = value1 + 8;
output = value1;
textresult.Text = output.ToString();
}
else if (second == true)
{
value2 = value2 + 8;
output = value2;
textresult.Text = output.ToString();
}
}
private void button9_Click(object sender, RoutedEventArgs e)
{
if (second == false)
{
value1 = value1 + 9;
output = value1;
textresult.Text = output.ToString();
}
else if (second == true)
{
value2 = value2 + 9;
output = value2;
textresult.Text = output.ToString();
}
}
private void buttonClear_Click(object sender, RoutedEventArgs e)
{
//remember to add all the other values :)
value1 = 0;
value2 = 0;
finalResult = 0;
output = 0;
second = false;
plus = false;
minus = false;
textresult.Text = output.ToString();
}
private void buttonPlus_Click(object sender, RoutedEventArgs e)
{
output = 0;
textresult.Text = output.ToString();
second = true;
plus = true;
}
private void buttonMinus_Click(object sender, RoutedEventArgs e)
{
output = 0;
textresult.Text = output.ToString();
second = true;
minus = true;
}
private void buttonIs_Click(object sender, RoutedEventArgs e)
{
if (plus == true)
{
finalResult = value1 + value2;
output = finalResult;
textresult.Text = output.ToString();
}
else if (minus == true)
{
finalResult = value1 - value2;
output = finalResult;
textresult.Text = output.ToString();
}
}
}

You can convert int - string with this.
int i = 1;
string s = Convert.ToString(i);

Just wanted to share a some ideas/recommendations to simplify your code
enum Operators
{
NewOperation = -1,
NotSet = 0,
Plus = 1,
Minus = 2
//...
};
int value1, value2;
const string RESET_VALUE = "0";
Operators operation;
public MainWindow() { InitializeComponent(); }
private void MainWindow_Load(object sender, EventArgs e) { textresult.Text = RESET_VALUE; }
private void button1_Click(object sender, EventArgs e) { AppendNumber(1); }
private void button2_Click(object sender, EventArgs e) { AppendNumber(2); }
private void button3_Click(object sender, EventArgs e) { AppendNumber(3); }
private void button4_Click(object sender, EventArgs e) { AppendNumber(4); }
private void button5_Click(object sender, EventArgs e) { AppendNumber(5); }
private void button6_Click(object sender, EventArgs e) { AppendNumber(6); }
private void button7_Click(object sender, EventArgs e) { AppendNumber(7); }
private void button8_Click(object sender, EventArgs e) { AppendNumber(8); }
private void button9_Click(object sender, EventArgs e) { AppendNumber(9); }
private void button0_Click(object sender, EventArgs e) { AppendNumber(0); }
private void AppendNumber(int selected_number)
{
if (operation == Operators.NewOperation)
{
operation = Operators.NotSet;
textresult.Text = $"{selected_number}";
}
else if (textresult.Text.StartsWith("0"))
{
textresult.Text = $"{selected_number}";
}
else
{
textresult.Text = $"{textresult.Text}{selected_number}";
}
if (operation == Operators.NotSet)
value1 = int.Parse(textresult.Text);
else
value2 = int.Parse(textresult.Text);
}
private void buttonPlus_Click(object sender, EventArgs e)
{
operation = Operators.Plus;
textresult.Text = RESET_VALUE;
}
private void buttonMinus_Click(object sender, EventArgs e)
{
operation = Operators.Minus;
textresult.Text = RESET_VALUE;
}
private void buttonIs_Click(object sender, EventArgs e)
{
switch (operation)
{
case Operators.Minus: textresult.Text = $"{(value1 - value2)}"; break;
case Operators.Plus: textresult.Text = $"{(value1 + value2)}"; break;
}
operation = Operators.NewOperation;
value1 = int.Parse(textresult.Text); //save result for next operation
value2 = 0;
}
private void buttonClear_Click(object sender, EventArgs e)
{
operation = Operators.NotSet;
textresult.Text = RESET_VALUE;
value1 = value2 = 0;
}

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
}

Calculator Application Crashes

I'm trying to build a simple calculator application in C# and I have no idea why it crashes when I do the following steps.
Enter 0.2
Click Subtraction
Enter 0
The application crashes immediately. I assume it is something to do with the Zero() function since that is what is executed when the Zero button is clicked. The conditional statements are meant to take care of instances where shouldn't occur like consecutive ciphers and so on. Here is the source code. The functions for the other digits are identical, by the way.
public partial class MainWindow : Window
{
protected double firstNumber, secondNumber;
protected string textBoxContents;
protected int selectedFunction;
public MainWindow()
{
InitializeComponent();
firstNumber = 0;
secondNumber = 0;
selectedFunction = 0;
textBoxContents = "0";
}
private void Zero(object sender, RoutedEventArgs e)
{
if (Convert.ToDouble(textBoxContents) > 0 || textBoxContents[textBoxContents.Length - 1] == '.')
{
if(selectedFunction != 0)
textBoxContents = textBoxContents + "0";
}
else if (textBoxContents == null)
{
textBoxContents = textBoxContents + "0";
}
ResultBox.Content = textBoxContents;
}
private void One(object sender, RoutedEventArgs e)
{
textBoxContents = textBoxContents + "1";
ResultBox.Content = textBoxContents;
}
private void Decimal(object sender, RoutedEventArgs e)
{
textBoxContents = textBoxContents + ".";
ResultBox.Content = textBoxContents;
}
private void Addition(object sender, RoutedEventArgs e)
{
firstNumber = Convert.ToDouble(textBoxContents);
textBoxContents = null;
selectedFunction = 1;
}
private void Subtraction(object sender, RoutedEventArgs e)
{
firstNumber = Convert.ToDouble(textBoxContents);
textBoxContents = null;
selectedFunction = 2;
}
private void Multiplication(object sender, RoutedEventArgs e)
{
firstNumber = Convert.ToDouble(textBoxContents);
textBoxContents = null;
selectedFunction = 3;
}
private void Division(object sender, RoutedEventArgs e)
{
firstNumber = Convert.ToDouble(textBoxContents);
textBoxContents = null;
selectedFunction = 4;
}
private void Result(object sender, RoutedEventArgs e)
{
secondNumber = Convert.ToDouble(textBoxContents);
double thirdNumber = 0;
switch (selectedFunction)
{
case 1:
thirdNumber = firstNumber + secondNumber;
break;
case 2:
thirdNumber = firstNumber - secondNumber;
break;
case 3:
thirdNumber = firstNumber * secondNumber;
break;
case 4:
thirdNumber = firstNumber / secondNumber;
break;
default:
break;
}
textBoxContents = Convert.ToString(thirdNumber);
ResultBox.Content = textBoxContents;
}
private void ClearEverything(object sender, RoutedEventArgs e)
{
textBoxContents = null;
firstNumber = 0;
secondNumber = 0;
selectedFunction = 1;
ResultBox.Content = Convert.ToString(0);
}
private void ToggleNegative(object sender, RoutedEventArgs e)
{
if (Convert.ToDouble(textBoxContents) != 0)
{
textBoxContents = Convert.ToString(Convert.ToDouble(textBoxContents) * -1);
ResultBox.Content = textBoxContents;
}
else
ResultBox.Content = Convert.ToString(0);
}
}
private void Zero(object sender, RoutedEventArgs e)
{
if (Convert.ToDouble(textBoxContents) > 0 ||
textBoxContents[textBoxContents.Length - 1] == '.')
{
if(selectedFunction != 0)
textBoxContents = textBoxContents + "0";
}
else if (textBoxContents == null)
{
textBoxContents = textBoxContents + "0";
}
ResultBox.Content = textBoxContents;
}
That logic seems a tidge off. If the value of the text box is empty then it's going to blow up because of the indexer on the other side of the ||. I think this can be rewritten to say:
private void Zero(object sender, RoutedEventArgs e)
{
var dblVal = Convert.ToDouble(textBoxContents.Text);
textBoxContents.Text = dblVal.ToString();
ResultBox.Content = textBoxContents.Text;
}
In other words, if the text box is empty the conversion will yield 0.0; if it ends in a 1. it will yield 1.0; if it's .5 it will yield 0.5. Just leverage the Convert.
The decimal separator is localized, are you sure that you are using the right culture ("," instead of ".")?
If that's the issue, check out this Stack Question
textBoxContents is null after clicking on the substraction button.
Instead of textBoxContents = null; use textBoxContents = "0"; or textBoxContents = string.Empty;. Why do you set it to null anyway?
Calling textBoxContents.Length in your Zero method causes a NullReferenceException.
As others mentioned before your logic in Zero() seams a bit circuitous and and certainly could be smaller.
in Subtraction function you are doing
textBoxContents = null;
and then in zero you have
textBoxContents[textBoxContents.Length - 1]
thats why it crashes
you should check for null before any operation on textBoxContents

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.)

windows forms input continuously showing

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

Categories