首页 > 编程语言 >c#四则运算

c#四则运算

时间:2022-12-04 21:44:36浏览次数:39  
标签:exception goto c# 四则运算 else ++ Length expression

来源:https://blog.csdn.net/slwsss/article/details/70432277

 

namespace ConsoleApp12
{
    internal class Program
    {
        static void Main(string[] args)
        {

            var str = "1+2+3+4+5*0";
            Console.WriteLine(Compute(str));
            Console.ReadLine();
        }

        static decimal Compute(string expression)
        {
            int i = 0;
            var r = _Compute(expression, ref i);
            if (i != expression.Length) throw new ArgumentException("╮(︶︿︶)╭");
            return r;
        }
        static decimal _Compute(string expression, ref int i)
        {
            int j;
            char c;
            decimal? r = null;
            decimal t;
            int f = 1, f3;
            bool f2;
        g1:
            while (i < expression.Length)
            {
                c = expression[i];
                if (c >= '0' && c <= '9')
                {
                    for (j = i + 1; j < expression.Length; j++)
                    {
                        c = expression[j];
                        if ((c < '0' || c > '9') && c != '.') break;
                    }
                    t = decimal.Parse(expression.Substring(i, j - i));
                    i = j;
                    goto g2;
                }
                else if (c == '+' || c == '-')
                {
                    f3 = c == '+' ? 1 : -1;
                    i++;
                    while (i < expression.Length)
                    {
                        c = expression[i];
                        if ((c >= '0' && c <= '9') || c == '.')
                        {
                            for (j = i + 1; j < expression.Length; j++)
                            {
                                c = expression[j];
                                if ((c < '0' || c > '9') && c != '.') break;
                            }
                            t = f3 * decimal.Parse(expression.Substring(i, j - i));
                            i = j;
                            goto g2;
                        }
                        else if (c == '(')
                        {
                            i++;
                            if (i >= expression.Length) goto exception;
                            t = _Compute(expression, ref i) * f3;
                            if (i >= expression.Length) goto exception;
                            i++;
                            goto g2;
                        }
                        else if (char.IsWhiteSpace(c))
                        {
                            i++;
                            continue;
                        }
                        else goto exception;
                    }
                    goto exception;
                }
                else if (c == '(')
                {
                    i++;
                    if (i >= expression.Length) goto exception;
                    t = _Compute(expression, ref i);
                    if (i >= expression.Length) goto exception;
                    i++;
                    goto g2;
                }
                else if (char.IsWhiteSpace(c))
                {
                    i++;
                    continue;
                }
                else goto exception;
            }
        exception:
            throw new ArgumentException("╮(︶︿︶)╭");
        g2:
            while (i < expression.Length)
            {
                c = expression[i];
                if (c == '+' || c == '-')
                {
                    r = r.HasValue ? (r.Value + f * t) : (f * t);
                    f = c == '+' ? 1 : -1;
                    i++;
                    goto g1;
                }
                else if (c == '*' || c == '/')
                {
                    f2 = c == '*';
                    i++;
                    while (i < expression.Length)
                    {
                        c = expression[i];
                        if (c >= '0' && c <= '9')
                        {
                            for (j = i + 1; j < expression.Length; j++)
                            {
                                c = expression[j];
                                if ((c < '0' || c > '9') && c != '.') break;
                            }
                            if (f2) t *= decimal.Parse(expression.Substring(i, j - i));
                            else t /= decimal.Parse(expression.Substring(i, j - i));
                            i = j;
                            goto g2;
                        }
                        else if (c == '+' || c == '-')
                        {
                            f3 = c == '+' ? 1 : -1;
                            i++;
                            while (i < expression.Length)
                            {
                                c = expression[i];
                                if ((c >= '0' && c <= '9') || c == '.')
                                {
                                    for (j = i + 1; j < expression.Length; j++)
                                    {
                                        c = expression[j];
                                        if ((c < '0' || c > '9') && c != '.') break;
                                    }
                                    if (f2) t *= (f3 * decimal.Parse(expression.Substring(i, j - i)));
                                    else t /= (f3 * decimal.Parse(expression.Substring(i, j - i)));
                                    i = j;
                                    goto g2;
                                }
                                else if (c == '(')
                                {
                                    i++;
                                    if (i >= expression.Length) goto exception;
                                    if (f2) t *= (_Compute(expression, ref i) * f3);
                                    else t /= (_Compute(expression, ref i) * f3);
                                    if (i >= expression.Length) goto exception;
                                    i++;
                                    goto g2;
                                }
                                else if (char.IsWhiteSpace(c))
                                {
                                    i++;
                                    continue;
                                }
                                else goto exception;
                            }
                            goto exception;
                        }
                        else if (c == '(')
                        {
                            i++;
                            if (i >= expression.Length) goto exception;
                            if (f2) t *= _Compute(expression, ref i);
                            else t /= _Compute(expression, ref i);
                            if (i >= expression.Length) goto exception;
                            i++;
                            goto g2;
                        }
                        else if (char.IsWhiteSpace(c))
                        {
                            i++;
                            continue;
                        }
                        goto exception;
                    }
                    goto exception;
                }
                else if (c == ')') goto g2_end;
                else if (char.IsWhiteSpace(c))
                {
                    i++;
                    continue;
                }
                goto exception;
            }
        g2_end:
            if (r.HasValue) return r.Value + f * t;
            return f * t;
        }
    }
}

  

