Building a calculator in C#, stuck on doing the math stuff.

Started by
49 comments, last by eFoDay 11 years, 9 months ago
So I fixed up the code, but instead of 25 86 it just shows 25 + in the message box.

[source lang="csharp"]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 Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<string> numbers = new List<string>();
string currentString = "";
char[] mathSymbols = { '+', '-', '*', '/'};

private Boolean lastCharIsSymbol { get; set; }

bool add = false;
bool subtract = false;
bool multiply = false;
bool divide = false;

private void button0_Click(object sender, EventArgs e)
{
textBox1.Text += button0.Text;
lastCharIsSymbol = false;
}

private void button1_Click(object sender, EventArgs e)
{
textBox1.Text += button1.Text;
lastCharIsSymbol = false;
}

private void button2_Click(object sender, EventArgs e)
{
textBox1.Text += button2.Text;
lastCharIsSymbol = false;
}

private void button3_Click(object sender, EventArgs e)
{
textBox1.Text += button3.Text;
lastCharIsSymbol = false;
}

private void button4_Click(object sender, EventArgs e)
{
textBox1.Text += button4.Text;
lastCharIsSymbol = false;
}

private void button5_Click(object sender, EventArgs e)
{
textBox1.Text += button5.Text;
lastCharIsSymbol = false;
}

private void button6_Click(object sender, EventArgs e)
{
textBox1.Text += button6.Text;
lastCharIsSymbol = false;
}

private void button7_Click(object sender, EventArgs e)
{
textBox1.Text += button7.Text;
lastCharIsSymbol = false;
}

private void button8_Click(object sender, EventArgs e)
{
textBox1.Text += button8.Text;
lastCharIsSymbol = false;
}

private void button9_Click(object sender, EventArgs e)
{
textBox1.Text += button9.Text;
lastCharIsSymbol = false;
}

private void buttonAdd_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || lastCharIsSymbol)
return;
else
{
textBox1.Text += buttonAdd.Text;
currentString = textBox1.Text;
currentString = textBox1.Text.TrimEnd(mathSymbols);
//string[] numbers = currentString.Split(mathSymbols);
lastCharIsSymbol = true;
add = true;
}
}

private void buttonSubtract_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || lastCharIsSymbol)
return;
else
{
textBox1.Text += buttonSubtract.Text;
currentString = textBox1.Text;
currentString = textBox1.Text.TrimEnd(mathSymbols);
//string[] numbers = currentString.Split(mathSymbols);
lastCharIsSymbol = true;
subtract = true;
}
}

private void buttonMultiply_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || lastCharIsSymbol)
return;
else
{
textBox1.Text += buttonMultiply.Text;
currentString = textBox1.Text;
currentString = textBox1.Text.TrimEnd(mathSymbols);
//string[] numbers = currentString.Split(mathSymbols);
lastCharIsSymbol = true;
multiply = true;
}
}

private void buttonDivide_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || lastCharIsSymbol)
return;
else
{
textBox1.Text += buttonDivide.Text;
currentString = textBox1.Text;
currentString = textBox1.Text.TrimEnd(mathSymbols);
//string[] numbers = currentString.Split(mathSymbols);
lastCharIsSymbol = true;
divide = true;
}
}

private void buttonClear_Click(object sender, EventArgs e)
{
textBox1.Text = String.Empty;
}

private void buttonEquals_Click(object sender, EventArgs e)
{
if (add)
{

}

if (subtract)
{

}

if (multiply)
{

}

if (divide)
{

}
MessageBox.Show(currentString);
}
}
}[/source]
Advertisement
Ah-ha, this is why I/we need to read the documentation. According to http://msdn.microsoft.com/en-us/library/system.string.trimend.aspx:

The TrimEnd method removes from the current string all trailing characters that are in the trimChars parameter. The trim operation stops when the first character that is not in trimChars is encountered at the end of the string. For example, if the current string is "123abc456xyz789" and trimChars contains the digits from "1" through "9", the TrimEnd method returns "123abc456xyz".[/quote]

We want to take the string and remove all of the math operators from it, yes? So it seems that TrimEnd() doesn't quite do what we want - since it trims EVERYTHING from the end of the string up until it finds a character in the list of characters we gave it. Let's go see what else is available.

It looks like there isn't any string method that removes all instances of a particular character. There is however, String.Replace. Now think: how can we "delete" the characters (or sub-strings) we don't want to see in our output string, given that we know this method exists? What kind of a "replace" operation is a "delete" operation analogous to?

Ah-ha, this is why I/we need to read the documentation. According to http://msdn.microsof...ng.trimend.aspx:

The TrimEnd method removes from the current string all trailing characters that are in the trimChars parameter. The trim operation stops when the first character that is not in trimChars is encountered at the end of the string. For example, if the current string is "123abc456xyz789" and trimChars contains the digits from "1" through "9", the TrimEnd method returns "123abc456xyz".


We want to take the string and remove all of the math operators from it, yes? So it seems that TrimEnd() doesn't quite do what we want - since it trims EVERYTHING from the end of the string up until it finds a character in the list of characters we gave it. Let's go see what else is available.

It looks like there isn't any string method that removes all instances of a particular character. There is however, String.Replace. Now think: how can we "delete" the characters (or sub-strings) we don't want to see in our output string, given that we know this method exists? What kind of a "replace" operation is a "delete" operation analogous to?
[/quote]

