GIS二次开发学习专题(一)C#入门
程序员文章站
2022-05-22 10:40:57
...
GIS二次开发学习专题(一)
假期时间想整理电脑内存,所以就把代码发到网上了,希望可以供大家参考,学习资料来自《牟乃夏 AE开发教程—基于C#.NET》
发的代码是我整理+老师布置的作业
第一部分是用c#写一个标准计算器和科学计算器
标准计算器与科学计算器在不同的窗体中,两者切换通过“按钮”来操作
首先是标准计算器的代码:
标准计算器是带括号的计算:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace HelloWorld
{
public partial class Form1 : Form
{
public static List<char> inputStr = new List<char>(1000); //用链表保存用户的输入
public Form1()
{
InitializeComponent();
}
/*控制显示界面*/
private void Form1_load(object sender, EventArgs e)
{
txtReadout.Enabled = false; //作用: 禁止人为手动修改
CText.Enabled = false; //如上
txtReadout.Text = ""; //初始化 显示
CText.Text = "0";
}
/*按键数字0-9*/
private void btnNum1_Click(object sender, EventArgs e)
{
inputStr.Add('1'); //在链表中追加记录
txtReadout.AppendText("1");
}
//////////////////*以此类推*/
/*小数点*/
private void btnPoint_Click(object sender, EventArgs e)
{
inputStr.Add('.'); //在链表中追加记录
txtReadout.AppendText("."); //在框中显示
}
/*左括号*/
private void btnLeft_Click(object sender, EventArgs e)
{
inputStr.Add('(');
txtReadout.AppendText("(");
}
/*右括号*////*加减乘除类似此处省略*/
private void btnRight_Click(object sender, EventArgs e)
{
inputStr.Add(')');
txtReadout.AppendText(")");
}
/*按钮[C]--所有值清零*/
private void btnClear_Click(object sender, EventArgs e)
{
txtReadout.Text = "";
inputStr.Clear(); //清空链表的所有元素
}
/*删除、退格*/
private void btnDele_Click(object sender, EventArgs e)
{
inputStr.RemoveAt(inputStr.Count - 1); //点一次移除一格
txtReadout.Text = "";
for (int i = 0; i < inputStr.Count; i++)
{
txtReadout.Text += inputStr[i];
}
}
/*等号*/
private void btnEqual_Click(object sender, EventArgs e)
{
txtReadout.AppendText("=");
CText.Text = txtReadout.Text;
txtReadout.Text = DataOp.DataMain();
string temp = DataOp.DataMain(); //读取栈中算式
inputStr.Clear(); //清除链表存算式
for (int i = 0; i < temp.Length; i++)
{
inputStr.Add(temp[i]);
}
}
/*输入栏*/
private void Tab(object sender, EventArgs e)
{
ScienceCa science = new ScienceCa();
this.Hide();
science.ShowDialog();
Application.ExitThread();
}
class DataOp : Form1
{
static Stack<double> number = new Stack<double>(); //创建栈存数字
static Stack<char> symbol = new Stack<char>(); //创建栈存符号
/*从inputStr中读值*/
public static void Read()
{
for (int i = 0; i < inputStr.Count; i++)
{
if (!IsOperator(inputStr[i])) //判断是否是操作符,取出算式中的数字入栈
{
string symbol = null;
while (i < inputStr.Count && !IsOperator(inputStr[i]))
{
symbol += inputStr[i];
i++;
}
i--;
double num2 = Convert.ToDouble(symbol); //将string转化为double
number.Push(num2); //入栈
}
else if (IsOper(inputStr[i])) //判断是否是运算符+ - * /
{
if (symbol.Count.Equals(0) || symbol.Peek().Equals('('))
{
symbol.Push(inputStr[i]);
}
else if (OperatorPrecedence(inputStr[i]) > OperatorPrecedence(symbol.Peek()))
{
symbol.Push(inputStr[i]);
}
else
{
double num1, num2;
char sym1;
num2 = number.Pop();
num1 = number.Pop();
sym1 = symbol.Pop();
double sum = Operat(num1, num2, sym1);
number.Push(sum);
symbol.Push(inputStr[i]);
}
}
else //(和)
{
if (inputStr[i].Equals('('))
{
symbol.Push(inputStr[i]);
}
else if (inputStr[i].Equals(')'))
{
while (!symbol.Peek().Equals('('))
{
double num1, num2;
char sym1;
num2 = number.Pop();
num1 = number.Pop();
sym1 = symbol.Pop();
double sum = Operat(num1, num2, sym1);
number.Push(sum);
}
symbol.Pop();
}
}
}
}
public static double PopStack() //栈顶
{
double result = 0;
double num1, num2;
char sym1;
while (symbol.Count != 0)
{
num2 = number.Pop();
num1 = number.Pop();
sym1 = symbol.Pop();
result = Operat(num1, num2, sym1);
number.Push(result);
}
return result;
}
/*未完*/
下面是判断是否是操作符和运算符
public static bool IsOperator(char c) //是否是操作符
{
if (c.Equals('+') || c.Equals('-') || c.Equals('*') || c.Equals('/') || c.Equals('(') || c.Equals(')') || c.Equals('²'))
return true;
return false;
}
public static bool IsOper(char c) //是否是运算符
{
if (c.Equals('+') || c.Equals('-') || c.Equals('*') || c.Equals('/') || c.Equals('²'))
return true;
return false;
}
public static int OperatorPrecedence(char a) //操作符优先级
{
int i = 0;
switch (a)
{
case '+': i = 3; break;
case '-': i = 3; break;
case '*': i = 4; break;
case '/': i = 4; break;
}
return i;
}
public static double Operat(double n1, double n2, char s1)
{
double result = 0;
switch (s1)
{
case '+': result = n1 + n2; break;
case '-': result = n1 - n2; break;
case '*': result = n1 * n2; break;
case '/': result = n1 / n2; break;
}
return result;
}
public static string DataMain()
{
Read();
return PopStack().ToString();
}
}
}
}
其中读取算式的是txtReadOut,显示输出结果的是CText,如果不是想提高难度的话,可以把括号去了,因为有括号就涉及到运算符运算顺序,这些写法我也是参照许多大佬的代码改的
侵删
科学计算器:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading.Tasks;
这两个计算器可以用TabControl放在一个Form里,但是我觉得那样按钮命名比较麻烦,所以我打算用button切换,两个代码要放到一个命名空间:
public partial class ScienceCa : Form
{
double num1, num2,num3, result; // 操作数及运算结果
bool decimalFlag = false; // 判断输入的是否为小数
string myOperator; // 操作类型
public ScienceCa()
{
InitializeComponent();
}
public void NumClick(int inNumber)
{
if (decimalFlag) //如果输入的是小数,直接增加到文本框中
{
txtReadout.Text = txtReadout.Text + inNumber;
}
else
{
if (txtReadout.Text == "0.") // 输入整数
{
// 输入的是整数的第一个数字
txtReadout.Text = inNumber + ".";
}
else
{
// 否则从第0个字符开始,截取输入的不是整数的第一个数字
txtReadout.Text = txtReadout.Text.Substring(0, txtReadout.Text.Length - 1) + inNumber + ".";
}
}
}
/*控制显示界面*/
private void Form1_load(object sender, EventArgs e)
{
txtReadout.Text = "0.";
txtReadout.TextAlign = HorizontalAlignment.Right;
}
/*按键数字0-9*/
private void btnNum1_Click(object sender, EventArgs e)
{
NumClick(1);
}
......
相同的按钮都是复制form1的按钮
/*[C]所有值清零*/
private void btnClear_Click(object sender, EventArgs e)
{
num1 = 0;
num2 = 0;
txtReadout.Text = "0.";
decimalFlag = false; //[C]所有数值回归
}
/*[CE]当前输入值清零*/
private void btnClearEntry_Click(object sender, EventArgs e)
{
result = 0; //上一次计算结果清除
num2 = 0; //本次输入数字清除
txtReadout.Text = "0.";
}
/*删除、退格*/
private void btnDele_Click(object sender, EventArgs e)
{
string str=txtReadout.Text;
if (str.Length > 0) //长度>0,点一次截取一次
{
str = str.Substring(0, str.Length - 1);
if (str.Length == 0)
{
txtReadout.Text = "0.";
}
else
txtReadout.Text = str;
}
}
/*加法*/
private void btnAdd_Click(object sender, EventArgs e)
{
myOperator = "Add";
num1 = double.Parse(txtReadout.Text); //将字符串转换为double类型
txtReadout.Text = "0.";
decimalFlag = false;
}
/*减法*/
private void btnSub_Click(object sender, EventArgs e)
{
myOperator = "Sub";
num1 = double.Parse(txtReadout.Text);
txtReadout.Text = "0.";
decimalFlag = false;
}
/*乘法*/
private void btnMulti_Click(object sender, EventArgs e)
{
myOperator = "Multi";
num1 = double.Parse(txtReadout.Text);
txtReadout.Text = "0.";
decimalFlag = false;
}
/*除法*/
private void btnDivi_Click(object sender, EventArgs e)
{
myOperator = "Divi";
num1 = double.Parse(txtReadout.Text);
txtReadout.Text = "0.";
decimalFlag = false;
}
/*求相反数*/
private void btnOpp_Click(object sender, EventArgs e)
{
myOperator = "Opp";
num1 = double.Parse(txtReadout.Text);
num3 = -num1;
txtReadout.Text = num3.ToString();
decimalFlag = false;
}
/*求二次根式*/
private void btnSqrt_Click(object sender, EventArgs e)
{
myOperator = "Sqrt";
num3 = double.Parse(txtReadout.Text);
if (num3 < 0) //负数不能开平方根
txtReadout.Text = "负数不可开平方根!错误!";
num3 = Math.Sqrt(num3);
txtReadout.Text = num3.ToString();
}
/*求倒数*/
private void btnOneDivi_Click(object sender, EventArgs e)
{
num3 = double.Parse(txtReadout.Text);
if (num3 == 0.0)
txtReadout.Text = "零不可做分母!错误!";
num3 = 1 / num3;
txtReadout.Text = num3.ToString();
}
/*百分号*/
private void btnPercent_Click(object sender, EventArgs e)
{
num3 = double.Parse(txtReadout.Text);
num3 = num3 / 100;
txtReadout.Text = num3.ToString();
}
/*Abs--计算显示数字的绝对值。*/
private void btn_Abs_Click(object sender, EventArgs e)
{
num3 = double.Parse(txtReadout.Text);
if (num3 < 0)
{
num3 = -num3;
}
txtReadout.Text = num3.ToString();
}
/*ln--求以e为底的对数值*/
private void btn_Ln_Click(object sender, EventArgs e)
{
num3 = double.Parse(txtReadout.Text);
if (num3 > 0)
{
num3 = Math.Log(num3);
txtReadout.Text = num3.ToString();
}
else
txtReadout.Text = "真数必须>0";
decimalFlag = false;
}
/*Mod--求余运算*/
private void btn_Mod_Click(object sender, EventArgs e)
{
myOperator = "Mod";
num1 = double.Parse(txtReadout.Text);
txtReadout.Text = "0.";
decimalFlag = false;
}
/*lg--求以10为底的对数值*/
private void btn_Lg_Click(object sender, EventArgs e)
{
num3 = double.Parse(txtReadout.Text);
if (num3 > 0)
{
num3 = Math.Log10(num3);
txtReadout.Text = num3.ToString();
}
else
txtReadout.Text = "真数必须>0";
}
/*sin--求显示数字的正弦值*/
private void btn_sin_Click(object sender, EventArgs e)
{
num3 = double.Parse(txtReadout.Text);
num3 = Math.PI * (num3 / 180);
num3 = Math.Sin(num3);
if (num3 < Math.Pow(10, -10))
txtReadout.Text = "0";
else txtReadout.Text = num3.ToString();
}
/*计算阶乘n!*/
private void btn_factori_Click(object sender, EventArgs e)
{
double number = double.Parse(txtReadout.Text);
num3=1;
for (int i = 1; i <= number; i++)
{
num3= i * num3;
}
txtReadout.Text = num3.ToString();
decimalFlag = false;
}
/*sin--求显示数字的余弦值*/
private void btn_cos_Click(object sender, EventArgs e)
{
num3 = double.Parse(txtReadout.Text);
num3 = Math.PI * (num3 / 180);
num3 = Math.Cos(num3);
if (num3 < Math.Pow(10, -10))
txtReadout.Text = "0";
else txtReadout.Text = num3.ToString();
}
/*计算以10为底的指数*/
private void btn_tenPower_Click(object sender, EventArgs e)
{
num3 = double.Parse(txtReadout.Text);
num3 = Math.Pow(10, num3);
txtReadout.Text = num3.ToString();
decimalFlag = false;
}
/*tan--求显示数字的正切值*/
private void btn_tan_Click(object sender, EventArgs e)
{
num3 = double.Parse(txtReadout.Text);
if ((num3+90) % 90 == 0) //tan函数的定义域不等于kπ+π/2
{
txtReadout.Text = "该值不在正切函数定义域内!";
}
else
{
num3 = Math.PI * (num3/180);
result = Math.Tan(num3);
if (num3 < Math.Pow(10, -10))
txtReadout.Text = "0";
else txtReadout.Text = result.ToString();
}
}
/*计算以e为底的指数 */
private void btn_ePower_Click(object sender, EventArgs e)
{
num3 = double.Parse(txtReadout.Text);
num3 = Math.Exp(num3);
txtReadout.Text = num3.ToString();
}
/*求显示数字的平方*/
private void btn_square_Click(object sender, EventArgs e)
{
num3 = double.Parse(txtReadout.Text);
num3 = num3 * num3 ;
txtReadout.Text = num3.ToString();
}
/*求显示数字的立方*/
private void btn_cube_Click(object sender, EventArgs e)
{
num3 = double.Parse(txtReadout.Text);
num3 = num3 * num3 * num3;
txtReadout.Text = num3.ToString();
}
/*求幂值(非单目运算)*/
private void btn_xn_power_Click(object sender, EventArgs e)
{
myOperator = "xn_power";
num1 = double.Parse(txtReadout.Text); //将字符串转换为double类型
txtReadout.Text = "0.";
}
/*求立方根*/
private void btn_cubeRoot_Click(object sender, EventArgs e)
{
num3 = double.Parse(txtReadout.Text);
double number = 3;
num3 = Math.Pow(num3,1/number);
txtReadout.Text = num3.ToString();
}
/*开n次方根(非单目运算)*/
private void btn_nRoot_Click(object sender, EventArgs e)
{
myOperator = "nRoot";
num1 = double.Parse(txtReadout.Text);
txtReadout.Text = "0.";
}
/*等号*/
private void btnEqual_Click(object sender, EventArgs e)
{
num2 = double.Parse(txtReadout.Text);
switch (myOperator)
{
case "Add":
result = num1 + num2;
txtReadout.Text = result.ToString();
break;
case "Sub":
result = num1 - num2;
txtReadout.Text = result.ToString();
break;
case "Multi":
result = num1 * num2;
txtReadout.Text = result.ToString();
break;
case "Divi":
if (num2 == 0)
{
txtReadout.Text = "除数不能为0";
}
else
{
result = num1 / num2;
txtReadout.Text = result.ToString();
}
break;
case "nRoot":
if (num2 == 0)
{
txtReadout.Text = "除数不能为0";
}
else
{
result = Math.Pow(num1, 1 / num2);
txtReadout.Text = result.ToString();
}
break;
case "xn_power":
result = Math.Pow(num1, num2);
txtReadout.Text = result.ToString();
break;
case "Mod":
if (num2 == 0)
{
txtReadout.Text = "除数不能为0";
}
result = num1 % num2;
txtReadout.Text = result.ToString();
break;
}
}
/*切换至标准计算器*/
private void btnChange_biao_Click(object sender, EventArgs e)
{
Form1 form = new Form1();
this.Hide();
form.ShowDialog();
Application.ExitThread();
}
}
以上就是所有的代码,关于窗口的具体设置可以自己摸索。
代码来源于老师+CDSN+自己摸索
源码链接:
https://download.csdn.net/download/Ziaaaaa/12111842
欢迎大家和我一起交流学习!