TOJ1302: 简单计算器 && TOJ 4873: 表达式求值&&TOJ3231: 表达式求值

这些都是应用Python的eval函数的一些题目!

TOJ1302传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1302

TOJ4873传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=4873

TOJ3231传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=3231

TOJ1302

 

描述
读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。
输入
测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。
输出
对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。
样例输入
1 + 2
4 + 2 * 5 - 7 / 11
0
样例输出
3.00
13.36

思路:Python大法好。直接拿eval函数瞎搞就行!

while True:
    n = input()
    m = eval(n)
    if (int(m) == 0 and len(n) == 1):
        break
    print('%.2f'%float(m))
View Code

 

TOJ4873

描述

给定一个只包含加法和乘的算术表达式,请你编程计算表达式的值。

输入

输入仅有一行,为需要你计算的表达式,表达式中只包含数字、加法运算符“ +”和乘法运算符“ *”,且没有括号,所有参与运算的数字均为0到2^31 -1之间的整数。输入数据保证这一行只有 0~ 9、+、* 这 12 种字符。

数据范围

对于 30% 的数据,0≤表达式中加法运算符和乘法运算符的总数≤100;

对于 80% 的数据,0≤表达式中加法运算符和乘法运算符的总数≤1000;

对于 100% 的数据,0≤表达式中加法运算符和乘法运算符的总数≤100000。

输出

输出只有一行,包含一个整数,表示这个表达式的值。注意:当答案长度多于4位时,请只输出最后4位,前导0不输出。

样例输入

1+1*3+4

样例输出

 8

思路:一样是用py的eval函数求值,具体看代码!注意:当答案长度多于4位时,请只输出最后4位,前导0不输出。对于这句话,可以直接%10000。

给出的是python2的代码,真_一行解决。

print eval(raw_input())%10000

TOJ3231

描述

给一些包含加减号和小括号的表达式,求出该表达式的值。表达式中的数值均为绝对值小于 10 的整数。

输入

第一行为表达式的个数 n,以下 n 行每行有一个表达式。每个表达式的长度不超过 40 个字符。

输出

对每个表达式,输出它的值。

样例输入

3
3-(2+3)
-9+8+(2+3)-(-1+4)
0-0

样例输出

-2
1
0

思路:无

代码:

n = input()
n = int(n)
while (n > 0):
     a = input()
     print(eval(a))
     n-=1
View Code

 

转载于:https://www.cnblogs.com/Esquecer/p/8440592.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一小部分代码 请参考 org 0000h jmp start ;Start of the program org 0100h start: mov A,#030h ;1 line, 8 bits call wrcmd mov A,#LCD_SETVISIBLE + 4 call wrcmd mov A,#LCD_SETDDADDR+15 ; Start at right hand side of the display call wrcmd mov A,#LCD_SETMODE + 3 ; Automatic Increment - Display shift left. call wrcmd mov 025h,#00h ; Set output mode (floating point). call boundsbuffer ; Initialise the bounds buffer - used for error checking. mov mode,#4 ; Initialise the constant buffer to 100. Primarily used for % ops. mov digitcode,#031h call storedigit mov digitcode,#030h call storedigit mov digitcode,#030h call storedigit mov status,#00h ; variable used to determine the first key press after an operation. mov bufferctr,#00h mov opcounter,#00h mov decimalcnt,#00h call waitkey halt: mov PCON,#1 ;Halt ;*********************************************************** ;**** Floating Point Package **** ;******************************** $INCLUDE (FP52.ASM) ;Routine to peek arg at DPTR argout: mov R0,#FP_NUMBER_SIZE aoloop: movx A,@DPTR anl A,#0F0h rr a rr a rr a rr a add A,#aodata-$-3 movc A,@A+PC call sndchr movx A,@DPTR anl A,#0Fh add A,#aodata-$-3 movc A,@A+PC call sndchr inc DPTR djnz R0, aoloop ret aodata: db '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' ;Routine to output character in A, preserving all but A. sndchr: push R0B0 push R1B0 call getmode mov digitcode,A call storedigit pop R1B0 pop R0B0 ret ;Routine to print error message at DPTR. prterr: jmp wrstr ;Routine to handle input parameter error. badprm: mov DPTR,#bpmsg jmp wrstr bpmsg: db 'Bad Parameter',0 ;*********************************************************** ;**** LCD Display Routines **** ;****************************** ;LCD Registers addresses LCD_CMD_WR equ 00h LCD_DATA_WR equ 01h LCD_BUSY_RD equ 02h LCD_DATA_RD equ 03h LCD_PAGE equ 80h ;LCD Commands LCD_CLS equ 1 LCD_HOME equ 2 LCD_SETMODE equ 4 LCD_SETVISIBLE equ 8 LCD_SHIFT equ 16 LCD_SETFUNCTION equ 32 LCD_SETCGADDR equ 64 LCD_SETDDADDR equ 128

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值