7-24 计算表达式(*,//, %)

该程序接收用户输入的形如操作数运算符操作数的表达式,然后进行乘法、整除和求余运算。如果运算符无效,程序会输出错误信息。示例中,输入21*8,程序将返回21*8=168;若输入非法运算符如!,则输出Invalidoperator。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

输入一个形式如"操作数 运算符 操作数"的表达式,对2个整数进行乘、整除和求余(%)运算。

输入格式:

在一行中输入形式如"操作数 运算符 操作数"的表达式。

输出格式:

在一行中输出表达式及计算结果。

输入样例1:

在这里给出一组输入。例如:

21  *  8

输出样例1:

在这里给出相应的输出。例如:

21*8=168

输入样例2:

在这里给出一组输入。例如:

21  !  8

输出样例1:

在这里给出相应的输出。例如:

Invalid operator
expression = input()  # 获取用户输入的表达式

# 将表达式拆分为操作数和运算符
operands, operator = expression.split()[::2]

# 将操作数转换为整数
operand1, operand2 = int(operands[0]), int(operands[1])

# 根据运算符计算结果
if operator == '*':
    result = operand1 * operand2
elif operator == '//':
    result = operand1 // operand2
elif operator == '%':
    result = operand1 % operand2
else:
    print('Invalid operator')  # 如果运算符无效,则输出错误信息并退出程序
    exit()

# 输出表达式及计算结果
print(f'{operand1} {operator} {operand2} = {result}')

 

{ /* CF_RULE */ "conditionType": 2 , "comparisonOperation": 0 , "formattingOptions": 0 , "formattingNotUsed": 0 , "fontFormatting": null , "borderFormatting": null , "patternFormatting": null , "formula1": { /* Formula */ "tokens": [ { /* RefNPtg */ "row": 0 , "rowRelative": true , "column": 0 , "colRelative": true , "formatReference": "RowOffset: 0 ColOffset: 0" }, { /* AreaPtg - index: 1 */ "firstRow": 15 /* 0x0000000f */ , "firstRowRelative": false , "firstColumn": 4 , "firstColRelative": false , "lastRow": 15 /* 0x0000000f */ , "lastRowRelative": false , "lastColumn": 4 , "lastColRelative": false , "formatReference": "$E$16:$E$16" }, { /* FuncPtg - index: 2 */ "functionIndex": 3 , "functionName": "ISERROR" , "numberOfOperands": 1 , "externalFunction": false , "defaultOperandClass": 32 /* 0x20 */ }, { /* AttrPtg - index: 3 */ "volatile": false , "options": 2 /* OPTI_IF */ , "space_count": 0 , "space_type": 11 /* */ }, { /* AttrPtg - index: 4 */ "volatile": false , "options": 64 /* SPACE */ , "space_count": 1 , "space_type": 0 /* SPACE_BEFORE */ }, { /* StringPtg - index: 5 */ "value": "" }, { /* AttrPtg - index: 6 */ "volatile": false , "options": 8 /* OPTI_SKIP */ , "space_count": 0 , "space_type": 63 /* */ }, { /* AreaPtg - index: 7 */ "firstRow": 15 /* 0x0000000f */ , "firstRowRelative": false , "firstColumn": 4 , "firstColRelative": false , "lastRow": 15 /* 0x0000000f */ , "lastRowRelative": false , "lastColumn": 4 , "lastColRelative": false , "formatReference": "$E$16:$E$16" }, { /* FuncPtg - index: 8 */ "functionIndex": 129 /* 0x0081 */ , "functionName": "ISBLANK" , "numberOfOperands": 1 , "externalFunction": false , "defaultOperandClass": 32 /* 0x20 */ }, { /* AttrPtg - index: 9 */ "volatile": false , "options": 2 /* OPTI_IF */ , "space_count": 0 , "space_type": 11 /* */ }, { /* AttrPtg - index: 10 */ "volatile": false , "options": 64 /* SPACE */ , "space_count": 1 , "space_type": 0 /* SPACE_BEFORE */ }, { /* StringPtg - index: 11 */ "value": "" }, { /* AttrPtg - index: 12 */ "volatile": false , "options": 8 /* OPTI_SKIP */ , "space_count": 0 , "space_type": 24 /* */ }, { /* AttrPtg - index: 13 */ "volatile": false , "options": 64 /* SPACE */ , "space_count": 1 , "space_type": 0 /* SPACE_BEFORE */ }, { /* AreaPtg - index: 14 */ "firstRow": 15 /* 0x0000000f */ , "firstRowRelative": false , "firstColumn": 4 , "firstColRelative": false , "lastRow": 15 /* 0x0000000f */ , "lastRowRelative": false , "lastColumn": 4 , "lastColRelative": false , "formatReference": "$E$16:$E$16" }, { /* AttrPtg - index: 15 */ "volatile": false , "options": 64 /* SPACE */ , "space_count": 1 , "space_type": 0 /* SPACE_BEFORE */ }, { /* AttrPtg - index: 16 */ "volatile": false , "options": 8 /* OPTI_SKIP */ , "space_count": 0 , "space_type": 3 /* CR_BEFORE_OPEN_PAREN */ }, { /* FuncVarPtg - index: 17 */ "functionIndex": 1 , "functionName": "IF" , "numberOfOperands": 3 , "externalFunction": false , "defaultOperandClass": 0 , "cetab": false }, { /* AttrPtg - index: 18 */ "volatile": false , "options": 64 /* SPACE */ , "space_count": 1 , "space_type": 0 /* SPACE_BEFORE */ }, { /* AttrPtg - index: 19 */ "volatile": false , "options": 8 /* OPTI_SKIP */ , "space_count": 0 , "space_type": 3 /* CR_BEFORE_OPEN_PAREN */ }, { /* FuncVarPtg - index: 20 */ "functionIndex": 1 , "functionName": "IF" , "numberOfOperands": 3 , "externalFunction": false , "defaultOperandClass": 0 , "cetab": false }, { /* FuncVarPtg - index: 21 */ "functionIndex": 5 , "functionName": "AVERAGE" , "numberOfOperands": 1 , "externalFunction": false , "defaultOperandClass": 32 /* 0x20 */ , "cetab": false }, { /* GreaterEqualPtg - index: 22 */ } ] , "expReference": null } , "formula2": { /* Formula */ "tokens": [ ] , "expReference": null } } 这段是什么意思
最新发布
07-08
这段 JSON 是 Apache POI 或 Excel 文件解析器对一个 **Excel 公(formula)** 的详细结构化表示,特别是用于条件格规则中的公表达式。我们可以从整体结构和每个字段的含义来解读它的作用。 --- ## 🧩 一、整体结构分析 ### 主要对象:`ConditionalFormattingRule` 中的 `formula1` ```json { "conditionType": 2, "comparisonOperation": 0, ... "formula1": { ... }, "formula2": { ... } } ``` - `"conditionType": 2` 表示这是一个 **expression 类型** 的条件格规则(即使用公判断是否应用样)。 - `"comparisonOperation": 0` 表示比较操作符是 `NO_COMPARISON`,因为这是 expression 类型,不需要比较符。 - `"formula1"` 字段是一个完整的 **Excel 公结构**,用 token 数组表示。 --- ## 🔍 二、重点解析 formula1 的 tokens 这部分是关键内容 —— Excel 公的内部结构,由一系列 `token` 构成。我们逐个解释这些 token 的含义: ### 📌 公原始逻辑(简化) 根据 token 序列可以还原出这个公的大致逻辑: ```excel =IF(ISERROR($E$16), "", IF(ISBLANK($E$16), "", AVERAGE($E$16) >= 0)) ``` ### ✅ 拆解说明: | Token | 含义 | |-------|------| | `AreaPtg("$E$16")` | 引用单元格 $E$16 | | `FuncPtg("ISERROR")` | 调用函数 ISERROR 判断是否错误值 | | `StringPtg("")` | 空字符串(作为 IF 的 false 分支) | | `FuncPtg("ISBLANK")` | 判断 E16 是否为空 | | `FuncVarPtg("IF", 3)` | IF 函数,三个参数:条件、真值、假值 | | `FuncPtg("AVERAGE")` | 计算平均值(这里其实只传了单个值) | | `GreaterEqualPtg` | 大于等于操作符 `>=` | | `NumberPtg(0)` | 数字 0 | ### 📝 整体公语义: 如果 E16 是错误值 → 显示空字符串 否则如果 E16 是空白 → 显示空字符串 否则判断 AVERAGE(E16) 是否 ≥ 0(这等价于判断 E16 ≥ 0) > 这个写法有点冗余(比如 AVERAGE 只计算一个单元格),但可能是某种兼容性或自动填充生成的公--- ## 💡 三、为什么会有这么多 AttrPtg 和 Space? 你看到很多类似这样的 token: ```json { "options": 64 /* SPACE */, "space_count": 1, "space_type": 0 /* SPACE_BEFORE */ } ``` 这些是 **公的布局信息**,用于在 Excel UI 中显示时保留用户输入的空格、换行、缩进等格,不影响实际计算逻辑。 --- ## 📁 四、这种结构常见于哪里? 这类数据通常出现在: - 使用 Apache POI 读取 `.xls` 文件时(HSSF) - 使用 BIFF 阅读器解析 Excel 二进制格(BIFF8) - 通过反序列化 `CF_RULE`(条件格规则)记录时 它不是 XML 格,而是基于 **Apache POI 的低层结构表示**,用于构建或解析 Excel 公的真实内容。 --- ## 🧠 五、如何将它转换为可读的公字符串? 如果你在 Java 中处理 POI 的 `Formula` 对象,可以通过以下方获取人类可读的公字符串: ```java ConditionalFormattingRule rule = cf.getRule(0); String formula = rule.getFormula1(); // 得到类似 "=IF(...)" 的字符串 System.out.println(formula); ``` POI 内部会把 token 转换成字符串形返回给你。 --- ## ✅ 总结 | 内容 | 说明 | |------|------| | `conditionType: 2` | 表示该条件格是 expression 类型(使用公) | | `formula1` | 包含了一个复杂的 Excel 公 | | `tokens[]` | 是公的底层结构,包括引用、函数、运算符等 | | 公语义 | 判断 E16 是否有错误、是否为空,否则判断其值是否大于等于 0 | | AttrPtg / Space | 用于保持公显示格(如空格、换行) | --- ##
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值