//
// Copyright (c) PlaceholderCompany. All rights reserved.
//
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 KepGridEditor
{
public partial class ExpressionZDialog : Form
{
private Dictionary descriptionMap = new Dictionary();
public ExpressionZDialog()
{
InitializeComponent();
InitDescriptionMap();
}
public string GetExpression()
{
return expresstionTextBox.Text;
}
private void InitDescriptionMap()
{
//descriptionMap.Add()
descriptionMap.Add("+", "加号运算符");
descriptionMap.Add("-", "减号运算符");
descriptionMap.Add("*", "乘号运算符");
descriptionMap.Add("/", "除号运算符");
descriptionMap.Add("()", "括号运算符");
descriptionMap.Add("^", "^;\r\n格式:x^y(x的y次方)");
descriptionMap.Add("sin()", "sin();\r\n格式:sin(表达式)");
descriptionMap.Add("cos()", "cos()\r\n格式:cos(表达式)");
descriptionMap.Add("int()", "int();\r\n格式:int(表达式)");
descriptionMap.Add("tan()", "tan();\r\n格式:tan(表达式)");
descriptionMap.Add("ctan()", "ctan();\r\n格式:ctan(表达式)");
descriptionMap.Add("sqrt()", "sqrt()\r\n格式:sqrt(表达式)");
descriptionMap.Add("exp()", "exp();\r\n格式:exp(表达式)");
descriptionMap.Add("log()", "log();\r\n格式:log(表达式)");
descriptionMap.Add("asin()", "asin();\r\n格式:asin(表达式)");
descriptionMap.Add("acos()", "acos();\r\n格式:acos(表达式)");
descriptionMap.Add("atan()", "atan();\r\n格式:atan(表达式)");
descriptionMap.Add("fabs()", "fabs();\r\n格式:fabs(表达式)");
descriptionMap.Add("<", "<;\r\n格式:x", ">;\r\n格式:x>y(x大于y时返回1,x小于或等于y时返回0)");
descriptionMap.Add("!", "!;\r\n格式:!x(将x值取非运算,当x为非零值时返回0,x为零时返回1)");
}
private void ExpressionZDialog_Load(object sender, EventArgs e)
{
functionCategoryComboBox.SelectedIndex = 0;
AddOperatorToFunctionCategoryComboBox();
AddFunctionToFunctionCategoryComboxBox();
AddLogicalOperationToFunctionCategoryComboxBox();
FillVariationListBox();
}
private void AddOperatorToFunctionCategoryComboBox()
{
functionListBox.Items.Add("+");
functionListBox.Items.Add("-");
functionListBox.Items.Add("*");
functionListBox.Items.Add("/");
functionListBox.Items.Add("()");
functionListBox.Items.Add("^");
}
private void AddFunctionToFunctionCategoryComboxBox()
{
functionListBox.Items.Add("sin()");
functionListBox.Items.Add("cos()");
functionListBox.Items.Add("int()");
functionListBox.Items.Add("tan()");
functionListBox.Items.Add("ctan()");
functionListBox.Items.Add("sqrt()");
functionListBox.Items.Add("exp()");
functionListBox.Items.Add("log()");
functionListBox.Items.Add("asin()");
functionListBox.Items.Add("acos()");
functionListBox.Items.Add("atan()");
functionListBox.Items.Add("fabs()");
}
private void AddLogicalOperationToFunctionCategoryComboxBox()
{
functionListBox.Items.Add("<");
functionListBox.Items.Add(">");
functionListBox.Items.Add("!");
}
private void functionListBox_SelectedValueChanged(object sender, EventArgs e)
{
string functionStr = functionListBox.SelectedItem.ToString();
if (functionStr.Length == 0)
return;
if (descriptionMap.ContainsKey(functionStr) == false)
return;
notationTextBox.Text = descriptionMap[functionStr];
}
public void InsertFunctionToExpresstionTextBox(string funcStr)
{
int start = expresstionTextBox.SelectionStart; //插入点位置
expresstionTextBox.Paste(funcStr);
//ScrollToCaret();
//expresstionTextBox.ScrollToCaret();
//expresstionTextBox.cu
}
private void FillVariationListBox()
{
variationListBox.Items.Add("l");
variationListBox.Items.Add("Pl");
variationListBox.Items.Add("x");
variationListBox.Items.Add("y");
variationListBox.Items.Add("z");
}
private void functionCategoryComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
int index = functionCategoryComboBox.SelectedIndex;
if (index < 0)
return;
functionListBox.Items.Clear();
if (index == 0)
{
AddOperatorToFunctionCategoryComboBox();
AddFunctionToFunctionCategoryComboxBox();
AddLogicalOperationToFunctionCategoryComboxBox();
}
else if (index == 1)
{
AddOperatorToFunctionCategoryComboBox();
}
else if (index == 2)
{
AddFunctionToFunctionCategoryComboxBox();
}
else if (index == 3)
{
AddLogicalOperationToFunctionCategoryComboxBox();
}
}
private void functionListBox_DoubleClick(object sender, EventArgs e)
{
string functionStr = functionListBox.SelectedItem.ToString();
expresstionTextBox.Paste(functionStr);
if (functionStr.Contains("()")) //如果是有括号的表达式 将插入点设置在括号里
{
expresstionTextBox.SelectionStart -= 1;
}
expresstionTextBox.Focus();
}
private void variationListBox_DoubleClick(object sender, EventArgs e)
{
string varStr = variationListBox.SelectedItem.ToString();
expresstionTextBox.Paste(varStr);
expresstionTextBox.Focus();
}
}
}