标签:exception,goto,c#,四则运算,else,++,Length,expression
From: https://www.cnblogs.com/chinasoft/p/16950900.html

相关文章

  • 暴力升级你的 ST-Link 及 STM32CubeIDE
    暴力升级你的ST-Link及STM32CubeIDE背景一些ST-Link在使用最新的IDE时,经常提示需要升级其固件,但是升级始终失败,提示容量不足。在KeilMDK上可能就提示一下升级......
  • AtCoder Beginner Contest 280
    A-PawnonaGrid(abc280a)题目大意给定一个矩形格子,问#的数量。解题思路直接统计即可。神奇的代码#include<bits/stdc++.h>usingnamespacestd;usingLL=......
  • 2022 年超详细过程步骤讲解 CentOS 7 安装jdk1.8
    linux系统下安装jdk以及环境变量的设置、真的是比window下方便一万倍1、卸载系统自带jdk1.1查看系统自带jdkjava-version1.2查看java相关文件rpm-qa|grepja......
  • 2022年超详细在CentOS 7上安装Nginx方法(源码安装)
    1、下载http://nginx.org/download/nginx-1.13.0.tar.gz2、上传到虚拟机中3、解压tar-zxvfnginx-1.13.0.tar.gz4、删除安装包rm-rfnginx-1.13.0.tar.gz5......
  • Centos7.6安装vim配置
    简介帮助对vim配置方法不熟悉的新手封装的一键式vim环境安装包.主要针对终端vim用户,适合远程ssh连接Linux服务器进行开发的场景(例如使用阿里云服务器或者腾讯云服务器......
  • Centos7.6更新git
    本文讲述如何升级centos系统的git版本。高版本git增加了一些好用的功能,比如"gitpull支持指定项目目录"等。本文以centos6/7为例讲解。升级centos6/7的git版本......
  • Centos7.x安装Python3(优化方法)
    安装相应的编译工具建议在root下操作,会方便很多,一定要安装,否则编译安装会报错。yum-ygroupinstall"Developmenttools"yum-yinstallzlib-develbzip2-developens......
  • Centos7.x将Python2升级到Python3
    查看Python版本python-V更新yum源yumupdate安装依赖yuminstallyum-utilsyum-builddeppython3下载pythonwgethttps://www.python.org/ftp/python/3.8.5/Py......
  • Centos7更新yum源
    查看当前的yum源yumrepolist对CentOS-Base.repo进行备份mv/etc/yum.repos.d/CentOS-Base.repo/etc/yum.repos.d/CentOS-Base.repo.backup使用阿里云源替换本地......
  • Centos7升级gcc
    不建议贸然升级,时间是比较久的并且容易出现一系列问题。升级GCC版本到11.1GCC11终于发正式版了,4月底官方终于发了11.1正式版.对于我们来说,项目实际使用基于C++17的......