Click here to Skip to main content
15,886,110 members
Articles / Desktop Programming / WPF

Equation Calculator with Graphing

Rate me:
Please Sign up or sign in to vote.
4.92/5 (69 votes)
25 Nov 2010CPOL9 min read 132.7K   4.2K   158  
Equation Calculator with Graphing
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CommonUtils
{
	class EquationUnitTestException : Exception
	{
		public string Equation {get; set;}
		public EquationUnitTestException(string equation, string message) : base(message)
		{
			Equation = equation;
		}
	}

	public class EquationUnitTest
	{
		Term m_term = new Term();
		public EquationUnitTest()
		{
		}
		public void UnitTest()
		{
			Assert("2+3*5", 2+3*5);
			Assert("2+3*5^2", 2+3*Math.Pow(5,2));
			Assert("2+3*5/2*2", 2d+3d*5d/2d*2d);
			Assert("2pi", 2d*Math.PI);
			Assert("2pi2", 2d*Math.PI*2d);
			Assert("3(3+2)(2*5)", 3*(3+2)*(2*5));
			Assert("100^-1", Math.Pow(100, -1));
			Assert("3^2+3*3-18", 0);
			Assert("4^2+3*4-18", 10);
			
			m_term.SetVar("x", -2);
			Assert("(2x+4)/(x^2-7)", 0);
			
			m_term.SetVar("a", 5);
			m_term.SetVar("b", 5);
			Assert("2aba", 250);
			Assert("2abs(-10)", 20);
			Assert("sqrt(10^2)", 10);
			
			Assert("sin(0)", 0);
			Assert("sin(pi/2)", 1);
			Assert("sin(pi/4)", Math.Sin(Math.PI / 4));
			Assert("sin((45/180)pi)", Math.Sin((45d/180d)*Math.PI));
			Assert("tan(pi/2)", Math.Tan(Math.PI/2));

			m_term.SetVar("ans", 5);
			Assert("ans+2", 7);

			m_term.SetVar("ans", 16);
			Assert("sqrt(ans)+2", 6);

			m_term.SetVar("ans", 10);
			Assert("2*-ans", -20);

			m_term.SetVar("ans", 10);
			m_term.SetVar("a", 2);
			m_term.SetVar("n", 3);
			Assert("a n ans", 60);

			Assert("2 * e", 2 * Math.E);
			Assert("2e", 2 * Math.E);
			Assert("2e10", (double)2e10);
			Assert("5.0805263425E-5 * 2", 2 * (double)5.0805263425E-5);
			Assert("4.35718618402138E+21*2", ((double)4.35718618402138E+21)*2);
			Assert("10E10*2", ((double)10E10)*2);

			Assert("ln(3^e)", Math.Log(Math.Pow(3, Math.E), Math.E/*3*/));

			Assert("Log(1000)", 3);
			Assert("Log(27, 3)", 3);
			Assert("Log(4^4, 4)", 4);
			Assert("-sin(-4)", -Math.Sin(-4));
		}

		void Assert(string equation, double result)
		{
			try
			{
				m_term.Parse(equation);
				if (m_term.Value != result)
				{
					string text = string.Format("FAILED - Equation {0}, Parsed {1}, Value {2}, Expected {3}", equation, m_term.ToString(), m_term.Value, result);
					throw new EquationUnitTestException(equation, text);
				}
				Console.WriteLine(string.Format("PASSED - Equation {0}, Parsed {1}, Value {2}", equation, m_term.ToString(), m_term.Value));
			}
			catch (Exception error)
			{
				throw new EquationUnitTestException(equation, error.Message);
			}
			m_term.ClearVars();
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions