Jump to content

Simple GTK# calculator in mono - help please

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
DrSpaceman

DrSpaceman

    Newbie

  • Members
  • Pip
  • 4 posts
Hi,

I've been working on a simple calculator program in mono using GTK#. So far it can only do very simple calculations, and there are some situations that it doesn't handle very well yet. I am very new to C# so I would imagine that even though the code works, it is probably not very well written. Before I continue to refine it or add functionality I would like to get some input on the code.

Specifically:
how bad are my naming conventions?
am I using inefficient/sloppy techniques?
have I appropriately used public,static, return types etc... for my objects?
how can I improve on this code?

I apologize for the lack of good commenting. I will add better comments as soon as I get a chance to work on it again, and then I will edit my post.

I did this on a Linux system, so I don't know whether that code would compile and run on windows in case anybody tries.

Any help would be greatly appreciated.


using Gtk;

using System ;



public class Calculator{


	static decimal result ;


	static decimal a ;

	static decimal b ;


	static string stringA ;

	static string stringB ;


	static int modify ;   // 0:a , 1:b      - which variable to modify

	static bool solved ;   // has an equation been solved, and nothing has happened since?

	

// The following methods should be subscribed to the events fired by all the buttons


	static void resultAction(){

		Program.CalcScreen.Text = result.ToString() ;

		solved = true ; modify = 0; stringA = ""; stringB = ""; // These clear out values and warn other methods to do so as well. use in every method that defines result


	}

	

	//Multiplication methods

	static void doMultiplication(object obj, EventArgs e){

		result = a*b ;	

		resultAction();

	}

	static EventHandler Multiplication = new EventHandler(doMultiplication);

	static void setMultiplication(object obj, EventArgs e){

		modify = 1 ; // if the multiply button was clicked, we are no longer working with a so set modify to 1 indicating b

		Program.Equal.Clicked += Multiplication ;

		Program.CalcScreen.Text += " X " ; 

		if(solved){

			a = result ; 

		}

		solved = false ;

	}

	public static EventHandler TimesHandler = new EventHandler(Calculator.setMultiplication);


	//Addition methods

	static void doAddition(object obj, EventArgs e){

		result = a+b ;	

		resultAction();

	}

	static EventHandler Addition = new EventHandler(doAddition);

	static void setAddition(object obj, EventArgs e){

		modify = 1 ; // if the addition button was clicked, we are no longer working with a so set modify to 1 indicating b

		Program.Equal.Clicked += Addition ;

		Program.CalcScreen.Text += " + " ; 

		if(solved){

			a = result ; 

		}

		solved = false ;

	}

	public static EventHandler PlusHandler = new EventHandler(Calculator.setAddition);


	//Division methods

	static void doDivision(object obj, EventArgs e){

		result = a/b ;	

		resultAction();

	}

	static EventHandler Division = new EventHandler(doDivision);

	static void setDivision(object obj, EventArgs e){

		modify = 1 ; // if the division button was clicked, we are no longer working with a so set modify to 1 indicating b

		Program.Equal.Clicked += Division ;

		Program.CalcScreen.Text += " / " ; 

		if(solved){

			a = result ; 

		}

		solved = false ;

	}

	public static EventHandler DividedHandler = new EventHandler(Calculator.setDivision);


	//Subtraction methods

	static void doSubtraction(object obj, EventArgs e){

		result = a-b ;	

		resultAction();

	}

	static EventHandler Subtraction = new EventHandler(doSubtraction);

	static void setSubtraction(object obj, EventArgs e){

		modify = 1 ; // if the subtraction button was clicked, we are no longer working with a so set modify to 1 indicating b

		Program.Equal.Clicked += Subtraction ;

		Program.CalcScreen.Text += " - " ; 

		if(solved){

			a = result ; 

		}

		solved = false ;

	}

	public static EventHandler MinusHandler = new EventHandler(Calculator.setSubtraction);




	//clickButton definition and subscription preparation(instantiation as a delegate)

	static void clickNumButton(object obj, EventArgs e){


		Button btn = obj as Button;	

		if(modify == 0){ // we are working with the a variable. Append the value of the button clicked to the variable a


			stringA += btn.Label;



			if(String.Compare(stringA.Substring(stringA.Length - 1) , "." ) == 1 ){			

				a = decimal.Parse(stringA);

			}

			//Console.WriteLine("A: " + a + " string:" + stringA);

			if(!solved){

				Program.CalcScreen.Text += btn.Label ;

			}else{

				Program.CalcScreen.Text = btn.Label ;

			} 

			solved = false ;

			

		}else{// we are working with the b variable/ Append the value of the button clicked to the variable b



			stringB += btn.Label;


			if(String.Compare(stringB.Substring(stringB.Length-1) , "." ) == 1 ){

				b = decimal.Parse(stringB);

			}

			//Console.WriteLine("B: " + b + " string:" + stringB );

			if(!solved){

				Program.CalcScreen.Text += btn.Label ;

			}else{

				Program.CalcScreen.Text = btn.Label ;

			} 

			solved = false ;

		}

		


	}

	public static EventHandler clickNumber = new EventHandler(clickNumButton) ;


}



public class Program{


	// Declare the objects that will make up the window here so they are in the scope of any methods in Program, not just main.


	static Window MyWindow;	

	static Table  MyTable ;

	static Button One ;

	static Button Two ;

	static Button Three ;

	static Button Four ;

	static Button Five ;

	static Button Six ;

	static Button Seven ;

	static Button Eight ;

	static Button Nine ;

	static Button Zero ;

	static Button DecPoint ;


	static Button Plus ;

