首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建一个简单的数学计算器+-/*只接受汉字

创建一个简单的数学计算器+-/*只接受汉字
EN

Code Golf用户
提问于 2014-02-15 12:21:11
回答 2查看 659关注 0票数 7

任务:

用中文建立一个简单的数学方程式的计算器,用中文输出正确的答案(用户输入的方程式)。

范围:

  • 任何编程语言都可以使用。
  • 输入必须是中文,否则会显示错误信息。
  • 输出必须是中文。
  • 只有0-999之间的整数输入和输出才在范围内(这是一个简单的计算)。
  • 完整的原始输入必须包括在等于之前,然后是答案。
  • 显示答案没有小数点。
  • 长数字形式的输出是强制性的。
  • 输入必须接受长数字形式,包括短格式是可选的。

示例输入/输出:

二乘以八等于十六(2*8=16)二乘以八(2*8)

备忘单:

零或0→0(无论是表单还是两者,你选择) 一→1 二→2 三→3 四→4 五→5 六→6 七→7 八→8 八→8 八→九→9 十→10 百→100 加→+ Operator加→- Operator加→* Operator / Operator免责=操作符中文编号为333只是3100 3 10 3三百三十三唯一的例外是十,您不需要一十来表示10 (但您可以这样做),但是您确实需要一百来表示100 (也就是说,不能只说百)。

有用的链接:http://www.mandarintools.com/numbers.html

如果你看不到中国的符号,你可以在这里看到他们

获得创造性和快乐的编码!

EN

回答 2

Code Golf用户

发布于 2014-02-15 16:59:06

Java

它将该方程解析为一种上下文无关的语言,并为此使用以下结果:

代码语言:javascript
复制
U1 = [一-九]                 (i.e. [1-9])
D1 = U1 十 U1?
DO = U1? 十 U1?
CDN = 〇 U1 | D1
C1 = U1 百 CDN?
F = C1 | DO | U1 | 〇
F2 = 負? F
OP = 加 | 减 | 乘以 | 除以    (i.e., + | - | * | / )
EQ = F2 (OP F2)*

DOD1产品在相同的方法中处理,并由一个参数进行不同的处理。其他产品各采用一种方法。EQ是语法的主要符号。在开始解析之前,它将零替换为0,因此在那之后只需要处理0。

負代表负数。它可以处理从-999到999 (或負九百九十九到九百九十九)的数字。

此外,为了使它更多的中文,所有的标识符都是中文。唯一的例外是类名(使用国际字符可能会造成问题),即音译为拉丁文的中文名称。

对于输入,我必须使用JOptionPane。对不起,但我在使用其他输入方法编码时遇到了麻烦。如果您不喜欢这种输入方法,可以直接使用解方程方法,它接受输入公式并生成输出方程(或错误消息)。

代码语言:javascript
复制
import javax.swing.JOptionPane;

public class ZhongguoJisuanQi {

    private final String 文字输入;
    private int 源指数;

    public ZhongguoJisuanQi(String 文字输入) {
        this.文字输入 = 文字输入.replace("零", "〇");
        this.源指数 = 0;
    }

    public static void main(String... 参数) {
        String 方程 = 解方程(读取文本行());
        System.out.println(方程);
        JOptionPane.showMessageDialog(null, 方程, "答案是", JOptionPane.INFORMATION_MESSAGE);
    }

    private static String 读取文本行() {
        return JOptionPane.showInputDialog(null, "输入数学公式", "输入数学公式", JOptionPane.QUESTION_MESSAGE);
    }

    public static String 解方程(String 数学公式) {
        if (数学公式 == null) return "错误:给定的数学公式的格式不正确。";
        ZhongguoJisuanQi 计算器 = new ZhongguoJisuanQi(数学公式);
        Integer 结果 = 计算器.解析并解决了式();
        if (结果 == null) return "错误:给定的数学公式的格式不正确。";
        return 数学公式 + "等于" + 整数转换为文本(结果);
    }

    private boolean 找到字符(char 字符, int 指数) {
        return 指数 < 文字输入.length() && 文字输入.charAt(指数) == 字符;
    }

    private Integer 解析生产一() {
        int 数 = "一二三四五六七八九".indexOf(文字输入.charAt(源指数));
        if (数 == -1) return null;
        源指数++;
        return 数 + 1;
    }

    private Integer 解析生产十(boolean 必须带有前缀) {
        int 原始来源索引 = 源指数;
        Integer 第一个数字 = 解析生产一();
        if (第一个数字 == null && 必须带有前缀) return null;
        if (!找到字符('十', 源指数)) {
            源指数 = 原始来源索引;
            return null;
        }
        源指数++;
        Integer 第二个数字 = 解析生产一();
        return (第一个数字 == null ? 1 : 第一个数字) * 10 + (第二个数字 == null ? 0 : 第二个数字);
    }

    private Integer 解析生产或零十() {
        int 原始来源索引 = 源指数;
        if (找到字符('〇', 源指数)) {
            源指数++;
            Integer 数 = 解析生产一();
            if (数 == null) {
                源指数 = 原始来源索引;
                return null;
            }
            return 数;
        }
        return 解析生产十(true);
    }

    private Integer 解析生产百() {
        int 原始来源索引 = 源指数;
        Integer 第一个数字 = 解析生产一();
        if (第一个数字 == null) return null;
        if (!找到字符('百', 源指数)) {
            源指数 = 原始来源索引;
            return null;
        }
        源指数++;
        Integer 第二个数字 = 解析生产或零十();
        return 第一个数字 * 100 + (第二个数字 == null ? 0 : 第二个数字);
    }

    private Integer 解析生产数无符号() {
        Integer 数 = 解析生产百();
        if (数 != null) return 数;
        数 = 解析生产十(false);
        if (数 != null) return 数;
        数 = 解析生产一();
        if (数 != null) return 数;
        if (找到字符('〇', 源指数)) {
            源指数++;
            return 0;
        }
        return null;
    }

    private Integer 解析该生产号() {
        boolean 负 = false;
        if (找到字符('負', 源指数)) {
            源指数++;
            负 = true;
        }
        Integer 数 = 解析生产数无符号();
        return 数 == null ? null : 负 ? -数 : 数;
    }

    private String 解析算术运算符() {
        if (找到字符('加', 源指数)) { 源指数++; return "加"; }
        if (找到字符('减', 源指数)) { 源指数++; return "减"; }
        if (找到字符('乘', 源指数) && 找到字符('以', 源指数 + 1)) { 源指数 += 2; return "乘以"; }
        if (找到字符('除', 源指数) && 找到字符('以', 源指数 + 1)) { 源指数 += 2; return "除以"; }
        return null;
    }

    public Integer 解析并解决了式() {
        Integer 第一个数字 = 解析该生产号();
        while (true) {
            String 算术运算符 = 解析算术运算符();
            if (算术运算符 == null) return 源指数 == 文字输入.length() ? 第一个数字 : null;
            Integer 第二个数字 = 解析该生产号();
            if (第二个数字 == null) return null;
            switch (算术运算符) {
                case "加": 第一个数字 += 第二个数字; break;
                case "减": 第一个数字 -= 第二个数字; break;
                case "乘以": 第一个数字 *= 第二个数字; break;
                case "除以": 第一个数字 /= 第二个数字; break;
                default: throw new AssertionError();
            }
        }
    }

    private static String 整数转换为文本(int 参数) {
        if (参数 == 0) return "〇";
        StringBuilder 字符串生成器 = new StringBuilder(7);
        int 数 = 参数;
        if (数 < 0) {
            字符串生成器.append("負");
            数 = -数;
        }
        if (数 >= 1000) return null;
        if (数 >= 100) {
            字符串生成器.append("〇一二三四五六七八九".charAt(数 / 100)).append("百");
            数 %= 100;
            if (数 >= 1 && 数 <= 9) 字符串生成器.append("〇");
            if (数 >= 10 && 数 <= 19) 字符串生成器.append("一");
        }
        if (数 >= 20) 字符串生成器.append("〇一二三四五六七八九".charAt(数 / 10));
        if (数 >= 10) 字符串生成器.append("十");
        数 %= 10;
        字符串生成器.append("〇一二三四五六七八九".charAt(数));
        return 字符串生成器.toString();
    }
}

用下列方法汇编:

代码语言:javascript
复制
javac -encoding UTF8 ZhongguoJisuanQi.java

与:

代码语言:javascript
复制
java ZhongguoJisuanQi

标识符名称的备忘单,相应地给google翻译器:

代码语言:javascript
复制
(english original text) -> (chinese text) -> (english text back from chinese)
chinese calculator -> 中国计算器 (Zhōngguó jìsuàn qì) -> china calculator
text input -> 文字输入 -> text input
source index -> 源指数 -> source index
original source index -> 原始来源索引 -> original source index
Enter the mathematical formula -> 输入数学公式 -> Enter mathematical formulas
mathematical formula -> 数学公式 -> mathematical formulas
Error: The given mathematical formula is malformed. -> 错误:给定的数学公式的格式不正确。 -> Error: The given mathematical formula is not correct.
character -> 字符 -> character
read a text line -> 读取文本行 -> read a line of text
arguments -> 参数 -> parameter
equation -> 方程 -> equation
index -> 指数 -> index
find character at -> 找到字符 -> find characters
parse the production -> 解析生产 -> resolve the production
parse the production or zero -> 解析生产或零 -> resolve the production or zero
parse the production number -> 解析该生产号 -> resolve the production number
parse the production number without sign -> 解析生产数无符号 -> resolve production unsigned
parse and solve the formula -> 解析并解决了式 -> parse and resolve the formula
parse an arithmetic operator -> 解析算术运算符 -> parsing arithmetic operators
arithmetic operator -> 算术运算符 -> arithmetic operators
must be prefixed -> 必须带有前缀 -> must be prefixed with
calculator -> 计算器 -> calculators
solve the equation -> 解方程 -> solving equations
result -> 结果 -> result
number -> 数 -> number
minus -> 负 -> minus
first number -> 第一个数字 -> the first number
second number -> 第二个数字 -> second number
string builder -> 字符串生成器 -> string builder
convert integer to text -> 整数转换为文本 -> integer is converted to text
the answer is -> 答案是 -> the answer is
票数 5
EN

Code Golf用户

发布于 2014-02-15 15:02:37

我试着用python来写这个:

代码语言:javascript
复制
import compiler
# encoding: utf-8

# python chinese calculator,

c2e = {('1','一'),('2','二'),('3','三'),('4','四'),('5','五'),('6','六'),('7','七'),('8','八'),('9','九'),('0','零')}

def tochi(eq):
    for en,ch in c2e:
        eq.replace(en,ch)
    return eq

def toen(eq):
    for en,ch in c2e:
        eq.replace(ch,en)
    return eq

if __name__ == "__main__":
    while True:
        eqin = raw_input('Equation with chinese numbers: ')
        eqin = toen(eqin)
        eqout = compiler.parse(eqin)
        eqout = tochi(eqout)
        print eqout
票数 1
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/20957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档