Jump to content
  • Advertisement
Sign in to follow this  
GraySnakeGenocide

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

This topic is 2178 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

[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();
}

private Boolean lastCharIsSymbol { get; set; }

int number;

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

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

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

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

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

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

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

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

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

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

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

private void buttonPlus_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || lastCharIsSymbol)
return;
else
{
add = true;
textBox1.Text += " + ";
lastCharIsSymbol = true;
}
}

private void buttonMinus_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || lastCharIsSymbol)
return;
else
{
subtract = true;
textBox1.Text += " - ";
lastCharIsSymbol = true;
}
}

private void buttonMultiply_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || lastCharIsSymbol)
return;
else
{
multiply = true;
textBox1.Text += " * ";
lastCharIsSymbol = true;
}
}

private void buttonDivide_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || lastCharIsSymbol)
return;
else
{
divide = true;
textBox1.Text += " / ";
lastCharIsSymbol = true;
}

}

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

}

if (subtract)
{

}

if (multiply)
{

}

if (divide)
{

}
}

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


What I want to do, is be able to use more than 2 numbers together in the one textbox.

For example, I want to be able to do like, 14 + 86 - 24, etc.

How do I go about doing this? If I had 2 textboxes it would seemingly be easy storing the values placed in them in 2 seperate ints. But since I want to be able to use more than 2 numbers how do I do this?

And for the record, this is a self-teaching project, not schoolwork.

Sorry if this is simple stuff/straight forward, my brains bunched up lately. Edited by Bill Fountaine

Share this post


Link to post
Share on other sites
Advertisement
honestly I don't understand the point of threads like these.
I mean, what are you looking for? A solution? How being given a solution would make you improve? Programming is all about finding solutions to problems. If somebody just gives you the solution you'll be back asking another solution for your next problem and you won't learn anything.

You are looking for an hint? Here's one: break down your problem into simple parts, start from a simple case were you are making assumptions.. ie, assume you'll have a sequence of number operation number operation and you solve it left to right without any operation precedence.
Look how to get a string and split this into parts, C# strings are very good at this.
Once you get it done you might look into more complex stuff, operator precedence, brackets and so on.. this usually involves a creation of a tree of operation that is then solved into a solution.

You mentioned you've been programming for 4 years. .I dont mean any offense, but if after 4 years you're stuck at this, maybe you should consider the possibility that perhaps programming isn't what you were born to do?

Share this post


Link to post
Share on other sites

honestly I don't understand the point of threads like these.
I mean, what are you looking for? A solution? How being given a solution would make you improve? Programming is all about finding solutions to problems. If somebody just gives you the solution you'll be back asking another solution for your next problem and you won't learn anything.

You are looking for an hint? Here's one: break down your problem into simple parts, start from a simple case were you are making assumptions.. ie, assume you'll have a sequence of number operation number operation and you solve it left to right without any operation precedence.
Look how to get a string and split this into parts, C# strings are very good at this.
Once you get it done you might look into more complex stuff, operator precedence, brackets and so on.. this usually involves a creation of a tree of operation that is then solved into a solution.

You mentioned you've been programming for 4 years. .I dont mean any offense, but if after 4 years you're stuck at this, maybe you should consider the possibility that perhaps programming isn't what you were born to do?


or maybe I've just been too lazy to actually PROGRAM instead of just reading constantly?

It's not like I'm not understanding anything. I've just been approaching it wrong.

Why do some people have to come across as such snobs.

Share this post


Link to post
Share on other sites
I think Telastyn meant that instead of storing your input as a single string, you can keep the input separated and THEN to display it you can construct the string from the input.

Share this post


Link to post
Share on other sites

I think Telastyn meant that instead of storing your input as a single string, you can keep the input separated and THEN to display it you can construct the string from the input.


I'm just not quite sure on how to do this stuff. Dealing with 2 numbers would be easy, but multiple is sketchy for me.

Share this post


Link to post
Share on other sites

honestly I don't understand the point of threads like these.
I mean, what are you looking for? A solution? How being given a solution would make you improve? Programming is all about finding solutions to problems. If somebody just gives you the solution you'll be back asking another solution for your next problem and you won't learn anything.

You are looking for an hint? Here's one: break down your problem into simple parts, start from a simple case were you are making assumptions.. ie, assume you'll have a sequence of number operation number operation and you solve it left to right without any operation precedence.
Look how to get a string and split this into parts, C# strings are very good at this.
Once you get it done you might look into more complex stuff, operator precedence, brackets and so on.. this usually involves a creation of a tree of operation that is then solved into a solution.

You mentioned you've been programming for 4 years. .I dont mean any offense, but if after 4 years you're stuck at this, maybe you should consider the possibility that perhaps programming isn't what you were born to do?


I'm a friend of his and he was talking to me about the issue. From what I can tell, he's been having trouble understanding how to solve it as opposed to understanding the what side.

Like if you told a person "You have to read the user input and store it as a variable." They'd know what you mean, but not the specifics of how (using Console.Readline, etc)

That's the best way I can explain his issue.

Share this post


Link to post
Share on other sites

[quote name='boogyman19946' timestamp='1341002263' post='4954081']
I think Telastyn meant that instead of storing your input as a single string, you can keep the input separated and THEN to display it you can construct the string from the input.


I'm just not quite sure on how to do this stuff.
[/quote]

Then start smaller. Can you make a program that takes two numbers as input and prints their output?

Snob or not, that sort of program is hour 2 or 3 of learning C# from nothing. I find it hard to believe that you've done much of anything in 4 years without realizing that you're accomplishing nothing. Just say "hey, I'm a beginner". Nothing wrong with that, and we'll be able to provide better answers for your situation.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!