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

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

输入一个形式如"操作数 运算符 操作数"的表达式,对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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值