cforth.c代码更新,解决了多余的每一个空格键会在DS栈留下一个0的问题。代码如下:
- #include <stdio.h>
- #include <string.h>
- int ds[256],rs[256];
- int *dsp=ds,*rsp=rs;
- int flag;
- #include "code_words.h"
- #include "colon_words.h"
- char inputBuff[1024];
- char strBuff[30];
- int main()
- {
- printf("Cforth 0.1.0, Copyright (C) 2008-2012 Free Software Foundation, Inc.\n");
- printf("Cforth comes with ABSOLUTELY NO WARRANTY; for details type `license'\n");
- printf("Type `bye' to exit\n");
- while (1){
- int i=0;
- int j=0;
- gets(inputBuff);
- while(inputBuff[j] != '\0'){
- if(inputBuff[j]!= ' '){
- strBuff[i] = inputBuff[j];
- i++;
- j++;
- }
- else {
- strBuff[i]='\0';
- if ( !strcmp(".s",strBuff) ) showDS();
- else if( !strcmp("dup",strBuff) ) dup();
- else if( !strcmp(".",strBuff) ) showtopDS();
- else if( !strcmp(">r",strBuff) ) tor();
- else if( !strcmp("r>",strBuff) ) rto();
- else if( !strcmp("drop",strBuff) ) drop();
- else if( !strcmp("+",strBuff) ) add();
- else if( !strcmp("-",strBuff) ) sub();
- else if( !strcmp("*",strBuff) ) mul();
- else if( !strcmp("/d",strBuff) ) ddiv();
- else if( !strcmp("/",strBuff) ) div();
- else if( !strcmp("%",strBuff) ) mod();
- else if( isNum() ) chgNum();
- else if( !strcmp("say",strBuff) ) say();
- else if( !strcmp("bye",strBuff) ) return 0;
- else printf("\n[%s]?\n",strBuff);
-
- for(i=0;i<30;i++) strBuff[i]='\0';
- i=0;
- j++;
- }
- }
- showOK();
- }
- return 0;
- }
- int isNum()
- {
- char *str=strBuff;
- while(*str) {
- if(*str<'0' || *str>'9') return 0;
- str++;
- }
- return 1;
- }
- int chgNum()
- {
- char *str=strBuff;
- int sum=0;
- while(*str) {
- sum=10*sum+(int)(*str-'0');
- str++;
- }
- //如果这个数字字符串只有一个'\0',则直接返回0。
- //用来解决多余的空格键误操作DS的BUG
- if(str==&strBuff[0]) return 0;
- push(sum);
- return 0;
- }
- int showOK()
- {
- printf("OK\n");
- return 0;
- }
- int say()
- {
- gets(strBuff);
- printf("cforth: [%s]\n",strBuff);
- return 0;
- }
阅读(950) | 评论(0) | 转发(0) |