科学计算器(源代码,c#)

</pre><pre name="code" class="csharp">今天写了一个科学计算器,实现了最基本的加减乘除乘方和一些函数,以线性格式输入。没写注释,仅供参考。
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 Calculator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            textBox1.Focus();
        }

        public void ActualMain()
        {
            try
            {
                string ep = textBox1.Lines[textBox1.Lines.Length-1];
                filter(ref ep);
                if (numOfCharInStr('(', ep) != numOfCharInStr(')', ep))
                    throw new Exception();
                while (hasChar(ep, '('))
                {
                    int L, R, j, LL;
                    string strfunc = "";
                    R = ep.IndexOf(')');
                    j = R - 1;
                    while (ep[j] != '(')
                    { j--; }
                    L = j;
                    j--;
                    while (j >= 0 && j < ep.Length && ep[j] >= 97 && ep[j] <= 122)
                    {
                        strfunc += ep[j];
                        j--;
                    }
                    LL = j + 1;
                    reverseStr(ref strfunc);
                    if (strfunc == "")
                        ep = ep.Replace(ep.Substring(L, R - L + 1), jjcc(ep.Substring(L + 1, R - L - 1)));
                    else
                    {
                        string result = function(strfunc, Convert.ToDouble(jjcc(ep.Substring(L + 1, R - L - 1))));
                        ep = ep.Replace(ep.Substring(LL, R - LL + 1), result);
                    }
                }
                textBox1.AppendText("\n= " + jjcc(ep)+"\n");
                textBox1.Focus();
                textBox1.Select(textBox1.TextLength, 0);
                textBox1.ScrollToCaret();
            }
            catch
            {
                MessageBox.Show("发生错误\n");
            }
        }


        ///转半角的函数(DBC case) <summary> 
        ///转半角的函数(DBC case)
        /// </summary>  
        /// <param name="input">任意字符串</param>  
        /// <returns>半角字符串</returns>  
        ///<remarks>  
        ///全角空格为12288,半角空格为32  
        ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248  
        ///</remarks>  
        public static string ToDBC(string input)
        {
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 12288) { c[i] = (char)32; continue; }//全角空格
                if (c[i] > 65280 && c[i] < 65375) { c[i] = (char)(c[i] - 65248); } //其他全角字符
            }
            return new string(c);
        }

        public static void filter(ref string eq)
        {
            eq = ToDBC(eq);
            string process = "";
            char[] terms = eq.ToCharArray();
            for (int i = 0; i < eq.Length; i++)
            {
                int c = terms[i];
                if ((c >= 97 && c <= 122 || (c >= 48 && c <= 57)) || c == '+' || c == '-' || c == '*' || c == '/' || c == '^'
                    || c == '(' || c == ')' || c == '.'||c=='%'||c=='!')
                {
                        process += terms[i];
                }
            }
            eq = process;
        }

        public static void reverseStr(ref string str)
        {
            char[] chs = str.ToCharArray();
            Array.Reverse(chs);
            str = new string(chs);
        }

        public static string function(string str,double num)
        {
            double output=0;
            if (str == "abs")
                output = Math.Abs(num);
            else if (str == "arccos")
                output = Math.Acos(num);
            else if (str == "arcsin")
                output = Math.Asin(num);
            else if (str == "arctan")
                output = Math.Atan(num);
            else if (str == "cos")
                output = Math.Cos(num);
            else if (str == "cosh")
                output = Math.Cosh(num);
            else if (str == "exp")
                output = Math.Exp(num);
            else if (str == "floor"
  • 2
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值