用c#写一个简易ios计算器源代码。不要小数点的那种,求源代码,谢谢各位大神!谢谢!

用C#编个计算器,那个0键和小数点的代码要怎么写?
土豆系列0107
直接写啊?有什么困难
为您推荐:
其他类似问题
扫描下载二维码C#编写简易计算器(附源代码)超详细_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
C#编写简易计算器(附源代码)超详细
上传于||文档简介
&&C​#​编​写​简​易​计​算​器​(​附​源​代​码​)​超​详​细​
​
​温​柔​一​刀​d​g​j​编​写
阅读已结束,如果下载本文需要使用2下载券
想免费下载本文?
定制HR最喜欢的简历
下载文档到电脑,查找使用更方便
还剩17页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢用c#做一个计算器,小数点和负号的算法怎么写,最好是不要定义变量的,求代码.
用YACC吧,这东西太简单了c#里面就自己写词法语法分析,简单的文法应付计算器搓搓有余啊
为您推荐:
其他类似问题
扫描下载二维码我用C#编写一个简单的计算器 我想问一下 小数点怎么处理?_百度知道求C#编写计算器的源代码? | 编写,计算器,源代码_生活问答_读美文网
求C#编写计算器的源代码?
做一个类似于Windows附带的计算器的或是直接输入表达式进行计算的。第一种简单,无非就是做一些按钮并执行制定操作而已。 第二种我写了两份,分别使用正则表达式和语法分析技术 使用正则表达式的: using S using System.Collections.G using System.L using System.T using System.Text.RegularE using System.Windows.F using System.D namespace Eval { public class EvalEventArgs : EventArgs { public EvalEventArgs(string Expressions) { this.Expressions = E } public string Expressions { } public string Message { } } public class Function { static Regex Num = new Regex(@&(\-?\d+\.?\d*)&); static Regex Power = new Regex(Num.ToString() + @&\^& + Num.ToString()); static Regex AddSub = new Regex(Num.ToString() + &([+-])& + Num.ToString()); static Regex MulDiv = new Regex(Num.ToString() + &([*/])& + Num.ToString()); 读美文网,dumeiwencom static Regex AndXor = new Regex(Num.ToString() + &(and|xor)& + Num.ToString()); static Regex InnerRegex = new Regex(@&\([^\(\)]+\)&); static Regex FunctionRegex = new Regex(@&(ln|lg|sin|cos|tan|ctg|sh|ch|th|arcsin|arccos|arctan|not)& +Num.ToString()); static Regex LBrackets = new Regex(@&\{|\[&); static Regex RBrackets = new Regex(@&\}|\]&); const double PI = Math.PI; const double e = Math.E; public event EventHandler&EvalEventArgs& E public event EventHandler&EvalEventArgs& EvalC public event EventHandler&EvalEventArgs& E private void OnError(string Expressions, string Message) { if (Error != null) Error(this, new EvalEventArgs(Expressions) { Message = Message }); IsAbort = } private bool IsAbort = /// &summary&计算表达式&/summary& /// &param name=&Expressions&&表达式&/param& 读美文网,dumeiwencom public void Eval(string Expressions) { IsAbort = Expressions = Expressions.ToLower(); Expressions = Expressions.Replace(&pi&, Math.PI.ToString()).Replace(&e&,Math.E.ToString()); Expressions = LBrackets.Replace(Expressions, (Match match) =& { return &(&; }); Expressions = RBrackets.Replace(Expressions, (Match match) =& { return &)&; }); if (Check(Expressions)) { try { do { Expressions = InnerRegex.Replace(Expressions, (Match match) =& { return EvalInner(match.Value.Substring(1, match.Value.Length - 2)); }); if (Evaling != null) Evaling(this, new EvalEventArgs(Expressions)); } while (InnerRegex.IsMatch(Expressions) && !IsAbort); if(!IsAbort) Expressions = EvalInner(Expressions); if (!IsAbort && EvalComplent != null) EvalComplent(this, new EvalEventArgs(Expressions)); } catch(Exception ex) { OnError(Expressions, ex.Message); 读美文 } } } /// &summary& /// 用于计算不含括号的表达式的值 /// &/summary& /// &param name=&Expressions&&要计算的表达式&/param& /// &returns&计算结果&/returns& public string EvalInner(string Expressions) { while (FunctionRegex.IsMatch(Expressions) && !IsAbort) { Expressions = FunctionRegex.Replace(Expressions, (Match match) =& { return EvalSingle(match.Value); }); if (Evaling != null) Evaling(this, new EvalEventArgs(Expressions)); } while (AndXor.IsMatch(Expressions) && !IsAbort) { Expressions = AndXor.Replace(Expressions, (Match match) =& { double P1=double.Parse(match.Groups[1].Value); double P2=double.Parse(match.Groups[3].Value); if((uint)P1!=P1 || (uint)P1!=P1 ) OnError(Expressions,&&); if (match.Value.Contains(&and&)) return ((uint)P1 & (uint)P2).ToString(); else return ((uint)P1 ^ (uint)P2).ToString(); 读美文网,dumeiwencom }); if (Evaling != null) Evaling(this, new EvalEventArgs(Expressions)); } while (Power.IsMatch(Expressions) && !IsAbort) { Expressions = Power.Replace(Expressions, (Match match) =& { return System.Math.Pow(double.Parse(match.Groups[0].Value), double.Parse(match.Groups[2].Value)).ToString(); }); if (Evaling != null) Evaling(this, new EvalEventArgs(Expressions)); } while (MulDiv.IsMatch(Expressions) && !IsAbort) { Expressions = MulDiv.Replace(Expressions, (Match match) =& { if (match.Value.Contains(&*&)) return (double.Parse(match.Groups[1].Value) * double.Parse(match.Groups[3].Value)).ToString(); else return (double.Parse(match.Groups[1].Value) / double.Parse(match.Groups[3].Value)).ToString(); }); if (Evaling != null) Evaling(this, new EvalEventArgs(Expressions)); } while (AddSub.IsMatch(Expressions) && !IsAbort) { Expressions = AddSub.Replace(Expressions, (Match match) =& copyright dumeiwen { if (match.Value.Contains(&+&)) return (double.Parse(match.Groups[1].Value) + double.Parse(match.Groups[3].Value)).ToString(); else return (double.Parse(match.Groups[1].Value) - double.Parse(match.Groups[3].Value)).ToString(); }); if (Evaling != null) Evaling(this, new EvalEventArgs(Expressions)); } return IsAbort ? null : E } /// &summary& /// 用于计算单变量函数表达式的值 /// &/summary& /// &param name=&Expressions&&要计算的表达式&/param& /// &returns&计算结果&/returns& public string EvalSingle(string Expressions) { while (FunctionRegex.IsMatch(Expressions) && !IsAbort) { Expressions = FunctionRegex.Replace(Expressions, (Match match) =& { return EvalSingle(match); }); } return IsAbort ? null : E } private string EvalSingle(Match match) { //ln|lg|sin|cos|tan|ctg|sh|ch|th|arcsin|arccos|arctan 本文来自读美文网 double Param = double.Parse(match.Groups[2].Value.Trim(char.Parse(&(&), char.Parse(&)&))); double Result = double.NaN; if (!double.IsNaN(Param)) { if (match.Value.StartsWith(&ln&)) { if (Param & 0) Result = System.Math.Log(Param); else OnError(match.Value, &表达式中有错误:对数的真数不能小于零!&); } else if (match.Value.StartsWith(&lg&)) { if (Param & 0) Result = System.Math.Log10(Param); else OnError(match.Value, &表达式中有错误:对数的真数不能小于零!&); } else if (match.Value.StartsWith(&sin&)) Result = System.Math.Sin(Param * PI / 180); else if (match.Value.StartsWith(&sqrt&)) Result = System.Math.Sqrt(Param); else if (match.Value.StartsWith(&cos&)) Result = System.Math.Cos(Param * PI / 180); else if (match.Value.StartsWith(&tan&)) Result = System.Math.Tan(Param * PI / 180); copyright dumeiwen else if (match.Value.StartsWith(&ctg&)) Result = 1 / System.Math.Tan(Param * PI / 180); else if (match.Value.StartsWith(&sh&)) Result = System.Math.Sinh(Param); else if (match.Value.StartsWith(&ch&)) Result = System.Math.Cosh(Param); else if (match.Value.StartsWith(&th&)) Result = System.Math.Tanh(Param); else if (match.Value.StartsWith(&arcsin&)) Result = System.Math.Asin(Param); else if (match.Value.StartsWith(&arccos&)) Result = System.Math.Acos(Param); else if (match.Value.StartsWith(&arctan&)) Result = System.Math.Atan(Param); else if (match.Value.StartsWith(&not&)) { if (((uint)Param) != Param) OnError(match.Value, &逻辑运算只能针对整数!&); else Result = ~(uint)P } else OnError(match.Value,&表达式含有不被支持的函数!&); } else OnError(match.Value, &表达式中有错误!&); copyright dumeiwen if (double.IsInfinity(Result)) OnError(match.Value, &结果超出了范围!&); return Result == double.NaN ? null : Result.ToString(); } /// &summary& /// 对表达式进行检查,确保其格式符合要求 /// &/summary& /// &param name=&Expressions&&表达式&/param& /// &returns&是否符合要求&/returns& public bool Check(string Expressions) { if (!CheckBrackets(Expressions)) { OnError(Expressions, &括号不匹配!&);
} /// &summary& /// 检查括号是否匹配 /// &/summary& /// &param name=&Expressions&&表达式&/param& /// &returns&是否匹配&/returns& private bool CheckBrackets(string Expressions) { int Num = 0; foreach (char c in Expressions) { if (c == char.Parse(&(&)) { Num++; } if (c == char.Parse(&)&)) { Num--; } } 读美文网,dumeiwencom if (Num != 0) { } else { } } } } Evaling事件可以获取中间结果 EvalComplent事件表明计算完成 Error事件表明发生错误 调用方法: Function f = new Function(); f.Eval(要计算的表达式) 语法分析的比较长,想要的话留下邮箱我发过去
相关推荐:
求C#编写计算器的源代码?

我要回帖

更多关于 安卓简易计算器源代码 的文章

 

随机推荐