	static Button Minus ;

	static Button Times ;

	static Button Divided;


	public static Entry CalcScreen ;


	public static Button Equal ;


	static void Main(){


		// Calls the static method Gtk.Application.init - initiates a gtk application

		Application.Init();


		// initialize the window object with an instance of MyWindow. Creates a window in which to put widgets, that the user will eventually interact with

		MyWindow = new Window("GTK# Application");

		MyWindow.SetDefaultSize(200,500);


	// All of the things that willl be added to the window

		// Create a table, add it to the window

		MyTable = new Table(7,5,true);

		MyWindow.Add(MyTable);


		One = new Button("1");

		MyTable.Attach(One, 0, 1, 3, 4);

		One.Clicked += Calculator.clickNumber ;


		Two = new Button("2");

		MyTable.Attach(Two, 1, 2, 3, 4);

		Two.Clicked += Calculator.clickNumber ;


		Three = new Button("3");

		MyTable.Attach(Three, 2, 3, 3, 4);

		Three.Clicked += Calculator.clickNumber ;


		Four = new Button("4");

		MyTable.Attach(Four, 0, 1, 2, 3);

		Four.Clicked += Calculator.clickNumber ;


		Five = new Button("5");

		MyTable.Attach(Five, 1, 2, 2, 3);

		Five.Clicked += Calculator.clickNumber ;


		Six = new Button("6");

		MyTable.Attach(Six, 2, 3, 2, 3);

		Six.Clicked += Calculator.clickNumber ;


		Seven = new Button("7");

		MyTable.Attach(Seven, 0, 1, 1, 2);

		Seven.Clicked += Calculator.clickNumber ;		


		Eight = new Button("8");

		MyTable.Attach(Eight, 1, 2, 1, 2);

		Eight.Clicked += Calculator.clickNumber ;


		Nine = new Button("9");

		MyTable.Attach(Nine, 2, 3, 1, 2);

		Nine.Clicked += Calculator.clickNumber ;


		Zero = new Button("0");

		MyTable.Attach(Zero, 0, 1, 4, 5);

		Zero.Clicked += Calculator.clickNumber ;


		DecPoint = new Button(".");

		MyTable.Attach(DecPoint, 1, 2, 4, 5);

		DecPoint.Clicked += Calculator.clickNumber ;


	// Equal

		Equal = new Button("=");

		MyTable.Attach(Equal, 3, 4, 5, 6);

		


	// Multiplication button and subscriptions	

		Times = new Button("X");

		MyTable.Attach(Times, 3, 4, 1, 2);

		Times.Clicked += Calculator.TimesHandler ;


	// Addition button and subscriptions

		Plus = new Button("+");

		MyTable.Attach(Plus, 3,4,2,3);

		Plus.Clicked += Calculator.PlusHandler ;


	// Division button and subscriptions

		Divided = new Button("/");

		MyTable.Attach(Divided, 3,4,3,4);

		Divided.Clicked += Calculator.DividedHandler ;


	// Subtraction button and subscriptions

		Minus = new Button("-");

		MyTable.Attach(Minus, 3,4,4,5);

		Minus.Clicked += Calculator.MinusHandler ;



	// Text Fields

		CalcScreen = new Entry();

		MyTable.Attach(CalcScreen, 0, 1, 0, 1);







		//Subscribe the window close event handler to the MyWindow.DeleteEvent event

		DeleteEventHandler CloseHandler = new DeleteEventHandler(window_close); // the DeleteEventHandler type is a delegate type. what we're doing is instantiating a delegate that, when called,  will reference the window_close method. Later we will subscribe that delegate to the event MyWindow.DeleteEvent (which is itsself basically a special type of delegate. At that point, calling MyWindow.DeleteEvent will call CloseEvent, which will call close_window 

		MyWindow.DeleteEvent += CloseHandler; // subscribe the new delegate to the event. Every time MyWindow.DeleteEvent is called, CloseHandler will now be called. Due to the previous line of code above, every time CloseHandler is called, close_window will be called. MyWindow.DeleteEvent is called by GTK any time the window is closed



	// everything has been added 



		// Expose the window to GTK

		MyWindow.ShowAll();


		// Run the application so the user can interact with the exposed window.

		Application.Run();


	}


	// Event handler method for MyWindow.DeleteEvent

	static void window_close(object obj, DeleteEventArgs e){// this needs these argument types so that it can subscribe to the delegate type DeleteEventHandler, which is defined in the GTK files. I do not know exactly why it uses these arguments.

		Application.Quit(); //Stop the application

		e.RetVal = true; // i don't really know what this line means

	}


	







}



#2
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts
If you are very new to C#, the biggest mistake that you can do is to use GTK# instead of .NET. It's a bit hard to get used to even if you know .NET, if you don't it's hard to find good documentation on the internet.
If you want to develop specifically for Linux, use Qt.
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

#3
DrSpaceman

DrSpaceman

    Newbie

  • Members
  • Pip
  • 4 posts
Davide,

Thanks for that advice.

I don't want to develop specifically for Linux, I just knew I wanted to learn C#, and that I didn't want to just learn how to create programs with visual c#.net. I figured if I started learning on Linux with GTK, I would have a better chance of learning the fundamentals of the language. So far I have not found GTK to be terribly difficult, but then again I probably haven't gotten to the more complicated parts.

I had never heard of Qt until now, but I'll try it out. It does look like it may be easier to learn first.

#4
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts
If you really want C#, start with .NET, if you want something easy and multi-platform use Java (very similar to C#). Qt works with C++ and it's very interesting. I can't tell you if you will find it easy or hard though.
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics