private void btnleft_Click(object sender, EventArgs e) //单机左键事件 { if (error) { ClearErrorinfo(); } if (DisPlay.SelectionStart > 0 && DisPlay.SelectionStart < 3) { DisPlay.SelectionStart--; DisPlay.Select(DisPlay.SelectionStart, 0); DisPlay.Focus(); } else { if (DisPlay.SelectionStart > 2) { char ch = DisPlay.Text[DisPlay.SelectionStart - 1]; if (ch == 's' || ch == 'n') { DisPlay.SelectionStart -= 3; //有Cos,Sin,Tan时,左则三格 } else { DisPlay.SelectionStart--; } } DisPlay.Select(DisPlay.SelectionStart, 0); DisPlay.Focus(); //若为空的处置 } }
private void btnPoint_Click(object sender, EventArgs e) //小数点 { Button btn = (Button)sender; DisPlay.Text += btnPoint.Text; DisPlay.Focus(); }
private string GetEnable_SubStr(ref bool Lastrow) //选择当前执行行数代码 { int RowIndex = DisPlay.GetLineFromCharIndex(DisPlay.SelectionStart); int Row = 0; pos[0] = 0; for (int i = 0; i < DisPlay.Text.Length; i++) { if (DisPlay.Text[i] == '\n') { Row++; pos[Row] = i + 1; } } if (RowIndex < Row) { string s = DisPlay.Text.Substring(pos[RowIndex], pos[RowIndex + 1] - pos[RowIndex] - 1); if (s[0] == '=') { s = s.Substring(1); } return(s); } else { int x = DisPlay.Text.Length - pos[RowIndex]; Lastrow = true; string s = DisPlay.Text.Substring(pos[RowIndex], x); if (s[0] == '=') { s = s.Substring(1); } return(s); } }
private void btnAnswer_Click(object sender, EventArgs e) { string s1 = ""; if (error) { ClearErrorinfo(); } int RowIndex = DisPlay.GetLineFromCharIndex(DisPlay.SelectionStart); CanOperation[RowIndex] = true; if (BIN.Checked) { s1 = System.Convert.ToString(preAnswer, 2); } if (OCT.Checked) { s1 = System.Convert.ToString(preAnswer, 8); } if (HEX.Checked) { s1 = System.Convert.ToString(preAnswer, 16); } if (DEC.Checked) { s1 = System.Convert.ToString(preAnswer); } DisPlay.SelectedText += s1; DisPlay.Select(DisPlay.SelectionStart, 0); DisPlay.Focus(); }
private void btnDiv_Click(object sender, EventArgs e) //除法运算 { if (error) { ClearErrorinfo(); } int RowIndex = DisPlay.GetLineFromCharIndex(DisPlay.SelectionStart); CanOperation[RowIndex] = true; int branch = Cheat_OpEnable(); if (branch == -1) //当前一个字符为+-*/(时,输入替换 { btnDel_Click(sender, e); DisPlay.SelectedText += '/'; DisPlay.Focus(); return; } if (branch == 0) { DisPlay.Focus(); return; } DisPlay.SelectedText += '/'; DisPlay.Select(DisPlay.SelectionStart, 0); DisPlay.Focus(); }
private void btnDiv_Click(object sender, EventArgs e) //运算键 除/ { Button btn = (Button)sender; DisPlay.SelectedText += " / "; DisPlay.Select(DisPlay.SelectionStart, 0); DisPlay.Focus(); }
private void btn4_Click(object sender, EventArgs e) { int RowIndex = DisPlay.GetLineFromCharIndex(DisPlay.SelectionStart); DisPlay.SelectedText += '4'; DisPlay.Select(DisPlay.SelectionStart, 0); DisPlay.Focus();//给richTextBox焦点 }
private void Show_Errorinfo(string S) //显示错误信息 { int pos = DisPlay.SelectionStart; DisPlay.Text = DisPlay.Text + '\n' + S; DisPlay.Select(pos, 0); DisPlay.Focus(); error = true; errorinfoLen = S.Length; }
private void btnPoint_Click(object sender, EventArgs e) { if (error) { ClearErrorinfo(); } DisPlay.SelectedText += '.'; DisPlay.Select(DisPlay.SelectionStart, 0); DisPlay.Focus(); }
private void ClearErrorinfo() //任意键清除错误信息 { int x = DisPlay.SelectionStart; errorinfoLen++; DisPlay.Text = DisPlay.Text.Substring(0, DisPlay.Text.Length - errorinfoLen); errorinfoLen = 0; error = false; DisPlay.Select(x, 0); //重新选中 DisPlay.Focus(); }
private void btnExp_Click(object sender, EventArgs e) { if (error) { ClearErrorinfo(); } int RowIndex = DisPlay.GetLineFromCharIndex(DisPlay.SelectionStart); CanOperation[RowIndex] = true; DisPlay.SelectedText += "E"; DisPlay.Select(DisPlay.SelectionStart, 0); DisPlay.Focus(); }
private void btnCE_Click(object sender, EventArgs e) { DisPlay.Text = string.Empty; //符号运算重置 DisPlay.Focus(); for (int i = 0; i < 50; i++) { CanOperation[i] = false; } error = false; //判是否错误 errorinfoLen = 0; pos = new int[50]; t_s = string.Empty; }
private void btnALT_Click(object sender, EventArgs e) { DisPlay.Focus(); if (ALT) { ALT = false; labALT.ForeColor = SystemColors.Control; } else { ALT = true; labALT.ForeColor = Color.SteelBlue; } }
private void btnDel_Click(object sender, EventArgs e) //退格键 { if (error) { ClearErrorinfo(); } int RowIndex = DisPlay.GetLineFromCharIndex(DisPlay.SelectionStart); int x = DisPlay.SelectionStart; if (x > 0) { if (DisPlay.Text[x - 1] == 'n' || DisPlay.Text[x - 1] == 's') { DisPlay.Text = DisPlay.Text.Remove(x - 3, 3); } else { if (DisPlay.Text[x - 1] == 'o' || DisPlay.Text[x - 1] == 'a') { DisPlay.Text = DisPlay.Text.Remove(x - 2, 3); } else { if (DisPlay.Text[x - 1] == 'T' || DisPlay.Text[x - 1] == 'C' || DisPlay.Text[x - 1] == 'S') { DisPlay.Text = DisPlay.Text.Remove(x - 1, 3); } else { DisPlay.Text = DisPlay.Text.Remove(x - 1, 1); //使用remove之后Selection会变更,用x记录变更前位置 } } } DisPlay.SelectionStart = x - 1; DisPlay.Select(DisPlay.SelectionStart, 0); DisPlay.Focus(); } else { DisPlay.Focus(); } }
private void btnLn_Click(object sender, EventArgs e) { if (error) { ClearErrorinfo(); } int RowIndex = DisPlay.GetLineFromCharIndex(DisPlay.SelectionStart); CanOperation[RowIndex] = true; if (ALT) { DisPlay.SelectedText += "e^"; DisPlay.Select(DisPlay.SelectionStart, 0); DisPlay.Focus(); ResetALT(); return; } DisPlay.SelectedText += "ln("; DisPlay.Select(DisPlay.SelectionStart, 0); DisPlay.Focus(); }
private void btnright_Click(object sender, EventArgs e) { if (error) { ClearErrorinfo(); } char ch = new char(); if (DisPlay.Text != string.Empty) { ch = DisPlay.Text[DisPlay.SelectionStart]; } if (ch == 'T' || ch == 'S' || ch == 'C') //有Cos,Sin,Tan时,右则三格 { DisPlay.SelectionStart += 3; } else { DisPlay.SelectionStart++; } DisPlay.Select(DisPlay.SelectionStart, 0); DisPlay.Focus(); }
private void btnup_Click(object sender, EventArgs e) //向上键事件实现 { if (error) { ClearErrorinfo(); } int RowIndex = DisPlay.GetLineFromCharIndex(DisPlay.SelectionStart); if (RowIndex > 0) { int Row = 0; for (int i = 0; i < DisPlay.Text.Length; i++) { if (DisPlay.Text[i] == '\n') { Row++; pos[Row] = i + 1; } } DisPlay.Select(pos[RowIndex - 1], 0); } DisPlay.Focus(); }
private void btnEqual_Click(object sender, EventArgs e) { int RowIndex = DisPlay.GetLineFromCharIndex(DisPlay.SelectionStart); if (!CanOperation[RowIndex]) { DisPlay.Focus(); return; } CanOperation[RowIndex] = false; bool Lastrow = false; string Str1; if ((Str1 = GetEnable_SubStr(ref Lastrow)) == "") { return; //Str1为得到的子串 } int Cheak = Cheak_Str(Str1); if (Cheak == 0) { DisPlay.Focus(); return; } string Str = Str1 + '='; Str = StrDespose(Str); //Str为处理过后可以进行运算的子串 Stack <char> OPTR = new Stack <char>(); //操作符栈 Stack <double> OPND = new Stack <double>(); //操作数栈 int Len = Str.Length; int curr = 0; char theta, x; double a = 0; //操作数1,2 double b = 0; double real_input = 0; //压入多位数字 bool is_num = false; //判断读入是否为数字,true为数字 OPTR.Push('='); char ch = new char(); if ((real_input = GetNumber(Str, ref ch, ref curr, ref is_num)) == -1) { return; } while (ch != '=' || OPTR.Peek() != '=') { if (is_num) { OPND.Push(real_input); is_num = false; //一旦压入数字,则为下一次判断初始化 real_input = 0; } else { while (ch == ')') //右括号的处理 { bool have_leftbracket = false; char[] StackTR = OPTR.ToArray(); for (int i = 0; i < OPTR.Count; i++) { if (StackTR[i] == '(') { have_leftbracket = true; break; } } if (have_leftbracket) { break; } if ((real_input = GetNumber(Str, ref ch, ref curr, ref is_num)) == -1) { return; } } if (ch == '=' && OPTR.Peek() == '(') { while (OPTR.Peek() == '(') { OPTR.Pop(); } } switch (Precede(OPTR.Peek(), ch)) { case '<': OPTR.Push(ch); if ((real_input = GetNumber(Str, ref ch, ref curr, ref is_num)) == -1) { return; } break; case '>': theta = OPTR.Pop(); if (theta == '+' || theta == '-' || theta == '*' || theta == '/' || theta == '^' || theta == 'x') //普通运算符运算 { if (OPND.Count == 0) //出栈前检测是否为空 { Show_Errorinfo("Error: \"" + theta + "\"not enough operand"); return; } b = OPND.Pop(); if (OPND.Count == 0) { Show_Errorinfo("Error: \"" + theta + "\"not enough operand"); return; } a = OPND.Pop(); OPND.Push(Operate(a, theta, b)); } else { if (OPND.Count == 0) { Show_Errorinfo("Error: not enough operand"); return; } a = OPND.Pop(); OPND.Push(Operate(a, theta)); if (flag) { flag = false; return; } } break; case '=': x = OPTR.Pop(); if ((real_input = GetNumber(Str, ref ch, ref curr, ref is_num)) == -1) { return; } break; } } } //输出 if (t_s == string.Empty) { if (OPND.Count == 0) { if (Str1 == "π") { DisPlay.Text = DisPlay.Text + "\n=" + Math.PI.ToString() + '\n'; t_s = DisPlay.Text; preAnswer = Math.PI; } else { DisPlay.Text = DisPlay.Text + "\n=" + Str.Remove(Str.Length - 1) + '\n'; t_s = DisPlay.Text; preAnswer = double.Parse(Str.Remove(Str.Length - 1)); } } else { if (Lastrow == true) { DisPlay.Text = DisPlay.Text + "\n=" + OPND.Peek() + '\n'; //最后一行的增加 t_s = DisPlay.Text; } else { DisPlay.Text = DisPlay.Text + Str1 + "\n=" + OPND.Peek() + '\n'; //非最后一行的增加 t_s = DisPlay.Text; } preAnswer = OPND.Peek(); } setMouse(); } else { if (OPND.Count == 0) { if (Str1 == "π") { DisPlay.Text = t_s + Str1 + "\n=" + Math.PI.ToString() + '\n'; t_s = DisPlay.Text; preAnswer = Math.PI; } else { DisPlay.Text = t_s + Str1 + "\n=" + Str.Remove(Str.Length - 1) + '\n'; t_s = DisPlay.Text; preAnswer = double.Parse(Str.Remove(Str.Length - 1)); } } else { if (Lastrow == true) { DisPlay.Text = t_s + Str1 + "\n=" + OPND.Peek() + '\n'; //最后一行的增加 t_s = DisPlay.Text; t_s = DisPlay.Text; } else { DisPlay.Text = t_s + Str1 + "\n=" + OPND.Peek() + '\n'; //非最后一行的增加 t_s = DisPlay.Text; } preAnswer = OPND.Peek(); } setMouse(); } }
private void btnEqual_Click(object sender, EventArgs e) { int d1; string d2; string s_txt = DisPlay.Text; int space = s_txt.IndexOf(' '); string s1 = s_txt.Substring(0, space).Trim(); //将第一个数字和空格合赋给字符串s1 char opration = Convert.ToChar(s_txt.Substring((space + 1), 1)); //运算符 string s2 = s_txt.Substring(space + 3); //将第二个数字赋给字符串s2 if (this.OCT.Checked) //八进制 { int i = System.Convert.ToInt32(s1, 8); int j = System.Convert.ToInt32(s2, 8); switch (opration) { case '+': d1 = i + j; d2 = System.Convert.ToString(d1, 8); break; case '-': d1 = i - j; d2 = System.Convert.ToString(d1, 8); break; case '*': d1 = i * j; d2 = System.Convert.ToString(d1, 8); break; case '/': if (j == 0) { throw new ApplicationException(); } else { d1 = i / j; d2 = System.Convert.ToString(d1, 8); } break; default: throw new ApplicationException(); } DisPlay.Text = d2; DisPlay.Focus(); preAnswer = System.Convert.ToInt32(d2, 8); } if (this.HEX.Checked)//十六进制 { int i = System.Convert.ToInt32(s1, 16); int j = System.Convert.ToInt32(s2, 16); switch (opration) { case '+': d1 = i + j; d2 = System.Convert.ToString(d1, 16); break; case '-': d1 = i - j; d2 = System.Convert.ToString(d1, 16); break; case '*': d1 = i * j; d2 = System.Convert.ToString(d1, 16); break; case '/': if (j == 0) { throw new ApplicationException(); } else { d1 = i / j; d2 = System.Convert.ToString(d1, 16); } break; default: throw new ApplicationException(); } DisPlay.Text = d2.ToUpper(); DisPlay.Focus(); DisPlay.Select(DisPlay.TextLength, 0); preAnswer = System.Convert.ToInt32(d2, 16); } if (this.BIN.Checked)//二进制 { int i = System.Convert.ToInt32(s1, 2); int j = System.Convert.ToInt32(s2, 2); switch (opration) { case '+': d1 = i + j; d2 = System.Convert.ToString(d1, 2); break; case '-': d1 = i - j; d2 = System.Convert.ToString(d1, 2); break; case '*': d1 = i * j; d2 = System.Convert.ToString(d1, 2); break; case '/': if (j == 0) { throw new ApplicationException(); } else { d1 = i / j; d2 = System.Convert.ToString(d1, 2); } break; default: throw new ApplicationException(); } DisPlay.Text = d2; DisPlay.Focus(); DisPlay.Select(DisPlay.TextLength, 0); preAnswer = System.Convert.ToInt32(d2, 2); } if (this.DEC.Checked)//十进制 { double d; double a1 = Convert.ToDouble(s1); double a2 = Convert.ToDouble(s2); switch (opration) { case '+': d = a1 + a2; break; case '-': d = a1 - a2; break; case '*': d = a1 * a2; break; case '/': if (a2 == 0) { throw new ApplicationException(); } else { d = a1 / a2; } break; default: throw new ApplicationException(); } DisPlay.Text = d.ToString(); DisPlay.Focus(); DisPlay.Select(DisPlay.TextLength, 0); preAnswer = (int)d; } }
public void setMouse() { DisPlay.Focus();//给richTextBox焦点 DisPlay.Select(DisPlay.TextLength, 0); }