有关科学计算器的方法设计(C#)
这只是一个纯小白的自治计算器,代码可能不是十分简便,希望可以用作参考。
有关普通计算器和科学计算器的代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string operation = null;
double number1 = 0, number2 = 0;
private void textpush(string x) //普通计算器的1,2,,3等等的输入函数
{
if (textBox1.Text == "0")
{
textBox1.Text = "";
}
textBox1.Text += x;
}
private void textpush_1(string x) //科学计算器的1,2,3等的输入函数
{
if (textBox2.Text == "0")
{
textBox2.Text = "";
}
textBox2.Text += x;
textBox3.Text += x;
}
#region General calculator
private void button34_Click(object sender, EventArgs e)
{
textpush("0");
}
private void button24_Click(object sender, EventArgs e)
{
textpush("1");
}
private void button35_Click(object sender, EventArgs e)
{
textpush("2");
}
private void button40_Click(object sender, EventArgs e)
{
textpush("3");
}
private void button31_Click(object sender, EventArgs e)
{
textpush("4");
}
private void button36_Click(object sender, EventArgs e)
{
textpush("5");
}
private void button41_Click(object sender, EventArgs e)
{
textpush("6");
}
private void button32_Click(object sender, EventArgs e)
{
textpush("7");
}
private void button37_Click(object sender, EventArgs e)
{
textpush("8");
}
private void button42_Click(object sender, EventArgs e)
{
textpush("9");
}
private void button23_Click(object sender, EventArgs e) //普通计算器正负号的运算
{
if (textBox1.Text == "")
textBox1.Text += "";
else
{
if (operation != null)
button49_Click(sender, e);
textBox1.Text = (-1 * Convert.ToDouble(textBox1.Text)).ToString();
}
}
private void button39_Click(object sender, EventArgs e) //普通计算器小数点的问题
{
if (textBox1.Text == "")
{
textBox1.Text += "0.";
}
else if (textBox1.Text.IndexOf(".") > 0)
{
textBox1.Text += "";
}
else
{
textBox1.Text += ".";
}
}
private void button47_Click(object sender, EventArgs e) //普通计算器"+"运算
{
if (operation != null)
button49_Click(sender, e);
if (textBox1.Text == "")
textBox1.Text = "0";
number1 = Convert.ToDouble(textBox1.Text);
operation = "+";
textBox1.Text = "";
}
private void button46_Click(object sender, EventArgs e) //普通计算器的"-"运算
{
if (operation != null)
button49_Click(sender, e);
if (textBox1.Text == "")
textBox1.Text = "0";
number1 = Convert.ToDouble(textBox1.Text);
operation = "-";
textBox1.Text = "";
}
private void button45_Click(object sender, EventArgs e) //普通计算器的"*"运算
{
if (operation != null)
button49_Click(sender, e);
if (textBox1.Text == "")
textBox1.Text = "0";
number1 = Convert.ToDouble(textBox1.Text);
operation = "*";
textBox1.Text = "";
}
private void button44_Click(object sender, EventArgs e) //普通计算器的"/"运算
{
if (operation != null)
button49_Click(sender, e);
if (textBox1.Text == "")
textBox1.Text = "0";
number1 = Convert.ToDouble(textBox1.Text);
operation = "/";
textBox1.Text = "";
}
private void button53_Click(object sender, EventArgs e) //普通计算器的退格处理
{
if (textBox1.Text.Length > 0)
{
textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
}
else
{
textBox1.Text = "";
}
}
private void button52_Click(object sender, EventArgs e) //普通计算器的清零处理
{
textBox1.Text = "";
number1 = 0;
number2 = 0;
operation = null;
}
private void button49_Click(object sender, EventArgs e) //普通计算器的清理处理
{
if (textBox1.Text != "")
{
number2 = Convert.ToDouble(textBox1.Text);
switch (operation)
{
case "+": textBox1.Text = (number1 + number2).ToString(); break;
case "-": textBox1.Text = (number1 - number2).ToString(); break;
case "*": textBox1.Text = (number1 * number2).ToString(); break;
case "/":
{
if (number2 == 0)
MessageBox.Show("0 不能作为除数", "WRONG!!");
else
textBox1.Text = (number1 / number2).ToString();
break;
}
}
number1 = Convert.ToDouble(textBox1.Text);
operation = null;
}
}
private void button48_Click(object sender, EventArgs e) //普通计算器的平方根处理
{
if (operation != null)
button49_Click(sender, e);
if (textBox1.Text != "")
{
if (Convert.ToDouble(textBox1.Text) < 0)
MessageBox.Show(" 小于0的数无法进行平方根运算.", "WRONG!!!");
else
{
textBox1.Text = (Math.Round(Math.Sqrt(Convert.ToDouble(textBox1.Text)), 10)).ToString();
}
}
}
private void button43_Click(object sender, EventArgs e) //普通计算器的log运算
{
if (operation != null)
button49_Click(sender, e);
if (textBox1.Text != "")
{
if (Convert.ToDouble(textBox1.Text) <= 0)
MessageBox.Show("真数必须大于0", "WRONG!!!");
else
{
textBox1.Text = (Math.Round(Math.Log10(Convert.ToDouble(textBox1.Text)), 10)).ToString();
}
}
}
private void button38_Click(object sender, EventArgs e) //普通计算器的平方运算
{
if (operation != null)
button49_Click(sender, e);
if (textBox1.Text != "")
{
textBox1.Text = (Convert.ToDouble(textBox1.Text) * Convert.ToDouble(textBox1.Text)).ToString();
}
}
private void button33_Click(object sender, EventArgs e) //普通计算器的倒数运算
{
if (operation != null)
button49_Click(sender, e);
if (textBox1.Text != "")
{
if (textBox1.Text == "0")
MessageBox.Show("0不能作为除数", "WRONG!!!");
else
{
textBox1.Text = (Math.Round(1 / (Convert.ToDouble(textBox1.Text)), 10)).ToString();
}
}
}
#endregion
#region Science calculator
private void button4_Click(object sender, EventArgs e)
{
textpush_1("1");
}
private void button9_Click(object sender, EventArgs e)
{
textpush_1("2");
}
private void button14_Click(object sender, EventArgs e)
{
textpush_1("3");
}
private void button3_Click(object sender, EventArgs e)
{
textpush_1("4");
}
private void button8_Click(object sender, EventArgs e)
{
textpush_1("5");
}
private void button13_Click(object sender, EventArgs e)
{
textpush_1("6");
}
private void button2_Click(object sender, EventArgs e)
{
textpush_1("7");
}
private void button7_Click(object sender, EventArgs e)
{
textpush_1("8");
}
private void button12_Click(object sender, EventArgs e)
{
textpush_1("9");
}
private void button10_Click(object sender, EventArgs e)
{
textpush_1("0");
}
private void button15_Click(object sender, EventArgs e)
{
if (textBox3.Text == "")
{
textBox2.Text += "0.";
textBox3.Text += "0.";
}
else if (textBox3.Text.IndexOf(".") > 0)
{
textBox2.Text += "";
textBox3.Text += "";
}
else
{
textBox2.Text += ".";
textBox3.Text += ".";
}
}
private void button17_Click(object sender, EventArgs e)
{
textBox2.Text += "+";
textBox3.Text = "";
}
private void button18_Click(object sender, EventArgs e)
{
textBox2.Text += "-";
textBox3.Text = "";
}
private void button19_Click(object sender, EventArgs e)
{
textBox2.Text += "*";
textBox3.Text = "";
}
private void button20_Click(object sender, EventArgs e)
{
textBox2.Text += "/";
textBox3.Text = "";
}
private void button29_Click(object sender, EventArgs e)
{
textBox2.Text += "(";
textBox3.Text = "";
}
private void button50_Click(object sender, EventArgs e)
{
textBox2.Text += ")";
textBox3.Text = "";
}
private void button1_Click(object sender, EventArgs e) //科学计算器的倒数运算
{
if (textBox3.Text == "")
textBox2.Text += "";
else if (textBox2.Text == textBox3.Text)
{
if (textBox3.Text == "0")
MessageBox.Show("0不能作为除数", "WRONG!!!");
else
{
textBox3.Text = (Math.Round(1 / (Convert.ToDouble(textBox3.Text)), 10)).ToString();
textBox2.Text = textBox3.Text;
}
}
else
{
if (textBox3.Text == "0")
MessageBox.Show("0不能作为除数", "WRONG!!!");
else
{
textBox2.Text = textBox2.Text.Substring(0, textBox2.Text.Length - textBox3.Text.Length);
textBox3.Text = (Math.Round(1 / (Convert.ToDouble(textBox3.Text)), 10)).ToString();
textBox2.Text += textBox3.Text;
}
}
}
private void button5_Click(object sender, EventArgs e) //变号操作
{
if (textBox3.Text == "")
textBox2.Text+="";
else if (textBox2.Text == textBox3.Text)
{
textBox3.Text = (-1 * Convert.ToDouble(textBox3.Text)).ToString();
textBox2.Text = textBox3.Text;
}
else
{
textBox2.Text = textBox2.Text.Substring(0, textBox2.Text.Length - textBox3.Text.Length );
textBox3.Text = (-1 * Convert.ToDouble(textBox3.Text)).ToString();
textBox2.Text += textBox3.Text;
}
}
private void button21_Click(object sender, EventArgs e) //退格操作
{
if (textBox3.Text.Length > 0)
{
textBox3.Text = textBox3.Text.Substring(0, textBox3.Text.Length - 1);
textBox2.Text=textBox2.Text.Substring(0, textBox2.Text.Length - 1);
}
else
{
textBox3.Text = "";
if(textBox2.Text.Length > 0)
{
textBox2.Text = textBox2.Text.Substring(0, textBox2.Text.Length - 1);
}
else
textBox2.Text = "";
}
}
private void button22_Click(object sender, EventArgs e) //清空操作
{
textBox2.Text = "";
textBox3.Text = "";
}
private void button6_Click(object sender, EventArgs e) //平方函数
{
if (textBox3.Text == "")
textBox2.Text += "";
else if (textBox2.Text == textBox3.Text)
{
textBox3.Text = (Convert.ToDouble(textBox3.Text) * Convert.ToDouble(textBox3.Text)).ToString();
textBox2.Text = textBox3.Text;
}
else
{
textBox2.Text = textBox2.Text.Substring(0, textBox2.Text.Length - textBox3.Text.Length);
textBox3.Text = (Convert.ToDouble(textBox3.Text) * Convert.ToDouble(textBox3.Text)).ToString();
textBox2.Text += textBox3.Text;
}
}
private void button11_Click(object sender, EventArgs e) //logx函数
{
if (textBox3.Text == "")
textBox2.Text += "";
else if (textBox2.Text == textBox3.Text)
{
if (Convert.ToDouble(textBox3.Text) <= 0)
MessageBox.Show("真数必须大于0", "WRONG!!!");
else
{
textBox3.Text = (Math.Round(Math.Log10(Convert.ToDouble(textBox3.Text)), 10)).ToString();
textBox2.Text = textBox3.Text;
}
}
else
{
if (Convert.ToDouble(textBox3.Text) <= 0)
MessageBox.Show("真数必须大于0", "WRONG!!!");
else
{
textBox2.Text = textBox2.Text.Substring(0, textBox2.Text.Length - textBox3.Text.Length);
textBox3.Text = (Math.Round(Math.Log10(Convert.ToDouble(textBox3.Text)), 10)).ToString();
textBox2.Text += textBox3.Text;
}
}
}
private void button16_Click(object sender, EventArgs e) //平方根函数
{
if (textBox3.Text == "")
textBox2.Text += "";
else if (textBox2.Text == textBox3.Text)
{
if (Convert.ToDouble(textBox3.Text) < 0)
MessageBox.Show(" 小于0的数无法进行平方根运算.", "WRONG!!!");
else
{
textBox3.Text = Math.Round(Math.Sqrt(Convert.ToDouble(textBox3.Text)), 10).ToString();
textBox2.Text = textBox3.Text;
}
}
else
{
if (Convert.ToDouble(textBox3.Text) < 0)
MessageBox.Show(" 小于0的数无法进行平方根运算.", "WRONG!!!");
else
{
textBox2.Text = textBox2.Text.Substring(0, textBox2.Text.Length - textBox3.Text.Length);
textBox3.Text = Math.Round(Math.Sqrt(Convert.ToDouble(textBox3.Text)), 10).ToString();
textBox2.Text += textBox3.Text;
}
}
}
private void button26_Click(object sender, EventArgs e) //sin函数
{
if (textBox3.Text == "")
textBox2.Text += "";
else if (textBox2.Text == textBox3.Text)
{
textBox3.Text = Math.Round(Math.Sin(Convert.ToDouble(textBox3.Text)),10).ToString();
textBox2.Text = textBox3.Text;
}
else
{
textBox2.Text = textBox2.Text.Substring(0, textBox2.Text.Length - textBox3.Text.Length);
textBox3.Text = Math.Round(Math.Sin(Convert.ToDouble(textBox3.Text)), 10).ToString();
textBox2.Text += textBox3.Text;
}
}
private void button27_Click(object sender, EventArgs e) //cos函数
{
if (textBox3.Text == "")
textBox2.Text += "";
else if (textBox2.Text == textBox3.Text)
{
textBox3.Text = Math.Round(Math.Cos(Convert.ToDouble(textBox3.Text)), 10).ToString();
textBox2.Text = textBox3.Text;
}
else
{
textBox2.Text = textBox2.Text.Substring(0, textBox2.Text.Length - textBox3.Text.Length);
textBox3.Text = Math.Round(Math.Cos(Convert.ToDouble(textBox3.Text)), 10).ToString();
textBox2.Text += textBox3.Text;
}
}
private void button28_Click(object sender, EventArgs e) //tan函数
{
if (textBox3.Text == "")
textBox2.Text += "";
else if (textBox2.Text == textBox3.Text)
{
textBox3.Text = Math.Round(Math.Tan(Convert.ToDouble(textBox3.Text)), 10).ToString();
textBox2.Text = textBox3.Text;
}
else
{
textBox2.Text = textBox2.Text.Substring(0, textBox2.Text.Length - textBox3.Text.Length);
textBox3.Text = Math.Round(Math.Tan(Convert.ToDouble(textBox3.Text)), 10).ToString();
textBox2.Text += textBox3.Text;
}
}
private void button25_Click(object sender, EventArgs e)
{
string expression_1 = textBox2.Text;
juge(expression_1);
expression_1 = textBox2.Text;
string expression_2 = test_turn(expression_1);
string number = getnumber(expression_2);
textBox3.Text = number;
}
private void juge(string expression_1) //判断是否有不符合常规的表达式的判断
{
if (expression_1[0] == '+' || expression_1[0] == '-' || expression_1[0] == '*' || expression_1[0] == '/' || expression_1[0] == '.')
textBox2.Text = "0" + textBox2.Text;
}
private double getvalue(char op, double ch1, double ch2) //运算符执行的步骤
{
switch (op)
{
case '+':
return ch2 + ch1;
case '-':
return ch2 - ch1;
case '*':
return ch2 * ch1;
case '/':
return ch2 / ch1;
default:
return 0;
}
}
private string getnumber(string test_2) //表达式的计算
{
Stack<string> stacks = new Stack<string>();
Stack<string> num = stacks;
double non1, non2, ret;
string number = null;
for (int i = 0; i < test_2.Length; i++)
{
if (test_2[i] != '+' && test_2[i] != '-' && test_2[i] != '*' && test_2[i] != '/')
{
if (test_2[i] == '#')
{
Console.WriteLine(number);
num.Push(number);
number = null;
continue;
}
number += test_2[i];
}
else
{
non1 = Convert.ToDouble(num.Pop());
non2 = Convert.ToDouble(num.Pop());
ret = getvalue(test_2[i], non1, non2);
num.Push(ret.ToString());
}
}
return num.Pop();
}
private string test_turn(string test_1) //转后缀表达式的函数
{
string test_2 = null;
Stack<char> stacks = new Stack<char>();
Stack<char> operation = stacks;
Console.WriteLine(test_1);
for (int i = 0; i < test_1.Length; i++)
{
if (test_1[i] != '+' && test_1[i] != '-' && test_1[i] != '*' && test_1[i] != '/' && test_1[i] != '(' && test_1[i] != ')')
{
test_2 += test_1[i];
if (i == test_1.Length - 1)
test_2 += "#";
if (i < test_1.Length - 1)
if (test_1[i + 1] == '+' || test_1[i + 1] == '-' || test_1[i + 1] == '*' || test_1[i + 1] == '/' || test_1[i + 1] == '(' || test_1[i + 1] == ')')
test_2 += "#";
}
else
{
switch (test_1[i])
{
case ')':
while (operation.Count > 0)
{
if (operation.Peek() == '(')
break;
test_2 += operation.Pop();
}
operation.Pop();
break;
case '(':
operation.Push(test_1[i]);
break;
case '+':
case '-':
case '*':
case '/':
while (operation.Count > 0)
{
if (test_number(test_1[i]) <= test_number(operation.Peek()))
{
test_2 += operation.Pop();
}
else
{
break;
}
}
operation.Push(test_1[i]);
break;
}
}
}
while (operation.Count > 0)
test_2 += operation.Pop();
Console.WriteLine(test_2);
return test_2;
}
private int test_number(char a) //符号优先级判断
{
switch (a)
{
case '+': return 1;
case '-': return 1;
case '*': return 2;
case '/': return 2;
default: return 0;
}
}
private void button30_Click(object sender, EventArgs e) //启动画图功能
{
Form2 form2 = new Form2();
form2.ShowDialog();
}
private void Form1_KeyDown(object sender, KeyEventArgs e) //实现按键输入
{
switch (e.KeyCode)
{
case Keys.NumPad1:
this.button4_Click(null, null);
button25.Focus();
break;
case Keys.NumPad2:
this.button9_Click(null, null);
button25.Focus();
break;
case Keys.NumPad3:
this.button14_Click(null, null);
button25.Focus();
break;
case Keys.NumPad4:
this.button3_Click(null, null);
button25.Focus();
break;
case Keys.NumPad5:
this.button8_Click(null, null);
button25.Focus();
break;
case Keys.NumPad6:
this.button13_Click(null, null);
button25.Focus();
break;
case Keys.NumPad7:
this.button2_Click(null, null);
button25.Focus();
break;
case Keys.NumPad8:
this.button7_Click(null, null);
button25.Focus();
break;
case Keys.NumPad9:
this.button12_Click(null, null);
button25.Focus();
break;
case Keys.NumPad0:
this.button10_Click(null, null);
button25.Focus();
break;
case Keys.Add:
this.button17_Click(null, null);
button25.Focus();
break;
case Keys.Multiply:
this.button19_Click(null, null);
button25.Focus();
break;
case Keys.Subtract:
this.button18_Click(null, null);
button25.Focus();
break;
case Keys.Divide:
this.button20_Click(null, null);
button25.Focus();
break;
case Keys.Back:
this.button21_Click(null, null);
button25.Focus();
break;
case Keys.Escape:
this.button22_Click(null, null);
button25.Focus();
break;
case Keys.Decimal:
this.button15_Click(null, null);
button25.Focus();
break;
case Keys.D9:
this.button29_Click(null, null);
button25.Focus();
break;
case Keys.D0:
this.button50_Click(null, null);
button25.Focus();
break;
default:
break;
}
}
#endregion
}
}
画图功能的实现如下
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 WindowsFormsApp3
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
int i=1;
int x1 = 0;
int y1 = 0;
int number = 0;
double[] y2=new double[2000];
int[] x2 = new int[2000];
private void button1_Click(object sender, EventArgs e) //绘图button
{
Form2_Paint(sender, null);
pictureBox1_Paint(sender, null);
}
private void button2_Click(object sender, EventArgs e) //sin函数
{
if(textBox1.Text=="")
textBox1.Text += "sin(x)";
else
textBox1.Text += "+sin(x)";
if (x1 != -628)
x1 -= 628;
y1 += -Convert.ToInt32(Math.Sin(0 / 100) * 100 );
for (; i < 1500; i++)
y2[i]+= - Math.Sin(i / 100.0) * 100;
i = 1;
}
private void button3_Click(object sender, EventArgs e) //cos函数
{
if (textBox1.Text == "")
textBox1.Text += "cos(x)";
else
textBox1.Text += "+cos(x)";
if (x1 != -628)
x1 -= 628;
y1 += -Convert.ToInt32(Math.Cos(0 / 100) * 100);
for (; i < 1500; i++)
y2[i] += -Math.Cos(i / 100.0) * 100;
i = 1;
}
private void button6_Click(object sender, EventArgs e) //tan函数
{
if (textBox1.Text == "")
textBox1.Text += "tan(x)";
else
textBox1.Text += "+tan(x)";
if(x1!=-628)
x1 -= 628;
y1 += -Convert.ToInt32(Math.Tan(0 / 100) * 100);
for (; i < 1500; i++)
y2[i] += -Math.Tan(i / 100.0) * 100;
i = 1;
}
private void button7_Click(object sender, EventArgs e) //清空按钮
{
pictureBox1.Image = null;
textBox1.Text = null;
for (int j = 1; j < 1500; j++)
y2[j] = 0;
y1 = 0;
x1 = 0;
number = 0;
n = 1;
m = 1;
}
private void button5_Click(object sender, EventArgs e) //logx函数
{
if (textBox1.Text == "")
textBox1.Text += "log(x)";
else
textBox1.Text += "+log(x)";
if (x1 == -628)
x1 += 628;
y1 += -Convert.ToInt32(Math.Log10(1) * 70)+150;
for (; i < 1500; i++)
y2[i] += -Math.Log10(i / 1.0) * 70+150;
i = 1;
}
private void button4_Click(object sender, EventArgs e) //lnx函数
{
if (textBox1.Text == "")
textBox1.Text += "ln(x)";
else
textBox1.Text += "+ln(x)";
if (x1 == -628)
x1 += 628;
y1 += -Convert.ToInt32(Math.Log( 1) * 70)+150;
for (; i < 1500; i++)
y2[i] += -Math.Log(i / 1.0) * 70+150;
i = 1;
}
private void button8_Click(object sender, EventArgs e) //x的幂次函数
{
number = 0;
number = (int)numericUpDown1.Value;
if (textBox1.Text == "")
textBox1.Text += "x^"+number.ToString();
else
textBox1.Text += "+x^" + number.ToString();
if (x1 != -628)
x1 -= 628;
y1 += Convert.ToInt32(Math.Pow(0,number));
if(number>1)
for (i=0; i < 1500; i++)
y2[i] += -Math.Pow(i-628, number)/100;
if(number<=1)
for (i = 0; i < 1500; i++)
y2[i] += -Math.Pow(i - 628, number);
i = 1;
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g;
g = this.pictureBox1.CreateGraphics();
Point begin = new Point();
Point end = new Point();
Pen pn_1 = new Pen(Color.Blue);
begin.X = x1 + pictureBox1.Width / 2; //起点
begin.Y = y1 + pictureBox1.Height / 2;
for (; i < 1500; i++)
{
end.X = x1 + i + pictureBox1.Width / 2;
end.Y = Convert.ToInt32(y2[i]) + pictureBox1.Height / 2;
g.DrawLine(pn_1, begin, end);
begin = end;
}
i = 1;
picture_line();
picture_line3();
}
private void picture_line(int n=1) //x轴标尺(放大)
{
Graphics g;
g = this.pictureBox1.CreateGraphics();
Point line_1 = new Point();
Point line_2 = new Point();
line_1.Y = pictureBox1.Height / 2;
line_2.Y = pictureBox1.Height / 2 - 10;
line_1.X = 0;
line_2.X = 0;
Pen pn_2 = new Pen(Color.Black);
for (; i < 1500; i++)
{
g.DrawLine(pn_2, line_1, line_2);
line_1.X += 10*n;
line_2.X += 10*n;
}
i = 1;
}
private void picture_line1(int n = 1) //x轴标尺(缩小)
{
Graphics g;
g = this.pictureBox1.CreateGraphics();
Point line_1 = new Point();
Point line_2 = new Point();
line_1.Y = pictureBox1.Height / 2;
line_2.Y = pictureBox1.Height / 2 - 10;
line_1.X = 0;
line_2.X = 0;
Pen pn_2 = new Pen(Color.Black);
for (; i < 1500; i++)
{
g.DrawLine(pn_2, line_1, line_2);
line_1.X += 10 / n;
line_2.X += 10 / n;
}
i = 1;
}
private void picture_line3(int n = 1) //y轴标尺(放大)
{
Graphics g;
g = this.pictureBox1.CreateGraphics();
Point line_1 = new Point();
Point line_2 = new Point();
line_1.Y = 0;
line_2.Y = 0;
line_1.X = pictureBox1.Width/2;
line_2.X = pictureBox1.Width / 2+10;
Pen pn_2 = new Pen(Color.Black);
for (; i < 1500; i++)
{
g.DrawLine(pn_2, line_1, line_2);
line_1.Y += 10 * n;
line_2.Y += 10 * n;
}
i = 1;
}
private void picture_line4(int m = 1) //y轴标尺(缩小)
{
Graphics g;
g = this.pictureBox1.CreateGraphics();
Point line_1 = new Point();
Point line_2 = new Point();
line_1.Y = 0;
line_2.Y = 0;
line_1.X = pictureBox1.Width / 2;
line_2.X = pictureBox1.Width / 2 + 10;
Pen pn_2 = new Pen(Color.Black);
for (; i < 1500; i++)
{
g.DrawLine(pn_2, line_1, line_2);
line_1.Y += 10 / m;
line_2.Y += 10 / m;
}
i = 1;
}
private void Form2_Paint(object sender, PaintEventArgs e) //画坐标轴
{
Graphics g;
g = this.pictureBox1.CreateGraphics();
Point axis_1 = new Point();
Point axis_2 = new Point();
Point axis_3 = new Point();
Point axis_4 = new Point();
Pen pn_2 = new Pen(Color.Black);
axis_1.X = pictureBox1.Width; //x 轴右端点
axis_1.Y = pictureBox1.Height / 2;
axis_2.X = 0; //x 轴左端点
axis_2.Y = pictureBox1.Height / 2;
axis_3.X = pictureBox1.Width / 2; //y 轴上端点
axis_3.Y = 0;
axis_4.X = pictureBox1.Width / 2; //y 轴下端点
axis_4.Y = pictureBox1.Height;
g.DrawLine(pn_2, axis_2, axis_1);
g.DrawLine(pn_2, axis_3, axis_4);
}
private void Form2_Load(object sender, EventArgs e) //判断鼠标滑轮
{
this.MouseWheel += pictureBox1_MouseWheel;
}
private void pictureBox11_Paint(object sender, PaintEventArgs e) //放大函数功能
{
Graphics g;
g = this.pictureBox1.CreateGraphics();
PointF begin = new Point();
PointF end = new Point();
Pen pn_1 = new Pen(Color.Blue);
begin.X = (x1 )*n + pictureBox1.Width / 2; //起点
begin.Y = (y1 )*n + pictureBox1.Height / 2;
for (; i < 1500; i++)
{
end.X = (x1 + i )*n + pictureBox1.Width / 2;
end.Y = (Convert.ToInt32(y2[i]) )*n + pictureBox1.Height / 2;
g.DrawLine(pn_1, begin, end);
begin = end;
}
i = 1;
Point axis_1 = new Point();
Point axis_2 = new Point();
Point axis_3 = new Point();
Point axis_4 = new Point();
Pen pn_2 = new Pen(Color.Black);
axis_1.X = pictureBox1.Width; //x 轴右端点
axis_1.Y = pictureBox1.Height / 2;
axis_2.X = 0; //x 轴左端点
axis_2.Y = pictureBox1.Height / 2;
axis_3.X = pictureBox1.Width / 2; //y 轴上端点
axis_3.Y = 0;
axis_4.X = pictureBox1.Width / 2; //y 轴下端点
axis_4.Y = pictureBox1.Height;
g.DrawLine(pn_2, axis_2, axis_1);
g.DrawLine(pn_2, axis_3, axis_4);
}
private void pictureBox12_Paint(object sender, PaintEventArgs e) //缩小函数图像功能
{
Graphics g;
g = this.pictureBox1.CreateGraphics();
PointF begin = new Point();
PointF end = new Point();
Pen pn_1 = new Pen(Color.Blue);
begin.X = (x1 ) / m + pictureBox1.Width / 2; //起点
begin.Y = (y1 ) / m + pictureBox1.Height / 2;
for (; i < 1500; i++)
{
end.X = (x1 + i ) / m + pictureBox1.Width / 2;
end.Y = (Convert.ToInt32(y2[i]) ) / m + pictureBox1.Height / 2;
g.DrawLine(pn_1, begin, end);
begin = end;
}
i = 1;
Point axis_1 = new Point();
Point axis_2 = new Point();
Point axis_3 = new Point();
Point axis_4 = new Point();
Pen pn_2 = new Pen(Color.Black);
axis_1.X = pictureBox1.Width; //x 轴右端点
axis_1.Y = pictureBox1.Height / 2;
axis_2.X = 0; //x 轴左端点
axis_2.Y = pictureBox1.Height / 2;
axis_3.X = pictureBox1.Width / 2; //y 轴上端点
axis_3.Y = 0;
axis_4.X = pictureBox1.Width / 2; //y 轴下端点
axis_4.Y = pictureBox1.Height;
g.DrawLine(pn_2, axis_2, axis_1);
g.DrawLine(pn_2, axis_3, axis_4);
}
int n = 1;
int m = 1;
private void pictureBox1_MouseWheel(object sender, MouseEventArgs e) //滑轮上下滑动执行的操作
{
if(e.Delta>0)
{
n++;
Refresh();
picture_line(n);
picture_line3(n);
pictureBox11_Paint(sender, null);
}
else
{
m++;
Refresh();
picture_line1(m);
picture_line4(m);
pictureBox12_Paint(sender, null);
}
}
}
}