C# Calculator

This is an old calculator program I wrote in C# :

using System;
using System.Threading;
namespace Rechner
{
	class MainClass
	{
		public static double vari1=1,vari2=1;
		public static void Main (string[] args)
		{

			do{
			Einführung();
			string einlese = Convert.ToString(Console.ReadLine());
			Double ergebniss=0;
			switch(einlese)
			{
			case "+" : ergebniss = plus();break;
			case "-" : ergebniss = minus();break;
			case "*" : ergebniss = Mal();break;
			case "/" : ergebniss = Div();break;
			case "var1" : var1();break;
			case "var2" : var2();break;
			}
			Console.WriteLine("Die Lösung ist nach Adam Rieße "+ergebniss);
				Console.WriteLine("");
			Console.WriteLine ("Enter zum fortsetzen,var1 oder var2 zum speichern in Variablen !");
			try{
					string speichern = Console.ReadLine();
					if(speichern == "var1")
					{vari1 = ergebniss;Console.WriteLine("Variable 1 ist "+ergebniss);Console.ReadLine();} 
					if(speichern == "var2")
					{vari2 = ergebniss;Console.WriteLine("Variable 2 ist "+ergebniss);Console.ReadLine();} 
			}catch{}
			}while(true);
		}
		static Double plus()
		{
			Double summe=0;
			do{
				Console.Clear();
			Console.WriteLine("= zum beenden eingeben !");
			Console.WriteLine("Ergebnis = "+summe);
								Console.WriteLine ("");
				Console.WriteLine ("Variable 1 :"+vari1);
				Console.WriteLine ("Variable 2 :"+vari2);
			Console.Write("Zahl (+):");
				
			string a = Console.ReadLine(); 
           if (a == "=")
           {
               break;
           }
				           if (a == "var1")
           {
               summe = summe + vari1;
           }
				           if (a == "var2")
           {
               summe = summe + vari2;
           }
           else
           {
               try
               {
                   summe = summe + Convert.ToDouble(a);
               }
               catch { }
           }		
			}while(true);
			
			return (summe);
			
		}
		static Double minus()
		{
			Double summe=0;
			do{Console.Clear();
			Console.WriteLine("= zum beenden eingeben !");
			Console.WriteLine("Ergebnis = "+summe);
								Console.WriteLine ("");
				Console.WriteLine ("Variable 1 :"+vari1);
				Console.WriteLine ("Variable 2 :"+vari2);
			Console.Write("Zahl (-):");
				
			string a = Console.ReadLine(); 
           if (a == "=")
           {
               break;
           }
				           if (a == "var1")
           {
               summe = summe - vari1;
           }
				           if (a == "var2")
           {
               summe = summe - vari2;
           }
           else
           {
               try
               {
                   summe = summe - Convert.ToDouble(a);
               }
               catch { }
           }		
			}while(true);
			
			return(summe);
			
		}
		static void var1()
		{			Console.Clear();	Console.Write ("Variable 1 ändern:");
			try{
			string einlese = Convert.ToString(Console.ReadLine());
			vari1 = Convert.ToDouble(einlese);
			Console.WriteLine ("Variable 1 geändert:"+vari1);
								if(vari2 == 0){vari2 = 1;Console.WriteLine ("0 nicht erlaubt !");Console.ReadLine();}
				if(vari1 == 0){vari1 = 1;Console.WriteLine ("0 nicht erlaubt !");Console.ReadLine();}
			}catch{Console.WriteLine ("ERROR !");}
		}
		static void var2()
		{			Console.Clear();	Console.Write ("Variable 2 ändern:");
			try{
			string einlese = Convert.ToString(Console.ReadLine());
				vari2 = Convert.ToDouble( einlese);
			Console.WriteLine ("Variable 2 geändert:"+vari2);
								if(vari2 == 0){vari2 = 1;Console.WriteLine ("0 nicht erlaubt !");Console.ReadLine();}
				if(vari1 == 0){vari1 = 1;Console.WriteLine ("0 nicht erlaubt !");Console.ReadLine();}
			}catch{Console.WriteLine ("ERROR !");}
		}
		static Double Mal()
		{Double summe = 0;
			try{
						Console.Write("Zahl :");
			 summe=Convert.ToDouble(Console.ReadLine());
			}catch{}
			do{
				Console.Clear();
			Console.WriteLine("= zum beenden eingeben !");
			Console.WriteLine("Ergebnis = "+summe);
								Console.WriteLine ("");
				Console.WriteLine ("Variable 1 :"+vari1);
				Console.WriteLine ("Variable 2 :"+vari2);
			Console.Write("Zahl (*):");
				
			string a = Console.ReadLine(); 
           if (a == "=")
           {
               break;
           }
				           if (a == "var1")
           {
               summe = summe * vari1;
           }
				           if (a == "var2")
           {
               summe = summe * vari2;
           }
           else
           {
               try
               {
                   summe = summe * Convert.ToDouble(a);
               }
               catch { }
           }		
			}while(true);
			
			return(summe);
			
		}
		static Double Div()
		{Double summe = 0;string summe1 = "";
			try{
			do{	
			Console.WriteLine("");
			Console.Write("Zahl :");
			summe1 =Convert.ToString( Console.ReadLine());
			}while(Convert.ToDouble(summe1) <= 0);
				summe = Convert.ToDouble(summe1);
			}catch{}
			if(summe <= 0)
			{summe = 1;}
			do{
				Console.Clear();
			Console.WriteLine("= zum beenden eingeben !");
			Console.WriteLine("Ergebnis = "+summe);
								Console.WriteLine ("");
				Console.WriteLine ("Variable 1 :"+vari1);
				Console.WriteLine ("Variable 2 :"+vari2);
			Console.Write("Zahl (/):");
				
			string a = Console.ReadLine(); 
           if (a == "=")
           {
               break;
           }
				           if (a == "var1")
           {
               summe = summe / vari1;
           }
				           if (a == "var2")
           {
               summe = summe / vari2;
           }
           else
           {
               try
               {
						if(Convert.ToDouble(a) != 0)
						{
                   summe = summe / Convert.ToDouble(a);
						}else{Console.WriteLine("nicht möglich !");
						System.Threading.Thread.Sleep(2400);
						}
               }
               catch { }
           }		
			}while(true);
			
			return(summe);
			
		}
		static void Einführung()
		{Console.Clear();
			Console.WriteLine ("+ Plus rechnen");
			Console.WriteLine ("- Minus rechnen");
				Console.WriteLine ("* Mal rechnen");
				Console.WriteLine ("/ Dividiert rechnen");
				Console.WriteLine ("");
				Console.WriteLine ("var1 variable 1 ändern ");
				Console.WriteLine ("var2 variable 2 ändern ");
				Console.WriteLine ("");
				Console.WriteLine ("Variable 1 :"+vari1);
				Console.WriteLine ("Variable 2 :"+vari2);
				Console.WriteLine ("");
				Console.Write (">");
				if(vari2 == 0){vari2 = 1;Console.WriteLine ("0 nicht erlaubt !");Console.ReadLine();}
				if(vari1 == 0){vari1 = 1;Console.WriteLine ("0 nicht erlaubt !");Console.ReadLine();}
		}

		
	}

}

Feel free to copy and share.

One thought on “C# Calculator”

  1. Greetings! Very helpful advice in this post! This is the little changes that will make
    the most crucial changes. Thanks for sharing!

Leave a Reply

Your email address will not be published.

4 × 4 =