计算器设计界面
本文主要用button作为按钮,richtextbox作为输出框来实现简单计算器,button数字的顺序为从左向右逐渐增大
具体代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication14
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string a = richTextBox1.Text;
double num3;
a = this.richTextBox1.Lines[0];
num3 = Convert.ToDouble(a);
richTextBox1.Text += "\ncos(" + a + ")\n";
richTextBox1.Text += "=" + Math.Cos(Math.PI * num3 / 180);
}
private void button2_Click(object sender, EventArgs e)
{
string a = richTextBox1.Text;
double num2;
a = this.richTextBox1.Lines[0];
num2 = Convert.ToDouble(a);
richTextBox1.Text += "\nsin(" + a + ")\n";
richTextBox1.Text += "=" + Math.Sin(Math.PI * num2 / 180);
}
private void button3_Click(object sender, EventArgs e)
{
string a = richTextBox1.Text;
double num4;
a = this.richTextBox1.Lines[0];
num4 = Convert.ToDouble(a);
double d = Math.Pow(num4, 2.0);
richTextBox1.Text += "\n^2(" + a + ")\n";
richTextBox1.Text += "=" +d;
}
private void button4_Click(object sender, EventArgs e)
{
string a = richTextBox1.Text;
double num1;
a= this.richTextBox1.Lines[0];
num1 = Convert.ToDouble(a);
richTextBox1.Text += "\nsqrt("+a+")\n";
richTextBox1.Text += "=" + Math.Sqrt(num1);
}
private void button5_Click(object sender, EventArgs e)
{
richTextBox1.Text = "";//清空过程显示框
richTextBox1.Text += "0";
}
private void button6_Click(object sender, EventArgs e)
{
richTextBox1.Text = "";//清空过程显示框
}
private void button7_Click(object sender, EventArgs e)
{
string a =richTextBox1.Text;
string temp = a.Remove(a.LastIndexOf(""), 1);
richTextBox1.Text = temp;
}
private void button8_Click(object sender, EventArgs e)
{
richTextBox1.Text += "\n/\n";
}
private void button9_Click(object sender, EventArgs e)
{
richTextBox1.Text += "9";
}
private void button10_Click(object sender, EventArgs e)
{
richTextBox1.Text += "8";
}
private void button11_Click(object sender, EventArgs e)
{
richTextBox1.Text += "7";
}
private void button12_Click(object sender, EventArgs e)
{
richTextBox1.Text += "\n*\n";
}
private void button13_Click(object sender, EventArgs e)
{
richTextBox1.Text += "6";
}
private void button14_Click(object sender, EventArgs e)
{
richTextBox1.Text += "5";
}
private void button15_Click(object sender, EventArgs e)
{
richTextBox1.Text += "4";
}
private void button16_Click(object sender, EventArgs e)
{
richTextBox1.Text += "\n-\n";
}
private void button17_Click(object sender, EventArgs e)
{
richTextBox1.Text += "3";
}
private void button18_Click(object sender, EventArgs e)
{
richTextBox1.Text += "2";
}
private void button19_Click(object sender, EventArgs e)
{
richTextBox1.Text += "1";
}
private void button20_Click(object sender, EventArgs e)
{
richTextBox1.Text += "\n+\n";
}
private void button21_Click(object sender, EventArgs e)
{
richTextBox1.Text += "\n^n\n";
}
private void button22_Click(object sender, EventArgs e)
{
richTextBox1.Text += "0";
}
private void button23_Click(object sender, EventArgs e)
{
richTextBox1.Text += ".";
}
private void button24_Click(object sender, EventArgs e)
{
double a = Convert.ToDouble(this.richTextBox1.Lines[0]);
double b = Convert.ToDouble(this.richTextBox1.Lines[2]);
string c = this.richTextBox1.Lines[1];
if (c == "+")
{
double d = a + b;
richTextBox1.Text += "=\n" + d;
}
if (c == "-")
{
double d = a - b;
richTextBox1.Text += "=\n" + d;
}
if (c == "*")
{
double d = a * b;
richTextBox1.Text += "=\n" + d;
}
if (c == "/")
{
double d = a / b;
richTextBox1.Text += "=\n" + d;
}
if (c == "^n")
{
double d = Math.Pow(a, b);
richTextBox1.Text += "=\n" + d;
}
}
}
}
本文总结
文章若出现缺少和错误之处,欢迎大家在评论区留言,纠正错误,补充本文不足之处,互相学习交流,在此共勉。