两个数的四则运算
从键盘输入两个数字(可正可负),分别输出这两个数字的四则运算表达式 本帖最后由 风过无痕1989 于 2020-11-2 22:12 编辑
#include<stdio.h>
int main()
{
int i;
double a,b;
char c,str;
scanf("%lf%lf%s",&a,&b,str);
for(i = 0;i < 4;i++)
{
if(str == '+' || str == '-' || str == '*' || str == '/')
{
c = str;
}
}
if(c == '+')
{
printf("%.2f\n",a+b);
}
else if(c == '-')
{
printf("%.2f\n",a-b);
}
else if(c== '*')
{
printf("%.2f\n",a*b);
}
else if(c== '/')
{
if(b != 0)
{
printf("%.2f\n",a/b);
}
else
{
printf("除数不能为0 !\n");
}
}
return 0;
} 风过无痕1989 发表于 2020-11-2 22:10
谢谢你,不过我忘记说了,是用汇编写程序{:10_250:} chichengsun 发表于 2020-11-2 22:17
谢谢你,不过我忘记说了,是用汇编写程序
今晚睡觉,明日给你写段代码. xieglt 发表于 2020-11-2 22:31
今晚睡觉,明日给你写段代码.
好的,谢谢你,主要是明天早上一二节课要上交这次的代码作业,我自己写不出来,所以现在很慌,你明天如果可以早的话,希望你能尽量早一点,麻烦你了 哈哈哈
xiaoxinzi 发表于 2020-11-2 23:34
哈哈哈
{:5_104:} 本帖最后由 xieglt 于 2020-11-7 10:15 编辑
这个代码好麻烦,本来想写个任意数的加减乘除。一直有错误,只好写了个控制在[-65535,65535]之间的加减乘除。
这么麻烦的作业,要提前做准备。
assume cs:code,ds:data
data segment
NumericSTRUC
Integer DW0,0,0
Signed DW 0
Numeric ENDS
num_1 Numeric <>
num_2 Numeric <>
num_3 Numeric <>
buffer DB 8 dup(0)
msg1 db 'Input a number[-65535,65535]:',36
msg2 db 'Please input a rightful number!',0ah,0dh,36
msg3 db 'The number is too big!',0ah,0dh,36
msg4 db 'divider can not be zero!',0ah,0dh,36
data ends
code segment
_Break MACRO
NOP
INT 1H
NOP
ENDM
_EnterLine MACRO
MOV AH,02
MOV DL,0AH
INT 21H
MOV DL,0DH
INT 21H
ENDM
_PushAll MACRO
PUSH SI
PUSH DI
PUSH BX
PUSH CX
PUSH DX
ENDM
_PopAll MACRO
POP DX
POP CX
POP BX
POP DI
POP SI
ENDM
_start:
MOV AX,DATA
MOV DS,AX
MOV ES,AX
PUSH OFFSET num_1
CALL _GetNumeric
PUSH OFFSET num_2
CALL _GetNumeric
CALL _CopyNumeric
PUSH OFFSET num_2
PUSH OFFSET num_3
CALL _AddNumeric
CALL _CopyNumeric
PUSH OFFSET num_2
PUSH OFFSET num_3
CALL _SubNumeric
CALL _CopyNumeric
PUSH OFFSET num_2
PUSH OFFSET num_3
CALL _MulNumeric
CALL _CopyNumeric
PUSH OFFSET num_2
PUSH OFFSET num_3
CALL _DivNumeric
MOV AX,4C00H
INT 21H
_AddNumeric PROC
PUSH BP
MOV BP,SP
_PushAll
PUSH
PUSH 0
CALL _OutputNumeric
MOV AH,2
MOV DL,'+'
INT 21H
PUSH
PUSH 0
CALL _OutputNumeric
MOV AH,2
MOV DL,'='
INT 21H
MOV SI,
MOV DI,
PUSH
PUSH
MOV AX,.Signed
SUB AX,.Signed
TEST AX,AX
JE _AnSameSigned
CALL _SimpleSub
MOV ,AX
MOV AX,.Signed
XOR AX,DX
MOV .Signed,AX
JMP _AnEnd
_AnSameSigned:
CALL _SimpleAdd
MOV ,AX
MOV ,DX
_AnEnd:
PUSH
PUSH 0
CALL _OutputNumeric
_EnterLine
_PopAll
MOV SP,BP
POP BP
RET 4
_AddNumeric ENDP
_SubNumeric PROC
PUSH BP
MOV BP,SP
_PushAll
PUSH
PUSH 0
CALL _OutputNumeric
MOV AH,2
MOV DL,'-'
INT 21H
PUSH
PUSH 0
CALL _OutputNumeric
MOV AH,2
MOV DL,'='
INT 21H
MOV SI,
MOV DI,
PUSH
PUSH
MOV AX,.Signed
SUB AX,.Signed
TEST AX,AX
JNE _SnSameSigned
CALL _SimpleSub
MOV ,AX
MOV AX,.Signed
XOR AX,DX
MOV .Signed,AX
JMP _SnEnd
_SnSameSigned:
CALL _SimpleAdd
MOV ,AX
MOV ,DX
_SnEnd:
PUSH
PUSH 0
CALL _OutputNumeric
_EnterLine
_PopAll
MOV SP,BP
POP BP
RET 4
_SubNumeric ENDP
_MulNumeric PROC
PUSH BP
MOV BP,SP
SUB SP,20
PUSH DS
PUSH ES
_PushAll
PUSH
PUSH 0
CALL _OutputNumeric
MOV AH,2
MOV DL,'*'
INT 21H
PUSH
PUSH 0
CALL _OutputNumeric
MOV AH,2
MOV DL,'='
INT 21H
MOV WORD PTR ,0
MOV WORD PTR ,10000
MOV SI,
MOV DI,
MOV AX,.Signed
XOR AX,.Signed
MOV .Signed,AX
MOV AX,
MOV DX,
MOV BX,10000
DIV BX
MOV ,DX
MOV ,AX
MOV AX,
MOV DX,
DIV BX
MOV ,DX
MOV ,AX
MOV AX,
MUL WORD PTR
DIV WORD PTR
MOV ,DX
MOV ,AX
MOV AX,
MUL WORD PTR
ADD AX,
ADC DX,0
DIV WORD PTR
MOV ,DX
MOV ,AX
MOV AX,
MUL WORD PTR
DIV WORD PTR
MOV ,DX
MOV ,AX
MOV AX,
MUL WORD PTR
ADD AX,
ADC DX,0
DIV WORD PTR
MOV ,DX
MOV ,AX
MOV AX,
MOV ,AX
XOR DX,DX
XOR BX,BX
MOV AX,
ADD AX,
ADC BX,0
DIV WORD PTR
MOV ,DX
ADD BX,AX
MOV AX,
ADD AX,BX
ADD AX,
MOV ,AX
PUSH SI
PUSH 1
CALL _OutputNumeric
_EnterLine
_PopAll
POP ES
POP DS
ADD SP,20
MOV SP,BP
POP BP
RET 4
_MulNumeric ENDP
_DivNumeric PROC
PUSH BP
MOV BP,SP
_PushAll
PUSH
PUSH 0
CALL _OutputNumeric
MOV AH,2
MOV DL,'/'
INT 21H
PUSH
PUSH 0
CALL _OutputNumeric
MOV AH,2
MOV DL,'='
INT 21H
MOV SI,
MOV DI,
MOV BX,
TEST BX,BX
JNE _DividerIsNotZero
MOV AH,9
LEA DX,msg4
INT 21H
JMP _DivEnd
_DividerIsNotZero:
MOV AX,.Signed
XOR AX,.Signed
MOV .Signed,AX
MOV AX,
XOR DX,DX
DIV BX
MOV ,AX
PUSH
PUSH 0
CALL _OutputNumeric
_DivEnd:
_EnterLine
_PopAll
MOV SP,BP
POP BP
RET 4
_DivNumeric ENDP
_SimpleAdd PROC
PUSH BP
MOV BP,SP
XOR DX,DX
MOV AX,
ADD AX,
ADC DX,0
MOV SP,BP
POP BP
RET 4
_SimpleAdd ENDP
_SimpleSub PROC
PUSH BP
MOV BP,SP
SUB SP,2
MOV WORD PTR ,0
MOV AX,
MOV DX,
CMP AX,DX
JAE _@15
XCHG AX,DX
INC WORD PTR
_@15:
SUB AX,DX
MOV DX,WORD PTR
ADD SP,2
MOV SP,BP
POP BP
RET 4
_SimpleSub ENDP
_GetNumeric PROC
PUSH BP
MOV BP,SP
SUB SP,6
_PushAll
JMP _GNBegin
_Number2Big:
MOV AH,09H
LEA DX,msg3
INT 21H
_GNBegin:
CALL _InputNumber
MOV ,AX
MOV WORD PTR ,0
MOV WORD PTR ,10
LEA SI,buffer
_GNLoop:
XOR AX,AX
LODSB
TEST AL,AL
JE _GNEnd
SUB AL,030H
MOV BX,AX
MOV AX,
MUL WORD PTR
JC _Number2Big
ADD AX,BX
JC _Number2Big
MOV ,AX
JMP _GNLoop
_GNEnd:
MOV SI,
PUSH
POP .Integer
PUSH
POP .Signed
_PopAll
ADD SP,6
MOV SP,BP
POP BP
RET 2
_GetNumeric ENDP
_InputNumber PROC
JMP _@10
_@9:
MOV AH,09
LEA DX,msg2
INT 21H
_@10:
MOV AH,09
LEA DX,msg1
INT 21H
XOR AX,AX
MOV CX,4
LEA DI,buffer
PUSH DI
REP STOSW
POP DI
XOR CX,CX
XOR BX,BX
MOV AH,01
INT 21H
CMP AL,'-'
JNE _@11
INC BX
JMP _@12
_@11:
CMP AL,'+'
JNE _@13
_@12:
MOV AH,01H
INT 21H
_@13:
CMP AL,0DH
JE _@14
CMP AL,'0'
JL _@9
CMP AL,'9'
JG _@9
STOSB
INC CX
CMP CX,5
JGE _@14
JMP _@12
_@14:
_EnterLine
MOV AX,BX
RET
_InputNumber ENDP
_OutputNumber PROC
PUSH BP
MOV BP,SP
SUB SP,4
_PushAll
MOV WORD PTR ,0
MOV WORD PTR ,1000
MOV CX,4
MOV AX,
_@1:
XOR DX,DX
DIV WORD PTR
XCHG AX,DX
TEST DX,DX
JNE _@2
MOV BX,
ADD BX,
TEST BX,BX
JE _@3
_@2:
PUSH AX
MOV AH,02
ADD DL,030H
INT 21H
POP AX
INC WORD PTR
_@3:
PUSH AX
XOR DX,DX
MOV AX,
MOV BX,10
DIV BX
MOV ,AX
POP AX
LOOP _@1
MOV AX,WORD PTR
_PopAll
ADD SP,4
MOV SP,BP
POP BP
RET 4
_OutputNumber ENDP
_CopyNumeric PROC
LEA SI,num_1
LEA DI,num_3
MOV CX,size Numeric
REP MOVSB
RET
_CopyNumeric ENDP
_OutputNumeric PROC
PUSH BP
MOV BP,SP
SUB SP,2
_PushAll
MOV WORD PTR ,0
MOV SI,
MOV AX,.Signed
TEST AX,AX
JE _@4
MOV AH,2
MOV DL,'('
INT 21H
MOV AH,2
MOV DL,'-'
INT 21H
_@4:
MOV AX,
TEST AX,AX
JNE _@5
MOV AX,
MOV DX,
MOV BX,10000
DIV BX
PUSH AX
PUSH
CALL _OutputNumber
MOV ,AX
PUSH DX
PUSH AX
CALL _OutputNumber
ADD ,AX
JMP _@7
_@5:
MOV CX,3
ADD SI,4
_@6:
MOV AX,
PUSH AX
PUSH
CALL _OutputNumber
ADD ,AX
SUB SI,2
LOOP _@6
_@7:
MOV AX,
TEST AX,AX
JNE _@8
MOV AH,2
MOV DL,030H
INT 21H
_@8:
MOV SI,
MOV AX,.Signed
TEST AX,AX
JE _OnEnd
MOV AH,2
MOV DL,')'
INT 21H
_OnEnd:
_PopAll
ADD SP,2
MOV SP,BP
POP BP
RET 4
_OutputNumeric ENDP
code ends
END _start 运行结果如图
xieglt 发表于 2020-11-7 09:51
运行结果如图
谢谢,看别人写的代码也是一种学习,500多行,真的幸苦你了,再次表示感谢
页:
[1]