C#计算器源代码

2022-03-25 教育 47阅读
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Drawing;
usingSystem.Windows.Forms;namespacewindshadow14
{

publicpartialclassMainForm
{

inti=0;
inta;
intb;
inti1;
longc;

publicstaticvoidMain(string[]args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(newMainForm());
}

publicMainForm()
{

InitializeComponent();

}

voidButton10Click(objectsender,System.EventArgse)
{

if(textBox1.Text=="")
{
textBox1.Text=textBox1.Text+"0";
}

}

voidButton1Click(objectsender,System.EventArgse)
{
textBox1.Text=textBox1.Text+"1";
if(textBox1.Text=="")
{
textBox1.Text=textBox1.Text+"1";
}

}

voidButton2Click(objectsender,System.EventArgse)
{
textBox1.Text=textBox1.Text+"2";
if(textBox1.Text=="")
{
textBox1.Text=textBox1.Text+"2";
}
}

voidButton3Click(objectsender,System.EventArgse)
{
textBox1.Text=textBox1.Text+"3";
if(textBox1.Text=="")
{
textBox1.Text=textBox1.Text+"3";
}
}

voidButton4Click(objectsender,System.EventArgse)
{
textBox1.Text=textBox1.Text+"4";
if(textBox1.Text=="")
{
textBox1.Text=textBox1.Text+"4";
}
}

voidButton5Click(objectsender,System.EventArgse)
{
textBox1.Text=textBox1.Text+"5";
if(textBox1.Text=="")
{
textBox1.Text=textBox1.Text+"5";
}
}

voidButton6Click(objectsender,System.EventArgse)
{
textBox1.Text=textBox1.Text+"6";
if(textBox1.Text=="")
{
textBox1.Text=textBox1.Text+"6";
}
}

voidButton7Click(objectsender,System.EventArgse)
{
textBox1.Text=textBox1.Text+"7";
if(textBox1.Text=="")
{
textBox1.Text=textBox1.Text+"7";
}
}

voidButton8Click(objectsender,System.EventArgse)
{
textBox1.Text=textBox1.Text+"8";
if(textBox1.Text=="")
{
textBox1.Text=textBox1.Text+"8";
}
}

voidButton9Click(objectsender,System.EventArgse)
{
textBox1.Text=textBox1.Text+"9";
if(textBox1.Text=="")
{
textBox1.Text=textBox1.Text+"9";
}
}

voidTextBox1TextChanged(objectsender,System.EventArgse)
{
if(i==1||i==2||i==3||i==4)
{
textBox1.Text="";

}

i=0;

}

voidButton12Click(objectsender,System.EventArgse)
{
i=1;
a=int.Parse(textBox1.Text);
i1=1;

}

voidButton16Click(objectsender,System.EventArgse)
{

b=int.Parse(textBox1.Text);
textBox1.Text="";
switch(i1)
{
case1:
c=a+b;
break;
case2:
c=a-b;
break;

case3:
c=a*b;
break;

case4:
c=a/b;
break;

}

textBox1.Text=c.ToString();

i=0;
a=0;
b=0;
i1=0;

}

voidButton13Click(objectsender,System.EventArgse)
{
i1=2;
i=1;

a=int.Parse(textBox1.Text);
}

voidMainFormLoad(objectsender,System.EventArgse)
{

}

voidButton14Click(objectsender,System.EventArgse)
{
i1=3;
i=3;

a=int.Parse(textBox1.Text);
}

voidButton15Click(objectsender,System.EventArgse)
{
i1=4;
i=4;

a=int.Parse(textBox1.Text);
}
}
}
还可以这样编usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;namespacewindshadow
{
publicpartialclassForm1:Form
{
inta;
intb;
intc;
intb1;
publicForm1()
{
InitializeComponent();
}privatevoidbutton1_Click(objectsender,EventArgse)
{

textBox1.Text=textBox1.Text+"1";
if(b==1)
{
textBox1.Text="";textBox1.Text=textBox1.Text+"1";
b=0;}}privatevoidbutton2_Click(objectsender,EventArgse)
{
textBox1.Text=textBox1.Text+"2";
if(b==1)
{
textBox1.Text="";textBox1.Text=textBox1.Text+"2";
b=0;}
}privatevoidbutton3_Click(objectsender,EventArgse)
{
textBox1.Text=textBox1.Text+"3";
if(b==1)
{
textBox1.Text="";textBox1.Text=textBox1.Text+"3";
b=0;}
}privatevoidbutton4_Click(objectsender,EventArgse)
{
textBox1.Text=textBox1.Text+"4";
if(b==1)
{
textBox1.Text="";textBox1.Text=textBox1.Text+"4";
b=0;}
}privatevoidbutton5_Click(objectsender,EventArgse)
{
textBox1.Text=textBox1.Text+"5";
if(b==1)
{
textBox1.Text="";textBox1.Text=textBox1.Text+"5";
b=0;}
}privatevoidbutton6_Click(objectsender,EventArgse)
{
textBox1.Text=textBox1.Text+"6";
if(b==1)
{
textBox1.Text="";textBox1.Text=textBox1.Text+"6";
b=0;}
}privatevoidbutton7_Click(objectsender,EventArgse)
{
textBox1.Text=textBox1.Text+"7";
if(b==1)
{
textBox1.Text="";textBox1.Text=textBox1.Text+"7";
b=0;}
}privatevoidbutton8_Click(objectsender,EventArgse)
{
textBox1.Text=textBox1.Text+"8";
if(b==1)
{
textBox1.Text="";textBox1.Text=textBox1.Text+"8";
b=0;}
}privatevoidbutton9_Click(objectsender,EventArgse)
{
textBox1.Text=textBox1.Text+"9";
if(b==1)
{
textBox1.Text="";textBox1.Text=textBox1.Text+"9";
b=0;}
}privatevoidbutton11_Click(objectsender,EventArgse)
{
textBox1.Text=textBox1.Text+"0";
if(b==1)
{
textBox1.Text="";textBox1.Text=textBox1.Text+"0";
b=0;}
}privatevoidbutton10_Click(objectsender,EventArgse)
{
textBox1.Text=textBox1.Text+".";
}privatevoidbutton13_Click(objectsender,EventArgse)
{
a=int.Parse(textBox1.Text);

b=1;
b1=1;
}privatevoidbutton17_Click(objectsender,EventArgse)
{
longd;
switch(b1)
{
case1:c=int.Parse(textBox1.Text);
d=a+c;
textBox1.Text="";
textBox1.Text=d.ToString();
break;
case2:
c=int.Parse(textBox1.Text);
d=a-c;
textBox1.Text="";
textBox1.Text=d.ToString();
break;
case3:
c=int.Parse(textBox1.Text);
d=a*c;
textBox1.Text="";
textBox1.Text=d.ToString();
break;
case4:
c=int.Parse(textBox1.Text);
d=a/c;
textBox1.Text="";
textBox1.Text=d.ToString();
break;
}
}privatevoidbutton14_Click(objectsender,EventArgse)
{
a=int.Parse(textBox1.Text);b=1;
b1=2;
}privatevoidbutton15_Click(objectsender,EventArgse)
{
a=int.Parse(textBox1.Text);b=1;
b1=3;
}privatevoidbutton16_Click(objectsender,EventArgse)
{
a=int.Parse(textBox1.Text);b=1;
b1=4;
}privatevoidbutton12_Click(objectsender,EventArgse)
{
textBox1.Text="";
b=0;
b1=0;
a=0;
c=0;
}privatevoidtextBox1_TextChanged(objectsender,EventArgse)
{
}
}
}这是退格键voidButton1Click(objectsender,System.EventArgse)
{
try
{
inta=int.Parse(textBox1.Text);
a=a/10;
textBox1.Text=a.ToString();
}
catch
{
doublea=double.Parse(textBox1.Text);

intb=(int)a;
textBox1.Text=a.ToString();

intzz=textBox1.Text.Length;

intt=zz-1;

textBox1.Text=textBox1.Text.Remove(t);

}

}
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com