Home > Net >  C # write a calculator program
C # write a calculator program

Time:09-24

C # calculator program, supporting brackets, the decimal point. Mainly improve the brackets and the decimal point, arithmetic problems, for your reference. Also refer to the online some methods, thanked here. Writing is not good, please comment.
The first part:
using System;
using System.Collections.Generic;
Using System.Com ponentModel;
Using System. The Data;
Using System. Drawing;
using System.Linq;
Using System. The Text;
Using System. The Threading. The Tasks;
Using System. Windows. Forms;

The namespace labman
{
Public partial class Calculator: Form
{
Public Calculator ()
{
InitializeComponent();
}

Public string s;
Public string input="";
Public string s1, s2, s3;
Int Num=1;

Bool Judge=true;
Bool dotjudge=true;
Bool bracketjudge=true;

Void DigitalInput (char c)
{
If (txtBox1. Text=="")
{
Input +=c;
}
Else if (Judge & amp; & (input. Length==1) & amp; & (input!="0"))

{
If (input. The Substring (input. The Length - 1, 1)!=") ")

{
Input +=c;
}
}
Else if (input. Length>
=2){
If ((input Substring (input. Length - 2, 2)!="/0") & amp; & (input. The Substring (input. The Length - 1, 1)! "=")))
{
Input +=c;
}
}

}
Void DotInput (char c)
{
Input +=c;
}

Void SymbolInput (char c)
{
If (txtBox1. Text=="")
{
Input +=c;

}
Else if (txtBox1 Text!="")
{
S1=input. The Substring (input. The Length - 1, 1);
Bracketjudge=true;
//((Judge & amp; & (s1!=") ") & amp; & (s1!=(") ") | | ((c=='+') | | (c=='-') | | (c=='*') | | (c=='/')))
If ((s1=="("))
{
Bracketjudge=false;
}

Else if ((Judge & amp; & (s1==") ") | | (c=='+') | | (c=='-') | | (c=='*') | | (c=='/')))
{
Input +=c;
}
}
}

Void LeftBracketInput (char c)
{
If (txtBox1. Text=="")
{
Input +=c;
}
Else if (bracketjudge & amp; & (input. The Substring (input. The Length - 1, 1)! "=")))
{
Int tempL=0, tempR=0;
For (int I=input. The Length - 1; I & gt;=0; I -)
{
S2=input. The Substring (I, 1);
If ((s2=="("))
{
TempL=tempL + 1;

}
If ((s2==") "))
{
TempR=tempR + 1;


}
}

S1=input. The Substring (input. The Length - 1, 1);
If ((s1=="1") | | (s1=="2") | | (s1=="3") | | (s1=="4") | | (s1=="5") | | (s1=="6") | | (s1=="7") | | (s1=="8") | | (s1=="9") | | (s1=="0") | | (s1==""))//| | (tempL % 2==0) | | (tempR % 2==0))
{
Bracketjudge=false;
}
The else
{input +=c; }
}


}
Void RightBracketInput (char c)
{

Int templb=0;
Int temprb=0;
For (int I=input. The Length - 1; I & gt;=0; I -)
{
If ((input Substring (I, 1))=="(")
{templb=templb + 1; }
}

For (int I=input. The Length - 1; I & gt;=0; I -)
{
If ((input Substring (I, 1))==") ")
{temprb=temprb + 1; }
}

If (txtBox1. Text=="")
{
Input +=c;
}
Else if (bracketjudge)//& amp; & (input. The Substring (input. The Length - 1, 1)! "=")))
{

S1=input. The Substring (input. The Length - 1, 1);
If ((s1=="+") | | (s1=="-") | | (s1=="*") | | (s1=="/") | | (s1=="") | | (s1==" ("))
{
Bracketjudge=false;
}
Else if (templb> Temprb)
{input +=c; }
}


}

Bool Pairjudge (string s)
{
return false;
}



///
///display function
///

///
Void ShowMessage ()
{
TxtBox1. Text=input;
}

Private void Button1_Click (object sender, EventArgs e)
{

Judge=true;//k=1;
DigitalInput (' 1 ');
ShowMessage ();

}

Private void Button2_Click (object sender, EventArgs e)
{
Judge=true;//k=1;
//Num=1;
DigitalInput (' 2 ');
ShowMessage ();
}

Private void Button3_Click (object sender, EventArgs e)
{
Judge=true;
DigitalInput (' 3 ');
ShowMessage ();
}

Private void Button4_Click (object sender, EventArgs e)
{
Judge=true;
DigitalInput (' 4 ');
ShowMessage ();
}

Private void Button5_Click (object sender, EventArgs e)
{
Judge=true;
DigitalInput (' 5 ');
ShowMessage ();
}

Private void Button6_Click (object sender, EventArgs e)
{
Judge=true;
DigitalInput (' 6 ');
ShowMessage ();
}

Private void Button7_Click (object sender, EventArgs e)
{
Judge=true;
DigitalInput (' 7 ');
ShowMessage ();
}

Private void Button8_Click (object sender, EventArgs e)
{
Judge=true;
DigitalInput (' 8 ');
ShowMessage ();
}

Private void Button9_Click (object sender, EventArgs e)
{
Judge=true;
DigitalInput (' 9 ');
ShowMessage ();
}

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C #
  • Related