c#四则运算
来源: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;
}
}
}
c#四则运算的更多相关文章
- 介绍一款原创的四则运算算式生成器:CalculateIt2
家里小朋友读一年级了,最近每天都有一些10以内的加减法口算练习,作为程序员爸爸,自然也是想办法能够偷懒,让电脑出题,给小朋友做些练习.于是,自己在业余时间开发了一个四则运算算式生成器,名为:Calcu ...
- 作业二:个人编程项目——编写一个能自动生成小学四则运算题目的程序
1. 编写一个能自动生成小学四则运算题目的程序.(10分) 基本要求: 除了整数以外,还能支持真分数的四则运算. 对实现的功能进行描述,并且对实现结果要求截图. 本题发一篇随笔,内容包括: 题 ...
- 四则运算appNABCD模型
团队: 郭志豪:http://www.cnblogs.com/gzh13692021053/ 杨子健:http://www.cnblogs.com/yzj666/ 刘森松:http://www.cnb ...
- 第一章-第一题(小学生四则运算)--By郭青云
1.项目需求 a) 除了整数以外,还要支持真分数的四则运算. (例如: 1/6 + 1/8 = 7/24) b) 让程序能接受用户输入答案,并判定对错. 最后给出总共 对/错 的数量. c) 逐步扩 ...
- 一个简易的四则运算单元...(15.12.15 BUG更新)
网上找的, 没有作者信息, 只能在这里感谢一下了, 支持标准写法的四则运算 --2015-12-15 修改了一个内存泄漏的BUG - Pop方法没有释放申请的内存 unit Base.Calculat ...
- 利用ANTLR4实现一个简单的四则运算计算器
利用ANTLR4实现一个简单的四则运算计算器 ANTLR4介绍 ANTLR能够自动地帮助你完成词法分析和语法分析的工作, 免去了手写去写词法分析器和语法分析器的麻烦 它是基于LL(k)的, 以递归下降 ...
- 【实践】js实现简易的四则运算计算器
最近看了一个大神推荐的某公司面试程序员的js 面试题,题目是用js 做一个计算器于是跟着大神的思想自己做了一下 ps:功能还没有完善好毕竟自己还是一只菜鸟还在不断学习中. 闲话不多说先上css代码 & ...
- HDU 5938 Four Operations(四则运算)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- C语言实现四则运算
学生:宋丹丹 张潇裕 #include<iostream>#include<ctime>using namespace std;void main(){ int x1,x2,a ...
- 第五篇——C++实现四则运算
写一个能自动生成小学四则运算题目的命令行 “软件”, 分别满足下面的各种需求.下面这些需求都可以用命令行参数的形式来指定: a) 除了整数以外,还要支持真分数的四则运算. (例如: 1/6 + 1/8 ...
随机推荐
- 袋鼠云数栈DTinsight与10家信创厂家完成产品兼容互认证,携手共建信创生态圈
信创产业是国家数据安全.网络安全的基础,也是"新基建"的重要内容,它将成为拉动经济发展的重要抓手之一.随着国际竞争形势发生新的变化,力争掌握核心科技的"自主可控" ...
- 数栈技术分享:一文带你了解Flink jm、tm启动过程和资源分配
一.JM启动过程 1.从日志角度分析启动流程 1)client生成jobGraph 详情请参考:https://www.bilibili.com/video/BV13K4y1P7ri 2)Yarn R ...
- 【Playwright + Python】系列(十七)揭秘 Playwright 处理 Handles:开启高效自动化之门
哈喽,大家好,我是六哥!今天来跟大家聊一聊Playwright 处理 Handles的方法,面向对象为功能测试及零基础小白,这里我尽量用大白话的方式举例讲解,力求所有人都能看懂,建议大家先收藏,以免后 ...
- ASP.NET 5 with Dapr 初体验
分布式应用运行时Dapr目前已经发布了1.1.0版本,阿里云也在积极地为Dapr贡献代码和落地实践.作为一名开发者,自然也想玩一玩,看看Dapr带来的新"视"界到底是怎么样的. 1 ...
- c#基础(视频)
可空类型 类型后面+?就可以声明可空类型了,可空类型和普通的类型是两种类型,赋值时需要进行类型转换 例如 double? num=null: 面向对象 三大特征 封装 打包,便于管理 继承 拿来主义, ...
- INNER JOIN, LEFT JOIN, RIGHT JOIN 的区别
今天我们来一起探索下 JOIN,JOIN 作为数据库操作的核心概念,用于合并两个或多个表中的数据. 一.JOIN (INNER JOIN) 1.基本功能:返回两个表中匹配成功的行. 2.特点: 只保留 ...
- SciTech-Mathmatics - Advanced Linear Algebra(高等线性代数): linalg + Proba.&Stats.. 大部分数学理论 在 NumPy/PyData 的实现、运用 和 可视化
SciTech-Mathmatics - Advanced Linear Algebra(高等线性代数) linalg. 大部分数学理论 在 NumPy/PyData 的实现及运用 import nu ...
- Science-化学英语基础:Periodic Table of Elements(元素周期表) 与基本化合物起名
https://zhuanlan.zhihu.com/p/478675889?utm_id=0#:~:text=物质的分类 - Classification of Matter 注意: 本文涉及的内容 ...
- Unity中Inspector面板显示提示
效果如下 上面有个 "可选变量" ,然后鼠标移动到变量上会显示一段文字 实现方法 [Header("可选变量")]//直接显示汉字在面板上 [Tooltip( ...
- UNR 6. D2T2 神隐
\(\mathbf{Part. -1}\) 这是一道交互题. hehe 蚤决定花费几天时间,游览下山市的最著名的旅游景点 -- 吓山. 吓山,以高低纵横,崔巍秀丽,错综复杂的地形闻名.据说无论用什么地 ...