Learn 2 CSharp 6

6. Scientific calculator supporting addition, subtraction, multiplication, division, square-root, square, cube, sin, cos, tan, Factorial, inverse, modulus

This is a fairly simple implementation of a console calculator. It only takes 2 number expressions ( ex: 1 + 1 ) as well as / commands for most other functions, except factorial ( n! ). Very little error checking is being done. All sorts of invalid inputs will cause this to throw exceptions.

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace lrn2csharp6_console { class Program { private static String Version = "0.0"; static void Main( string[ ] args ) { Console.WriteLine( "neSP: Calc v" + Version ); Console.WriteLine( "---------------------------------" ); Console.WriteLine( "Enter Command. ( example: /help )" ); int end = 0; while ( end == 0 ) { String exp = ""; Console.Write( ":" ); try { exp = Console.ReadLine( ); } catch ( InvalidOperationException ) { Console.WriteLine( "Not a valid command." ); exp = ""; } if ( exp.Length == 0 ) { // empty command } else if ( exp.ToCharArray( ).First( ) == '/' ) { if ( exp.Split( ' ' ).ToString( )[ 0 ].Equals( "/help" ) ) { Console.WriteLine( "You are being helped:" ); Console.WriteLine( "/help - bring up this help system." ); Console.WriteLine( "/quit or /q - quit program." ); Console.WriteLine( "/prime n - generate prime numbers from 2 to n." ); Console.WriteLine( "/factor numerator/denominator - factors fraction." ); Console.WriteLine( "/sqrt n - finds the square root of n." ); Console.WriteLine( "/cbrt n - finds the cube root of n." ); Console.WriteLine( "/sin n - finds sine of n." ); Console.WriteLine( "/cos n - finds cosine of n." ); Console.WriteLine( "/tan n - finds tangent of n." ); Console.WriteLine( "/inv numerator/denominator - finds inverse of fraction." ); Console.WriteLine( "n! - finds factorial of n." ); } else if ( exp.Split( ' ' )[ 0 ].Equals( "/quit" ) || exp.Split( ' ' )[ 0 ].Equals( "/q" ) ) { end = 1; } else if ( exp.Split( ' ' )[ 0 ].Equals( "/primes" ) ) { Console.Write( "primes:" ); prime( Convert.ToInt32( exp.Split( ' ' )[ 1 ] ) ); Console.WriteLine( ); } else if ( exp.Split( ' ' )[ 0 ].Equals( "/factor" ) ) { } else if ( exp.Split( ' ' )[ 0 ].Equals( "/sqrt" ) ) { Console.WriteLine( Math.Sqrt( Convert.ToDouble( exp.Split( ' ' )[ 1 ] ) ) ); } else if ( exp.Split( ' ' )[ 0 ].Equals( "/cbrt" ) ) { double power = 1.0 / 3.0; Console.WriteLine( Math.Pow( Convert.ToDouble( exp.Split( ' ' )[ 1 ] ), power ) ); } else if ( exp.Split( ' ' )[ 0 ].Equals( "/sin" ) ) { Console.WriteLine( Math.Sin( Convert.ToDouble( exp.Split( ' ' )[ 1 ] ) ) ); } else if ( exp.Split( ' ' )[ 0 ].Equals( "/cos" ) ) { Console.WriteLine( Math.Cos( Convert.ToDouble( exp.Split( ' ' )[ 1 ] ) ) ); } else if ( exp.Split( ' ' )[ 0 ].Equals( "/tan" ) ) { Console.WriteLine( Math.Tan( Convert.ToDouble( exp.Split( ' ' )[ 1 ] ) ) ); } else if ( exp.Split( ' ' )[ 0 ].Equals( "/inv" ) ) { String fraction = exp.Replace( "/inv ", "" ); double numer = Convert.ToDouble( fraction.Split( '/' )[ 0 ] ); double denom = Convert.ToDouble( fraction.Split( '/' )[ 1 ] ); Console.WriteLine( denom / numer ); } else { Console.WriteLine( "Invalid command ( " + exp + " ) supplied." ); } } else if ( exp.Contains( '!' ) ) { int f = Convert.ToInt32( exp.Split( '!' )[ 0 ] ); Console.WriteLine( f ); int total = 1; for ( int i = 0; i < f; i++ ) { total = total * ( i + 1 ); } Console.WriteLine( total ); } else { calc( exp ); } } } static void calc( String expression ) { String[ ] stack = expression.Split( ' ' ); Console.WriteLine( doCalc( Convert.ToDouble( stack[ 0 ] ), Convert.ToChar( stack[ 1 ] ), Convert.ToDouble( stack[ 2 ] ) ) ); } static double doCalc( double left, char theOperator, double right ) { double total = 0.0; if ( theOperator == '+' ) { total = add( left, right ); } else if ( theOperator == '-' ) { total = subtract( left, right ); } else if ( theOperator == '*' ) { total = multiply( left, right ); } else if ( theOperator == '/' ) { total = divide( left, right ); } else if ( theOperator == '^' ) { total = Math.Pow( left, right ); } else if ( theOperator == '%' ) { total = mod( Convert.ToInt32( left ), Convert.ToInt32( right ) ); } return total; } static double add( double num, double num2 ) { return ( num + num2 ); } static double subtract( double num, double num2 ) { return ( num - num2 ); } static double multiply( double num, double num2 ) { return ( num * num2 ); } static double divide( double num, double num2 ) { if ( num2 == 0 ) { Console.WriteLine( "ERROR: Division by zero." ); return num; } else { return ( num / num2 ); } } static int mod( int num, int num2 ) { return ( num % num2 ); } static void prime( int num ) { int count = 0; for ( int _i = 2; _i < num; _i++ ) { if ( !isPrime( _i ) ) { if ( ( count % 10 ) == 0 ) Console.WriteLine( ); Console.Write( " {0,6}", _i ); count++; } } } static bool isPrime( int num ) { bool notPrime = false; for ( int k = num - 1; k > 1 && !notPrime; k-- ) { if ( num % k == 0 ) notPrime = true; } return notPrime; } } }

posted by dharh 8:46 PM Nov 29th, 2009



2011: 5 3 1
2010: 12 9 7 1
2009: 12 11 8 5
2008: 12 5 4 3 2 1
2007: 12 11 10 9 8 7 6 5 4 3 2 1
2006: 12 11 10 9 8 7 6 5 4 3 2 1
2005: 12 10 7 6
2004: 10 9 6 5 4 3 2 1