C# Math Calculator

C# Math calculator

DataTable has a Compute method that allows you to write this:

var result = new DataTable().Compute("2-3/4*12", null);

Note that this is limited to simple math expressions.

Other option consist in using a dynamic language in the DLR such as IronPython and IronRuby. Check-out this post:

var engine = new IronPython.Hosting.PythonEngine();
double result = pythonEngine.EvaluateAs<double>("2-3/4*12");

You may also check the NCalc library on GitHub.

Percentage calculation

(current / maximum) * 100. In your case, (2 / 10) * 100.

Simple C# Calculator with Properties

You have left out two closing braces and claimed a function returns an int which actually returns nothing.
It helps to either fix one thing at a time - and in fact try to write only little code at a time, compiling as you go. If you can't see what's wrong comment out large chunks and try to keep your code neatly lined up so you can see where you may have missed a brace or similar.

using System;

namespace Calculator
{
class Program
{
public static void Main(string[] args)
{

}//<-----------

public int number01;
public int number02;
public int Number03
{
get
{
return number02 / number01;
}
}//<----------

class Program1 : Program
{

public void DivideFinal()//<---- void not int
{
Console.Write("Enter a number to be divided: ");
Console.ReadKey();
number01 = Convert.ToInt32(Console.ReadKey());
Console.WriteLine("Enter another number to be divided");
number02 = Convert.ToInt32(Console.ReadKey());
Console.WriteLine("The result is: " + Number03);
Console.ReadKey();
}
}

}
}

Right - now we have it compiling let's look at the essence of what you have.

namespace Calculator
{
class Program
{
public static void Main(string[] args)
{
}
}
}

You have a class called Program in a namespace with the expected static void Main entry point. It doesn't do anything, so when you run it nothing much will happen. If you run it in a debugger, you might get "Press any key to continue" printed.

If you want something to happen, it needs code in your entry point- this Main function by default.

You have added some properties to this class, which you don't use. You have started to write another class, called Program1 inside this class, which also inherits from this class.

Consider calling it Calculator instead, since that's what you are going to write. It doesn't need to inherit from your main class - they are unrelated. It is much neater to just make a new class in a new file.

Start a new class, and add the properties and other methods like DivideFinal in there. (By the way why have you called it DivideFinal?)

namespace Calculator
{
class Calculator
{
public int number01;
public int number02;
public int Number03
{
get
{
return number02 / number01;
}
}

public void DivideFinal()
{
Console.Write("Enter a number to be divided: ");
Console.ReadKey();
number01 = Convert.ToInt32(Console.ReadKey());
Console.WriteLine("Enter another number to be divided");
number02 = Convert.ToInt32(Console.ReadKey());
Console.WriteLine("The result is: " + Number03);
Console.ReadKey();
}
}
}

Finally, let's make the main function do something:

    public static void Main(string[] args)
{
var calculator = new Calculator();
calculator.DivideFinal();
}

This will lead to some errors, which we can deal with in another question.
For example, Convert.ToInt32 expects a String but you are giving it the result of Console.ReadKey which isn't a string. You may wish to consider Console.ReadLine() instead: see this question for example.

C# Calculator I made is giving nonsense results

I agree with @bolov that debugging your program would be a good learning exercise here, but if you're struggling, and for the academic purpose of learning how to improve what you've got rather than someone telling you a totally different way, the following is what's going on:

What you program is doing is (focusing on just the bits that cause the bug):

Button '1' is clicked which sets the following things:
- activestring = "1"

Button '+' is clicked which sets the following things:
- num1 = 1

- activestring = ""

- isnum1 = true;

Button '1' is clicked which sets the following things:
- activestring = "1"

- num2 = 1

- total = 2 (total(0) + num1(1) + num2(1))

Button '1' is clicked again which sets the following things:
- activestring = "11"

- num2 = 11

- total = 14 (total(2) + num1(1) + num2(11))

This is a result of trying to accumulate numbers which have only been partially written.

There are lots of horrible ways you could fix this up inside the '1' button click code (e.g. parsing before and after updating activestring, and then taking off the previous value and adding the new one), but it feels like using 2 totals as in the code example below would achieve your goal in a slightly tidier way without breaking too far away from the structure you've set out:

(I've also tweaked it to support continued addition (multiple calls to '+') as it seems like that's what you were eventually aiming for).

(Disclaimer: I've not actually run this through a compiler at all, but it should help to set you on the right track).

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Calculator
{
public partial class Form1 : Form
{
public double num1;
public double num2;
public double total;
public string activestring;
public int operand;
public bool Isnum1;
public bool IsPrevCharNumeric; // New

public Form1()
{
InitializeComponent();
}

//a #1 button on a calculator.
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text += 1;
activestring += 1;

if (Isnum1)
{
num2 = double.Parse(activestring);
if (operand == 1)
{
total = num1 + num2; // Changed
}
isPrevCharNumeric = true; // New
}
}
// a + button on a calculator.
private void buttonPlus_Click(object sender, EventArgs e)
{
if (IsPrevCharNumeric) // Changed
{
operand = 1;
total = num1 + double.Parse(activestring); // Changed
num1 = total; // Changed
activestring = "";
Isnum1 = true;
IsPrevCharNumeric = false; // New
textBox1.Text += "+";
}
}

// an = button on a calculator.
private void buttonEQ_Click(object sender, EventArgs e)
{
textBox1.Clear();
textBox1.Text = total.ToString();
}
}
}

c# math cos different values from calculator

Math.Cos() is using radians while your calculator is using degrees. A simple way to convert from degrees to radians is degrees*(pi/180).

In code:

Math.Cos(28.545742*(Math.PI/180))

String calculator

Regular Expression evaluation can be done using DataTable.Compute method (from MSDN) :

Computes the given expression on the current rows that pass the filter
criteria.

Try this:

using System.Data;//import this namespace

string math = "100 * 5 - 2";
string value = new DataTable().Compute(math, null).ToString();

c# Math/Formula Calculation

As discussed in comment we can use modulo (%) operator in your formula.

int dayInt = (nthWeek % 3);

This will give you result in 0, 1, 2. 0 - friday, 1 - Monday, 2-
wednesday

Here is the code:

int nthWeek = 49;

int result = (nthWeek % 3);

switch(result)
{
case 0:
Console.WriteLine("Worker is working on Friday");
break;

case 1:
Console.WriteLine("Worker is working on Monday");
break;

case 2:
Console.WriteLine("Worker is working on Wednesday");
break;

default:
Console.WriteLine("Worker is working on other day");
break;
}

Implementation: DotnetFiddler



Related Topics



Leave a reply



Submit