I originally thought of the Replace method, but instead of nothing, best you can do is use a whitespace character, which I figured would mess the program up somehow.

Edit: So I got rid of the + character, but now I have a pesky whitespace spot to deal with.

[quote name='Oberon_Command' timestamp='1341072630' post='4954330']
Ah-ha, this is why I/we need to read the documentation. According to http://msdn.microsof...ng.trimend.aspx:

The TrimEnd method removes from the current string all trailing characters that are in the trimChars parameter. The trim operation stops when the first character that is not in trimChars is encountered at the end of the string. For example, if the current string is "123abc456xyz789" and trimChars contains the digits from "1" through "9", the TrimEnd method returns "123abc456xyz".


We want to take the string and remove all of the math operators from it, yes? So it seems that TrimEnd() doesn't quite do what we want - since it trims EVERYTHING from the end of the string up until it finds a character in the list of characters we gave it. Let's go see what else is available.

It looks like there isn't any string method that removes all instances of a particular character. There is however, String.Replace. Now think: how can we "delete" the characters (or sub-strings) we don't want to see in our output string, given that we know this method exists? What kind of a "replace" operation is a "delete" operation analogous to?
[/quote]

I originally thought of the Replace method, but instead of nothing, best you can do is use a whitespace character, which I figured would mess the program up somehow
[/quote]

Why can't you use an empty string? Or by "nothing" did you mean null? And how did you figure it would "mess the program up somehow?"

[source lang="csharp"]
currentString = textBox1.Text;

foreach (char symbol in mathSymbols)
currentString = currentString.Replace(symbol.ToString(), "");
[/source]

[quote name='Oberon_Command' timestamp='1341072630' post='4954330']
Ah-ha, this is why I/we need to read the documentation. According to http://msdn.microsof...ng.trimend.aspx:

The TrimEnd method removes from the current string all trailing characters that are in the trimChars parameter. The trim operation stops when the first character that is not in trimChars is encountered at the end of the string. For example, if the current string is "123abc456xyz789" and trimChars contains the digits from "1" through "9", the TrimEnd method returns "123abc456xyz".


We want to take the string and remove all of the math operators from it, yes? So it seems that TrimEnd() doesn't quite do what we want - since it trims EVERYTHING from the end of the string up until it finds a character in the list of characters we gave it. Let's go see what else is available.

It looks like there isn't any string method that removes all instances of a particular character. There is however, String.Replace. Now think: how can we "delete" the characters (or sub-strings) we don't want to see in our output string, given that we know this method exists? What kind of a "replace" operation is a "delete" operation analogous to?
[/quote]

I originally thought of the Replace method, but instead of nothing, best you can do is use a whitespace character, which I figured would mess the program up somehow.

Edit: Buuuuuut maybe, i've yet to try this out, after using Replace, using Trim to get rid of said whitespace.
[/quote]

you can replace with an empty string "" rather than a whitespace " ".

Edit, ninjad. need to learn to refresh before i reply ...
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

[quote name='Bill Fountaine' timestamp='1341073622' post='4954333']
[quote name='Oberon_Command' timestamp='1341072630' post='4954330']
Ah-ha, this is why I/we need to read the documentation. According to http://msdn.microsof...ng.trimend.aspx:

The TrimEnd method removes from the current string all trailing characters that are in the trimChars parameter. The trim operation stops when the first character that is not in trimChars is encountered at the end of the string. For example, if the current string is "123abc456xyz789" and trimChars contains the digits from "1" through "9", the TrimEnd method returns "123abc456xyz".


We want to take the string and remove all of the math operators from it, yes? So it seems that TrimEnd() doesn't quite do what we want - since it trims EVERYTHING from the end of the string up until it finds a character in the list of characters we gave it. Let's go see what else is available.

It looks like there isn't any string method that removes all instances of a particular character. There is however, String.Replace. Now think: how can we "delete" the characters (or sub-strings) we don't want to see in our output string, given that we know this method exists? What kind of a "replace" operation is a "delete" operation analogous to?
[/quote]

I originally thought of the Replace method, but instead of nothing, best you can do is use a whitespace character, which I figured would mess the program up somehow
[/quote]

Why can't you use an empty string? Or by "nothing" did you mean null? And how did you figure it would "mess the program up somehow?"

[source lang="csharp"]
currentString = textBox1.Text;

foreach (char symbol in mathSymbols)
currentString = currentString.Replace(symbol.ToString(), "");
[/source]
[/quote]

oh wow, i feel stupid.

When i was doing the replacing, I was replacing a char, not a string

and when replacing chars, you can't have an empty '', it has to be ' '.

It works with the string stuff, man I feel dumb >_>
Hm, even though i replaced with an empty string it still looks as if the number has a whitespace next to it.

Edit: Because when I do the debugging, it shows "56 " instead of "56"

Hm, even though i replaced with an empty string it still looks as if the number has a whitespace next to it.

Edit: Because when I do the debugging, it shows "56 " instead of "56"


if the string is entered as x + y rather than x+y then that is the expected result, it shouldn't matter though as the extra whitespace should be ignored when you convert from string to int anyway.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
You replaced math symbols with nothing... not whitespace.
right, duh.

So many obvious stupid mistakes, I think thats my cue to go to sleep.

This topic is closed to new replies.

Advertisement