资源简介

改进版C#计算器,已修复部分Bug,异常处理。可以进行复杂表达式的计算,多重括号等等

资源截图

代码片段和文件信息

// -----------------------------------------------------------------------
// 
// TODO: Update copyright text.
// 
// -----------------------------------------------------------------------
using System.Collections;
namespace WinFormSamples
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    /// 
    /// TODO: Update summary.
    /// 

    public class Calculate
    {
        public string GetResult(string str)
        {
            str = str + “+“;
            char[] expArry =  str.ToCharArray();
            string operations=“()+-*/“;
            Hashtable signs =new Hashtable();
            string[] numArry = str.Split(‘(‘‘)‘‘+‘‘-‘‘*‘‘/‘); //分割字符串,提取表达式中的数字
            //定义操作符的优先级并存入哈希表
            signs.Add(“(“0);
            signs.Add(“)“0);
            signs.Add(“+“1);
            signs.Add(“-“1);
            signs.Add(“*“2);
            signs.Add(“/“2);            
                string[] resultArry = new string[99];
                int resultIndex = 0;
                int numIndex = 0;
                for (int i = 0; i < str.Length - 1; i++)
                {
                    if (operations.Contains(expArry[i]))
                    {
                        //遇到符号直接存入resultArry
                        resultArry[resultIndex] = expArry[i].ToString();
                        resultIndex++;
                    }
                    else if (operations.Contains(expArry[i + 1]))
                    {
                        //连续非符号字符,从numArry取出一个元素放入resultArry
                        while (numArry[numIndex] == ““)
                        //去除numArry中的空元素
                        {
                            numIndex++;
                        }
                        resultArry[resultIndex] = numArry[numIndex];
                        numIndex++;
                        resultIndex++;
                    }
                }
                Stack sign_stack = new Stack();//定义符号栈
                Stack num_stack = new Stack();//定义操作数栈
                char calc_sign;
                try
                {
                for (int i = 0; i < resultIndex + 1; i++)
                {
                    if (i == resultIndex)
                    {
                        if (sign_stack.Count != 0)
                        {
                            calc_sign = sign_stack.Pop();
                            Calc(ref sign_stack ref num_stackref resultArry[i]ref calc_sign);
                            while (num_stack.Count() > 1)
                            //计算到栈中只剩一个操作数为止
                            {
                                calc_sign = sign_stack.Pop();
                                Calc(ref sign_stack ref num_stack ref resultArry[i] ref calc_sign);
          

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2014-10-27 11:12  WinFormSamples\
     目录           0  2014-10-14 10:47  WinFormSamples\bin\
     目录           0  2014-10-14 10:59  WinFormSamples\bin\Debug\
     文件      894464  2014-10-27 14:32  WinFormSamples\bin\Debug\WinFormSamples.exe
     文件       40448  2014-10-27 14:32  WinFormSamples\bin\Debug\WinFormSamples.pdb
     文件       11600  2014-11-10 16:48  WinFormSamples\bin\Debug\WinFormSamples.vshost.exe
     文件         490  2013-03-19 06:00  WinFormSamples\bin\Debug\WinFormSamples.vshost.exe.manifest
     文件        6640  2014-10-27 11:28  WinFormSamples\Calculate.cs
     文件        3301  2014-10-27 14:32  WinFormSamples\Form1.cs
     文件       17040  2014-10-27 14:32  WinFormSamples\Form1.Designer.cs
     文件        5817  2014-10-27 14:32  WinFormSamples\Form1.resx
     目录           0  2014-10-14 10:47  WinFormSamples\obj\
     目录           0  2014-10-14 10:47  WinFormSamples\obj\x86\
     目录           0  2014-10-27 14:32  WinFormSamples\obj\x86\Debug\
     文件        5420  2014-10-23 09:52  WinFormSamples\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
     文件        6715  2014-10-27 14:32  WinFormSamples\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     目录           0  2014-10-27 14:32  WinFormSamples\obj\x86\Debug\TempPE\
     文件        5120  2014-10-27 14:32  WinFormSamples\obj\x86\Debug\TempPE\Properties.Resources.Designer.cs.dll
     文件         914  2014-11-10 16:48  WinFormSamples\obj\x86\Debug\WinFormSamples.csproj.FileListAbsolute.txt
     文件        1130  2014-10-27 14:32  WinFormSamples\obj\x86\Debug\WinFormSamples.csproj.GenerateResource.Cache
     文件        9174  2014-10-23 09:55  WinFormSamples\obj\x86\Debug\WinFormSamples.csprojResolveAssemblyReference.cache
     文件      894464  2014-10-27 14:32  WinFormSamples\obj\x86\Debug\WinFormSamples.exe
     文件         180  2014-10-27 14:32  WinFormSamples\obj\x86\Debug\WinFormSamples.Form1.resources
     文件       40448  2014-10-27 14:32  WinFormSamples\obj\x86\Debug\WinFormSamples.pdb
     文件      876850  2014-10-27 14:32  WinFormSamples\obj\x86\Debug\WinFormSamples.Properties.Resources.resources
     文件         506  2014-10-14 10:47  WinFormSamples\Program.cs
     目录           0  2014-10-14 10:47  WinFormSamples\Properties\
     文件        1440  2014-10-14 10:47  WinFormSamples\Properties\AssemblyInfo.cs
     文件        4048  2014-10-27 14:32  WinFormSamples\Properties\Resources.Designer.cs
     文件        6753  2014-10-27 14:32  WinFormSamples\Properties\Resources.resx
     文件        1101  2014-10-14 10:47  WinFormSamples\Properties\Settings.Designer.cs
............此处省略8个文件信息

评论

共有 条评论