26285 total geeks with 3498 solutions
Recent challengers:
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 

Articles

GEEK

User's box
Username:
Password:

Forgot password?
New account

Shoutbox
MaxMouse
It's Friday... That's good enough for me!
CodeX
non stop lolz here but thats soon to end thanks to uni, surely the rest of the world is going good?
stabat
how things are going guys? Here... boring...
CodeX
I must be going wrong on the password lengths then, as long as it was done on ECB
MaxMouse
lol... the key is in hex (MD5: of the string "doit" without the "'s) and is in lower case. Maybe i should have submitted this as a challenge!

Donate
Donate and help us fund new challenges
Donate!
Due Date: May 31
May Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00
Contributors


News Feeds
The Register
Soylent Corporation
prepares to DEFEAT
FOOD
VMware public cloud
aims at ESXi
customers, not AWS
Microsoft reveals
Xbox One, the
console that can
read your heartbeat
New Intel CEO
Krzanich takes
reins of core
product groups
CSIRO scales up
solar cell printing
Cook: Apple has "no
current plan" to
pull profits out of
Ireland
AWS cloud gains
critical federal
security
certification
IBM puts
supercomputer
Watson to work in
ROBOT CALL CENTRE
Irish deputy PM:
You want more tax
from Apple? Your
problem, not ours
Private equity firm
coughs £1bn for
Websense
Slashdot
Aurora Attackers
Were Looking For
Google"s
Surveillance
Database
Dart Is Not the
Language You Think
It Is
EPA Makes a Rad
Decision
Ask Slashdot: Can
Yahoo Actually
Stage a Comeback?
3-D Printable Food
Gets Funding From
NASA
Transporting a
15-Meter-Wide,
600-Ton Magnet
Cross Country
House Bill Would
Mandate Smart Gun
Tech By U.S.
Manufacturers
Do Developers Need
Free Perks To
Thrive?
So You"ve Always
Wanted a
Hovercraft...
(Video)
Microsoft Unveils
Xbox One
Article viewer

Calculator In C#



Written by:dimport
Published by:Nightscript
Published on:2003-06-21 07:19:46
Topic:C
Search OSI about C.More articles by dimport.
 viewed 30175 times send this article printer friendly

Digg this!
    Rate this article :
This is a simple calculator that can perform addition, subtraction, multiplication and division.
It has a graphical interface using forms.

using System;
 
using System.Windows.Forms;
 
using System.Drawing;
 
 
 
 
public class win:Form {
 
 
     Button[] b = new Button[10];
 
    Button bDot,bPlus,bSub,bMul,bDiv,bEqu,bClr;
 
    Panel panCalc;
 
    TextBox txtCalc;
 
    
 
    Double dblAcc;
 
    Double dblSec;
 
    bool blnClear,blnFrstOpen;
 
    String strOper;
 
    
 
    public win() {
 
       try {
 
        this.Text="Calculator";
 
        panCalc=new Panel();
 
        txtCalc = new TextBox();
 
 
         txtCalc.Location = new Point(10,10);
 
        txtCalc.Size=new Size(150,10);
 
        txtCalc.ReadOnly=true;
 
        txtCalc.RightToLeft=RightToLeft.Yes;
 
        panCalc.Size=new Size(200,200);
 
        panCalc.BackColor=Color.Aqua;
 
        panCalc.Controls.Add(txtCalc);
 
        addButtons(panCalc);
 
        this.Size=new Size(200,225);
 
        this.Controls.Add(panCalc);
 
        
 
        dblAcc=0;
 
        dblSec=0;
 
        blnFrstOpen=true;
 
        blnClear=false;
 
        strOper=new String('=',1);
 
        }
 
        catch (Exception e) {
 
        Console.WriteLine("error ...... " + e.StackTrace);
 
        }
 
    }
 
    
 
    private void addButtons(Panel p) {
 
        for (int i=0;i<=9;i++) {
 
            b[i]=new Button();
 
            b[i].Text=Convert.ToString(i);
 
            b[i].Size=new Size(25,25);
 
            b[i].BackColor=Color.White;
 
            b[i].Click+=new EventHandler(btn_clk);
 
            p.Controls.Add(b[i]);
 
        }
 
        b[0].Location=new Point(10,160);
 
        b[1].Location=new Point(10,120);
 
        b[4].Location=new Point(10,80);
 
        b[7].Location=new Point(10,40);
 
        
 
        b[2].Location=new Point(50,120);
 
        b[5].Location=new Point(50,80);
 
        b[8].Location=new Point(50,40);
 
        
 
        b[3].Location=new Point(90,120);
 
        b[6].Location=new Point(90,80);
 
        b[9].Location=new Point(90,40);
 
        
 
        bDot=new Button();
 
        bDot.Size=new Size(25,25);
 
        bDot.Location=new Point(50,160);
 
        bDot.BackColor=Color.White;
 
        bDot.Text=".";
 
        bDot.Click+=new EventHandler(btn_clk);
 
        
 
        bPlus=new Button();
 
        bPlus.Size=new Size(25,25);
 
        bPlus.Location=new Point(130,160);
 
        bPlus.BackColor=Color.White;
 
        bPlus.Text="+";
 
        bPlus.Click+=new EventHandler(btn_Oper);
 
        
 
        bSub=new Button();
 
        bSub.Size=new Size(25,25);
 
        bSub.Location=new Point(130,120);
 
        bSub.BackColor=Color.White;
 
        bSub.Text="-";
 
        bSub.Click+=new EventHandler(btn_Oper);
 
        
 
        bMul=new Button();
 
        bMul.Size=new Size(25,25);
 
        bMul.Location=new Point(130,80);
 
        bMul.BackColor=Color.White;
 
        bMul.Text="*";
 
        bMul.Click+=new EventHandler(btn_Oper);
 
        
 
        bDiv=new Button();
 
        bDiv.Size=new Size(25,25);
 
        bDiv.Location=new Point(130,40);
 
        bDiv.BackColor=Color.White;
 
        bDiv.Text="/";
 
        bDiv.Click+=new EventHandler(btn_Oper);
 
        
 
        bEqu=new Button();
 
        bEqu.Size=new Size(25,25);
 
        bEqu.Location=new Point(90,160);
 
        bEqu.BackColor=Color.White;
 
        bEqu.Text="=";
 
        bEqu.Click+=new EventHandler(btn_equ);
 
        
 
        bClr=new Button();
 
        bClr.Size=new Size(20,45);
 
        bClr.Location=new Point(170,40);
 
        bClr.BackColor=Color.Orange;
 
        bClr.Text="AC";
 
        bClr.Click+=new EventHandler(btn_clr);
 
 
         p.Controls.Add(bDot);
 
        p.Controls.Add(bPlus);
 
        p.Controls.Add(bSub);
 
        p.Controls.Add(bMul);
 
        p.Controls.Add(bDiv);
 
        p.Controls.Add(bEqu);
 
        p.Controls.Add(bClr);
 
    }
 
    
 
    private void btn_clk(object obj,EventArgs ea)
{
 
        if(blnClear)
 
            txtCalc.Text="";
 
        
 
        Button b3=(Button)obj;
 
        
 
        txtCalc.Text+=b3.Text;
 
        
 
        if (txtCalc.Text==".")
 
            txtCalc.Text="0.";
 
        dblSec=Convert.ToDouble(txtCalc.Text);
 
        
 
        blnClear=false;
 
    }
 
    
 
    private static void Main() {
 
        Application.Run(new win());
 
    }
 
    
 
    private void btn_Oper(object obj,EventArgs ea) {
 
        Button tmp=(Button)obj;
 
        strOper=tmp.Text;
 
        if (blnFrstOpen)
 
            dblAcc=dblSec;
 
        else
 
            calc();
 
 
         blnFrstOpen=false;
 
        blnClear=true;
 
    }
 
 
     private void btn_clr(object obj,EventArgs ea) {
 
        clear();
 
    }
 
 
     private void btn_equ(object obj,EventArgs ea) {
 
        calc();
 
        
 
    }
 
    
 
    private void calc() {
 
 
         switch(strOper) {
 
        
 
            case "+":
 
                dblAcc+=dblSec;
 
                break;
 
            case "-":
 
                dblAcc-=dblSec;
 
                break;
 
            case "*":
 
                dblAcc*=dblSec;
 
                break;
 
            case "/":
 
                dblAcc/=dblSec;
 
                break;
 
        }
 
    
 
        strOper="=";
 
        blnFrstOpen=true;
 
        txtCalc.Text=Convert.ToString(dblAcc);
 
        dblSec=dblAcc;
 
    }
 
    
 
    private void clear() {
 
        dblAcc=0;
 
        dblSec=0;
 
        blnFrstOpen=true;
 
        txtCalc.Text="";
 
        txtCalc.Focus();
 
 
     }
 
}


This article was originally written by

Did you like this article? There are hundreds more.

Comments:
Anonymous
2006-11-02 22:29:20
what have to be done if we are to use the () in between the equation and performing the BODMASS functions
Anonymous
2006-11-07 04:58:43
This calc code should have
b[i]= new Button();//in place of b=new Button();
Anonymous
2011-03-28 12:09:46
U2326078
ObatAsamUrat
2011-06-16 07:44:33
Great article. We've genuinely liked surfing around your blog blogposts. No matter the reason I’ll always be registering to your current supply i we imagine you produce yet again before long! The knowledge below can be very necessary to us! Let me article one of the links to this particular web site in my blog site. More than likely our website visitors will quickly realize that will invaluable. Cheerful Chrismas!
kaos distro
Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:


Blogs: (People who have posted blogs on this subject..)
amisauv
Creating a Lexical Analyzer in C on Tue 9th Dec 11am
#include<stdio.h> #include<string.h> #include<conio.h> #include<ctype.h> /*************************************** ************************* Functions prototype. **************************************** *************************/ void Open_File(
amisauv
Controling digital circuit through computer on Tue 9th Dec 10am
this code access the lpt port.here only 4 of the total 8 pins are used but can be modified for full 8 pins.it has a complete GUI with mouse & keyboard interactive control panel.works well in win98, but not in winxp. #include<stdio.h> #include<conio.
amisauv
/* Computerised Electrical Equipment Control */ /* PC BASED DEVICE CONTROLLER * on Tue 9th Dec 10am
#include<stdio.h> #include<conio.h> #include<dos.h> void main() { void tone(void); int p=0x0378; char ex={"Created By Mrc"}; int j; char ex1={"For Further Details & Improvements"}; int k; char ex2={"Contact : E-mail : anbudan
amisauv
Calendar Program on Tue 9th Dec 10am
This program prints Weekdays of specified date. It even prints calendar of a given year too. /*Ccalendar library*/ #include<stdio.h> #include<string.h> #include<conio.h> int getNumberOfDays(int month,int year) { switch(month) { case
amisauv
Calculator: on Tue 9th Dec 10am
#include"graphics.h" #include"dos.h" #include"stdio.h" #include"math.h" union REGS i,o; char text={ "7","8","9","*","4","5","6","/","1","2", "3","+","0","00",".","-","M","M+", "M-","+/-","MR","MC","x^2","sr","OFF","A C","CE","="}; int s=0,k=0,pass
amisauv
INFECTED CODES WRITTEN IN C\C++ on Tue 9th Dec 10am
This is a simple code that changes system time and date. It is written using c/c++ but can be easily converted to java. #include "stdio.h" #include "process.h" #include "dos.h" int main(void) { struct date new_date; struct date old_date; s
amisauv
A C programme which can print the file name it is kept in on Tue 9th Dec 9am
#include<stdio.h> main(){ printf(”the source file name is %s\n”,__FILE__); } actually __FILE__ is a macro which stands for the file name the programme is kept in and the compiler does the rest .. for you ..
amisauv
BOOTSECTOR EDITOR: on Tue 9th Dec 9am
Code : /*program to save the partion table of your hard disk for future use. it will save your partition table in a file partition.dat */ #include<stdio.h> #include<bios.h> #include<conio.h> #include<stdlib.h> #include<ctype.h> void main () {
amisauv
BLINKING STAR : on Tue 9th Dec 9am
#include<conio.h> #include<graphics.h> #include<stdlib.h> #include<dos.h> void main() { int gdriver=DETECT,gmode; int i,x,y; initgraph(&gdriver,&gmode,"e: cgi"); while(!kbhit()) { x=random(640); y=random(480); setcolor
amisauv
// To print semicolons using C programming without using semicolons any where i on Tue 9th Dec 9am
// To print semicolons using C programming without using semicolons any where in the C code in program. // #include<stdio.h> #include<conio.h> void main() { char a; a=59; if(printf("%c",a)){} getch();

Test Yourself: (why not try testing your skill on this subject? Clicking the link will start the test.)
BSD sockets API by skrye

This is a test of your knowledge of the BSD socket interface
C Programming by keoki

This test is aimed at a C programmer that is at an intermediate level.


     
Your Ad Here
 
Copyright Open Source Institute, 2006