Im quite new to C# and I'm only really half way through learning about classes, objects and properties. Does anyone have easy projects I can do to start programming small easy programs, to get some practice?
Thanks,
-Ascension
have you done a calculator? that should be small and easy
Posted via CodeCall Mobile
No i haven't done that. Can you give me a short step list. I have no idea where to start.
n Basic Calculator program we have some Declaretion
1) if Prefix of variable start with bttn this is BUTTON
2) if Prefix of variable start with lb this is LABEL
3) if Prefix of variable start with m_this is member variable for Example:
*bttnD5 -> button Digit 5
*bRes -> label result
*m_Value-> member variable naming value
So We have
- 10 buttons digit (bttnD0->9)
- 5 buttons operator (bttnPlus,bttnMinus,bttnMul,bttnDiv,bttnEqul)
- 1 dot button bttnDot and 1 clear button bttnClr.
- 1 labels for display display result (mlbRes)
- m_value this to store data on it
- m_store this arraylist to store all string in lbRes
Also We will make 4 void function
1)void AddToArray(Button bttn) this function to display Digit on lbRes
and save this digit on m_value
2)void AddOperatorToArray(Button bttn) this function to add m_value in
m_Store then add operator to m_Store
3)void Reset() this is to clear lbRes,m_value and m_Store
4)void SetEnableOperatorBttns(bool enable) this sets operators to be enabled or not
Example:- To How To use this Program ?
Here we'll exmine how this program should work:
- asume you want to calculate 2.2 + 0.8 - 1 * 3 / 2
Just enter this Equation as we write here by click on buttons
Important Note:-in this program we neglect the periorty of operators
1) 2.2 + 0.8 = 3
2) 3 - 1 = 2
3) 2 * 3 = 6
4) 6 / 2 = 3 # final answer
how program work internaly?
We will discuss what happened when we enter Any Equation
Input: Equation :- 2 + 1.6 =
Output: Result :- 3.6
1) When you Press(click) on 2
this subroutine or Event will call void function [AddArray(bttnD2)]Code:private void bttnD2_Click(object sender, System.EventArgs e) { AddToArray(bttnD2); }
this function do the following:
[1] set the value equal 2
[2] set label lbRes = 2 to display it to user
[3] make all operator Enabled to use it
2) When you Press(click) on "+"Code:private void AddToArray(Button bttn) { //Step[1] m_value += bttn.Text; //Step[2] lbRes.Text += bttn.Text; //Step[3] SetEnableOperatorBttns(true); }
this subroutine or Event will call void function [AddOperatorArray(bttnPlus)]this function doCode:private void bttnPlus_Click(object sender, System.EventArgs e) { AddOperatorToArray(bttnPlus); }
[1]Add value in step 1) to arrayList
[2]display on lbRes to user 2.2
[3]make value empty
[4]add operator to arraylist
[5]make Dot button enabled
[6]call SetEnableOperatorBttns(false); to make all operator unenabled
(sure we can't make 2.2++)
3) When you press(click) on 1 like step 1)Code:private void AddOperatorToArray(Button bttn) { //[1] m_store.Add( m_value ); //[2] lbRes.Text += bttn.Text; //[3] m_value = ""; //[4] m_store.Add( bttn.Text ); //[5] bttnDot.Enabled = true; //[6] SetEnableOperatorBttns(false); }
4) When you Press(click) on "." this subroutine or Event will
[1]make all things like step 1)
[2]make dot unenabled because we can't make 2..2))),so after one click it will disable
5) When you press(click) on 6 like step 1)Code:private void bttnDot_Click(object sender, System.EventArgs e) { //[1] AddToArray(bttnDot); //[2] bttnDot.Enabled = false; }
6) When you press(click) on "="
[1] add value to arrylist value = 6
[2] add operator to arrylist "="
[3] take the first element in array sure it's digit and save it on m_res else it will give exception
[4] make forLoop on arraylist and check every element if equal to operator or not (+,-,/,*) if equal operator
(1)make make dispaly screen empty
(2)make calculation
(3)display res on screen
[5] make arraylist empty
[6] make m_value = lbRes
[7] check if m)value has dot or not because we can't make 2.2.2 this step handle this format
[8] make Equalbttn disable
ConclusionCode:private void bttnEqual_Click(object sender, System.EventArgs e) { try { //[1] m_store.Add(m_value); //[2] m_store.Add( bttnEqual.Text ); /* m_res here take the first element in m_store if this element is * operator like +*-/ Exception give us Error else it will take the * Digits or dot point */ //[3] float m_res = float.Parse ( m_store[0].ToString() ); /* this loop extract all element in Array then check it * if the element is operator it will calculate the prefix * and postfix and give us the result * Example 1+2/3*5 * 1) 1+2 = 3 * 2) 3/3 = 1 * 3) 1*5 = 5 * note:- no periority operator and no brakets so it's basic calculator:) */ //[4] for( int i = 0 ; i{ if( m_store[i].ToString() == "+" ) { lbRes.Text="";//(1) m_res += float.Parse (m_store[i+1].ToString());//(2) lbRes.Text = m_res.ToString();//(3) } else if( m_store[i].ToString() == "-" ) { lbRes.Text="";//(1) m_res -= float.Parse (m_store[i+1].ToString());//(2) lbRes.Text= m_res.ToString();//(3) } else if( m_store[i].ToString() == "*" ) { lbRes.Text="";//(1) m_res *= float.Parse ( m_store[i+1].ToString() );//(2) lbRes.Text= m_res.ToString();//(3) } else if( m_store[i].ToString() == "/" ) { lbRes.Text= "";//(1) m_res /= float.Parse ( m_store[i+1].ToString() );//(2) lbRes.Text= m_res.ToString();//(3) } } m_store.Clear();//[5] m_value = lbRes.Text;//[6] for( int i = 0 ; i < m_value.Length ; i++ )//[7] { if( m_value[i].ToString() == "." ) { bttnDot.Enabled = false; break; } else { bttnDot.Enabled = true; } } bttnEqual.Enabled = false;//[8] } catch(Exception exception) { MessageBox.Show(exception.Message,"Error"); } }
This was very simple C# application for beginers, this will put your hand on C# language and prepare you start programming with .Net Framework.
Last edited by Jaan; 12-20-2008 at 05:39 AM. Reason: Please use code tags when you're posting your codes!
After you do the calculator you could try making Tick-Tack-Toe or a Binary clock. Those were the two projects that we worked on in my C# class after the calculator.
thanks. I think I will keep reading the book I am learning from because I have not learned buttons and forms. Any suggestions for even easier projects? Thanks
Calculators are very easy. How about a paint application?
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks