Skip to content

Commit

Permalink
命令行计算器
Browse files Browse the repository at this point in the history
  • Loading branch information
yourtion committed May 20, 2016
1 parent 70b3352 commit 3871740
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 1 deletion.
7 changes: 6 additions & 1 deletion 30_day/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 22,8 @@ haribote.img : haribote/ipl20.bin haribote/haribote.sys Makefile \
beepdown/beepdown.hrb color/color.hrb color2/color2.hrb \
sosu/sosu.hrb sosu2/sosu2.hrb sosu3/sosu3.hrb \
type/type.hrb iroha/iroha.hrb chklang/chklang.hrb \
notrec/notrec.hrb bball/bball.hrb invader/invader.hrb
notrec/notrec.hrb bball/bball.hrb invader/invader.hrb \
calc/calc.hrb
$(EDIMG) imgin:../z_tools/fdimg0at.tek \
wbinimg src:haribote/ipl20.bin len:512 from:0 to:0 \
copy from:haribote/haribote.sys to:@: \
Expand Down Expand Up @@ -54,6 55,7 @@ haribote.img : haribote/ipl20.bin haribote/haribote.sys Makefile \
copy from:notrec/notrec.hrb to:@: \
copy from:bball/bball.hrb to:@: \
copy from:invader/invader.hrb to:@: \
copy from:calc/calc.hrb to:@: \
copy from:nihongo/nihongo.fnt to:@: \
imgout:haribote.img

Expand Down Expand Up @@ -97,6 99,7 @@ full :
$(MAKE) -C notrec
$(MAKE) -C bball
$(MAKE) -C invader
$(MAKE) -C calc
$(MAKE) haribote.img

run_full :
Expand Down Expand Up @@ -148,6 151,7 @@ clean_full :
$(MAKE) -C notrec clean
$(MAKE) -C bball clean
$(MAKE) -C invader clean
$(MAKE) -C calc clean

src_only_full :
$(MAKE) -C haribote src_only
Expand Down Expand Up @@ -178,6 182,7 @@ src_only_full :
$(MAKE) -C notrec src_only
$(MAKE) -C bball src_only
$(MAKE) -C invader src_only
$(MAKE) -C calc src_only
-$(DEL) haribote.img

refresh :
Expand Down
1 change: 1 addition & 0 deletions 30_day/calc/!cons_9x.bat
Original file line number Diff line number Diff line change
@@ -0,0 1 @@
command
1 change: 1 addition & 0 deletions 30_day/calc/!cons_nt.bat
Original file line number Diff line number Diff line change
@@ -0,0 1 @@
cmd.exe
5 changes: 5 additions & 0 deletions 30_day/calc/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 1,5 @@
APP = calc
STACK = 4k
MALLOC = 0k

include ../app_make.txt
163 changes: 163 additions & 0 deletions 30_day/calc/calc.c
Original file line number Diff line number Diff line change
@@ -0,0 1,163 @@
#include "apilib.h"
#include <stdio.h> /* sprintf */

#define INVALID -0x7fffffff

int strtol(char *s, char **endp, int base); /* 标准函数 <stdlib.h> */

char *skipspace(char *p);
int getnum(char **pp, int priority);

void HariMain(void)
{
int i;
char s[30], *p;

api_cmdline(s, 30);
for (p = s; *p > ' '; p ) { } /* 一直读到空格为止 */
i = getnum(&p, 9);
if (i == INVALID) {
api_putstr0("error!\n");
} else {
sprintf(s, "= %d = 0x%x\n", i, i);
api_putstr0(s);
}
api_end();
}

char *skipspace(char *p)
{
for (; *p == ' '; p ) { } /* 将空格跳过去 */
return p;
}

int getnum(char **pp, int priority)
{
char *p = *pp;
int i = INVALID, j;
p = skipspace(p);

/*单项运算符*/
if (*p == ' ') {
p = skipspace(p 1);
i = getnum(&p, 0);
} else if (*p == '-') {
p = skipspace(p 1);
i = getnum(&p, 0);
if (i != INVALID) {
i = - i;
}
} else if (*p == '~') {
p = skipspace(p 1);
i = getnum(&p, 0);
if (i != INVALID) {
i = ~i;
}
} else if (*p == '(') { /*括号*/
p = skipspace(p 1);
i = getnum(&p, 9);
if (*p == ')') {
p = skipspace(p 1);
} else {
i = INVALID;
}
} else if ('0' <= *p && *p <= '9') { /*数值*/
i = strtol(p, &p, 0);
} else { /*错误 */
i = INVALID;
}

/*二项运算符*/
for (;;) {
if (i == INVALID) {
break;
}
p = skipspace(p);
if (*p == ' ' && priority > 2) {
p = skipspace(p 1);
j = getnum(&p, 2);
if (j != INVALID) {
i = j;
} else {
i = INVALID;
}
} else if (*p == '-' && priority > 2) {
p = skipspace(p 1);
j = getnum(&p, 2);
if (j != INVALID) {
i -= j;
} else {
i = INVALID;
}
} else if (*p == '*' && priority > 1) {
p = skipspace(p 1);
j = getnum(&p, 1);
if (j != INVALID) {
i *= j;
} else {
i = INVALID;
}
} else if (*p == '/' && priority > 1) {
p = skipspace(p 1);
j = getnum(&p, 1);
if (j != INVALID && j != 0) {
i /= j;
} else {
i = INVALID;
}
} else if (*p == '%' && priority > 1) {
p = skipspace(p 1);
j = getnum(&p, 1);
if (j != INVALID && j != 0) {
i %= j;
} else {
i = INVALID;
}
} else if (*p == '<' && p[1] == '<' && priority > 3) {
p = skipspace(p 2);
j = getnum(&p, 3);
if (j != INVALID && j != 0) {
i <<= j;
} else {
i = INVALID;
}
} else if (*p == '>' && p[1] == '>' && priority > 3) {
p = skipspace(p 2);
j = getnum(&p, 3);
if (j != INVALID && j != 0) {
i >>= j;
} else {
i = INVALID;
}
} else if (*p == '&' && priority > 4) {
p = skipspace(p 1);
j = getnum(&p, 4);
if (j != INVALID) {
i &= j;
} else {
i = INVALID;
}
} else if (*p == '^' && priority > 5) {
p = skipspace(p 1);
j = getnum(&p, 5);
if (j != INVALID) {
i ^= j;
} else {
i = INVALID;
}
} else if (*p == '|' && priority > 6) {
p = skipspace(p 1);
j = getnum(&p, 6);
if (j != INVALID) {
i |= j;
} else {
i = INVALID;
}
} else {
break;
}
}
p = skipspace(p);
*pp = p;
return i;
}
1 change: 1 addition & 0 deletions 30_day/calc/make.bat
Original file line number Diff line number Diff line change
@@ -0,0 1 @@
..\..\z_tools\make.exe %1 %2 %3 %4 %5 %6 %7 %8 %9

0 comments on commit 3871740

Please sign in to comment.